1110 lines
39 KiB
C#
1110 lines
39 KiB
C#
using MediatR;
|
|
using cuqmbr.TravelGuide.Application.Common.Models;
|
|
using cuqmbr.TravelGuide.Application.Common.Exceptions;
|
|
using cuqmbr.TravelGuide.Application.Countries.Commands.AddCountry;
|
|
using cuqmbr.TravelGuide.Application.Regions.Commands.AddRegion;
|
|
using cuqmbr.TravelGuide.Application.Regions.Commands.UpdateRegion;
|
|
using cuqmbr.TravelGuide.Application.Regions.Commands.DeleteRegion;
|
|
using cuqmbr.TravelGuide.Application.Regions.Queries.GetRegion;
|
|
using cuqmbr.TravelGuide.Application.Regions.Queries.GetRegionsPage;
|
|
|
|
|
|
namespace cuqmbr.TravelGuide.Application.IntegrationTests;
|
|
|
|
public class RegionsTests : TestBase
|
|
{
|
|
[Fact]
|
|
public async Task AddRegion_WithAdminRole_RegionCreated()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName = "Country Name";
|
|
|
|
var createCountryResult = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName = "Region Name";
|
|
|
|
var createRegionResult = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName,
|
|
CountryUuid = createCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getRegionResult = await mediator.Send(
|
|
new GetRegionQuery()
|
|
{
|
|
Uuid = createRegionResult.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionResult);
|
|
Assert.NotNull(getRegionResult.Name);
|
|
Assert.Equal(regionName, getRegionResult.Name);
|
|
Assert.Equal(createCountryResult.Uuid, getRegionResult.CountryUuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
AddDuplicateRegion_WithAdminRole_ThrowsDuplicateEntityException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName = "Country Name";
|
|
|
|
var createCountryResult = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName = "Region Name";
|
|
|
|
var createRegionResult = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName,
|
|
CountryUuid = createCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
await Assert.ThrowsAsync<DuplicateEntityException>(() =>
|
|
mediator.Send(new AddRegionCommand()
|
|
{
|
|
Name = regionName,
|
|
CountryUuid = createCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
AddSameRegionsToDifferentCountries_WithAdminRole_RegionsCreated()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName1 = "Country Name 1";
|
|
|
|
var createCountryResult1 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string countryName2 = "Country Name2 ";
|
|
|
|
var createCountryResult2 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName2
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName = "Region Name";
|
|
|
|
var createRegionResult1 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName,
|
|
CountryUuid = createCountryResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var createRegionResult2 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName,
|
|
CountryUuid = createCountryResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getRegionResult1 = await mediator.Send(
|
|
new GetRegionQuery()
|
|
{
|
|
Uuid = createRegionResult1.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionResult1);
|
|
Assert.NotNull(getRegionResult1.Name);
|
|
Assert.Equal(regionName, getRegionResult1.Name);
|
|
Assert.Equal(createCountryResult1.Uuid, getRegionResult1.CountryUuid);
|
|
|
|
var getRegionResult2 = await mediator.Send(
|
|
new GetRegionQuery()
|
|
{
|
|
Uuid = createRegionResult2.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionResult2);
|
|
Assert.NotNull(getRegionResult2.Name);
|
|
Assert.Equal(regionName, getRegionResult2.Name);
|
|
Assert.Equal(createCountryResult2.Uuid, getRegionResult2.CountryUuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
AddRegion_WithNonExistentCountryUuid_WithAdminRole_ThrowsNotFoundException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
mediator.Send(new AddRegionCommand()
|
|
{
|
|
Name = "Name",
|
|
CountryUuid = Guid.NewGuid()
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("01234567890123456789012345678901234567890123456789012345678901234")]
|
|
public async Task
|
|
AddRegion_WithInvalidName_WithAdminRole_ThrowsValidationException
|
|
(string name)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(new AddRegionCommand()
|
|
{
|
|
Name = name
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("not an uuid")]
|
|
public async Task
|
|
AddRegion_WithInvalidCountryUuid_WithAdminRole_ThrowsValidationException
|
|
(string uuid)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(new AddRegionCommand()
|
|
{
|
|
Name = "Name",
|
|
CountryUuid =
|
|
Guid.TryParse(uuid, out var guid) ? guid : Guid.Empty
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AddRegion_WithUserRole_ThrowsForbiddenException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.User });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new AddRegionCommand(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with user role (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task AddRegion_UnAuthnticatedUser_ThrowsForbiddenException()
|
|
{
|
|
SetUnAuthenticatedUser();
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new AddRegionCommand(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with unauthenticated user
|
|
// (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task UpdateRegion_WithAdminRole_RegionUpdated()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName = "Country Name";
|
|
|
|
var createCountryResult = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName = "Region Name";
|
|
|
|
var createRegionResult = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName,
|
|
CountryUuid = createCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string newName = "Different Name";
|
|
|
|
var editRegionResult = await mediator.Send(
|
|
new UpdateRegionCommand()
|
|
{
|
|
Uuid = createRegionResult.Uuid,
|
|
Name = newName,
|
|
CountryUuid = createCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getRegionResult = await mediator.Send(
|
|
new GetRegionQuery()
|
|
{
|
|
Uuid = createRegionResult.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionResult);
|
|
Assert.NotNull(getRegionResult.Name);
|
|
Assert.Equal(newName, getRegionResult.Name);
|
|
Assert.Equal(createCountryResult.Uuid, getRegionResult.CountryUuid);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("01234567890123456789012345678901234567890123456789012345678901234")]
|
|
public async Task
|
|
UpdateRegion_WithInvalidName_WithAdminRole_ThrowsValidationException
|
|
(string name)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(new UpdateRegionCommand()
|
|
{
|
|
Name = name,
|
|
CountryUuid = Guid.NewGuid()
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("not an uuid")]
|
|
public async Task
|
|
UpdateRegion_WithInvalidCountryUuid_WithAdminRole_ThrowsValidationException
|
|
(string uuid)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(new UpdateRegionCommand()
|
|
{
|
|
Uuid = Guid.NewGuid(),
|
|
Name = "Name",
|
|
CountryUuid =
|
|
Guid.TryParse(uuid, out var guid) ? guid : Guid.Empty
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("not an uuid")]
|
|
public async Task
|
|
UpdateRegion_WithInvalidUuid_WithAdminRole_ThrowsValidationException
|
|
(string uuid)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(new UpdateRegionCommand()
|
|
{
|
|
Uuid = Guid.TryParse(uuid, out var guid) ? guid : Guid.Empty,
|
|
Name = "Name",
|
|
CountryUuid = Guid.NewGuid()
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
UpdateRegion_WithNonExistentUuid_WithAdminRole_ThrowsNotFoundException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
var createCountryResult = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = "Name"
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
mediator.Send(new UpdateRegionCommand()
|
|
{
|
|
Uuid = Guid.NewGuid(),
|
|
Name = "Different Name",
|
|
CountryUuid = createCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
UpdateRegion_WithNonExistentCountryUuid_WithAdminRole_ThrowsNotFoundException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
var createCountryResult = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = "Name"
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var createRegionResult = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = "Name",
|
|
CountryUuid = createCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
mediator.Send(new UpdateRegionCommand()
|
|
{
|
|
Uuid = createCountryResult.Uuid,
|
|
Name = "Different Name",
|
|
CountryUuid = Guid.NewGuid()
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateRegion_WithUserRole_ThrowsForbiddenException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.User });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new UpdateRegionCommand(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with user role (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task UpdateRegion_UnAuthnticatedUser_ThrowsForbiddenException()
|
|
{
|
|
SetUnAuthenticatedUser();
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new UpdateRegionCommand(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with unauthenticated user
|
|
// (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task DeleteRegion_WithAdminRole_RegionDeleted()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
var createCountryResult = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = "Name"
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var createRegionResult = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = "Name",
|
|
CountryUuid = createCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
await mediator.Send(
|
|
new DeleteRegionCommand()
|
|
{
|
|
Uuid = createRegionResult.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
mediator.Send(new GetRegionQuery()
|
|
{
|
|
Uuid = createRegionResult.Uuid,
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("not an uuid")]
|
|
public async Task
|
|
DeleteRegion_WithInvalidUuid_WithAdminRole_ThrowsValidationException
|
|
(string uuid)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(new DeleteRegionCommand()
|
|
{
|
|
Uuid = Guid.TryParse(uuid, out var guid) ? guid : Guid.Empty
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
DeleteRegion_WithNonExistentUuid_WithAdminRole_ThrowsNotFoundException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
mediator.Send(new DeleteRegionCommand()
|
|
{
|
|
Uuid = Guid.NewGuid()
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteRegion_WithUserRole_ThrowsForbiddenException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.User });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new DeleteRegionCommand(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with user role (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task DeleteRegion_UnAuthnticatedUser_ThrowsForbiddenException()
|
|
{
|
|
SetUnAuthenticatedUser();
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new DeleteRegionCommand(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with unauthenticated user
|
|
// (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task GetRegion_WithAdminRole_RegionReturned()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName = "Name";
|
|
|
|
var createCountryResult = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName = "Name";
|
|
|
|
var createRegionResult = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName,
|
|
CountryUuid = createCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getRegionResult = await mediator.Send(
|
|
new GetRegionQuery()
|
|
{
|
|
Uuid = createRegionResult.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionResult);
|
|
Assert.NotNull(getRegionResult.Name);
|
|
Assert.Equal(regionName, getRegionResult.Name);
|
|
Assert.Equal(createRegionResult.Uuid, getRegionResult.Uuid);
|
|
Assert.Equal(createCountryResult.Uuid, getRegionResult.CountryUuid);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("not an uuid")]
|
|
public async Task
|
|
GetRegion_WithInvalidUuid_WithAdminRole_ThrowsValidationException
|
|
(string uuid)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(
|
|
new GetRegionQuery()
|
|
{
|
|
Uuid = Guid.TryParse(uuid, out var guid) ? guid : Guid.Empty
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
GetRegion_WithNonExistentUuid_WithAdminRole_ThrowsNotFoundException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
mediator.Send(
|
|
new GetRegionQuery()
|
|
{
|
|
Uuid = Guid.NewGuid()
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetRegion_WithUserRole_ThrowsForbiddenException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.User });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new GetRegionQuery(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with user role (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task GetRegion_UnAuthnticatedUser_ThrowsForbiddenException()
|
|
{
|
|
SetUnAuthenticatedUser();
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new GetRegionQuery(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with unauthenticated user
|
|
// (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task GetRegionsPage_WithAdminRole_RegionsPageReturned()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName1 = "Country Name 1";
|
|
string countryName2 = "CountryName 1";
|
|
|
|
var createCountryResult1 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var createCountryResult2 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName2
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName1 = "Region Name 1";
|
|
string regionName2 = "Region Name 2";
|
|
|
|
var createRegionResult1 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName1,
|
|
CountryUuid = createCountryResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var createRegionResult2 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName2,
|
|
CountryUuid = createCountryResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getRegionsResult = await mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionsResult);
|
|
Assert.Equal(1, getRegionsResult.PageNumber);
|
|
Assert.Equal(2, getRegionsResult.TotalCount);
|
|
Assert.Equal(2, getRegionsResult.TotalPages);
|
|
Assert.True(getRegionsResult.HasNextPage);
|
|
Assert.False(getRegionsResult.HasPreviousPage);
|
|
Assert.NotNull(getRegionsResult.Items);
|
|
Assert.Single(getRegionsResult.Items);
|
|
Assert.NotNull(getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult1.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult1.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
createCountryResult1.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
|
|
getRegionsResult = await mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
PageNumber = 2,
|
|
PageSize = 1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionsResult);
|
|
Assert.Equal(2, getRegionsResult.PageNumber);
|
|
Assert.Equal(2, getRegionsResult.TotalCount);
|
|
Assert.Equal(2, getRegionsResult.TotalPages);
|
|
Assert.False(getRegionsResult.HasNextPage);
|
|
Assert.True(getRegionsResult.HasPreviousPage);
|
|
Assert.NotNull(getRegionsResult.Items);
|
|
Assert.Single(getRegionsResult.Items);
|
|
Assert.NotNull(getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult2.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult2.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
createCountryResult2.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
|
|
getRegionsResult = await mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionsResult);
|
|
Assert.Equal(1, getRegionsResult.PageNumber);
|
|
Assert.Equal(2, getRegionsResult.TotalCount);
|
|
Assert.Equal(1, getRegionsResult.TotalPages);
|
|
Assert.False(getRegionsResult.HasNextPage);
|
|
Assert.False(getRegionsResult.HasPreviousPage);
|
|
Assert.Equal(2, getRegionsResult.Items.Count());
|
|
Assert.NotNull(getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult1.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult1.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
createCountryResult1.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
Assert.NotNull(getRegionsResult.Items.Last().Name);
|
|
Assert.Equal(
|
|
createRegionResult2.Name, getRegionsResult.Items.Last().Name);
|
|
Assert.Equal(
|
|
createRegionResult2.Uuid, getRegionsResult.Items.Last().Uuid);
|
|
Assert.Equal(
|
|
createCountryResult2.Uuid, getRegionsResult.Items.Last().CountryUuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
GetRegionsPage_WithSearch_WithAdminRole_SearchedRegionsPageReturned()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName1 = "Country Name 1";
|
|
string countryName2 = "CountryName 1";
|
|
|
|
var createCountryResult1 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var createCountryResult2 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName2
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName1 = "Region Name 1";
|
|
string regionName2 = "Region Name 2";
|
|
|
|
var createRegionResult1 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName1,
|
|
CountryUuid = createCountryResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var createRegionResult2 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName2,
|
|
CountryUuid = createCountryResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getRegionsResult = await mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
Search = "cOUNTRYn"
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionsResult);
|
|
Assert.Equal(1, getRegionsResult.PageNumber);
|
|
Assert.Equal(1, getRegionsResult.TotalCount);
|
|
Assert.Equal(1, getRegionsResult.TotalPages);
|
|
Assert.False(getRegionsResult.HasNextPage);
|
|
Assert.False(getRegionsResult.HasPreviousPage);
|
|
Assert.NotNull(getRegionsResult.Items);
|
|
Assert.Single(getRegionsResult.Items);
|
|
Assert.NotNull(getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult2.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult2.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
createCountryResult2.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
|
|
getRegionsResult = await mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
Search = "region name 1"
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionsResult);
|
|
Assert.Equal(1, getRegionsResult.PageNumber);
|
|
Assert.Equal(1, getRegionsResult.TotalCount);
|
|
Assert.Equal(1, getRegionsResult.TotalPages);
|
|
Assert.False(getRegionsResult.HasNextPage);
|
|
Assert.False(getRegionsResult.HasPreviousPage);
|
|
Assert.NotNull(getRegionsResult.Items);
|
|
Assert.Single(getRegionsResult.Items);
|
|
Assert.NotNull(getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult1.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult1.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
createCountryResult1.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
GetRegionsPage_WithSort_WithAdminRole_SortedRegionsPageReturned()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName1 = "Country Name 1";
|
|
string countryName2 = "Country Name 2";
|
|
|
|
var createCountryResult1 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var createCountryResult2 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName2
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName1 = "Region Name 1";
|
|
string regionName2 = "Region Name 2";
|
|
|
|
var createRegionResult1 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName1,
|
|
CountryUuid = createCountryResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var createRegionResult2 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName2,
|
|
CountryUuid = createCountryResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getRegionsResult = await mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
Sort = "-name"
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionsResult);
|
|
Assert.Equal(1, getRegionsResult.PageNumber);
|
|
Assert.Equal(2, getRegionsResult.TotalCount);
|
|
Assert.Equal(1, getRegionsResult.TotalPages);
|
|
Assert.False(getRegionsResult.HasNextPage);
|
|
Assert.False(getRegionsResult.HasPreviousPage);
|
|
Assert.NotNull(getRegionsResult.Items);
|
|
Assert.Equal(2, getRegionsResult.Items.Count());
|
|
Assert.NotNull(getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult2.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult2.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
createCountryResult2.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
|
|
getRegionsResult = await mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
Sort = "+name"
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionsResult);
|
|
Assert.Equal(1, getRegionsResult.PageNumber);
|
|
Assert.Equal(2, getRegionsResult.TotalCount);
|
|
Assert.Equal(1, getRegionsResult.TotalPages);
|
|
Assert.False(getRegionsResult.HasNextPage);
|
|
Assert.False(getRegionsResult.HasPreviousPage);
|
|
Assert.NotNull(getRegionsResult.Items);
|
|
Assert.Equal(2, getRegionsResult.Items.Count());
|
|
Assert.NotNull(getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult1.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult1.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
createCountryResult1.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
GetRegionsPage_WithFilterByCountryUuid_WithAdminRole_SearchedRegionsPageReturned()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName1 = "Country Name 1";
|
|
string countryName2 = "Country Name 2";
|
|
|
|
var createCountryResult1 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var createCountryResult2 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName2
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName1 = "Region Name 1";
|
|
string regionName2 = "Region Name 2";
|
|
string regionName3 = "Region Name 3";
|
|
|
|
var createRegionResult1 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName1,
|
|
CountryUuid = createCountryResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var createRegionResult2 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName2,
|
|
CountryUuid = createCountryResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var createRegionResult3 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName3,
|
|
CountryUuid = createCountryResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getRegionsResult = await mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
CountryUuid = createCountryResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionsResult);
|
|
Assert.Equal(1, getRegionsResult.PageNumber);
|
|
Assert.Equal(1, getRegionsResult.TotalCount);
|
|
Assert.Equal(1, getRegionsResult.TotalPages);
|
|
Assert.False(getRegionsResult.HasNextPage);
|
|
Assert.False(getRegionsResult.HasPreviousPage);
|
|
Assert.NotNull(getRegionsResult.Items);
|
|
Assert.Single(getRegionsResult.Items);
|
|
Assert.NotNull(getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult1.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult1.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
createCountryResult1.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
|
|
getRegionsResult = await mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
CountryUuid = createCountryResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionsResult);
|
|
Assert.Equal(1, getRegionsResult.PageNumber);
|
|
Assert.Equal(2, getRegionsResult.TotalCount);
|
|
Assert.Equal(1, getRegionsResult.TotalPages);
|
|
Assert.False(getRegionsResult.HasNextPage);
|
|
Assert.False(getRegionsResult.HasPreviousPage);
|
|
Assert.NotNull(getRegionsResult.Items);
|
|
Assert.Equal(2, getRegionsResult.Items.Count());
|
|
Assert.NotNull(getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult2.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
createRegionResult2.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
createCountryResult2.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
Assert.NotNull(getRegionsResult.Items.Last().Name);
|
|
Assert.Equal(
|
|
createRegionResult3.Name, getRegionsResult.Items.Last().Name);
|
|
Assert.Equal(
|
|
createRegionResult3.Uuid, getRegionsResult.Items.Last().Uuid);
|
|
Assert.Equal(
|
|
createCountryResult2.Uuid, getRegionsResult.Items.Last().CountryUuid);
|
|
}
|
|
|
|
[Theory]
|
|
// Length > 64 (65)
|
|
[InlineData("01234567890123456789012345678901234567890123456789012345678901234")]
|
|
public async Task
|
|
GetRegionsPage_WithInvalidSearch_WithAdminRole_ThrowsValidationException
|
|
(string search)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
Search = search
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(int.MinValue)]
|
|
[InlineData(0)]
|
|
public async Task
|
|
GetRegionsPage_WithInvalidPageNumber_WithAdminRole_ThrowsValidationException
|
|
(int pageNumber)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
PageNumber = pageNumber
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(int.MinValue)]
|
|
[InlineData(0)]
|
|
[InlineData(51)]
|
|
[InlineData(int.MaxValue)]
|
|
public async Task
|
|
GetRegionsPage_WithInvalidPageSize_WithAdminRole_ThrowsValidationException
|
|
(int pageSize)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
PageSize = pageSize
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetRegionsPage_WithUserRole_ThrowsForbiddenException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.User });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new GetRegionsPageQuery(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with user role (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task GetRegionsPage_UnAuthnticatedUser_ThrowsForbiddenException()
|
|
{
|
|
SetUnAuthenticatedUser();
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new GetRegionQuery(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with unauthenticated user
|
|
// (copy tests with admin role)
|
|
}
|