Skip to content

Commit

Permalink
Added more test
Browse files Browse the repository at this point in the history
  • Loading branch information
annygumeniuk committed Dec 21, 2024
1 parent ed090c1 commit cc14130
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public async Task DeleteAsync(Guid id, Guid providerOwnerId)
{
var position = await _entityRepositoryBase.GetById(id);

if (position == null || position.IsDeleted)
if (position == null || position.IsDeleted == true)
{
_logger.LogError($"Position with ID {id} not found.");
throw new KeyNotFoundException($"Position with ID {id} not found or it was deleted.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,45 @@ public async Task UpdatePosition_WithValidInput_ShouldReturnUpdatedPosition()
Assert.AreEqual(positionDto.FullName, updatedPosition.FullName);
}

[Test]
public async Task DeletePosition_WhichDeleted_ShouldReturnNotFound()
{
// Arrange
currentUserService.Setup(s => s.UserId).Returns(providerId.ToString());
currentUserService.Setup(s => s.IsInRole(Role.Provider)).Returns(true);

var deletedPosition = positionDto;
deletedPosition.IsDeleted = true;

positionService
.Setup(s => s.DeleteAsync(deletedPosition.Id, providerId))
.ThrowsAsync(new KeyNotFoundException($"Position with ID {deletedPosition.Id} not found or it was deleted."));

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

// Assert
Assert.IsInstanceOf<NotFoundObjectResult>(result);
var objectResult = result as NotFoundObjectResult;
Assert.IsNotNull(objectResult); // Ensure the cast succeeded
Assert.AreEqual($"Position with ID {deletedPosition.Id} not found or it was deleted.", objectResult.Value);
}

[Test]
public async Task DeletePosition_WithValidInput_ShoudReturnNoContent()
{
// Arrange
currentUserService.Setup(s => s.UserId).Returns(providerId.ToString());
currentUserService.Setup(s => s.IsInRole(Role.Provider)).Returns(true);

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

// Act
Assert.IsInstanceOf<NoContentResult>(result); // Verify the result type
}


private PositionUpdateDto FakePositionUpdateDto(Guid providerId, PositionDto oldPosition)
{
return new PositionUpdateDto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace OutOfSchool.WebApi.Controllers.V1;
[Route("api/v{version:apiVersion}/[controller]")]
[Authorize]
[Authorize(Roles = "provider")]
[ApiController]
public class PositionController : ControllerBase
{
Expand Down Expand Up @@ -121,6 +121,14 @@ public async Task<IActionResult> Delete(Guid id)
await positionService.DeleteAsync(id, providerOwnerId);
return NoContent();
}
catch (KeyNotFoundException ex)
{
return NotFound(ex.Message);
}
catch (UnauthorizedAccessException ex)
{
return Forbid(ex.Message);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
Expand Down

0 comments on commit cc14130

Please sign in to comment.