1577 lines
53 KiB
C#
1577 lines
53 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.Cities.Commands.AddCity;
|
|
using cuqmbr.TravelGuide.Application.Cities.Commands.UpdateCity;
|
|
using cuqmbr.TravelGuide.Application.Cities.Commands.DeleteCity;
|
|
using cuqmbr.TravelGuide.Application.Cities.Queries.GetCity;
|
|
using cuqmbr.TravelGuide.Application.Cities.Queries.GetCitiesPage;
|
|
|
|
|
|
namespace cuqmbr.TravelGuide.Application.IntegrationTests;
|
|
|
|
public class CitiesTests : TestBase
|
|
{
|
|
[Fact]
|
|
public async Task AddCity_WithAdminRole_CityAdded()
|
|
{
|
|
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 cityName = "City Name";
|
|
|
|
var addCityResult = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName,
|
|
RegionUuid = addRegionResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getCityResult = await mediator.Send(
|
|
new GetCityQuery()
|
|
{
|
|
Uuid = addCityResult.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getCityResult);
|
|
|
|
Assert.NotNull(getCityResult.Name);
|
|
Assert.Equal(cityName, getCityResult.Name);
|
|
|
|
Assert.NotNull(getCityResult.CountryName);
|
|
Assert.Equal(countryName, getCityResult.CountryName);
|
|
Assert.Equal(addCountryResult.Uuid, getCityResult.CountryUuid);
|
|
|
|
Assert.NotNull(getCityResult.RegionName);
|
|
Assert.Equal(regionName, getCityResult.RegionName);
|
|
Assert.Equal(addRegionResult.Uuid, getCityResult.RegionUuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
AddDuplicateCity_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);
|
|
|
|
string cityName = "City Name";
|
|
|
|
var addCityResult = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName,
|
|
RegionUuid = addRegionResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
await Assert.ThrowsAsync<DuplicateEntityException>(() =>
|
|
mediator.Send(new AddCityCommand()
|
|
{
|
|
Name = cityName,
|
|
RegionUuid = addRegionResult.Uuid
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
AddSameCitiesToDifferentRegions_WithAdminRole_CitiesAdded()
|
|
{
|
|
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 regionName1 = "Region Name 1";
|
|
string regionName2 = "Region Name 2";
|
|
|
|
var addRegionResult1 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName1,
|
|
CountryUuid = addCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addRegionResult2 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName2,
|
|
CountryUuid = addCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string cityName = "City Name";
|
|
|
|
var addCityResult1 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName,
|
|
RegionUuid = addRegionResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addCityResult2 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName,
|
|
RegionUuid = addRegionResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getCityResult1 = await mediator.Send(
|
|
new GetCityQuery()
|
|
{
|
|
Uuid = addCityResult1.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getCityResult1);
|
|
|
|
Assert.NotNull(getCityResult1.Name);
|
|
Assert.Equal(cityName, getCityResult1.Name);
|
|
|
|
Assert.NotNull(getCityResult1.CountryName);
|
|
Assert.Equal(countryName, getCityResult1.CountryName);
|
|
Assert.Equal(addCountryResult.Uuid, getCityResult1.CountryUuid);
|
|
|
|
Assert.NotNull(getCityResult1.RegionName);
|
|
Assert.Equal(regionName1, getCityResult1.RegionName);
|
|
Assert.Equal(addRegionResult1.Uuid, getCityResult1.RegionUuid);
|
|
|
|
var getCityResult2 = await mediator.Send(
|
|
new GetCityQuery()
|
|
{
|
|
Uuid = addCityResult2.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getCityResult2);
|
|
|
|
Assert.NotNull(getCityResult2.Name);
|
|
Assert.Equal(cityName, getCityResult2.Name);
|
|
|
|
Assert.NotNull(getCityResult2.CountryName);
|
|
Assert.Equal(countryName, getCityResult2.CountryName);
|
|
Assert.Equal(addCountryResult.Uuid, getCityResult2.CountryUuid);
|
|
|
|
Assert.NotNull(getCityResult2.RegionName);
|
|
Assert.Equal(regionName2, getCityResult2.RegionName);
|
|
Assert.Equal(addRegionResult2.Uuid, getCityResult2.RegionUuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
AddCity_WithNonExistentRegionUuid_WithAdminRole_ThrowsNotFoundException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
mediator.Send(new AddCityCommand()
|
|
{
|
|
Name = "Name",
|
|
RegionUuid = Guid.NewGuid()
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("01234567890123456789012345678901234567890123456789012345678901234")]
|
|
public async Task
|
|
AddCity_WithInvalidName_WithAdminRole_ThrowsValidationException
|
|
(string name)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(new AddCityCommand()
|
|
{
|
|
Name = name
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("not an uuid")]
|
|
public async Task
|
|
AddCity_WithInvalidRegionUuid_WithAdminRole_ThrowsValidationException
|
|
(string uuid)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(new AddCityCommand()
|
|
{
|
|
Name = "Name",
|
|
RegionUuid =
|
|
Guid.TryParse(uuid, out var guid) ? guid : Guid.Empty
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AddCity_WithUserRole_ThrowsForbiddenException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.User });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new AddCityCommand(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with user role (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task AddCity_UnAuthnticatedUser_ThrowsForbiddenException()
|
|
{
|
|
SetUnAuthenticatedUser();
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new AddCityCommand(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with unauthenticated user
|
|
// (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task UpdateCity_WithAdminRole_CityUpdated()
|
|
{
|
|
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 cityName = "City Name";
|
|
|
|
var addCityResult = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName,
|
|
RegionUuid = addRegionResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string newName = "Different Name";
|
|
|
|
var updateCityResult = await mediator.Send(
|
|
new UpdateCityCommand()
|
|
{
|
|
Uuid = addCityResult.Uuid,
|
|
Name = newName,
|
|
RegionUuid = addRegionResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(updateCityResult);
|
|
|
|
Assert.NotNull(updateCityResult.Name);
|
|
Assert.Equal(newName, updateCityResult.Name);
|
|
Assert.Equal(addCityResult.Uuid, updateCityResult.Uuid);
|
|
|
|
Assert.NotNull(updateCityResult.CountryName);
|
|
Assert.Equal(countryName, updateCityResult.CountryName);
|
|
Assert.Equal(addCountryResult.Uuid, updateCityResult.CountryUuid);
|
|
|
|
Assert.NotNull(updateCityResult.RegionName);
|
|
Assert.Equal(regionName, updateCityResult.RegionName);
|
|
Assert.Equal(addRegionResult.Uuid, updateCityResult.RegionUuid);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("01234567890123456789012345678901234567890123456789012345678901234")]
|
|
public async Task
|
|
UpdateCity_WithInvalidName_WithAdminRole_ThrowsValidationException
|
|
(string name)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(new UpdateCityCommand()
|
|
{
|
|
Name = name,
|
|
RegionUuid = Guid.NewGuid()
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("not an uuid")]
|
|
public async Task
|
|
UpdateCity_WithInvalidRegionUuid_WithAdminRole_ThrowsValidationException
|
|
(string uuid)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(new UpdateCityCommand()
|
|
{
|
|
Uuid = Guid.NewGuid(),
|
|
Name = "Name",
|
|
RegionUuid =
|
|
Guid.TryParse(uuid, out var guid) ? guid : Guid.Empty
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("not an uuid")]
|
|
public async Task
|
|
UpdateCity_WithInvalidUuid_WithAdminRole_ThrowsValidationException
|
|
(string uuid)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(new UpdateCityCommand()
|
|
{
|
|
Uuid = Guid.TryParse(uuid, out var guid) ? guid : Guid.Empty,
|
|
Name = "Name",
|
|
RegionUuid = Guid.NewGuid()
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
UpdateCity_WithNonExistentUuid_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 UpdateCityCommand()
|
|
{
|
|
Uuid = Guid.NewGuid(),
|
|
Name = "Different Name",
|
|
RegionUuid = addRegionResult.Uuid
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
UpdateCity_WithNonExistentRegionUuid_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);
|
|
|
|
var addCityResult = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = "Name",
|
|
RegionUuid = addRegionResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
mediator.Send(new UpdateCityCommand()
|
|
{
|
|
Uuid = addCityResult.Uuid,
|
|
Name = "Different Name",
|
|
RegionUuid = Guid.NewGuid()
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateCity_WithUserRole_ThrowsForbiddenException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.User });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new UpdateCityCommand(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with user role (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task UpdateCity_UnAuthnticatedUser_ThrowsForbiddenException()
|
|
{
|
|
SetUnAuthenticatedUser();
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new UpdateCityCommand(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with unauthenticated user
|
|
// (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task DeleteCity_WithAdminRole_CityDeleted()
|
|
{
|
|
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);
|
|
|
|
var addCityResult = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = "Name",
|
|
RegionUuid = addRegionResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
await mediator.Send(
|
|
new DeleteCityCommand()
|
|
{
|
|
Uuid = addCityResult.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
mediator.Send(new GetCityQuery()
|
|
{
|
|
Uuid = addCityResult.Uuid,
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("not an uuid")]
|
|
public async Task
|
|
DeleteCity_WithInvalidUuid_WithAdminRole_ThrowsValidationException
|
|
(string uuid)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(new DeleteCityCommand()
|
|
{
|
|
Uuid = Guid.TryParse(uuid, out var guid) ? guid : Guid.Empty
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
DeleteCity_WithNonExistentUuid_WithAdminRole_ThrowsNotFoundException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
mediator.Send(new DeleteCityCommand()
|
|
{
|
|
Uuid = Guid.NewGuid()
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteCity_WithUserRole_ThrowsForbiddenException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.User });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new DeleteCityCommand(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with user role (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task DeleteCity_UnAuthnticatedUser_ThrowsForbiddenException()
|
|
{
|
|
SetUnAuthenticatedUser();
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new DeleteCityCommand(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with unauthenticated user
|
|
// (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task GetCity_WithAdminRole_CityReturned()
|
|
{
|
|
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);
|
|
|
|
string cityName = "Name";
|
|
|
|
var addCityResult = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName,
|
|
RegionUuid = addRegionResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getCityResult = await mediator.Send(
|
|
new GetCityQuery()
|
|
{
|
|
Uuid = addCityResult.Uuid,
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getCityResult);
|
|
|
|
Assert.NotNull(getCityResult.Name);
|
|
Assert.Equal(cityName, getCityResult.Name);
|
|
|
|
Assert.NotNull(getCityResult.CountryName);
|
|
Assert.Equal(countryName, getCityResult.CountryName);
|
|
Assert.Equal(addCountryResult.Uuid, getCityResult.CountryUuid);
|
|
|
|
Assert.NotNull(getCityResult.RegionName);
|
|
Assert.Equal(regionName, getCityResult.RegionName);
|
|
Assert.Equal(addRegionResult.Uuid, getCityResult.RegionUuid);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("not an uuid")]
|
|
public async Task
|
|
GetCity_WithInvalidUuid_WithAdminRole_ThrowsValidationException
|
|
(string uuid)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(
|
|
new GetCityQuery()
|
|
{
|
|
Uuid = Guid.TryParse(uuid, out var guid) ? guid : Guid.Empty
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
GetCity_WithNonExistentUuid_WithAdminRole_ThrowsNotFoundException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
mediator.Send(
|
|
new GetCityQuery()
|
|
{
|
|
Uuid = Guid.NewGuid()
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetCity_WithUserRole_ThrowsForbiddenException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.User });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new GetCityQuery(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with user role (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task GetCity_UnAuthnticatedUser_ThrowsForbiddenException()
|
|
{
|
|
SetUnAuthenticatedUser();
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new GetCityQuery(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with unauthenticated user
|
|
// (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task GetCitiesPage_WithAdminRole_CitiesPageReturned()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
string countryName = "Country Name 1";
|
|
|
|
var addCountryResult = await mediator.Send(
|
|
new AddCountryCommand()
|
|
{
|
|
Name = countryName
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string regionName1 = "Region Name 1";
|
|
string regionName2 = "RegionName 1";
|
|
|
|
var addRegionResult1 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName1,
|
|
CountryUuid = addCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addRegionResult2 = await mediator.Send(
|
|
new AddRegionCommand()
|
|
{
|
|
Name = regionName2,
|
|
CountryUuid = addCountryResult.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
string cityName1 = "City Name 1";
|
|
string cityName2 = "City Name 2";
|
|
|
|
var addCityResult1 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName1,
|
|
RegionUuid = addRegionResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addCityResult2 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName2,
|
|
RegionUuid = addRegionResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getCitiesResult = await mediator.Send(
|
|
new GetCitiesPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getCitiesResult);
|
|
|
|
Assert.Equal(1, getCitiesResult.PageNumber);
|
|
Assert.Equal(2, getCitiesResult.TotalCount);
|
|
Assert.Equal(2, getCitiesResult.TotalPages);
|
|
Assert.True(getCitiesResult.HasNextPage);
|
|
Assert.False(getCitiesResult.HasPreviousPage);
|
|
Assert.NotNull(getCitiesResult.Items);
|
|
Assert.Single(getCitiesResult.Items);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First());
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addCityResult1.Name, getCitiesResult.Items.First().Name);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
countryName, getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult.Uuid, getCitiesResult.Items.First().CountryUuid);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
regionName1, getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
addRegionResult1.Uuid, getCitiesResult.Items.First().RegionUuid);
|
|
|
|
|
|
getCitiesResult = await mediator.Send(
|
|
new GetCitiesPageQuery()
|
|
{
|
|
PageNumber = 2,
|
|
PageSize = 1
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getCitiesResult);
|
|
|
|
Assert.Equal(2, getCitiesResult.PageNumber);
|
|
Assert.Equal(2, getCitiesResult.TotalCount);
|
|
Assert.Equal(2, getCitiesResult.TotalPages);
|
|
Assert.False(getCitiesResult.HasNextPage);
|
|
Assert.True(getCitiesResult.HasPreviousPage);
|
|
Assert.NotNull(getCitiesResult.Items);
|
|
Assert.Single(getCitiesResult.Items);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First());
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addCityResult2.Name, getCitiesResult.Items.First().Name);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
countryName, getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult.Uuid, getCitiesResult.Items.First().CountryUuid);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
regionName2, getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
addRegionResult2.Uuid, getCitiesResult.Items.First().RegionUuid);
|
|
|
|
|
|
getCitiesResult = await mediator.Send(
|
|
new GetCitiesPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getCitiesResult);
|
|
|
|
Assert.Equal(1, getCitiesResult.PageNumber);
|
|
Assert.Equal(2, getCitiesResult.TotalCount);
|
|
Assert.Equal(1, getCitiesResult.TotalPages);
|
|
Assert.False(getCitiesResult.HasNextPage);
|
|
Assert.False(getCitiesResult.HasPreviousPage);
|
|
Assert.NotNull(getCitiesResult.Items);
|
|
Assert.Equal(2, getCitiesResult.Items.Count());
|
|
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First());
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addCityResult1.Name, getCitiesResult.Items.First().Name);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
countryName, getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult.Uuid, getCitiesResult.Items.First().CountryUuid);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
regionName1, getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
addRegionResult1.Uuid, getCitiesResult.Items.First().RegionUuid);
|
|
|
|
|
|
Assert.NotNull(getCitiesResult.Items.Last());
|
|
|
|
Assert.NotNull(getCitiesResult.Items.Last().Name);
|
|
Assert.Equal(
|
|
addCityResult2.Name, getCitiesResult.Items.Last().Name);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.Last().CountryName);
|
|
Assert.Equal(
|
|
countryName, getCitiesResult.Items.Last().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult.Uuid, getCitiesResult.Items.Last().CountryUuid);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.Last().RegionName);
|
|
Assert.Equal(
|
|
regionName2, getCitiesResult.Items.Last().RegionName);
|
|
Assert.Equal(
|
|
addRegionResult2.Uuid, getCitiesResult.Items.Last().RegionUuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
GetCitiesPage_WithSearchByCountryName_WithAdminRole_SearchedCitiesPageReturned()
|
|
{
|
|
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 = "RegionName 1";
|
|
|
|
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);
|
|
|
|
string cityName1 = "City Name 1";
|
|
string cityName2 = "City Name 2";
|
|
|
|
var addCityResult1 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName1,
|
|
RegionUuid = addRegionResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addCityResult2 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName2,
|
|
RegionUuid = addRegionResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getCitiesResult = await mediator.Send(
|
|
new GetCitiesPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
Search = "cOuNtRy nAme 1"
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getCitiesResult);
|
|
|
|
Assert.Equal(1, getCitiesResult.PageNumber);
|
|
Assert.Equal(1, getCitiesResult.TotalCount);
|
|
Assert.Equal(1, getCitiesResult.TotalPages);
|
|
Assert.False(getCitiesResult.HasNextPage);
|
|
Assert.False(getCitiesResult.HasPreviousPage);
|
|
Assert.NotNull(getCitiesResult.Items);
|
|
Assert.Single(getCitiesResult.Items);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First());
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addCityResult1.Name, getCitiesResult.Items.First().Name);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
countryName1, getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult1.Uuid, getCitiesResult.Items.First().CountryUuid);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
regionName1, getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
addRegionResult1.Uuid, getCitiesResult.Items.First().RegionUuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
GetCitiesPage_WithSearchByRegionName_WithAdminRole_SearchedCitiesPageReturned()
|
|
{
|
|
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);
|
|
|
|
string cityName1 = "City Name 1";
|
|
string cityName2 = "City Name 2";
|
|
|
|
var addCityResult1 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName1,
|
|
RegionUuid = addRegionResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addCityResult2 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName2,
|
|
RegionUuid = addRegionResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getCitiesResult = await mediator.Send(
|
|
new GetCitiesPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
Search = "reGioN nAme 2"
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getCitiesResult);
|
|
|
|
Assert.Equal(1, getCitiesResult.PageNumber);
|
|
Assert.Equal(1, getCitiesResult.TotalCount);
|
|
Assert.Equal(1, getCitiesResult.TotalPages);
|
|
Assert.False(getCitiesResult.HasNextPage);
|
|
Assert.False(getCitiesResult.HasPreviousPage);
|
|
Assert.NotNull(getCitiesResult.Items);
|
|
Assert.Single(getCitiesResult.Items);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First());
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addCityResult2.Name, getCitiesResult.Items.First().Name);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
countryName2, getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult2.Uuid, getCitiesResult.Items.First().CountryUuid);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
regionName2, getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
addRegionResult2.Uuid, getCitiesResult.Items.First().RegionUuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
GetCitiesPage_WithSearchByCityName_WithAdminRole_SearchedCitiesPageReturned()
|
|
{
|
|
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);
|
|
|
|
string cityName1 = "City Name 1";
|
|
string cityName2 = "City Name 2";
|
|
|
|
var addCityResult1 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName1,
|
|
RegionUuid = addRegionResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addCityResult2 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName2,
|
|
RegionUuid = addRegionResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getCitiesResult = await mediator.Send(
|
|
new GetCitiesPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
Search = "cItY nAme 2"
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getCitiesResult);
|
|
|
|
Assert.Equal(1, getCitiesResult.PageNumber);
|
|
Assert.Equal(1, getCitiesResult.TotalCount);
|
|
Assert.Equal(1, getCitiesResult.TotalPages);
|
|
Assert.False(getCitiesResult.HasNextPage);
|
|
Assert.False(getCitiesResult.HasPreviousPage);
|
|
Assert.NotNull(getCitiesResult.Items);
|
|
Assert.Single(getCitiesResult.Items);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First());
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addCityResult2.Name, getCitiesResult.Items.First().Name);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
countryName2, getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult2.Uuid, getCitiesResult.Items.First().CountryUuid);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
regionName2, getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
addRegionResult2.Uuid, getCitiesResult.Items.First().RegionUuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
GetCitiesPage_WithSort_WithAdminRole_SortedCitiesPageReturned()
|
|
{
|
|
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);
|
|
|
|
string cityName1 = "City Name 1";
|
|
string cityName2 = "City Name 2";
|
|
|
|
var addCityResult1 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName1,
|
|
RegionUuid = addRegionResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addCityResult2 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName2,
|
|
RegionUuid = addRegionResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getCitiesResult = await mediator.Send(
|
|
new GetCitiesPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
Sort = "-countryName"
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getCitiesResult);
|
|
|
|
Assert.Equal(1, getCitiesResult.PageNumber);
|
|
Assert.Equal(2, getCitiesResult.TotalCount);
|
|
Assert.Equal(1, getCitiesResult.TotalPages);
|
|
Assert.False(getCitiesResult.HasNextPage);
|
|
Assert.False(getCitiesResult.HasPreviousPage);
|
|
Assert.NotNull(getCitiesResult.Items);
|
|
Assert.Equal(2, getCitiesResult.Items.Count());
|
|
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First());
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addCityResult2.Name, getCitiesResult.Items.First().Name);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
countryName2, getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult2.Uuid, getCitiesResult.Items.First().CountryUuid);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
regionName2, getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
addRegionResult2.Uuid, getCitiesResult.Items.First().RegionUuid);
|
|
|
|
|
|
Assert.NotNull(getCitiesResult.Items.Last());
|
|
|
|
Assert.NotNull(getCitiesResult.Items.Last().Name);
|
|
Assert.Equal(
|
|
addCityResult1.Name, getCitiesResult.Items.Last().Name);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.Last().CountryName);
|
|
Assert.Equal(
|
|
countryName1, getCitiesResult.Items.Last().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult1.Uuid, getCitiesResult.Items.Last().CountryUuid);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.Last().RegionName);
|
|
Assert.Equal(
|
|
regionName1, getCitiesResult.Items.Last().RegionName);
|
|
Assert.Equal(
|
|
addRegionResult1.Uuid, getCitiesResult.Items.Last().RegionUuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
GetCitiesPage_WithFilterByCountryUuid_WithAdminRole_SearchedCitiesPageReturned()
|
|
{
|
|
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);
|
|
|
|
string cityName1 = "City Name 1";
|
|
string cityName2 = "City Name 2";
|
|
|
|
var addCityResult1 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName1,
|
|
RegionUuid = addRegionResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addCityResult2 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName2,
|
|
RegionUuid = addRegionResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getCitiesResult = await mediator.Send(
|
|
new GetCitiesPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
CountryUuid = addCountryResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getCitiesResult);
|
|
|
|
Assert.Equal(1, getCitiesResult.PageNumber);
|
|
Assert.Equal(1, getCitiesResult.TotalCount);
|
|
Assert.Equal(1, getCitiesResult.TotalPages);
|
|
Assert.False(getCitiesResult.HasNextPage);
|
|
Assert.False(getCitiesResult.HasPreviousPage);
|
|
Assert.NotNull(getCitiesResult.Items);
|
|
Assert.Single(getCitiesResult.Items);
|
|
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First());
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addCityResult1.Name, getCitiesResult.Items.First().Name);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
countryName1, getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult1.Uuid, getCitiesResult.Items.First().CountryUuid);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
regionName1, getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
addRegionResult1.Uuid, getCitiesResult.Items.First().RegionUuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task
|
|
GetCitiesPage_WithFilterByRegionUuid_WithAdminRole_SearchedCitiesPageReturned()
|
|
{
|
|
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);
|
|
|
|
string cityName1 = "City Name 1";
|
|
string cityName2 = "City Name 2";
|
|
|
|
var addCityResult1 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName1,
|
|
RegionUuid = addRegionResult1.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var addCityResult2 = await mediator.Send(
|
|
new AddCityCommand()
|
|
{
|
|
Name = cityName2,
|
|
RegionUuid = addRegionResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
var getCitiesResult = await mediator.Send(
|
|
new GetCitiesPageQuery()
|
|
{
|
|
PageNumber = 1,
|
|
PageSize = 10,
|
|
RegionUuid = addRegionResult2.Uuid
|
|
}, TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(getCitiesResult);
|
|
|
|
Assert.Equal(1, getCitiesResult.PageNumber);
|
|
Assert.Equal(1, getCitiesResult.TotalCount);
|
|
Assert.Equal(1, getCitiesResult.TotalPages);
|
|
Assert.False(getCitiesResult.HasNextPage);
|
|
Assert.False(getCitiesResult.HasPreviousPage);
|
|
Assert.NotNull(getCitiesResult.Items);
|
|
Assert.Single(getCitiesResult.Items);
|
|
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First());
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().Name);
|
|
Assert.Equal(
|
|
addCityResult2.Name, getCitiesResult.Items.First().Name);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
countryName2, getCitiesResult.Items.First().CountryName);
|
|
Assert.Equal(
|
|
addCountryResult2.Uuid, getCitiesResult.Items.First().CountryUuid);
|
|
|
|
Assert.NotNull(getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
regionName2, getCitiesResult.Items.First().RegionName);
|
|
Assert.Equal(
|
|
addRegionResult2.Uuid, getCitiesResult.Items.First().RegionUuid);
|
|
}
|
|
|
|
[Theory]
|
|
// Length > 64 (65)
|
|
[InlineData("01234567890123456789012345678901234567890123456789012345678901234")]
|
|
public async Task
|
|
GetCitiesPage_WithInvalidSearch_WithAdminRole_ThrowsValidationException
|
|
(string search)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(
|
|
new GetCitiesPageQuery()
|
|
{
|
|
Search = search
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(int.MinValue)]
|
|
[InlineData(0)]
|
|
public async Task
|
|
GetCitiesPage_WithInvalidPageNumber_WithAdminRole_ThrowsValidationException
|
|
(int pageNumber)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(
|
|
new GetCitiesPageQuery()
|
|
{
|
|
PageNumber = pageNumber
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(int.MinValue)]
|
|
[InlineData(0)]
|
|
[InlineData(51)]
|
|
[InlineData(int.MaxValue)]
|
|
public async Task
|
|
GetCitiesPage_WithInvalidPageSize_WithAdminRole_ThrowsValidationException
|
|
(int pageSize)
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.Administrator });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ValidationException>(() =>
|
|
mediator.Send(
|
|
new GetCitiesPageQuery()
|
|
{
|
|
PageSize = pageSize
|
|
}, TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetCitiesPage_WithUserRole_ThrowsForbiddenException()
|
|
{
|
|
SetAuthenticatedUserRoles(new[] { IdentityRole.User });
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new GetCitiesPageQuery(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with user role (copy tests with admin role)
|
|
|
|
[Fact]
|
|
public async Task GetCitiesPage_UnAuthnticatedUser_ThrowsForbiddenException()
|
|
{
|
|
SetUnAuthenticatedUser();
|
|
|
|
var mediator = GetService<IMediator>();
|
|
|
|
await Assert.ThrowsAsync<ForbiddenException>(() =>
|
|
mediator.Send(
|
|
new GetCityQuery(),
|
|
TestContext.Current.CancellationToken));
|
|
}
|
|
|
|
// TODO: Add more tests with unauthenticated user
|
|
// (copy tests with admin role)
|
|
}
|