Files
Check/MagicCarpetProject/MagicCarpetTests/WebApiControllersTests/TourControllerTests.cs

395 lines
17 KiB
C#

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<List<TourViewModel>>(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<List<TourViewModel>>(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<List<TourHistoryViewModel>>(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<List<TourViewModel>>(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<TourViewModel>(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<TourViewModel>(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.TourType.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", string tourCountry = "country", TourType tourType = TourType.Beach, double price = 1)
=> new()
{
Id = id ?? Guid.NewGuid().ToString(),
TourName = tourName,
TourCountry = tourCountry,
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.TourType.ToString(), Is.EqualTo(expected.TourType));
Assert.That(actual.Price, Is.EqualTo(expected.Price));
});
}
}