public async Task <IActionResult> UpdateQuiz([FromRoute] Guid id, [FromBody] UpdateQuizModel updatedQuiz) { if (updatedQuiz.Id != id) { return(BadRequest()); } var quiz = await _quizService.GetQuizAsync(id).ConfigureAwait(false); if (quiz == null) { return(NotFound()); } var userId = GetUserIdFromToken(); if (quiz.QuizCreatorId != userId) { return(Unauthorized()); } var entity = _mapper.Map <QuizEntity>(updatedQuiz); entity.QuizCreatorId = userId; await _quizService.UpdateAsync(entity).ConfigureAwait(false); return(NoContent()); }
public async Task When_UpdateMethodIsCalledWithNoAuthorization_Then_ShouldReturnUnauthorizedResult() { // Arrange UpdateQuizModel entity = new UpdateQuizModel { Id = new Guid(), Name = "valid name", Description = "valid description", Questions = new List <CreateQuestionModel> { new CreateQuestionModel() }, IsRanked = false }; var quizEntity = new QuizEntity { QuizCreatorId = Guid.NewGuid() }; UpdateQuizMocking(quizEntity); // Act var result = await SystemUnderTest.UpdateQuiz(entity.Id, entity); // Assert result.Should().BeOfType <UnauthorizedResult>(); }
public async Task When_UpdateMethodIsCalledWithValidParameter_Then_ShouldReturnNoContent() { // Arrange UpdateQuizModel entity = new UpdateQuizModel { Id = new Guid(), Name = "valid name", Description = "valid description", Questions = new List <CreateQuestionModel> { new CreateQuestionModel() }, IsRanked = false }; var quizEntity = new QuizEntity { QuizCreatorId = Guid.NewGuid() }; UpdateQuizMocking(quizEntity); _mockUserValidation.Setup(x => x.GetUserIdFromClaims(It.IsAny <HttpContext>())).Returns(quizEntity.QuizCreatorId); // Act var result = await SystemUnderTest.UpdateQuiz(entity.Id, entity); // Assert result.Should().BeOfType <NoContentResult>(); _mockQuizService.Verify(x => x.UpdateAsync(It.IsAny <QuizEntity>()), Times.Exactly(1)); _mockQuizService.Verify(x => x.GetQuizAsync(It.IsAny <Guid>()), Times.Exactly(1)); }
public async Task When_UpdateMethodIsCalledWithDifferentIds_Then_ShouldReturnBadRequest() { // Arrange UpdateQuizModel entity = new UpdateQuizModel { Id = new Guid(), Name = "valid name", Description = "valid description", Questions = new List <CreateQuestionModel> { new CreateQuestionModel() }, IsRanked = false }; // Act var result = await SystemUnderTest.UpdateQuiz(Guid.Parse("ce17d9f9-449c-4c78-b9eb-94c14f020eb2"), entity); // Assert result.Should().BeOfType <BadRequestResult>(); }
public async Task When_UpdateMethodIsCalledWithNoQuizFound_Then_ShouldReturnNotFound() { // Arrange UpdateQuizModel entity = new UpdateQuizModel { Id = new Guid(), Name = "valid name", Description = "valid description", Questions = new List <CreateQuestionModel> { new CreateQuestionModel() }, IsRanked = false }; _mockQuizService.Setup(x => x.GetQuizAsync(new Guid())) .Returns(Task.FromResult((QuizEntity)null)); // Act var result = await SystemUnderTest.UpdateQuiz(entity.Id, entity); // Assert result.Should().BeOfType <NotFoundResult>(); }