diff --git a/MagicCarpetProject/MagicCarpetTests/Infrastructure/CustomWebApplicationFactory.cs b/MagicCarpetProject/MagicCarpetTests/Infrastructure/CustomWebApplicationFactory.cs new file mode 100644 index 0000000..ba99c3c --- /dev/null +++ b/MagicCarpetProject/MagicCarpetTests/Infrastructure/CustomWebApplicationFactory.cs @@ -0,0 +1,36 @@ +using MagicCarpetContracts.Infrastructure; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetTests.Infrastructure; + +internal class CustomWebApplicationFactory : WebApplicationFactory + where TProgram : class +{ + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + builder.ConfigureServices(services => + { + var databaseConfig = services.SingleOrDefault(x => x.ServiceType == typeof(IConfigurationDatabase)); + if (databaseConfig is not null) + services.Remove(databaseConfig); + + var loggerFactory = services.SingleOrDefault(x => x.ServiceType == typeof(LoggerFactory)); + if (loggerFactory is not null) + services.Remove(loggerFactory); + + services.AddSingleton(); + }); + + builder.UseEnvironment("Development"); + + base.ConfigureWebHost(builder); + } +} diff --git a/MagicCarpetProject/MagicCarpetTests/Infrastructure/MagicCarpetDbContextExtensions.cs b/MagicCarpetProject/MagicCarpetTests/Infrastructure/MagicCarpetDbContextExtensions.cs new file mode 100644 index 0000000..ca2680b --- /dev/null +++ b/MagicCarpetProject/MagicCarpetTests/Infrastructure/MagicCarpetDbContextExtensions.cs @@ -0,0 +1,107 @@ +using MagicCarpetContracts.Enums; +using MagicCarpetDatabase; +using MagicCarpetDatabase.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetTests.Infrastructure; + +internal static class MagicCarpetDbContextExtensions +{ + public static Client InsertClientToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string? id = null, string fio = "test", string phoneNumber = "+7-777-777-77-77", double discountSize = 10) + { + var client = new Client() { Id = id ?? Guid.NewGuid().ToString(), FIO = fio, PhoneNumber = phoneNumber, DiscountSize = discountSize }; + dbContext.Clients.Add(client); + dbContext.SaveChanges(); + return client; + } + + public static Post InsertPostToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string? id = null, string postName = "test", PostType postType = PostType.Manager, double salary = 10, bool isActual = true, DateTime? changeDate = null) + { + var post = new Post() { Id = Guid.NewGuid().ToString(), PostId = id ?? Guid.NewGuid().ToString(), PostName = postName, PostType = postType, Salary = salary, IsActual = isActual, ChangeDate = changeDate ?? DateTime.UtcNow }; + dbContext.Posts.Add(post); + dbContext.SaveChanges(); + return post; + } + + public static Tour InsertTourToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string? id = null, string tourName = "test", TourType tourType = TourType.Beach, double price = 1) + { + var tour = new Tour() { Id = id ?? Guid.NewGuid().ToString(), TourName = tourName, Type = tourType, Price = price }; + dbContext.Tours.Add(tour); + dbContext.SaveChanges(); + return tour; + } + + public static TourHistory InsertTourHistoryToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string tourId, double price = 10, DateTime? changeDate = null) + { + var tourHistory = new TourHistory() { Id = Guid.NewGuid().ToString(), TourId = tourId, OldPrice = price, ChangeDate = changeDate ?? DateTime.UtcNow }; + dbContext.TourHistories.Add(tourHistory); + dbContext.SaveChanges(); + return tourHistory; + } + + public static Salary InsertSalaryToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string employeeId, double employeeSalary = 1, DateTime? salaryDate = null) + { + var salary = new Salary() { EmployeeId = employeeId, EmployeeSalary = employeeSalary, SalaryDate = salaryDate ?? DateTime.UtcNow }; + dbContext.Salaries.Add(salary); + dbContext.SaveChanges(); + return salary; + } + + public static Sale InsertSaleToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string employeeId, string? clientId = null, DateTime? saleDate = null, double sum = 1, DiscountType discountType = DiscountType.OnSale, double discount = 0, bool isCancel = false, List<(string, int, double)>? tours = null) + { + var sale = new Sale() { EmployeeId = employeeId, ClientId = clientId, SaleDate = saleDate ?? DateTime.UtcNow, Sum = sum, DiscountType = discountType, Discount = discount, IsCancel = isCancel, SaleTours = [] }; + if (tours is not null) + { + foreach (var elem in tours) + { + sale.SaleTours.Add(new SaleTour { TourId = elem.Item1, SaleId = sale.Id, Count = elem.Item2, Price = elem.Item3 }); + } + } + dbContext.Sales.Add(sale); + dbContext.SaveChanges(); + return sale; + } + + public static Employee InsertEmployeeToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string? id = null, string fio = "test", string email = "abc@gmail.com", string? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false) + { + var employee = new Employee() { Id = id ?? Guid.NewGuid().ToString(), FIO = fio, Email = email, PostId = postId ?? Guid.NewGuid().ToString(), BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-20), EmploymentDate = employmentDate ?? DateTime.UtcNow, IsDeleted = isDeleted }; + dbContext.Employees.Add(employee); + dbContext.SaveChanges(); + return employee; + } + + public static Client? GetClientFromDatabase(this MagicCarpetDbContext dbContext, string id) => dbContext.Clients.FirstOrDefault(x => x.Id == id); + + public static Post? GetPostFromDatabaseByPostId(this MagicCarpetDbContext dbContext, string id) => dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual); + + public static Post[] GetPostsFromDatabaseByPostId(this MagicCarpetDbContext dbContext, string id) => [.. dbContext.Posts.Where(x => x.PostId == id).OrderByDescending(x => x.ChangeDate)]; + + public static Tour? GetTourFromDatabaseById(this MagicCarpetDbContext dbContext, string id) => dbContext.Tours.FirstOrDefault(x => x.Id == id); + + public static Salary[] GetSalariesFromDatabaseByEmployeeId(this MagicCarpetDbContext dbContext, string id) => [.. dbContext.Salaries.Where(x => x.EmployeeId == id)]; + + public static Sale? GetSaleFromDatabaseById(this MagicCarpetDbContext dbContext, string id) => dbContext.Sales.Include(x => x.SaleTours).FirstOrDefault(x => x.Id == id); + + public static Sale[] GetSalesByClientId(this MagicCarpetDbContext dbContext, string? clientId) => [.. dbContext.Sales.Include(x => x.SaleTours).Where(x => x.ClientId == clientId)]; + + public static Employee? GetEmployeeFromDatabaseById(this MagicCarpetDbContext dbContext, string id) => dbContext.Employees.FirstOrDefault(x => x.Id == id); + + public static void RemoveClientsFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Clients\" CASCADE;"); + + public static void RemovePostsFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Posts\" CASCADE;"); + + public static void RemoveToursFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Tours\" CASCADE;"); + + public static void RemoveSalariesFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Salaries\" CASCADE;"); + + public static void RemoveSalesFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Sales\" CASCADE;"); + + public static void RemoveEmployeesFromDatabase(this MagicCarpetDbContext dbContext) => dbContext.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;"); + + private static void ExecuteSqlRaw(this MagicCarpetDbContext dbContext, string command) => dbContext.Database.ExecuteSqlRaw(command); +} \ No newline at end of file diff --git a/MagicCarpetProject/MagicCarpetTests/MagicCarpetTests.csproj b/MagicCarpetProject/MagicCarpetTests/MagicCarpetTests.csproj index 26c8651..d5903b7 100644 --- a/MagicCarpetProject/MagicCarpetTests/MagicCarpetTests.csproj +++ b/MagicCarpetProject/MagicCarpetTests/MagicCarpetTests.csproj @@ -1,7 +1,7 @@ - + - net8.0 + net9.0 enable enable @@ -9,19 +9,30 @@ true + + + PreserveNewest + true + PreserveNewest + + + + + + diff --git a/MagicCarpetProject/MagicCarpetTests/StoragesContractsTests/ClientStorageContractTests.cs b/MagicCarpetProject/MagicCarpetTests/StoragesContractsTests/ClientStorageContractTests.cs index 75d9300..7492a7a 100644 --- a/MagicCarpetProject/MagicCarpetTests/StoragesContractsTests/ClientStorageContractTests.cs +++ b/MagicCarpetProject/MagicCarpetTests/StoragesContractsTests/ClientStorageContractTests.cs @@ -16,13 +16,10 @@ namespace MagicCarpetTests.StoragesContracts; [TestFixture] internal class ClientStorageContractTests : BaseStorageContractTest { - private ClientStorageContarct _clientStorageContract; + private IClientStorageContract _clientStorageContract; [SetUp] - public void SetUp() - { - _clientStorageContract = new ClientStorageContarct(MagicCarpetDbContext); - } + public void SetUp() => _clientStorageContract = new ClientStorageContract(MagicCarpetDbContext); [TearDown] public void TearDown() diff --git a/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/BaseWebApiControllerTest.cs b/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/BaseWebApiControllerTest.cs new file mode 100644 index 0000000..eb40edd --- /dev/null +++ b/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/BaseWebApiControllerTest.cs @@ -0,0 +1,72 @@ +using MagicCarpetDatabase; +using MagicCarpetTests.Infrastructure; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Serilog; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace MagicCarpetTests.WebApiControllersTests; + +internal class BaseWebApiControllerTest +{ + private WebApplicationFactory _webApplication; + + protected HttpClient HttpClient { get; private set; } + + protected MagicCarpetDbContext MagicCarpetDbContext { get; private set; } + + protected static readonly JsonSerializerOptions JsonSerializerOptions = new() { PropertyNameCaseInsensitive = true }; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _webApplication = new CustomWebApplicationFactory(); + HttpClient = _webApplication + .WithWebHostBuilder(builder => + { + builder.ConfigureTestServices(services => + { + using var loggerFactory = new LoggerFactory(); + loggerFactory.AddSerilog(new LoggerConfiguration() + .ReadFrom.Configuration(new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json") + .Build()) + .CreateLogger()); + services.AddSingleton(loggerFactory); + }); + }) + .CreateClient(); + + var request = HttpClient.GetAsync("/login/user").GetAwaiter().GetResult(); + var data = request.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + HttpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {data}"); + + MagicCarpetDbContext = _webApplication.Services.GetRequiredService(); + MagicCarpetDbContext.Database.EnsureDeleted(); + MagicCarpetDbContext.Database.EnsureCreated(); + } + + [OneTimeTearDown] + public void OneTimeTearDown() + { + MagicCarpetDbContext?.Database.EnsureDeleted(); + MagicCarpetDbContext?.Dispose(); + HttpClient?.Dispose(); + _webApplication?.Dispose(); + } + + protected static async Task GetModelFromResponseAsync(HttpResponseMessage response) => + JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync(), JsonSerializerOptions); + + protected static StringContent MakeContent(object model) => + new(JsonSerializer.Serialize(model), Encoding.UTF8, "application/json"); +} diff --git a/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/ClientControllerTests.cs b/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/ClientControllerTests.cs new file mode 100644 index 0000000..4583d57 --- /dev/null +++ b/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/ClientControllerTests.cs @@ -0,0 +1,375 @@ +using MagicCarpetContracts.BindingModels; +using MagicCarpetContracts.ViewModels; +using MagicCarpetDatabase.Models; +using MagicCarpetTests.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetTests.WebApiControllersTests; + +[TestFixture] +internal class ClientControllerTests : BaseWebApiControllerTest +{ + [TearDown] + public void TearDown() + { + MagicCarpetDbContext.RemoveSalesFromDatabase(); + MagicCarpetDbContext.RemoveEmployeesFromDatabase(); + MagicCarpetDbContext.RemoveClientsFromDatabase(); + } + + [Test] + public async Task GetList_WhenHaveRecords_ShouldSuccess_Test() + { + //Arrange + var client = MagicCarpetDbContext.InsertClientToDatabaseAndReturn(phoneNumber: "+5-555-555-55-55"); + MagicCarpetDbContext.InsertClientToDatabaseAndReturn(phoneNumber: "+6-666-666-66-66"); + MagicCarpetDbContext.InsertClientToDatabaseAndReturn(phoneNumber: "+7-777-777-77-77"); + //Act + var response = await HttpClient.GetAsync("/api/clients"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.Multiple(() => + { + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(3)); + }); + AssertElement(data.First(x => x.Id == client.Id), client); + } + + [Test] + public async Task GetList_WhenNoRecords_ShouldSuccess_Test() + { + //Act + var response = await HttpClient.GetAsync("/api/clients"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.Multiple(() => + { + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(0)); + }); + } + + [Test] + public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test() + { + //Arrange + var client = MagicCarpetDbContext.InsertClientToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/clients/{client.Id}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + AssertElement(await GetModelFromResponseAsync(response), client); + } + + [Test] + public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test() + { + //Arrange + MagicCarpetDbContext.InsertClientToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/clients/{Guid.NewGuid()}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task GetElement_ByFIO_WhenHaveRecord_ShouldSuccess_Test() + { + //Arrange + var client = MagicCarpetDbContext.InsertClientToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/clients/{client.FIO}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + AssertElement(await GetModelFromResponseAsync(response), client); + } + + [Test] + public async Task GetElement_ByFIO_WhenNoRecord_ShouldNotFound_Test() + { + //Arrange + MagicCarpetDbContext.InsertClientToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/clients/New%20Fio"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task GetElement_ByPhoneNumber_WhenHaveRecord_ShouldSuccess_Test() + { + //Arrange + var client = MagicCarpetDbContext.InsertClientToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/clients/{client.PhoneNumber}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + AssertElement(await GetModelFromResponseAsync(response), client); + } + + [Test] + public async Task GetElement_ByPhoneNumber_WhenNoRecord_ShouldNotFound_Test() + { + //Arrange + MagicCarpetDbContext.InsertClientToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/clients/+8-888-888-88-88"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task Post_ShouldSuccess_Test() + { + //Arrange + MagicCarpetDbContext.InsertClientToDatabaseAndReturn(); + var clientModel = CreateBindingModel(); + //Act + var response = await HttpClient.PostAsync($"/api/clients", MakeContent(clientModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + AssertElement(MagicCarpetDbContext.GetClientFromDatabase(clientModel.Id!), clientModel); + } + + [Test] + public async Task Post_WhenHaveRecordWithSameId_ShouldBadRequest_Test() + { + //Arrange + var clientModel = CreateBindingModel(); + MagicCarpetDbContext.InsertClientToDatabaseAndReturn(clientModel.Id); + //Act + var response = await HttpClient.PostAsync($"/api/clients", MakeContent(clientModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Post_WhenHaveRecordWithSamePhoneNumber_ShouldBadRequest_Test() + { + //Arrange + var clientModel = CreateBindingModel(); + MagicCarpetDbContext.InsertClientToDatabaseAndReturn(phoneNumber: clientModel.PhoneNumber!); + //Act + var response = await HttpClient.PostAsync($"/api/clients", MakeContent(clientModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test() + { + //Arrange + var clientModelWithIdIncorrect = new ClientBindingModel { Id = "Id", FIO = "fio", PhoneNumber = "+7-111-111-11-11", DiscountSize = 10 }; + var clientModelWithFioIncorrect = new ClientBindingModel { Id = Guid.NewGuid().ToString(), FIO = string.Empty, PhoneNumber = "+7-111-111-11-11", DiscountSize = 10 }; + var clientModelWithPhoneNumberIncorrect = new ClientBindingModel { Id = Guid.NewGuid().ToString(), FIO = "fio", PhoneNumber = "71", DiscountSize = 10 }; + //Act + var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/clients", MakeContent(clientModelWithIdIncorrect)); + var responseWithFioIncorrect = await HttpClient.PostAsync($"/api/clients", MakeContent(clientModelWithFioIncorrect)); + var responseWithPhoneNumberIncorrect = await HttpClient.PostAsync($"/api/clients", MakeContent(clientModelWithPhoneNumberIncorrect)); + //Assert + Assert.Multiple(() => + { + Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect"); + Assert.That(responseWithFioIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Fio is incorrect"); + Assert.That(responseWithPhoneNumberIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "PhoneNumber is incorrect"); + }); + } + + [Test] + public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PostAsync($"/api/clients", MakeContent(string.Empty)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Post_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PostAsync($"/api/clients", MakeContent(new { Data = "test", Position = 10 })); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_ShouldSuccess_Test() + { + //Arrange + var clientModel = CreateBindingModel(fio: "new fio"); + MagicCarpetDbContext.InsertClientToDatabaseAndReturn(clientModel.Id); + //Act + var response = await HttpClient.PutAsync($"/api/clients", MakeContent(clientModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + MagicCarpetDbContext.ChangeTracker.Clear(); + AssertElement(MagicCarpetDbContext.GetClientFromDatabase(clientModel.Id!), clientModel); + } + + [Test] + public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test() + { + //Arrange + var clientModel = CreateBindingModel(fio: "new fio"); + MagicCarpetDbContext.InsertClientToDatabaseAndReturn(); + //Act + var response = await HttpClient.PutAsync($"/api/clients", MakeContent(clientModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_WhenHaveRecordWithSamePhoneNumber_ShouldBadRequest_Test() + { + //Arrange + var clientModel = CreateBindingModel(fio: "new fio"); + MagicCarpetDbContext.InsertClientToDatabaseAndReturn(clientModel.Id); + MagicCarpetDbContext.InsertClientToDatabaseAndReturn(phoneNumber: clientModel.PhoneNumber!); + //Act + var response = await HttpClient.PostAsync($"/api/clients", MakeContent(clientModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_WhenDataIsIncorrect_ShouldBadRequest_Test() + { + //Arrange + var clientModelWithIdIncorrect = new ClientBindingModel { Id = "Id", FIO = "fio", PhoneNumber = "+7-111-111-11-11", DiscountSize = 10 }; + var clientModelWithFioIncorrect = new ClientBindingModel { Id = Guid.NewGuid().ToString(), FIO = string.Empty, PhoneNumber = "+7-111-111-11-11", DiscountSize = 10 }; + var clientModelWithPhoneNumberIncorrect = new ClientBindingModel { Id = Guid.NewGuid().ToString(), FIO = "fio", PhoneNumber = "71", DiscountSize = 10 }; + //Act + var responseWithIdIncorrect = await HttpClient.PutAsync($"/api/clients", MakeContent(clientModelWithIdIncorrect)); + var responseWithFioIncorrect = await HttpClient.PutAsync($"/api/clients", MakeContent(clientModelWithFioIncorrect)); + var responseWithPhoneNumberIncorrect = await HttpClient.PutAsync($"/api/clients", MakeContent(clientModelWithPhoneNumberIncorrect)); + //Assert + Assert.Multiple(() => + { + Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect"); + Assert.That(responseWithFioIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Fio is incorrect"); + Assert.That(responseWithPhoneNumberIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "PhoneNumber is incorrect"); + }); + } + + [Test] + public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PutAsync($"/api/clients", MakeContent(string.Empty)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PutAsync($"/api/clients", MakeContent(new { Data = "test", Position = 10 })); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Delete_ShouldSuccess_Test() + { + //Arrange + var clientId = Guid.NewGuid().ToString(); + MagicCarpetDbContext.InsertClientToDatabaseAndReturn(clientId); + //Act + var response = await HttpClient.DeleteAsync($"/api/clients/{clientId}"); + //Assert + Assert.Multiple(() => + { + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + Assert.That(MagicCarpetDbContext.GetClientFromDatabase(clientId), Is.Null); + }); + } + + [Test] + public async Task Delete_WhenHaveSalesByThisClient_ShouldSuccess_Test() + { + //Arrange + var client = MagicCarpetDbContext.InsertClientToDatabaseAndReturn(); + var employee = MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(employee.Id, client.Id); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(employee.Id, client.Id); + var salesBeforeDelete = MagicCarpetDbContext.GetSalesByClientId(client.Id); + //Act + var response = await HttpClient.DeleteAsync($"/api/clients/{client.Id}"); + //Assert + MagicCarpetDbContext.ChangeTracker.Clear(); + var element = MagicCarpetDbContext.GetClientFromDatabase(client.Id); + var salesAfterDelete = MagicCarpetDbContext.GetSalesByClientId(client.Id); + var salesNoClients = MagicCarpetDbContext.GetSalesByClientId(null); + Assert.Multiple(() => + { + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + Assert.That(element, Is.Null); + Assert.That(salesBeforeDelete, Has.Length.EqualTo(2)); + Assert.That(salesAfterDelete, Is.Empty); + Assert.That(salesNoClients, Has.Length.EqualTo(2)); + }); + } + + [Test] + public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test() + { + //Arrange + MagicCarpetDbContext.InsertClientToDatabaseAndReturn(); + //Act + var response = await HttpClient.DeleteAsync($"/api/clients/{Guid.NewGuid()}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Delete_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.DeleteAsync($"/api/clients/id"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + private static void AssertElement(ClientViewModel? actual, Client expected) + { + Assert.That(actual, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(actual.Id, Is.EqualTo(expected.Id)); + Assert.That(actual.FIO, Is.EqualTo(expected.FIO)); + Assert.That(actual.PhoneNumber, Is.EqualTo(expected.PhoneNumber)); + Assert.That(actual.DiscountSize, Is.EqualTo(expected.DiscountSize)); + }); + } + + private static ClientBindingModel CreateBindingModel(string? id = null, string fio = "fio", string phoneNumber = "+7-666-666-66-66", double discountSize = 10) => + new() + { + Id = id ?? Guid.NewGuid().ToString(), + FIO = fio, + PhoneNumber = phoneNumber, + DiscountSize = discountSize + }; + + private static void AssertElement(Client? actual, ClientBindingModel expected) + { + Assert.That(actual, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(actual.Id, Is.EqualTo(expected.Id)); + Assert.That(actual.FIO, Is.EqualTo(expected.FIO)); + Assert.That(actual.PhoneNumber, Is.EqualTo(expected.PhoneNumber)); + Assert.That(actual.DiscountSize, Is.EqualTo(expected.DiscountSize)); + }); + } +} \ No newline at end of file diff --git a/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/EmployeeControllerTests.cs b/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/EmployeeControllerTests.cs new file mode 100644 index 0000000..11f79c3 --- /dev/null +++ b/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/EmployeeControllerTests.cs @@ -0,0 +1,557 @@ +using MagicCarpetContracts.BindingModels; +using MagicCarpetContracts.ViewModels; +using MagicCarpetDatabase; +using MagicCarpetDatabase.Models; +using MagicCarpetTests.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetTests.WebApiControllersTests; + +[TestFixture] +internal class EmployeeControllerTests : BaseWebApiControllerTest +{ + private Post _post; + + [SetUp] + public void SetUp() + { + _post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(); + } + + [TearDown] + public void TearDown() + { + MagicCarpetDbContext.RemovePostsFromDatabase(); + MagicCarpetDbContext.RemoveEmployeesFromDatabase(); + } + + [Test] + public async Task GetList_WhenHaveRecords_ShouldSuccess_Test() + { + //Arrange + var employee = MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 1", postId: _post.PostId) + .AddPost(_post); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 2", postId: _post.PostId); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 3", postId: _post.PostId); + //Act + var response = await HttpClient.GetAsync("/api/employees/getrecords"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.Multiple(() => + { + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(3)); + }); + AssertElement(data.First(x => x.Id == employee.Id), employee); + } + + [Test] + public async Task GetList_WhenNoRecords_ShouldSuccess_Test() + { + //Act + var response = await HttpClient.GetAsync("/api/employees/getrecords"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.Multiple(() => + { + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(0)); + }); + } + + [Test] + public async Task GetList_OnlyActual_ShouldSuccess_Test() + { + //Arrange + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 1", postId: _post.PostId, isDeleted: true); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 2", postId: _post.PostId, isDeleted: false); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 3", postId: _post.PostId, isDeleted: false); + //Act + var response = await HttpClient.GetAsync("/api/employees/getrecords?includeDeleted=false"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.That(data, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(data, Has.Count.EqualTo(2)); + Assert.That(data.All(x => !x.IsDeleted)); + }); + } + + [Test] + public async Task GetList_IncludeNoActual_ShouldSuccess_Test() + { + //Arrange + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 1", postId: _post.PostId, isDeleted: true); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 2", postId: _post.PostId, isDeleted: true); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 3", postId: _post.PostId, isDeleted: false); + //Act + var response = await HttpClient.GetAsync("/api/employees/getrecords?includeDeleted=true"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.That(data, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(data, Has.Count.EqualTo(3)); + Assert.That(data.Any(x => x.IsDeleted)); + Assert.That(data.Any(x => !x.IsDeleted)); + }); + } + + [Test] + public async Task GetList_ByPostId_ShouldSuccess_Test() + { + //Arrange + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 1", postId: _post.PostId); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 2", postId: _post.PostId); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 3", postId: _post.PostId, isDeleted: true); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 4"); + //Act + var response = await HttpClient.GetAsync($"/api/employees/getpostrecords?id={_post.PostId}&includeDeleted=true"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.That(data, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(data, Has.Count.EqualTo(3)); + Assert.That(data.All(x => x.PostId == _post.PostId)); + }); + } + + [Test] + public async Task GetList_ByPostId_WhenNoRecords_ShouldSuccess_Test() + { + //Arrange + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 1", postId: _post.PostId); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 4"); + //Act + var response = await HttpClient.GetAsync($"/api/employees/getpostrecords?id={Guid.NewGuid()}&includeDeleted=true"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(0)); + } + + [Test] + public async Task GetList_ByPostId_WhenIdIsIncorrect_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.GetAsync($"/api/employees/getpostrecords?id=id&includeDeleted=true"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task GetList_ByBirthDate_ShouldSuccess_Test() + { + //Arrange + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 1", birthDate: DateTime.UtcNow.AddYears(-25)); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 2", birthDate: DateTime.UtcNow.AddYears(-21)); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 3", birthDate: DateTime.UtcNow.AddYears(-20), isDeleted: true); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 4", birthDate: DateTime.UtcNow.AddYears(-19)); + //Act + var response = await HttpClient.GetAsync($"/api/employees/getbirthdaterecords?fromDate={DateTime.UtcNow.AddYears(-21).AddMinutes(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddYears(-20).AddMinutes(1):MM/dd/yyyy HH:mm:ss}&includeDeleted=true"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.That(data, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(data, Has.Count.EqualTo(2)); + }); + } + + [Test] + public async Task GetList_ByBirthDate_WhenDateIsIncorrect_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.GetAsync($"/api/employees/getbirthdaterecords?fromDate={DateTime.UtcNow.AddMinutes(1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddMinutes(-1):MM/dd/yyyy HH:mm:ss}&includeDeleted=true"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task GetList_ByEmploymentDate_ShouldSuccess_Test() + { + //Arrange + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 1", employmentDate: DateTime.UtcNow.AddDays(-2)); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 2", employmentDate: DateTime.UtcNow.AddDays(-1)); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 3", employmentDate: DateTime.UtcNow.AddDays(1), isDeleted: true); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "fio 4", employmentDate: DateTime.UtcNow.AddDays(2)); + //Act + var response = await HttpClient.GetAsync($"/api/employees/getemploymentrecords?fromDate={DateTime.UtcNow.AddDays(-1).AddMinutes(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1).AddMinutes(1):MM/dd/yyyy HH:mm:ss}&includeDeleted=true"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.That(data, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(data, Has.Count.EqualTo(2)); + }); + } + + [Test] + public async Task GetList_ByEmploymentDate_WhenDateIsIncorrect_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.GetAsync($"/api/employees/getemploymentrecords?fromDate={DateTime.UtcNow.AddMinutes(1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddMinutes(-1):MM/dd/yyyy HH:mm:ss}&includeDeleted=true"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test() + { + //Arrange + var employee = MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(postId: _post.PostId).AddPost(_post); + //Act + var response = await HttpClient.GetAsync($"/api/employees/getrecord/{employee.Id}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + AssertElement(await GetModelFromResponseAsync(response), employee); + } + + [Test] + public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test() + { + //Arrange + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/employees/getrecord/{Guid.NewGuid()}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task GetElement_ById_WhenRecordWasDeleted_ShouldNotFound_Test() + { + //Arrange + var employee = MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(postId: _post.PostId, isDeleted: true); + //Act + var response = await HttpClient.GetAsync($"/api/employees/getrecord/{employee.Id}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task GetElement_ByFIO_WhenHaveRecord_ShouldSuccess_Test() + { + //Arrange + var employee = MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(postId: _post.PostId).AddPost(_post); + //Act + var response = await HttpClient.GetAsync($"/api/employees/getrecord/{employee.FIO}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + AssertElement(await GetModelFromResponseAsync(response), employee); + } + + [Test] + public async Task GetElement_ByFIO_WhenNoRecord_ShouldNotFound_Test() + { + //Arrange + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/employees/getrecord/New%20Fio"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task GetElement_ByFIO_WhenRecordWasDeleted_ShouldNotFound_Test() + { + //Arrange + var employee = MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(postId: _post.PostId, isDeleted: true); + //Act + var response = await HttpClient.GetAsync($"/api/employees/getrecord/{employee.FIO}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task GetElement_ByEmail_WhenHaveRecord_ShouldSuccess_Test() + { + //Arrange + var employee = MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(postId: _post.PostId).AddPost(_post); + //Act + var response = await HttpClient.GetAsync($"/api/employees/getrecord/{employee.Email}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + AssertElement(await GetModelFromResponseAsync(response), employee); + } + + [Test] + public async Task GetElement_ByEmail_WhenNoRecord_ShouldNotFound_Test() + { + //Arrange + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/employees/getrecord/New%20Email"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task GetElement_ByEmail_WhenRecordWasDeleted_ShouldNotFound_Test() + { + //Arrange + var employee = MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(postId: _post.PostId, isDeleted: true); + //Act + var response = await HttpClient.GetAsync($"/api/employees/getrecord/{employee.Email}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task Post_ShouldSuccess_Test() + { + //Arrange + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(); + var employeeModel = CreateModel(_post.Id); + //Act + var response = await HttpClient.PostAsync($"/api/employees/register", MakeContent(employeeModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + AssertElement(MagicCarpetDbContext.GetEmployeeFromDatabaseById(employeeModel.Id!), employeeModel); + } + + [Test] + public async Task Post_WhenHaveRecordWithSameId_ShouldBadRequest_Test() + { + //Arrange + var employeeModel = CreateModel(_post.Id); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(employeeModel.Id); + //Act + var response = await HttpClient.PostAsync($"/api/employees/register", MakeContent(employeeModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test() + { + //Arrange + var employeeModelWithIdIncorrect = new EmployeeBindingModel { Id = "Id", FIO = "fio", BirthDate = DateTime.UtcNow.AddYears(-22), EmploymentDate = DateTime.UtcNow.AddDays(-5), PostId = _post.Id }; + var employeeModelWithFioIncorrect = new EmployeeBindingModel { Id = Guid.NewGuid().ToString(), FIO = string.Empty, BirthDate = DateTime.UtcNow.AddYears(-22), EmploymentDate = DateTime.UtcNow.AddDays(-5), PostId = _post.Id }; + var employeeModelWithEmailIncorrect = new EmployeeBindingModel { Id = Guid.NewGuid().ToString(), FIO = "fio", Email = "abc", BirthDate = DateTime.UtcNow.AddYears(-22), EmploymentDate = DateTime.UtcNow.AddDays(-5), PostId = _post.Id }; + var employeeModelWithPostIdIncorrect = new EmployeeBindingModel { Id = Guid.NewGuid().ToString(), FIO = "fio", BirthDate = DateTime.UtcNow.AddYears(-22), EmploymentDate = DateTime.UtcNow.AddDays(-5), PostId = "Id" }; + var employeeModelWithBirthDateIncorrect = new EmployeeBindingModel { Id = Guid.NewGuid().ToString(), FIO = "fio", BirthDate = DateTime.UtcNow.AddYears(-15), EmploymentDate = DateTime.UtcNow.AddDays(-5), PostId = _post.Id }; + //Act + var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/employees/register", MakeContent(employeeModelWithIdIncorrect)); + var responseWithFioIncorrect = await HttpClient.PostAsync($"/api/employees/register", MakeContent(employeeModelWithFioIncorrect)); + var responseWithEmailIncorrect = await HttpClient.PostAsync($"/api/employees/register", MakeContent(employeeModelWithEmailIncorrect)); + var responseWithPostIdIncorrect = await HttpClient.PostAsync($"/api/employees/register", MakeContent(employeeModelWithPostIdIncorrect)); + var responseWithBirthDateIncorrect = await HttpClient.PostAsync($"/api/employees/register", MakeContent(employeeModelWithBirthDateIncorrect)); + //Assert + Assert.Multiple(() => + { + Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect"); + Assert.That(responseWithFioIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Fio is incorrect"); + Assert.That(responseWithEmailIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Email is incorrect"); + Assert.That(responseWithPostIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "PostId is incorrect"); + Assert.That(responseWithBirthDateIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "BirthDate is incorrect"); + }); + } + + [Test] + public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PostAsync($"/api/employees/register", MakeContent(string.Empty)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Post_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PostAsync($"/api/employees/register", MakeContent(new { Data = "test", Position = 10 })); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_ShouldSuccess_Test() + { + //Arrange + var employeeModel = CreateModel(_post.Id); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(employeeModel.Id); + //Act + var response = await HttpClient.PutAsync($"/api/employees/changeinfo", MakeContent(employeeModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + MagicCarpetDbContext.ChangeTracker.Clear(); + AssertElement(MagicCarpetDbContext.GetEmployeeFromDatabaseById(employeeModel.Id!), employeeModel); + } + + [Test] + public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test() + { + //Arrange + var employeeModel = CreateModel(_post.Id, fio: "new fio"); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(); + //Act + var response = await HttpClient.PutAsync($"/api/employees/changeinfo", MakeContent(employeeModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_WhenRecordWasDeleted_ShouldBadRequest_Test() + { + //Arrange + var employeeModel = CreateModel(_post.Id, fio: "new fio"); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(employeeModel.Id, isDeleted: true); + //Act + var response = await HttpClient.PutAsync($"/api/employees/changeinfo", MakeContent(employeeModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_WhenDataIsIncorrect_ShouldBadRequest_Test() + { + //Arrange + var employeeModelWithIdIncorrect = new EmployeeBindingModel { Id = "Id", FIO = "fio", Email = "abc@mail.ru", BirthDate = DateTime.UtcNow.AddYears(-22), EmploymentDate = DateTime.UtcNow.AddDays(-5), PostId = _post.Id }; + var employeeModelWithFioIncorrect = new EmployeeBindingModel { Id = Guid.NewGuid().ToString(), FIO = string.Empty, Email = "abc@mail.ru", BirthDate = DateTime.UtcNow.AddYears(-22), EmploymentDate = DateTime.UtcNow.AddDays(-5), PostId = _post.Id }; + var employeeModelWithEmailIncorrect = new EmployeeBindingModel { Id = Guid.NewGuid().ToString(), FIO = "fio", Email = "abc", BirthDate = DateTime.UtcNow.AddYears(-22), EmploymentDate = DateTime.UtcNow.AddDays(-5), PostId = _post.Id }; + var employeeModelWithPostIdIncorrect = new EmployeeBindingModel { Id = Guid.NewGuid().ToString(), FIO = "fio", Email = "abc@mail.ru", BirthDate = DateTime.UtcNow.AddYears(-22), EmploymentDate = DateTime.UtcNow.AddDays(-5), PostId = "Id" }; + var employeeModelWithBirthDateIncorrect = new EmployeeBindingModel { Id = Guid.NewGuid().ToString(), FIO = "fio", Email = "abc@mail.ru", BirthDate = DateTime.UtcNow.AddYears(-15), EmploymentDate = DateTime.UtcNow.AddDays(-5), PostId = _post.Id }; + //Act + var responseWithIdIncorrect = await HttpClient.PutAsync($"/api/employees/changeinfo", MakeContent(employeeModelWithIdIncorrect)); + var responseWithFioIncorrect = await HttpClient.PutAsync($"/api/employees/changeinfo", MakeContent(employeeModelWithFioIncorrect)); + var responseWithEmailIncorrect = await HttpClient.PutAsync($"/api/employees/changeinfo", MakeContent(employeeModelWithEmailIncorrect)); + var responseWithPostIdIncorrect = await HttpClient.PutAsync($"/api/employees/changeinfo", MakeContent(employeeModelWithPostIdIncorrect)); + var responseWithBirthDateIncorrect = await HttpClient.PutAsync($"/api/employees/changeinfo", MakeContent(employeeModelWithBirthDateIncorrect)); + //Assert + Assert.Multiple(() => + { + Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect"); + Assert.That(responseWithFioIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Fio is incorrect"); + Assert.That(responseWithEmailIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Email is incorrect"); + Assert.That(responseWithPostIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "PostId is incorrect"); + Assert.That(responseWithBirthDateIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "BirthDate is incorrect"); + }); + } + + [Test] + public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PutAsync($"/api/employees/changeinfo", MakeContent(string.Empty)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PutAsync($"/api/employees/changeinfo", MakeContent(new { Data = "test", Position = 10 })); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Delete_ShouldSuccess_Test() + { + //Arrange + var employeeId = Guid.NewGuid().ToString(); + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(employeeId); + //Act + var response = await HttpClient.DeleteAsync($"/api/employees/delete/{employeeId}"); + MagicCarpetDbContext.ChangeTracker.Clear(); + //Assert + Assert.Multiple(() => + { + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + Assert.That(MagicCarpetDbContext.GetEmployeeFromDatabaseById(employeeId)!.IsDeleted); + }); + } + + [Test] + public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test() + { + //Arrange + MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(); + //Act + var response = await HttpClient.DeleteAsync($"/api/employees/delete/{Guid.NewGuid()}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Delete_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.DeleteAsync($"/api/employees/delete/id"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Delete_WhenRecordWasDeleted_ShouldBadRequest_Test() + { + //Arrange + var employeeId = MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(isDeleted: true).Id; + //Act + var response = await HttpClient.DeleteAsync($"/api/employees/delete/{employeeId}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + private static void AssertElement(EmployeeViewModel? actual, Employee expected) + { + Assert.That(actual, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(actual.Id, Is.EqualTo(expected.Id)); + Assert.That(actual.PostId, Is.EqualTo(expected.PostId)); + Assert.That(actual.PostName, Is.EqualTo(expected.Post!.PostName)); + Assert.That(actual.FIO, Is.EqualTo(expected.FIO)); + Assert.That(actual.Email, Is.EqualTo(expected.Email)); + Assert.That(actual.BirthDate.ToString(), Is.EqualTo(expected.BirthDate.ToString())); + Assert.That(actual.EmploymentDate.ToString(), Is.EqualTo(expected.EmploymentDate.ToString())); + Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted)); + }); + } + + private static EmployeeBindingModel CreateModel(string postId, string? id = null, string fio = "fio", string email = "abc@gmail.com", DateTime? birthDate = null, DateTime? employmentDate = null) + { + return new() + { + Id = id ?? Guid.NewGuid().ToString(), + FIO = fio, + Email = email, + BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-22), + EmploymentDate = employmentDate ?? DateTime.UtcNow.AddDays(-5), + PostId = postId + }; + } + + private static void AssertElement(Employee? actual, EmployeeBindingModel expected) + { + Assert.That(actual, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(actual.Id, Is.EqualTo(expected.Id)); + Assert.That(actual.PostId, Is.EqualTo(expected.PostId)); + Assert.That(actual.FIO, Is.EqualTo(expected.FIO)); + Assert.That(actual.Email, Is.EqualTo(expected.Email)); + Assert.That(actual.BirthDate.ToString(), Is.EqualTo(expected.BirthDate.ToString())); + Assert.That(actual.EmploymentDate.ToString(), Is.EqualTo(expected.EmploymentDate.ToString())); + Assert.That(!actual.IsDeleted); + }); + } +} diff --git a/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/PostControllerTests.cs b/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/PostControllerTests.cs new file mode 100644 index 0000000..30f2e19 --- /dev/null +++ b/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/PostControllerTests.cs @@ -0,0 +1,460 @@ +using MagicCarpetContracts.BindingModels; +using MagicCarpetContracts.Enums; +using MagicCarpetContracts.ViewModels; +using MagicCarpetDatabase.Models; +using MagicCarpetTests.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetTests.WebApiControllersTests; + +[TestFixture] +internal class PostControllerTests : BaseWebApiControllerTest +{ + [TearDown] + public void TearDown() + { + MagicCarpetDbContext.RemovePostsFromDatabase(); + } + + [Test] + public async Task GetRecords_WhenHaveRecords_ShouldSuccess_Test() + { + //Arrange + var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 1"); + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 2"); + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 3"); + //Act + var response = await HttpClient.GetAsync("/api/posts"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.Multiple(() => + { + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(3)); + }); + AssertElement(data.First(x => x.Id == post.PostId), post); + } + + [Test] + public async Task GetHistory_WhenHaveRecords_ShouldSuccess_Test() + { + //Arrange + var postId = Guid.NewGuid().ToString(); + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 1", isActual: true); + var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true); + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false); + //Act + var response = await HttpClient.GetAsync($"/api/posthistory/{postId}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.Multiple(() => + { + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(2)); + }); + AssertElement(data.First(x => x.Id == post.PostId), post); + } + + [Test] + public async Task GetHistory_WhenNoRecords_ShouldSuccess_Test() + { + //Arrange + var postId = Guid.NewGuid().ToString(); + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 1", isActual: true); + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true); + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false); + //Act + var response = await HttpClient.GetAsync($"/api/posthistory/{Guid.NewGuid()}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.Multiple(() => + { + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(0)); + }); + } + + [Test] + public async Task GetHistory_WhenWrongData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.GetAsync($"/api/posthistory/id"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test() + { + //Arrange + var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/posts/{post.PostId}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + AssertElement(await GetModelFromResponseAsync(response), post); + } + + [Test] + public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test() + { + //Arrange + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/posts/{Guid.NewGuid()}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task GetElement_ById_WhenRecordWasDeleted_ShouldNotFound_Test() + { + //Arrange + var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: false); + //Act + var response = await HttpClient.GetAsync($"/api/posts/{post.PostId}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task GetElement_ByName_WhenHaveRecord_ShouldSuccess_Test() + { + //Arrange + var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/posts/{post.PostName}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + AssertElement(await GetModelFromResponseAsync(response), post); + } + + [Test] + public async Task GetElement_ByName_WhenNoRecord_ShouldNotFound_Test() + { + //Arrange + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/posts/New%20Name"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task GetElement_ByName_WhenRecordWasDeleted_ShouldNotFound_Test() + { + //Arrange + var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: false); + //Act + var response = await HttpClient.GetAsync($"/api/posts/{post.PostName}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task Post_ShouldSuccess_Test() + { + //Arrange + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(); + var postModel = CreateModel(); + //Act + var response = await HttpClient.PostAsync($"/api/posts", MakeContent(postModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + AssertElement(MagicCarpetDbContext.GetPostFromDatabaseByPostId(postModel.Id!), postModel); + } + + [Test] + public async Task Post_WhenHaveRecordWithSameId_ShouldBadRequest_Test() + { + //Arrange + var postModel = CreateModel(); + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postModel.Id); + //Act + var response = await HttpClient.PostAsync($"/api/posts", MakeContent(postModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Post_WhenHaveRecordWithSameName_ShouldBadRequest_Test() + { + //Arrange + var postModel = CreateModel(postName: "unique name"); + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: postModel.PostName!); + //Act + var response = await HttpClient.PostAsync($"/api/posts", MakeContent(postModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test() + { + //Arrange + var postModelWithIdIncorrect = new PostBindingModel { Id = "Id", PostName = "name", PostType = PostType.Manager.ToString(), Salary = 10 }; + var postModelWithNameIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manager.ToString(), Salary = 10 }; + var postModelWithPostTypeIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = string.Empty, Salary = 10 }; + var postModelWithSalaryIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manager.ToString(), Salary = -10 }; + //Act + var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithIdIncorrect)); + var responseWithNameIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithNameIncorrect)); + var responseWithPostTypeIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithPostTypeIncorrect)); + var responseWithSalaryIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithSalaryIncorrect)); + //Assert + Assert.Multiple(() => + { + Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect"); + Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Name is incorrect"); + Assert.That(responseWithPostTypeIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Type is incorrect"); + Assert.That(responseWithSalaryIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Salary is incorrect"); + }); + } + + [Test] + public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PostAsync($"/api/posts", MakeContent(string.Empty)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Post_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PostAsync($"/api/posts", MakeContent(new { Data = "test", Position = 10 })); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_ShouldSuccess_Test() + { + //Arrange + var postModel = CreateModel(); + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postModel.Id); + //Act + var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + MagicCarpetDbContext.ChangeTracker.Clear(); + AssertElement(MagicCarpetDbContext.GetPostFromDatabaseByPostId(postModel.Id!), postModel); + } + + [Test] + public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test() + { + //Arrange + var postModel = CreateModel(); + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(); + //Act + var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_WhenRecordWasDeleted_ShouldBadRequest_Test() + { + //Arrange + var postModel = CreateModel(); + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postModel.Id, isActual: false); + //Act + var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_WhenHaveRecordWithSameName_ShouldBadRequest_Test() + { + //Arrange + var postModel = CreateModel(postName: "unique name"); + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postModel.Id); + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: postModel.PostName!); + //Act + var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_WhenDataIsIncorrect_ShouldBadRequest_Test() + { + //Arrange + var postModelWithIdIncorrect = new PostBindingModel { Id = "Id", PostName = "name", PostType = PostType.Manager.ToString(), Salary = 10 }; + var postModelWithNameIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manager.ToString(), Salary = 10 }; + var postModelWithPostTypeIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = string.Empty, Salary = 10 }; + var postModelWithSalaryIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manager.ToString(), Salary = -10 }; + //Act + var responseWithIdIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithIdIncorrect)); + var responseWithNameIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithNameIncorrect)); + var responseWithPostTypeIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithPostTypeIncorrect)); + var responseWithSalaryIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithSalaryIncorrect)); + //Assert + Assert.Multiple(() => + { + Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect"); + Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Name is incorrect"); + Assert.That(responseWithPostTypeIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Type is incorrect"); + Assert.That(responseWithSalaryIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Salary is incorrect"); + }); + } + + [Test] + public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PutAsync($"/api/posts", MakeContent(string.Empty)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PutAsync($"/api/posts", MakeContent(new { Data = "test", Position = 10 })); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Delete_ShouldSuccess_Test() + { + //Arrange + var postId = MagicCarpetDbContext.InsertPostToDatabaseAndReturn().PostId; + //Act + var response = await HttpClient.DeleteAsync($"/api/posts/{postId}"); + //Assert + Assert.Multiple(() => + { + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + Assert.That(MagicCarpetDbContext.GetPostFromDatabaseByPostId(postId), Is.Null); + }); + } + + [Test] + public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test() + { + //Arrange + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(); + //Act + var response = await HttpClient.DeleteAsync($"/api/posts/{Guid.NewGuid()}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Delete_WhenRecordWasDeleted_Test() + { + //Arrange + var postId = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: false).PostId; + //Act + var response = await HttpClient.DeleteAsync($"/api/posts/{postId}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Delete_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.DeleteAsync($"/api/posts/id"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Patch_ShouldSuccess_Test() + { + //Arrange + var postId = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: false).PostId; + //Act + var response = await HttpClient.PatchAsync($"/api/posts/{postId}", null); + //Assert + Assert.Multiple(() => + { + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + Assert.That(MagicCarpetDbContext.GetPostFromDatabaseByPostId(postId), Is.Not.Null); + }); + } + + [Test] + public async Task Patch_WhenNoFoundRecord_ShouldBadRequest_Test() + { + //Arrange + MagicCarpetDbContext.InsertPostToDatabaseAndReturn(); + //Act + var response = await HttpClient.PatchAsync($"/api/posts/{Guid.NewGuid()}", null); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Patch_WhenRecordNotWasDeleted_ShouldSuccess_Test() + { + //Arrange + var postId = MagicCarpetDbContext.InsertPostToDatabaseAndReturn().PostId; + //Act + var response = await HttpClient.PatchAsync($"/api/posts/{postId}", null); + //Assert + Assert.Multiple(() => + { + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + Assert.That(MagicCarpetDbContext.GetPostFromDatabaseByPostId(postId), Is.Not.Null); + }); + } + + [Test] + public async Task Patch_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PatchAsync($"/api/posts/id", null); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + private static void AssertElement(PostViewModel? actual, Post expected) + { + Assert.That(actual, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(actual.Id, Is.EqualTo(expected.PostId)); + Assert.That(actual.PostName, Is.EqualTo(expected.PostName)); + Assert.That(actual.PostType, Is.EqualTo(expected.PostType.ToString())); + Assert.That(actual.Salary, Is.EqualTo(expected.Salary)); + }); + } + + private static PostBindingModel CreateModel(string? postId = null, string postName = "name", PostType postType = PostType.Manager, double salary = 10) + => new() + { + Id = postId ?? Guid.NewGuid().ToString(), + PostName = postName, + PostType = postType.ToString(), + Salary = salary + }; + + private static void AssertElement(Post? actual, PostBindingModel expected) + { + Assert.That(actual, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(actual.PostId, Is.EqualTo(expected.Id)); + Assert.That(actual.PostName, Is.EqualTo(expected.PostName)); + Assert.That(actual.PostType.ToString(), Is.EqualTo(expected.PostType)); + Assert.That(actual.Salary, Is.EqualTo(expected.Salary)); + }); + } +} diff --git a/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/SaleControllerTests.cs b/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/SaleControllerTests.cs new file mode 100644 index 0000000..74b7ced --- /dev/null +++ b/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/SaleControllerTests.cs @@ -0,0 +1,509 @@ +using MagicCarpetContracts.BindingModels; +using MagicCarpetContracts.Enums; +using MagicCarpetContracts.ViewModels; +using MagicCarpetDatabase.Models; +using MagicCarpetTests.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetTests.WebApiControllersTests; + +[TestFixture] +internal class SaleControllerTests : BaseWebApiControllerTest +{ + private string _clientId; + private string _employeeId; + private string _tourId; + + [SetUp] + public void SetUp() + { + _clientId = MagicCarpetDbContext.InsertClientToDatabaseAndReturn().Id; + _employeeId = MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn().Id; + _tourId = MagicCarpetDbContext.InsertTourToDatabaseAndReturn().Id; + } + + [TearDown] + public void TearDown() + { + MagicCarpetDbContext.RemoveSalesFromDatabase(); + MagicCarpetDbContext.RemoveEmployeesFromDatabase(); + MagicCarpetDbContext.RemoveClientsFromDatabase(); + MagicCarpetDbContext.RemoveToursFromDatabase(); + } + + [Test] + public async Task GetList_WhenHaveRecords_ShouldSuccess_Test() + { + //Arrange + var sale = MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, clientId: _clientId, sum: 10, tours: [(_tourId, 10, 1.1)]); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, tours: [(_tourId, 10, 1.1)]); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, tours: [(_tourId, 10, 1.1)]); + //Act + var response = await HttpClient.GetAsync($"/api/sales/getrecords?fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.Multiple(() => + { + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(3)); + }); + AssertElement(data.First(x => x.Sum == sale.Sum), sale); + } + + [Test] + public async Task GetList_WhenNoRecords_ShouldSuccess_Test() + { + //Act + var response = await HttpClient.GetAsync($"/api/sales/getrecords?fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.Multiple(() => + { + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(0)); + }); + } + + [Test] + public async Task GetList_ByPeriod_ShouldSuccess_Test() + { + //Arrange + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-3), tours: [(_tourId, 1, 1.1)]); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), tours: [(_tourId, 1, 1.1)]); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, null, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(_tourId, 1, 1.1)]); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, null, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(3), tours: [(_tourId, 1, 1.1)]); + //Act + var response = await HttpClient.GetAsync($"/api/sales/getrecords?fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.Multiple(() => + { + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(2)); + }); + } + + [Test] + public async Task GetList_ByPeriod_WhenDateIsIncorrect_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.GetAsync($"/api/sales/getrecords?fromDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task GetList_ByEmployeeId_ShouldSuccess_Test() + { + //Arrange + var employee = MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "Other employee"); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 1, 1.1)]); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 1, 1.1)]); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(employee.Id, null, tours: [(_tourId, 1, 1.1)]); + //Act + var response = await HttpClient.GetAsync($"/api/sales/getemployeerecords?id={_employeeId}&fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.That(data, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(data, Has.Count.EqualTo(2)); + Assert.That(data.All(x => x.EmployeeId == _employeeId)); + }); + } + + [Test] + public async Task GetList_ByEmployeeId_WhenNoRecords_ShouldSuccess_Test() + { + //Arrange + var employee = MagicCarpetDbContext.InsertEmployeeToDatabaseAndReturn(fio: "Other employee"); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 1, 1.1)]); + //Act + var response = await HttpClient.GetAsync($"/api/sales/getemployeerecords?id={employee.Id}&fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(0)); + } + + [Test] + public async Task GetList_ByEmployeeId_WhenDateIsIncorrect_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.GetAsync($"/api/sales/getemployeerecords?id={_employeeId}&fromDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task GetList_ByEmployeeId_WhenIdIsIncorrect_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.GetAsync($"/api/sales/getemployeerecords?id=Id&fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task GetList_ByClientId_ShouldSuccess_Test() + { + //Arrange + var client = MagicCarpetDbContext.InsertClientToDatabaseAndReturn(fio: "Other fio", phoneNumber: "+8-888-888-88-88"); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 1, 1.1)]); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 1, 1.1)]); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, client.Id, tours: [(_tourId, 1, 1.1)]); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, null, tours: [(_tourId, 1, 1.1)]); + //Act + var response = await HttpClient.GetAsync($"/api/sales/getclientrecords?id={_clientId}&fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.That(data, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(data, Has.Count.EqualTo(2)); + Assert.That(data.All(x => x.ClientId == _clientId)); + }); + } + + [Test] + public async Task GetList_ByClientId_WhenNoRecords_ShouldSuccess_Test() + { + //Arrange + var client = MagicCarpetDbContext.InsertClientToDatabaseAndReturn(fio: "Other client", phoneNumber: "+8-888-888-88-88"); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 1, 1.1)]); + //Act + var response = await HttpClient.GetAsync($"/api/sales/getclientrecords?id={client.Id}&fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(0)); + } + + [Test] + public async Task GetList_ByClientId_WhenDateIsIncorrect_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.GetAsync($"/api/sales/getclientrecords?id={_clientId}&fromDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task GetList_ByClientId_WhenIdIsIncorrect_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.GetAsync($"/api/sales/getclientrecords?id=Id&fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task GetList_ByTourId_ShouldSuccess_Test() + { + //Arrange + var tour = MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourName: "Other name"); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 5, 1.1)]); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 1, 1.1), (tour.Id, 4, 1.1)]); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, null, tours: [(tour.Id, 1, 1.1)]); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, null, tours: [(tour.Id, 1, 1.1), (_tourId, 1, 1.1)]); + //Act + var response = await HttpClient.GetAsync($"/api/sales/gettourrecords?id={_tourId}&fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.That(data, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(data, Has.Count.EqualTo(3)); + Assert.That(data.All(x => x.Tours.Any(y => y.TourId == _tourId))); + }); + } + + [Test] + public async Task GetList_ByTourId_WhenNoRecords_ShouldSuccess_Test() + { + //Arrange + var tour = MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourName: "Other tour"); + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 1, 1.1)]); + //Act + var response = await HttpClient.GetAsync($"/api/sales/gettourrecords?id={tour.Id}&fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(0)); + } + + [Test] + public async Task GetList_ByTourId_WhenDateIsIncorrect_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.GetAsync($"/api/sales/gettourrecords?id={_tourId}&fromDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task GetList_ByTourId_WhenIdIsIncorrect_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.GetAsync($"/api/sales/gettourrecords?id=Id&fromDate={DateTime.UtcNow.AddDays(-1):MM/dd/yyyy HH:mm:ss}&toDate={DateTime.UtcNow.AddDays(1):MM/dd/yyyy HH:mm:ss}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test() + { + //Arrange + var sale = MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 5, 1.1)]); + //Act + var response = await HttpClient.GetAsync($"/api/sales/getrecord/{sale.Id}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + AssertElement(await GetModelFromResponseAsync(response), sale); + } + + [Test] + public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test() + { + //Arrange + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 5, 1.1)]); + //Act + var response = await HttpClient.GetAsync($"/api/sales/getrecord/{Guid.NewGuid()}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task GetElement_ById_WhenRecordHasCanceled_ShouldSuccess_Test() + { + //Arrange + var sale = MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 1, 1.2)], isCancel: true); + //Act + var response = await HttpClient.GetAsync($"/api/sales/getrecord/{sale.Id}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + } + + [Test] + public async Task GetElement_ById_WhenIdIsIncorrect_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.GetAsync($"/api/sales/getrecord/Id"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Post_ShouldSuccess_Test() + { + //Arrange + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, null, tours: [(_tourId, 5, 1.1)]); + var saleModel = CreateModel(_employeeId, _clientId, _tourId); + //Act + var response = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(saleModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + AssertElement(MagicCarpetDbContext.GetSalesByClientId(_clientId)[0], saleModel); + } + + [Test] + public async Task Post_WhenNoClient_ShouldSuccess_Test() + { + //Arrange + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 5, 1.1)]); + var saleModel = CreateModel(_employeeId, null, _tourId); + //Act + var response = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(saleModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + AssertElement(MagicCarpetDbContext.GetSalesByClientId(null)[0], saleModel); + } + + [Test] + public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test() + { + //Arrange + var saleId = Guid.NewGuid().ToString(); + var saleModelWithIdIncorrect = new SaleBindingModel { Id = "Id", EmployeeId = _employeeId, ClientId = _clientId, DiscountType = 1, Tours = [new SaleTourBindingModel { SaleId = saleId, TourId = _tourId, Count = 10, Price = 1.1 }] }; + var saleModelWithEmployeeIdIncorrect = new SaleBindingModel { Id = saleId, EmployeeId = "Id", ClientId = _clientId, DiscountType = 1, Tours = [new SaleTourBindingModel { SaleId = saleId, TourId = _tourId, Count = 10, Price = 1.1 }] }; + var saleModelWithClientIdIncorrect = new SaleBindingModel { Id = saleId, EmployeeId = _employeeId, ClientId = "Id", DiscountType = 1, Tours = [new SaleTourBindingModel { SaleId = saleId, TourId = _tourId, Count = 10, Price = 1.1 }] }; + var saleModelWithToursIncorrect = new SaleBindingModel { Id = saleId, EmployeeId = _employeeId, ClientId = _clientId, DiscountType = 1, Tours = null }; + var saleModelWithTourIdIncorrect = new SaleBindingModel { Id = saleId, EmployeeId = _employeeId, ClientId = _clientId, DiscountType = 1, Tours = [new SaleTourBindingModel { SaleId = saleId, TourId = "Id", Count = 10, Price = 1.1 }] }; + var saleModelWithCountIncorrect = new SaleBindingModel { Id = saleId, EmployeeId = _employeeId, ClientId = _clientId, DiscountType = 1, Tours = [new SaleTourBindingModel { SaleId = saleId, TourId = _tourId, Count = -10, Price = 1.1 }] }; + var saleModelWithPriceIncorrect = new SaleBindingModel { Id = saleId, EmployeeId = _employeeId, ClientId = _clientId, DiscountType = 1, Tours = [new SaleTourBindingModel { SaleId = saleId, TourId = _tourId, Count = 10, Price = -1.1 }] }; + //Act + var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(saleModelWithIdIncorrect)); + var responseWithEmployeeIdIncorrect = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(saleModelWithEmployeeIdIncorrect)); + var responseWithClientIdIncorrect = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(saleModelWithClientIdIncorrect)); + var responseWithToursIncorrect = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(saleModelWithToursIncorrect)); + var responseWithTourIdIncorrect = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(saleModelWithTourIdIncorrect)); + var responseWithCountIncorrect = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(saleModelWithCountIncorrect)); + var responseWithPriceIncorrect = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(saleModelWithPriceIncorrect)); + //Assert + Assert.Multiple(() => + { + Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "EmployeeId is incorrect"); + Assert.That(responseWithEmployeeIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "EmployeeId is incorrect"); + Assert.That(responseWithClientIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "ClientId is incorrect"); + Assert.That(responseWithToursIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Tours is incorrect"); + Assert.That(responseWithTourIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "TourId is incorrect"); + Assert.That(responseWithCountIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Count is incorrect"); + Assert.That(responseWithPriceIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Price is incorrect"); + }); + } + + [Test] + public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(string.Empty)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Post_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PostAsync($"/api/sales/sale", MakeContent(new { Data = "test", Position = 10 })); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Delete_ShouldSuccess_Test() + { + //Arrange + var sale = MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 5, 1.1)]); + //Act + var response = await HttpClient.DeleteAsync($"/api/sales/cancel/{sale.Id}"); + MagicCarpetDbContext.ChangeTracker.Clear(); + //Assert + Assert.Multiple(() => + { + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + Assert.That(MagicCarpetDbContext.GetSaleFromDatabaseById(sale.Id)!.IsCancel); + }); + } + + [Test] + public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test() + { + //Arrange + MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 5, 1.1)]); + //Act + var response = await HttpClient.DeleteAsync($"/api/sales/cancel/{Guid.NewGuid()}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Delete_WhenIsCanceled_ShouldBadRequest_Test() + { + //Arrange + var sale = MagicCarpetDbContext.InsertSaleToDatabaseAndReturn(_employeeId, _clientId, tours: [(_tourId, 5, 1.1)], isCancel: true); + //Act + var response = await HttpClient.DeleteAsync($"/api/sales/cancel/{sale.Id}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Delete_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.DeleteAsync($"/api/sales/cancel/id"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + private static void AssertElement(SaleViewModel? actual, Sale expected) + { + Assert.That(actual, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(actual.Id, Is.EqualTo(expected.Id)); + Assert.That(actual.EmployeeFIO, Is.EqualTo(expected.Employee!.FIO)); + Assert.That(actual.ClientFIO, Is.EqualTo(expected.Client?.FIO)); + Assert.That(actual.DiscountType, Is.EqualTo(expected.DiscountType.ToString())); + Assert.That(actual.Discount, Is.EqualTo(expected.Discount)); + Assert.That(actual.IsCancel, Is.EqualTo(expected.IsCancel)); + }); + + if (expected.SaleTours is not null) + { + Assert.That(actual.Tours, Is.Not.Null); + Assert.That(actual.Tours, Has.Count.EqualTo(expected.SaleTours.Count)); + for (int i = 0; i < actual.Tours.Count; ++i) + { + Assert.Multiple(() => + { + Assert.That(actual.Tours[i].TourName, Is.EqualTo(expected.SaleTours[i].Tour!.TourName)); + Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.SaleTours[i].Count)); + }); + } + } + else + { + Assert.That(actual.Tours, Is.Null); + } + } + + private static SaleBindingModel CreateModel(string employeeId, string? clientId, string tourId, string? id = null, DiscountType discountType = DiscountType.OnSale, int count = 1, double price = 1.1) + { + var saleId = id ?? Guid.NewGuid().ToString(); + return new() + { + Id = saleId, + EmployeeId = employeeId, + ClientId = clientId, + DiscountType = (int)discountType, + Tours = [new SaleTourBindingModel { SaleId = saleId, TourId = tourId, Count = count, Price = price }] + }; + } + + private static void AssertElement(Sale? actual, SaleBindingModel expected) + { + Assert.That(actual, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(actual.EmployeeId, Is.EqualTo(expected.EmployeeId)); + Assert.That(actual.ClientId, Is.EqualTo(expected.ClientId)); + Assert.That((int)actual.DiscountType, Is.EqualTo(expected.DiscountType)); + Assert.That(!actual.IsCancel); + }); + + if (expected.Tours is not null) + { + Assert.That(actual.SaleTours, Is.Not.Null); + Assert.That(actual.SaleTours, Has.Count.EqualTo(expected.Tours.Count)); + for (int i = 0; i < actual.SaleTours.Count; ++i) + { + Assert.Multiple(() => + { + Assert.That(actual.SaleTours[i].TourId, Is.EqualTo(expected.Tours[i].TourId)); + Assert.That(actual.SaleTours[i].Count, Is.EqualTo(expected.Tours[i].Count)); + Assert.That(actual.SaleTours[i].Price, Is.EqualTo(expected.Tours[i].Price)); + }); + } + } + else + { + Assert.That(actual.SaleTours, Is.Null); + } + } +} diff --git a/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/TourControllerTests.cs b/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/TourControllerTests.cs new file mode 100644 index 0000000..6f729ab --- /dev/null +++ b/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/TourControllerTests.cs @@ -0,0 +1,393 @@ +using MagicCarpetContracts.BindingModels; +using MagicCarpetContracts.Enums; +using MagicCarpetContracts.ViewModels; +using MagicCarpetDatabase.Models; +using MagicCarpetTests.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetTests.WebApiControllersTests; + +[TestFixture] +internal class TourControllerTests : BaseWebApiControllerTest +{ + [TearDown] + public void TearDown() + { + MagicCarpetDbContext.RemoveToursFromDatabase(); + } + + [Test] + public async Task GetList_WhenHaveRecords_ShouldSuccess_Test() + { + //Arrange + var tour = MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourName: "name 1"); + MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourName: "name 2"); + MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourName: "name 3"); + //Act + var response = await HttpClient.GetAsync("/api/tours/getrecords"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.Multiple(() => + { + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(3)); + }); + AssertElement(data.First(x => x.Id == tour.Id), tour); + } + + [Test] + public async Task GetList_WhenNoRecords_ShouldSuccess_Test() + { + //Act + var response = await HttpClient.GetAsync("/api/tours/getrecords"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.Multiple(() => + { + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(0)); + }); + } + + [Test] + public async Task GetHistoryByTourId_WhenHaveRecords_ShouldSuccess_Test() + { + //Arrange + var tour = MagicCarpetDbContext.InsertTourToDatabaseAndReturn(); + MagicCarpetDbContext.InsertTourHistoryToDatabaseAndReturn(tour.Id, 20, DateTime.UtcNow.AddDays(-1)); + MagicCarpetDbContext.InsertTourHistoryToDatabaseAndReturn(tour.Id, 30, DateTime.UtcNow.AddMinutes(-10)); + var history = MagicCarpetDbContext.InsertTourHistoryToDatabaseAndReturn(tour.Id, 40, DateTime.UtcNow.AddDays(1)); + //Act + var response = await HttpClient.GetAsync($"/api/tours/gethistory?id={tour.Id}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(3)); + AssertElement(data[0], history); + } + + [Test] + public async Task GetHistoryByTourId_WhenNoRecords_ShouldSuccess_Test() + { + //Arrange + var tour = MagicCarpetDbContext.InsertTourToDatabaseAndReturn(); + MagicCarpetDbContext.InsertTourHistoryToDatabaseAndReturn(tour.Id, 20, DateTime.UtcNow.AddDays(-1)); + MagicCarpetDbContext.InsertTourHistoryToDatabaseAndReturn(tour.Id, 30, DateTime.UtcNow.AddMinutes(-10)); + MagicCarpetDbContext.InsertTourHistoryToDatabaseAndReturn(tour.Id, 40, DateTime.UtcNow.AddDays(1)); + //Act + var response = await HttpClient.GetAsync($"/api/tours/gethistory?id={Guid.NewGuid()}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var data = await GetModelFromResponseAsync>(response); + Assert.That(data, Is.Not.Null); + Assert.That(data, Has.Count.EqualTo(0)); + } + + [Test] + public async Task GetHistoryByTourId_WhenTourIdIsNotGuid_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.GetAsync($"/api/tours/gethistory?id=id"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test() + { + //Arrange + var tour = MagicCarpetDbContext.InsertTourToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/tours/getrecord/{tour.Id}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + AssertElement(await GetModelFromResponseAsync(response), tour); + } + + [Test] + public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test() + { + //Arrange + MagicCarpetDbContext.InsertTourToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/tours/getrecord/{Guid.NewGuid()}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task GetElement_ByName_WhenHaveRecord_ShouldSuccess_Test() + { + //Arrange + var tour = MagicCarpetDbContext.InsertTourToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/tours/getrecord/{tour.TourName}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + AssertElement(await GetModelFromResponseAsync(response), tour); + } + + [Test] + public async Task GetElement_ByName_WhenNoRecord_ShouldNotFound_Test() + { + //Arrange + MagicCarpetDbContext.InsertTourToDatabaseAndReturn(); + //Act + var response = await HttpClient.GetAsync($"/api/tours/getrecord/New%20Name"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + + + [Test] + public async Task Post_ShouldSuccess_Test() + { + //Arrange + MagicCarpetDbContext.InsertTourToDatabaseAndReturn(); + var tourModel = CreateModel(); + //Act + var response = await HttpClient.PostAsync($"/api/tours/register", MakeContent(tourModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + AssertElement(MagicCarpetDbContext.GetTourFromDatabaseById(tourModel.Id!), tourModel); + } + + [Test] + public async Task Post_WhenHaveRecordWithSameId_ShouldBadRequest_Test() + { + //Arrange + var tourModel = CreateModel(); + MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourModel.Id); + //Act + var response = await HttpClient.PostAsync($"/api/tours/register", MakeContent(tourModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Post_WhenHaveRecordWithSameName_ShouldBadRequest_Test() + { + //Arrange + var tourModel = CreateModel(tourName: "unique name"); + MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourName: tourModel.TourName!); + //Act + var response = await HttpClient.PostAsync($"/api/tours/register", MakeContent(tourModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test() + { + //Arrange + var tourModelWithIdIncorrect = new TourBindingModel { Id = "Id", TourName = "name", TourCountry = "country", Price = 100, TourType = TourType.Beach.ToString() }; + var tourModelWithNameIncorrect = new TourBindingModel { Id = Guid.NewGuid().ToString(), TourName = string.Empty, TourCountry = "country", Price = 100, TourType = TourType.Beach.ToString() }; + var tourModelWithCountryIncorrect = new TourBindingModel { Id = Guid.NewGuid().ToString(), TourName = "name", TourCountry = string.Empty, Price = 100, TourType = TourType.Beach.ToString() }; + var tourModelWithTourTypeIncorrect = new TourBindingModel { Id = Guid.NewGuid().ToString(), TourName = "name", TourCountry = "country", Price = 100, TourType = string.Empty }; + var tourModelWithPriceIncorrect = new TourBindingModel { Id = Guid.NewGuid().ToString(), TourName = "name", TourCountry = "country", Price = 0, TourType = TourType.Beach.ToString() }; + //Act + var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/tours/register", MakeContent(tourModelWithIdIncorrect)); + var responseWithNameIncorrect = await HttpClient.PostAsync($"/api/tours/register", MakeContent(tourModelWithNameIncorrect)); + var responseWithCountryIncorrect = await HttpClient.PostAsync($"/api/tours/register", MakeContent(tourModelWithCountryIncorrect)); + var responseWithTourTypeIncorrect = await HttpClient.PostAsync($"/api/tours/register", MakeContent(tourModelWithTourTypeIncorrect)); + var responseWithPriceIncorrect = await HttpClient.PostAsync($"/api/tours/register", MakeContent(tourModelWithPriceIncorrect)); + //Assert + Assert.Multiple(() => + { + Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect"); + Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Name is incorrect"); + Assert.That(responseWithCountryIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Country is incorrect"); + Assert.That(responseWithTourTypeIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "TourType is incorrect"); + Assert.That(responseWithPriceIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Price is incorrect"); + }); + } + + [Test] + public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PostAsync($"/api/tours/register", MakeContent(string.Empty)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Post_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PostAsync($"/api/tours/register", MakeContent(new { Data = "test", Position = 10 })); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_ShouldSuccess_Test() + { + //Arrange + var tourModel = CreateModel(); + MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourModel.Id); + //Act + var response = await HttpClient.PutAsync($"/api/tours/changeinfo", MakeContent(tourModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + MagicCarpetDbContext.ChangeTracker.Clear(); + AssertElement(MagicCarpetDbContext.GetTourFromDatabaseById(tourModel.Id!), tourModel); + } + + [Test] + public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test() + { + //Arrange + var tourModel = CreateModel(); + MagicCarpetDbContext.InsertTourToDatabaseAndReturn(); + //Act + var response = await HttpClient.PutAsync($"/api/tours/changeinfo", MakeContent(tourModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_WhenHaveRecordWithSameName_ShouldBadRequest_Test() + { + //Arrange + var tourModel = CreateModel(tourName: "unique name"); + MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourModel.Id); + MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourName: tourModel.TourName!); + //Act + var response = await HttpClient.PutAsync($"/api/tours/changeinfo", MakeContent(tourModel)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_WhenDataIsIncorrect_ShouldBadRequest_Test() + { + //Arrange + var tourModelWithIdIncorrect = new TourBindingModel { Id = "Id", TourName = "name", Price = 100, TourType = TourType.Beach.ToString() }; + var tourModelWithNameIncorrect = new TourBindingModel { Id = Guid.NewGuid().ToString(), TourName = string.Empty, Price = 100, TourType = TourType.Beach.ToString() }; + var tourModelWithCountryIncorrect = new TourBindingModel { Id = Guid.NewGuid().ToString(), TourName = "name", TourCountry = string.Empty, Price = 100, TourType = TourType.Beach.ToString() }; + var tourModelWithTourTypeIncorrect = new TourBindingModel { Id = Guid.NewGuid().ToString(), TourName = "name", Price = 100, TourType = string.Empty }; + var tourModelWithPriceIncorrect = new TourBindingModel { Id = Guid.NewGuid().ToString(), TourName = "name", Price = 0, TourType = TourType.Beach.ToString() }; + //Act + var responseWithIdIncorrect = await HttpClient.PutAsync($"/api/tours/changeinfo", MakeContent(tourModelWithIdIncorrect)); + var responseWithNameIncorrect = await HttpClient.PutAsync($"/api/tours/changeinfo", MakeContent(tourModelWithNameIncorrect)); + var responseWithCountryIncorrect = await HttpClient.PostAsync($"/api/tours/register", MakeContent(tourModelWithCountryIncorrect)); + var responseWithTourTypeIncorrect = await HttpClient.PutAsync($"/api/tours/changeinfo", MakeContent(tourModelWithTourTypeIncorrect)); + var responseWithPriceIncorrect = await HttpClient.PutAsync($"/api/tours/changeinfo", MakeContent(tourModelWithPriceIncorrect)); + //Assert + Assert.Multiple(() => + { + Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect"); + Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Name is incorrect"); + Assert.That(responseWithCountryIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Country is incorrect"); + Assert.That(responseWithTourTypeIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "TourType is incorrect"); + Assert.That(responseWithPriceIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Price is incorrect"); + }); + } + + [Test] + public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PutAsync($"/api/tours/changeinfo", MakeContent(string.Empty)); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Put_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.PutAsync($"/api/tours/changeinfo", MakeContent(new { Data = "test", Position = 10 })); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Delete_ShouldSuccess_Test() + { + //Arrange + var tourId = Guid.NewGuid().ToString(); + MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourId); + //Act + var response = await HttpClient.DeleteAsync($"/api/tours/delete/{tourId}"); + MagicCarpetDbContext.ChangeTracker.Clear(); + //Assert + Assert.Multiple(() => + { + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + }); + } + + [Test] + public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test() + { + //Arrange + MagicCarpetDbContext.InsertTourToDatabaseAndReturn(); + //Act + var response = await HttpClient.DeleteAsync($"/api/tours/delete/{Guid.NewGuid()}"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async Task Delete_WhenSendWrongFormatData_ShouldBadRequest_Test() + { + //Act + var response = await HttpClient.DeleteAsync($"/api/tours/delete/id"); + //Assert + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + private static void AssertElement(TourViewModel? actual, Tour expected) + { + Assert.That(actual, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(actual.Id, Is.EqualTo(expected.Id)); + Assert.That(actual.TourName, Is.EqualTo(expected.TourName)); + Assert.That(actual.TourType, Is.EqualTo(expected.Type.ToString())); + Assert.That(actual.Price, Is.EqualTo(expected.Price)); + }); + } + + private static void AssertElement(TourHistoryViewModel? actual, TourHistory expected) + { + Assert.That(actual, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(actual.TourName, Is.EqualTo(expected.Tour!.TourName)); + Assert.That(actual.OldPrice, Is.EqualTo(expected.OldPrice)); + Assert.That(actual.ChangeDate.ToString(), Is.EqualTo(expected.ChangeDate.ToString())); + }); + } + + private static TourBindingModel CreateModel(string? id = null, string tourName = "name", TourType tourType = TourType.Beach, double price = 1) + => new() + { + Id = id ?? Guid.NewGuid().ToString(), + TourName = tourName, + TourType = tourType.ToString(), + Price = price + }; + + private static void AssertElement(Tour? actual, TourBindingModel expected) + { + Assert.That(actual, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(actual.Id, Is.EqualTo(expected.Id)); + Assert.That(actual.TourName, Is.EqualTo(expected.TourName)); + Assert.That(actual.Type.ToString(), Is.EqualTo(expected.TourType)); + Assert.That(actual.Price, Is.EqualTo(expected.Price)); + }); + } +} diff --git a/MagicCarpetProject/MagicCarpetTests/appsettings.json b/MagicCarpetProject/MagicCarpetTests/appsettings.json new file mode 100644 index 0000000..a1d65fa --- /dev/null +++ b/MagicCarpetProject/MagicCarpetTests/appsettings.json @@ -0,0 +1,28 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": { + "Default": "Information" + }, + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "../logs/magiccarpet-.log", + "rollingInterval": "Day", + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {CorrelationId} {Level:u3} {Username} {Message:lj}{Exception}{NewLine}" + } + } + ] + }, + "AllowedHosts": "*" + //"DataBaseSettings": { + // "ConnectionString": "Host=127.0.0.1;Port=5432;Database=MagicCarpetTest;Username=postgres;Password=postgres;" + //} +}