forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
557 lines
26 KiB
C#
557 lines
26 KiB
C#
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 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<List<EmployeeViewModel>>(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<List<EmployeeViewModel>>(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<List<EmployeeViewModel>>(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<List<EmployeeViewModel>>(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<List<EmployeeViewModel>>(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<List<EmployeeViewModel>>(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<List<EmployeeViewModel>>(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<List<EmployeeViewModel>>(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<EmployeeViewModel>(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<EmployeeViewModel>(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<EmployeeViewModel>(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);
|
|
});
|
|
}
|
|
}
|