Skip to content

Commit

Permalink
#530 Romanchuk lukashov/deletion direction department class (#534)
Browse files Browse the repository at this point in the history
* #530 Admin should not be able to delete direction, department or class if they have at least 1 workshop attached. Some tests were added.

* Small fix

* ; was deleted
  • Loading branch information
Sandaljero authored Feb 1, 2022
1 parent 9a2e037 commit 7963d7d
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Localization;
using Moq;
using NUnit.Framework;
using OutOfSchool.WebApi.Common;
using OutOfSchool.WebApi.Controllers.V1;
using OutOfSchool.WebApi.Models;
using OutOfSchool.WebApi.Services;
Expand Down Expand Up @@ -162,7 +163,7 @@ public async Task Update_WhenModelIsInvalid_ReturnsBadRequestObjectResult()
public async Task Delete_WhenIdIsValid_ReturnsNoContentResult(long id)
{
// Arrange
service.Setup(x => x.Delete(id));
service.Setup(x => x.Delete(id)).ReturnsAsync(Result<ClassDto>.Success(classEntity));

// Act
var result = await controller.Delete(id) as NoContentResult;
Expand All @@ -189,7 +190,7 @@ public void Delete_WhenIdIsInvalid_ReturnsBadRequestObjectResult(long id)
public async Task Delete_WhenIdIsInvalid_ReturnsNull(long id)
{
// Arrange
service.Setup(x => x.Delete(id));
service.Setup(x => x.Delete(id)).ReturnsAsync(Result<ClassDto>.Success(classEntity));

// Act
var result = await controller.Delete(id) as OkObjectResult;
Expand All @@ -198,6 +199,25 @@ public async Task Delete_WhenIdIsInvalid_ReturnsNull(long id)
Assert.That(result, Is.Null);
}

[Test]
[TestCase(10)]
public async Task Delete_WhenThereAreRelatedWorkshops_ReturnsBadRequestObjectResult(long id)
{
// Arrange
service.Setup(x => x.Delete(id)).ReturnsAsync(Result<ClassDto>.Failed(new OperationError
{
Code = "400",
Description = "Some workshops assosiated with this class. Deletion prohibited.",
}));

// Act
var result = await controller.Delete(id);

// Assert
Assert.That(result, Is.TypeOf<BadRequestObjectResult>());
Assert.That((result as BadRequestObjectResult).StatusCode, Is.EqualTo(400));
}

[Test]
[TestCase(3)]
public async Task GetByDepartmentId_WhenIdIsInvalid_ReturnsNoContent(long id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Localization;
using Moq;
using NUnit.Framework;
using OutOfSchool.WebApi.Common;
using OutOfSchool.WebApi.Controllers.V1;
using OutOfSchool.WebApi.Models;
using OutOfSchool.WebApi.Services;
Expand Down Expand Up @@ -162,7 +163,7 @@ public async Task Update_WhenModelIsInvalid_ReturnsBadRequestObjectResult()
public async Task Delete_WhenIdIsValid_ReturnsNoContentResult(long id)
{
// Arrange
service.Setup(x => x.Delete(id));
service.Setup(x => x.Delete(id)).ReturnsAsync(Result<DepartmentDto>.Success(department));

// Act
var result = await controller.Delete(id) as NoContentResult;
Expand All @@ -189,7 +190,7 @@ public void Delete_WhenIdIsInvalid_ReturnsBadRequestObjectResult(long id)
public async Task Delete_WhenIdIsInvalid_ReturnsNull(long id)
{
// Arrange
service.Setup(x => x.Delete(id));
service.Setup(x => x.Delete(id)).ReturnsAsync(Result<DepartmentDto>.Success(department));

// Act
var result = await controller.Delete(id) as OkObjectResult;
Expand All @@ -198,6 +199,25 @@ public async Task Delete_WhenIdIsInvalid_ReturnsNull(long id)
Assert.That(result, Is.Null);
}

[Test]
[TestCase(10)]
public async Task Delete_WhenThereAreRelatedWorkshops_ReturnsBadRequestObjectResult(long id)
{
// Arrange
service.Setup(x => x.Delete(id)).ReturnsAsync(Result<DepartmentDto>.Failed(new OperationError
{
Code = "400",
Description = "Some workshops assosiated with this department. Deletion prohibited.",
}));

// Act
var result = await controller.Delete(id);

// Assert
Assert.That(result, Is.TypeOf<BadRequestObjectResult>());
Assert.That((result as BadRequestObjectResult).StatusCode, Is.EqualTo(400));
}

[Test]
[TestCase(3)]
public async Task GetByDirectionId_WhenIdIsInvalid_ReturnsNoContent(long id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Localization;
using Moq;
using NUnit.Framework;
using OutOfSchool.WebApi.Common;
using OutOfSchool.WebApi.Controllers.V1;
using OutOfSchool.WebApi.Models;
using OutOfSchool.WebApi.Services;
Expand Down Expand Up @@ -161,7 +162,7 @@ public async Task Update_WhenModelIsInvalid_ReturnsBadRequestObjectResult()
public async Task Delete_WhenIdIsValid_ReturnsNoContentResult(long id)
{
// Arrange
service.Setup(x => x.Delete(id));
service.Setup(x => x.Delete(id)).ReturnsAsync(Result<DirectionDto>.Success(direction));

// Act
var result = await controller.Delete(id) as NoContentResult;
Expand All @@ -188,7 +189,7 @@ public void Delete_WhenIdIsInvalid_ReturnsBadRequestObjectResult(long id)
public async Task Delete_WhenIdIsInvalid_ReturnsNull(long id)
{
// Arrange
service.Setup(x => x.Delete(id));
service.Setup(x => x.Delete(id)).ReturnsAsync(Result<DirectionDto>.Success(direction));

// Act
var result = await controller.Delete(id) as OkObjectResult;
Expand All @@ -197,6 +198,25 @@ public async Task Delete_WhenIdIsInvalid_ReturnsNull(long id)
Assert.That(result, Is.Null);
}

[Test]
[TestCase(10)]
public async Task Delete_WhenThereAreRelatedWorkshops_ReturnsBadRequestObjectResult(long id)
{
// Arrange
service.Setup(x => x.Delete(id)).ReturnsAsync(Result<DirectionDto>.Failed(new OperationError
{
Code = "400",
Description = "Some workshops assosiated with this direction. Deletion prohibited.",
}));

// Act
var result = await controller.Delete(id);

// Assert
Assert.That(result, Is.TypeOf<BadRequestObjectResult>());
Assert.That((result as BadRequestObjectResult).StatusCode, Is.EqualTo(400));
}

private DirectionDto FakeDirection()
{
return new DirectionDto()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ClassServiceTests
private DbContextOptions<OutOfSchoolDbContext> options;
private OutOfSchoolDbContext context;
private IClassRepository repo;
private IWorkshopRepository repositoryWorkshop;
private IClassService service;
private Mock<IStringLocalizer<SharedResource>> localizer;
private Mock<ILogger<ClassService>> logger;
Expand All @@ -38,9 +39,10 @@ public void SetUp()
context = new OutOfSchoolDbContext(options);

repo = new ClassRepository(context);
repositoryWorkshop = new WorkshopRepository(context);
localizer = new Mock<IStringLocalizer<SharedResource>>();
logger = new Mock<ILogger<ClassService>>();
service = new ClassService(repo, logger.Object, localizer.Object);
service = new ClassService(repo, repositoryWorkshop, logger.Object, localizer.Object);

SeedDatabase();
}
Expand Down Expand Up @@ -180,6 +182,19 @@ public void Delete_WhenIdIsInvalid_ThrowsDbUpdateConcurrencyException(long id)
async () => await service.Delete(id).ConfigureAwait(false));
}

[Test]
[Order(10)]
[TestCase(2)]
public async Task Delete_WhenThereAreRelatedWorkshops_ReturnsNotSucceeded(long id)
{
// Act
var result = await service.Delete(id).ConfigureAwait(false);

// Assert
Assert.False(result.Succeeded);
Assert.That(result.OperationResult.Errors, Is.Not.Empty);
}

[Test]
[Order(10)]
[TestCase(1)]
Expand Down Expand Up @@ -261,6 +276,17 @@ private void SeedDatabase()

ctx.Classes.AddRangeAsync(classes);

var workshops = new List<Workshop>()
{
new Workshop()
{
Title = "Test1",
ClassId = 2,
},
};

ctx.Workshops.AddRangeAsync(workshops);

ctx.SaveChangesAsync();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class DepartmentServiceTests
private DbContextOptions<OutOfSchoolDbContext> options;
private OutOfSchoolDbContext context;
private IDepartmentRepository repo;
private IWorkshopRepository repositoryWorkshop;
private IDepartmentService service;
private Mock<IStringLocalizer<SharedResource>> localizer;
private Mock<ILogger<DepartmentService>> logger;
Expand All @@ -38,9 +39,10 @@ public void SetUp()
context = new OutOfSchoolDbContext(options);

repo = new DepartmentRepository(context);
repositoryWorkshop = new WorkshopRepository(context);
localizer = new Mock<IStringLocalizer<SharedResource>>();
logger = new Mock<ILogger<DepartmentService>>();
service = new DepartmentService(repo, logger.Object, localizer.Object);
service = new DepartmentService(repo, repositoryWorkshop, logger.Object, localizer.Object);

SeedDatabase();
}
Expand Down Expand Up @@ -180,6 +182,19 @@ public void Delete_WhenIdIsInvalid_ThrowsDbUpdateConcurrencyException(long id)
async () => await service.Delete(id).ConfigureAwait(false));
}

