1162 lines
41 KiB
C#
1162 lines
41 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_RegionAdded()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName = "Country Name";
|
|
|
|
var addCountryResult = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName = "Region Name";
|
|
|
|
var addRegionResult = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName,
|
|
CountryUuid = addCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getRegionResult = await mediator.Send(
|
|
new GetRegionQuery()
|
|
{
|
|
Uuid = addRegionResult.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionResult);
|
|
Assert.NotNull(getRegionResult.Name);
|
|
Assert.Equal(regionName, getRegionResult.Name);
|
|
Assert.Equal(addCountryResult.Uuid, getRegionResult.CountryUuid);
|
|
Assert.NotNull(getRegionResult.CountryName);
|
|
Assert.Equal(addCountryResult.Name, getRegionResult.CountryName);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
AddDuplicateRegion_WithAdminRole_ThrowsDuplicateEntityException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName = "Country Name";
|
|
|
|
var addCountryResult = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName = "Region Name";
|
|
|
|
var addRegionResult = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName,
|
|
CountryUuid = addCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
await Assert.ThrowsAsync<DuplicateEntityException>(() =>
|
|
mediator.Send(new AddRegionCommand()
|
|
{
|
|
Name = regionName,
|
|
CountryUuid = addCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
AddSameRegionsToDifferentCountries_WithAdminRole_RegionsAdded()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName1 = "Country Name 1";
|
|
|
|
var addCountryResult1 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string countryName2 = "Country Name2 ";
|
|
|
|
var addCountryResult2 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName2
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName = "Region Name";
|
|
|
|
var addRegionResult1 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName,
|
|
CountryUuid = addCountryResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addRegionResult2 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName,
|
|
CountryUuid = addCountryResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getRegionResult1 = await mediator.Send(
|
|
new GetRegionQuery()
|
|
{
|
|
Uuid = addRegionResult1.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionResult1);
|
|
Assert.NotNull(getRegionResult1.Name);
|
|
Assert.Equal(regionName, getRegionResult1.Name);
|
|
Assert.Equal(addCountryResult1.Uuid, getRegionResult1.CountryUuid);
|
|
|
|
var getRegionResult2 = await mediator.Send(
|
|
new GetRegionQuery()
|
|
{
|
|
Uuid = addRegionResult2.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionResult2);
|
|
Assert.NotNull(getRegionResult2.Name);
|
|
Assert.Equal(regionName, getRegionResult2.Name);
|
|
Assert.Equal(addCountryResult2.Uuid, getRegionResult2.CountryUuid);
|
|
Assert.NotNull(getRegionResult2.CountryName);
|
|
Assert.Equal(addCountryResult2.Name, getRegionResult2.CountryName);
|
|
}
|
|
|
|
[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 addCountryResult = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName = "Region Name";
|
|
|
|
var addRegionResult = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName,
|
|
CountryUuid = addCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string newName = "Different Name";
|
|
|
|
var updateRegionResult = await mediator.Send(
|
|
new UpdateRegionCommand()
|
|
{
|
|
Uuid = addRegionResult.Uuid,
|
|
Name = newName,
|
|
CountryUuid = addCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getRegionResult = await mediator.Send(
|
|
new GetRegionQuery()
|
|
{
|
|
Uuid = addRegionResult.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionResult);
|
|
Assert.NotNull(getRegionResult.Name);
|
|
Assert.Equal(newName, getRegionResult.Name);
|
|
Assert.Equal(addCountryResult.Uuid, getRegionResult.CountryUuid);
|
|
Assert.NotNull(getRegionResult.CountryName);
|
|
Assert.Equal(addCountryResult.Name, getRegionResult.CountryName);
|
|
}
|
|
|
|
[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 addCountryResult = 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 = addCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
UpdateRegion_WithNonExistentCountryUuid_WithAdminRole_ThrowsNotFoundException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
var addCountryResult = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = "Name"
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addRegionResult = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = "Name",
|
|
CountryUuid = addCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
mediator.Send(new UpdateRegionCommand()
|
|
{
|
|
Uuid = addCountryResult.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 addCountryResult = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = "Name"
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addRegionResult = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = "Name",
|
|
CountryUuid = addCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
await mediator.Send(
|
|
new DeleteRegionCommand()
|
|
{
|
|
Uuid = addRegionResult.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
mediator.Send(new GetRegionQuery()
|
|
{
|
|
Uuid = addRegionResult.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 addCountryResult = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName = "Name";
|
|
|
|
var addRegionResult = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName,
|
|
CountryUuid = addCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getRegionResult = await mediator.Send(
|
|
new GetRegionQuery()
|
|
{
|
|
Uuid = addRegionResult.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getRegionResult);
|
|
Assert.NotNull(getRegionResult.Name);
|
|
Assert.Equal(regionName, getRegionResult.Name);
|
|
Assert.Equal(addRegionResult.Uuid, getRegionResult.Uuid);
|
|
Assert.Equal(addCountryResult.Uuid, getRegionResult.CountryUuid);
|
|
Assert.NotNull(getRegionResult.CountryName);
|
|
Assert.Equal(addCountryResult.Name, getRegionResult.CountryName);
|
|
}
|
|
|
|
[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 addCountryResult1 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addCountryResult2 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName2
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName1 = "Region Name 1";
|
|
string regionName2 = "Region Name 2";
|
|
|
|
var addRegionResult1 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName1,
|
|
CountryUuid = addCountryResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addRegionResult2 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName2,
|
|
CountryUuid = addCountryResult2.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(
|
|
addRegionResult1.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addRegionResult1.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
addCountryResult1.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(
|
|
addRegionResult2.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addRegionResult2.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
addCountryResult2.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
Assert.NotNull(getRegionsResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult2.Name, getRegionsResult.Items.First().CountryName);
|
|
|
|
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(
|
|
addRegionResult1.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addRegionResult1.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
addCountryResult1.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
Assert.NotNull(getRegionsResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult1.Name, getRegionsResult.Items.First().CountryName);
|
|
Assert.NotNull(getRegionsResult.Items.Last().Name);
|
|
Assert.Equal(
|
|
addRegionResult2.Name, getRegionsResult.Items.Last().Name);
|
|
Assert.Equal(
|
|
addRegionResult2.Uuid, getRegionsResult.Items.Last().Uuid);
|
|
Assert.Equal(
|
|
addCountryResult2.Uuid, getRegionsResult.Items.Last().CountryUuid);
|
|
Assert.NotNull(getRegionsResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult2.Name, getRegionsResult.Items.Last().CountryName);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
GetRegionsPage_WithSearchByCountryName_WithAdminRole_SearchedRegionsPageReturned()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName1 = "Country Name 1";
|
|
string countryName2 = "CountryName 1";
|
|
|
|
var addCountryResult1 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addCountryResult2 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName2
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName1 = "Region Name 1";
|
|
string regionName2 = "Region Name 2";
|
|
|
|
var addRegionResult1 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName1,
|
|
CountryUuid = addCountryResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addRegionResult2 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName2,
|
|
CountryUuid = addCountryResult2.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(
|
|
addRegionResult2.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addRegionResult2.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
addCountryResult2.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
Assert.NotNull(getRegionsResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult2.Name, getRegionsResult.Items.First().CountryName);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
GetRegionsPage_WithSearchByRegionName_WithAdminRole_SearchedRegionsPageReturned()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName1 = "Country Name 1";
|
|
string countryName2 = "CountryName 1";
|
|
|
|
var addCountryResult1 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addCountryResult2 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName2
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName1 = "Region Name 1";
|
|
string regionName2 = "Region Name 2";
|
|
|
|
var addRegionResult1 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName1,
|
|
CountryUuid = addCountryResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addRegionResult2 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName2,
|
|
CountryUuid = addCountryResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var 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(
|
|
addRegionResult1.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addRegionResult1.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
addCountryResult1.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
Assert.NotNull(getRegionsResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult1.Name, getRegionsResult.Items.First().CountryName);
|
|
}
|
|
|
|
[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 addCountryResult1 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addCountryResult2 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName2
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName1 = "Region Name 1";
|
|
string regionName2 = "Region Name 2";
|
|
|
|
var addRegionResult1 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName1,
|
|
CountryUuid = addCountryResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addRegionResult2 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName2,
|
|
CountryUuid = addCountryResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getRegionsResult = await mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
Sort = "-countryName"
|
|
}, 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(
|
|
addRegionResult2.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addRegionResult2.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
addCountryResult2.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
Assert.NotNull(getRegionsResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult2.Name, getRegionsResult.Items.First().CountryName);
|
|
}
|
|
|
|
[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 addCountryResult1 = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addCountryResult2 = 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 addRegionResult1 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName1,
|
|
CountryUuid = addCountryResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addRegionResult2 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName2,
|
|
CountryUuid = addCountryResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addRegionResult3 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName3,
|
|
CountryUuid = addCountryResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getRegionsResult = await mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
CountryUuid = addCountryResult1.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(
|
|
addRegionResult1.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addRegionResult1.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
addCountryResult1.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
Assert.NotNull(getRegionsResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult1.Name, getRegionsResult.Items.First().CountryName);
|
|
|
|
getRegionsResult = await mediator.Send(
|
|
new GetRegionsPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
CountryUuid = addCountryResult2.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(
|
|
addRegionResult2.Name, getRegionsResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addRegionResult2.Uuid, getRegionsResult.Items.First().Uuid);
|
|
Assert.Equal(
|
|
addCountryResult2.Uuid, getRegionsResult.Items.First().CountryUuid);
|
|
Assert.NotNull(getRegionsResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult2.Name, getRegionsResult.Items.First().CountryName);
|
|
Assert.NotNull(getRegionsResult.Items.Last().Name);
|
|
Assert.Equal(
|
|
addRegionResult3.Name, getRegionsResult.Items.Last().Name);
|
|
Assert.Equal(
|
|
addRegionResult3.Uuid, getRegionsResult.Items.Last().Uuid);
|
|
Assert.Equal(
|
|
addCountryResult2.Uuid, getRegionsResult.Items.Last().CountryUuid);
|
|
Assert.NotNull(getRegionsResult.Items.Last().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult2.Name, getRegionsResult.Items.First().CountryName);
|
|
}
|
|
|
|
[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)
|
|
}
|