Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abenezer.backend assessment #359

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using CineFlex.Application.Features.Posts.CQRS.Commands;
using CineFlex.Application.Features.Posts.CQRS.Queries;
using CineFlex.Application.Features.Posts.DTOs;
using Microsoft.AspNetCore.Mvc;

namespace CineFlex.API.Controllers
{
[ApiController]

[Route("api/[Controller]")]
public class PostController : BaseApiController
{
[HttpGet("{id}")]
public async Task<ActionResult> Get(int id)
{
var query = new GetPostDetailQuery { Id = id };
var response = await Mediator.Send(query);
return HandleResult(response);
}
[HttpGet]
public async Task<ActionResult<List<PostDto>>> GetPosts()
{
var query = new GetPostListQuery();
var response = await Mediator.Send(query);
return HandleResult(response);
}
[HttpPost]
public async Task<ActionResult> Post([FromBody] CreatePostDto createPostDto)
{
var command = new CreatePostCommand { CreatePostDto = createPostDto };
var response = await Mediator.Send(command);
return HandleResult(response);
}
[HttpPatch]
public async Task<ActionResult> Put([FromBody] UpdatePostDto updatePostDto)
{
var command = new UpdatePostCommand { UpdatePostDto = updatePostDto };
var response = await Mediator.Send(command);
return HandleResult(response);
}
[HttpDelete]
public async Task<ActionResult> Delete(int Id)
{
var command = new DeletePostCommand { Id = Id };
var response = await Mediator.Send(command);
return HandleResult(response);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using CineFlex.Application.Contracts.Identity;
using CineFlex.Application.Models.Identity;
using CineFlex.Application.Responses;
using Microsoft.AspNetCore.Mvc;

namespace CineFlex.API.Controllers
{
[Route("api/[Controller]")]
[ApiController]
public class UserController : BaseApiController
{
private readonly IAuthService _authenticationService;
public UserController(IAuthService authenticationService)
{
_authenticationService = authenticationService;
}
[HttpPost("login")]
public async Task<ActionResult<Result<AuthResponse>>> Login(AuthRequest request)
{
return Ok(await _authenticationService.Login(request));
}
[HttpPost("signUp")]
public async Task<ActionResult<Result<RegistrationResponse>>> Registor(RegistrationRequest request)
{
return Ok(await _authenticationService.Registor(request));
}
}
}
11 changes: 11 additions & 0 deletions Backend/backend_assessment/CineFlex/CineFlex.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@

var builder = WebApplication.CreateBuilder(args);

builder.Services.Configure<IdentityOptions>(options => {
options.Password.RequireDigit = true;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 1;


});

// Add services
builder.Services.ConfigureApplicationServices();
builder.Services.ConfigurePersistenceServices(builder.Configuration);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using CineFlex.Application.Contracts.Persistence;
using CineFlex.Domain;
using Moq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CineFlex.Application.UnitTest.Mocks
{
public static class MockPostRepository
{
public static Mock<IPostRepository> GetPostRepository()
{
var posts = new List<Post>{
new Post(){
Id = 1,
Title = "This is backend assessement first",
Content = "It contain the description of assessment first",
PostUserId= 1
}
,
new Post(){
Id=2,
Title = "This is backend assessement second",
Content = "It contain the description of assessment second",
PostUserId = 1
},
new Post(){
Id= 3,
Title= "This is backend assessement third",
Content="It contain the description of assessment third"

}

};

var mockRepo = new Mock<IPostRepository>();

mockRepo.Setup(r => r.Get(It.IsAny<int>())).ReturnsAsync((int id) =>
{
var post = posts.FirstOrDefault(p => p.Id == id);
return post;
});

mockRepo.Setup(r => r.GetAll()).ReturnsAsync(posts);

mockRepo.Setup(r => r.Add(It.IsAny<Post>())).ReturnsAsync((Post post) => {
posts.Add(post);
return post;
});

mockRepo.Setup(p => p.Update(It.IsAny<Post>())).Callback((Post post) => {
var newPost = posts.FirstOrDefault(p => p.Id == post.Id);
if (newPost != null)
{
newPost.Title = post.Title;
newPost.Content = post.Content;
}

});

mockRepo.Setup(p => p.Delete(It.IsAny<Post>())).Callback((Post post) => {
var newPost = posts.FirstOrDefault(p => p.Id == post.Id);
if (newPost != null)
{
posts.Remove(newPost);
}
});

return mockRepo;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CineFlex.Application.Contracts.Persistence;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CineFlex.Application.UnitTest.Mocks
{
public class MockUnitOfWork
{
public static Mock<IUnitOfWork> GetUnitOfWork()
{
var mockUow = new Mock<IUnitOfWork>();
var mockPostRepository = MockPostRepository.GetPostRepository();

mockUow.Setup(uow => uow.PostRepository).Returns(mockPostRepository.Object);
return mockUow;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using AutoMapper;
using CineFlex.Application.Contracts.Persistence;
using CineFlex.Application.Features.Posts.CQRS.Commands;
using CineFlex.Application.Features.Posts.CQRS.Handlers;
using CineFlex.Application.Features.Posts.DTOs;
using CineFlex.Application.Profiles;
using CineFlex.Application.Responses;
using CineFlex.Application.UnitTest.Mocks;
using Moq;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace CineFlex.Application.UnitTest.Posts
{
public class CreatePostCommandHandlerTest
{

private readonly IMapper _mapper;
private readonly Mock<IUnitOfWork> _mockUow;
private readonly CreatePostCommandHandler _handler;

public CreatePostCommandHandlerTest()
{
_mockUow = MockUnitOfWork.GetUnitOfWork();
var mapperConfig = new MapperConfiguration(c => { c.AddProfile<MappingProfile>(); });
_mapper = mapperConfig.CreateMapper();
_handler = new CreatePostCommandHandler(_mockUow.Object, _mapper);

}
[Fact]
public async Task ValidatePostCreationTest()
{
// arrange
var request = new CreatePostCommand()
{
CreatePostDto = new CreatePostDto
{
Title = "This is backend assessement firs",
Content = "It contain the description of assessment first",
PostUserId = 1
}
};


// Act
var response = await _handler.Handle(request, CancellationToken.None);
// Assert
response.ShouldNotBeNull();
response.ShouldBeOfType<BaseCommandResponse<int>>();
response.Success.ShouldBeTrue();
response.Value.ShouldBe(4);



}
[Fact]
public async Task InValidatePostCreationWithoutUserId()
{

//Arrange
var request = new CreatePostCommand
{
CreatePostDto = new CreatePostDto
{
Title = "This is backend assessement nothing",
Content = "It contain the description of assessment first"
}
};

//Act
var response = await _handler.Handle(request, CancellationToken.None);
response.ShouldNotBeNull();
response.ShouldBeOfType<BaseCommandResponse<int>>();
response.Success.ShouldBeFalse();

}


[Fact]
public async Task InvalidatePostCreationWithoutTitle()
{
var request = new CreatePostCommand
{
CreatePostDto = new CreatePostDto
{
Content = "It contain the description of assessment first",
PostUserId = 1
}
};

var response = await _handler.Handle(request, CancellationToken.None);
response.ShouldNotBeNull();
response.ShouldBeOfType<BaseCommandResponse<int>>();
response.Success.ShouldBeFalse();
}

[Fact]
public async Task InvalidatePostCreationWithoutContent()
{
var request = new CreatePostCommand
{
CreatePostDto = new CreatePostDto
{
Title = "This is backend assessement first",
PostUserId = 1
}
};

var response = await _handler.Handle(request, CancellationToken.None);
response.ShouldNotBeNull();
response.ShouldBeOfType<BaseCommandResponse<int>>();
response.Success.ShouldBeFalse();
}

[Fact]
public async Task InvalidatePostCreationWithNullTitle()
{
var request = new CreatePostCommand
{
CreatePostDto = new CreatePostDto
{
Title = null,
Content = "It contain the description of assessment first",
PostUserId = 1
}
};

var response = await _handler.Handle(request, CancellationToken.None);
response.ShouldNotBeNull();
response.ShouldBeOfType<BaseCommandResponse<int>>();
response.Success.ShouldBeFalse();
}

[Fact]
public async Task InvalidatePostCreationWithNullContent()
{
var request = new CreatePostCommand
{
CreatePostDto = new CreatePostDto
{
Title = "This is backend assessement first",
Content = null,
PostUserId = 1
}
};

var response = await _handler.Handle(request, CancellationToken.None);
response.ShouldNotBeNull();
response.ShouldBeOfType<BaseCommandResponse<int>>();
response.Success.ShouldBeFalse();
}
}
}
Loading