[Test]
[Order(10)]
[TestCase(2)]
public async Task Delete_WhenThereAreRelatedWorkshops_ReturnsNotSucceeded(long id)
{
// Act
var result = await service.Delete(id).ConfigureAwait(false);

// Assert
Assert.False(result.Succeeded);
Assert.That(result.OperationResult.Errors, Is.Not.Empty);
}

[Test]
[Order(10)]
[TestCase(1)]
Expand Down Expand Up @@ -258,6 +273,17 @@ private void SeedDatabase()

ctx.Departments.AddRangeAsync(departments);

var workshops = new List<Workshop>()
{
new Workshop()
{
Title = "Test1",
DepartmentId = 2,
},
};

ctx.Workshops.AddRangeAsync(workshops);

ctx.SaveChangesAsync();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class DirectionServiceTests
private DbContextOptions<OutOfSchoolDbContext> options;
private OutOfSchoolDbContext context;
private IEntityRepository<Direction> repo;
private IWorkshopRepository repositoryWorkshop;
private IDirectionService service;
private Mock<IStringLocalizer<SharedResource>> localizer;
private Mock<ILogger<DirectionService>> logger;
Expand All @@ -38,9 +39,10 @@ public void SetUp()
context = new OutOfSchoolDbContext(options);

repo = new EntityRepository<Direction>(context);
repositoryWorkshop = new WorkshopRepository(context);
localizer = new Mock<IStringLocalizer<SharedResource>>();
logger = new Mock<ILogger<DirectionService>>();
service = new DirectionService(repo, logger.Object, localizer.Object);
service = new DirectionService(repo, repositoryWorkshop, logger.Object, localizer.Object);

SeedDatabase();
}
Expand Down Expand Up @@ -176,6 +178,19 @@ public void Delete_WhenIdIsInvalid_ThrowsDbUpdateConcurrencyException(long id)
async () => await service.Delete(id).ConfigureAwait(false));
}

[Test]
[Order(10)]
[TestCase(2)]
public async Task Delete_WhenThereAreRelatedWorkshops_ReturnsNotSucceeded(long id)
{
// Act
var result = await service.Delete(id).ConfigureAwait(false);

// Assert
Assert.False(result.Succeeded);
Assert.That(result.OperationResult.Errors, Is.Not.Empty);
}

private void SeedDatabase()
{
using var ctx = new OutOfSchoolDbContext(options);
Expand Down Expand Up @@ -203,6 +218,18 @@ private void SeedDatabase()
};

ctx.Directions.AddRangeAsync(directions);

var workshops = new List<Workshop>()
{
new Workshop()
{
Title = "Test1",
DirectionId = 2,
},
};

ctx.Workshops.AddRangeAsync(workshops);

ctx.SaveChangesAsync();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,25 @@ public async Task<ActionResult> Update(ClassDto classDto)
/// <param name="id">The key of the class in table.</param>
/// <returns>Status Code.</returns>
/// <response code="204">Class was successfully deleted.</response>
/// <response code="400">If some workshops assosiated with this class.</response>
/// <response code="401">If the user is not authorized.</response>
/// <response code="403">If the user has no rights to use this method.</response>
/// <response code="500">If any server error occures.</response>
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult> Delete(long id)
{
this.ValidateId(id, localizer);

await service.Delete(id).ConfigureAwait(false);
var result = await service.Delete(id).ConfigureAwait(false);
if (!result.Succeeded)
{
return BadRequest(result.OperationResult);
}

return NoContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,25 @@ public async Task<ActionResult> Update(DepartmentDto departmentDto)
/// <param name="id">The key of the Department in table.</param>
/// <returns>Status Code.</returns>
/// <response code="204">Department was successfully deleted.</response>
/// <response code="400">If some workshops assosiated with this department.</response>
/// <response code="401">If the user is not authorized.</response>
/// <response code="403">If the user has no rights to use this method.</response>
/// <response code="500">If any server error occures.</response>
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult> Delete(long id)
{
this.ValidateId(id, localizer);

await service.Delete(id).ConfigureAwait(false);
var result = await service.Delete(id).ConfigureAwait(false);
if (!result.Succeeded)
{
return BadRequest(result.OperationResult);
}

return NoContent();
}
Expand Down
Loading

0 comments on commit 7963d7d

Please sign in to comment.