public async Task <IActionResult> Get( [FromQuery] int?pageNum, [FromQuery] int?pageSize, [FromQuery] string title, [FromQuery] string description, [FromQuery] string price, [FromQuery] string regionName, // [FromQuery] string createdDate, CancellationToken cancellationToken) { Listinguser user = (Listinguser)HttpContext.Items["User"]; object parameters = new List <KeyValuePair <string, string> > { // new KeyValuePair<string, string> ("CreatorId", user.Id.ToString()), new KeyValuePair <string, string> ("RegionId", user.RegionId.ToString()), new KeyValuePair <string, string> ("Title", title), new KeyValuePair <string, string> ("Description", description), new KeyValuePair <string, string> ("Price", price), new KeyValuePair <string, string> ("RegionName", regionName), }; // Repository returns paged data if pageNum and pageSize are provided var listings = await _unitOfWork.Region_ListingRepository .GetAsync(parameters, pageNum, pageSize); return(Ok(listings)); }
public AuthenticateResponse(Listinguser user, string token) { Id = user.Id; Firstname = user.Firstname; Lastname = user.Lastname; Userid = user.Userid; Email = user.Email; RegionId = user.RegionId; Token = token; }
public static ControllerContext GetControllerContext(Listinguser user = null) { var context = new DefaultHttpContext(); if (user != null) { context.Items["User"] = user; context.Request.Headers["Authorization"] = GenerateJwtToken(user); } return(new ControllerContext() { HttpContext = context }); }
public async Task <IActionResult> Get( int id, CancellationToken cancellationToken) { Listinguser user = (Listinguser)HttpContext.Items["User"]; var region_Listing = await _unitOfWork.Region_ListingRepository .GetAsync(id.ToString()); if (region_Listing?.RegionId != user.RegionId) { return(Ok(null)); } return(Ok(region_Listing)); }
public async void ShouldDenyInvalidPassword(int id, string userid, string email, string firstname, string lastname, string password, int regionId) { // Arrange var userIdParam = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string> ("UserId", userid) }; var expectedUser = new Listinguser { Id = id, Userid = userid, Email = email, Firstname = firstname, Lastname = lastname, Password = Util.HashPassword(password), RegionId = regionId }; mockListinguserRepository .Setup(x => x.GetAsync( It.Is <List <KeyValuePair <string, string> > >(x => x.First(p => p.Key == "UserId").Value == userid ), null, null )) .ReturnsAsync(new List <Listinguser> { expectedUser }); mockUnitOfWork .Setup(uow => uow.ListinguserRepository) .Returns(mockListinguserRepository.Object); // Act var loginModel = new LoginModel { Userid = userid, Password = password + "abc" }; var result = await _controller.Login(loginModel, cancellationToken); // Assert Assert.IsType <UnauthorizedResult>(result); Assert.Equal(401, (result as UnauthorizedResult).StatusCode); }
public async Task <IActionResult> Post( ListinguserRequest listinguserCreateRequest, CancellationToken cancellationToken) { var newListingUser = new Listinguser { Userid = listinguserCreateRequest.Userid, Email = listinguserCreateRequest.Email, Firstname = listinguserCreateRequest.Firstname, Lastname = listinguserCreateRequest.Lastname, Password = listinguserCreateRequest.Password, RegionId = listinguserCreateRequest.RegionId }; try { var password = newListingUser.Password; #nullable enable newListingUser.Password = new PasswordHasher <object?>() .HashPassword(null, password); #nullable disable
public ListingControllerShould() { mockLogger = new Mock <ILogger <ListingController> >(); mockUnitOfWork = new Mock <IUnitOfWork>(); mockListingRepository = new Mock <IRepository <Listing> >(); mockListinguserRepository = new Mock <IRepository <Listinguser> >(); mockRegion_ListingRepository = new Mock <IRepository <Region_Listing> >(); httpContextMock = new Mock <HttpContext>(); authenticatedUser = new Listinguser { Id = 1, Userid = "jsmith", Email = "*****@*****.**", Firstname = "John", Lastname = "Smith", Password = Util.HashPassword("test1"), RegionId = 5 }; cancellationToken = new CancellationToken(); _controller = new ListingController( mockLogger.Object, mockUnitOfWork.Object); }
public async void ShouldLoginValidCredential(int id, string userid, string email, string firstname, string lastname, string password, int regionId) { // Arrange Mock <IRepository <Listinguser> > mockListinguserRepository = new Mock <IRepository <Listinguser> >(); var userIdParam = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string> ("UserId", userid) }; var expectedUser = new Listinguser { Id = id, Userid = userid, Email = email, Firstname = firstname, Lastname = lastname, Password = Util.HashPassword(password), RegionId = regionId }; mockListinguserRepository .Setup(x => x.GetAsync( It.Is <List <KeyValuePair <string, string> > >(x => x.First(p => p.Key == "UserId").Value == userid ), null, null )) .ReturnsAsync(new List <Listinguser> { expectedUser }); mockUnitOfWork .Setup(uow => uow.ListinguserRepository) .Returns(mockListinguserRepository.Object); // Act var loginModel = new LoginModel { Userid = userid, Password = password }; var result = await _controller.Login(loginModel, cancellationToken); // Assert var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.Equal(200, okResult.StatusCode); var authenticateResponse = okResult.Value as AuthenticateResponse; Assert.NotNull(authenticateResponse); Assert.False(string.IsNullOrEmpty(authenticateResponse.Token)); Assert.Equal(id, authenticateResponse.Id); Assert.Equal(userid, authenticateResponse.Userid); Assert.Equal(email, authenticateResponse.Email); Assert.Equal(firstname, authenticateResponse.Firstname); Assert.Equal(lastname, authenticateResponse.Lastname); Assert.Equal(regionId, authenticateResponse.RegionId); }
public async void ShouldNotRegisterIfDuplicateUserIdFound( int id, string userid, string email, string firstname, string lastname, string password, int regionId) { // Arrange var newUser = new Listinguser { Id = id, Userid = userid, Email = email, Firstname = firstname, Lastname = lastname, Password = password, RegionId = regionId }; var newUserRequest = new ListinguserRequest { Userid = userid, Email = email, Firstname = firstname, Lastname = lastname, Password = password, RegionId = regionId }; mockListinguserRepository .Setup(x => x.GetAsync( It.Is <List <KeyValuePair <string, string> > >(x => x.First(p => p.Key == "UserId").Value == userid ), null, null )) .ReturnsAsync(new List <Listinguser> { new Listinguser() }); mockListinguserRepository .Setup(x => x.PostAsync( It.Is <Listinguser>(x => x.Userid == userid) )) .ReturnsAsync(new Listinguser { Id = 1, Userid = userid, Email = email, Firstname = firstname, Lastname = lastname, Password = password, RegionId = regionId }); mockUnitOfWork .Setup(uow => uow.ListinguserRepository) .Returns(mockListinguserRepository.Object); // Act var result = await _controller.Post(newUserRequest, cancellationToken); // Assert Assert.NotNull(result); Assert.IsType <ObjectResult>(result); var objResult = result as ObjectResult; Assert.Equal(500, objResult.StatusCode); var valueResult = objResult.Value as ProblemDetails; Assert.Contains("Registration Unsucessful", valueResult.Title); Assert.Contains("UserId already exists", valueResult.Detail); }
public async void ShouldRegisterUser(int id, string userid, string email, string firstname, string lastname, string password, int regionId) { // Arrange var newUser = new Listinguser { Id = id, Userid = userid, Email = email, Firstname = firstname, Lastname = lastname, Password = password, RegionId = regionId }; var newUserRequest = new ListinguserRequest { Userid = userid, Email = email, Firstname = firstname, Lastname = lastname, Password = password, RegionId = regionId }; mockListinguserRepository .Setup(x => x.GetAsync( It.Is <List <KeyValuePair <string, string> > >(x => x.First(p => p.Key == "UserId").Value == userid ), null, null )) .ReturnsAsync(new List <Listinguser>()); mockListinguserRepository .Setup(x => x.PostAsync( It.Is <Listinguser>(x => x.Userid == userid) )) .ReturnsAsync(new Listinguser { Id = 1, Userid = userid, Email = email, Firstname = firstname, Lastname = lastname, Password = password, RegionId = regionId }); mockUnitOfWork .Setup(uow => uow.ListinguserRepository) .Returns(mockListinguserRepository.Object); // Act var result = await _controller.Post(newUserRequest, cancellationToken); // Assert var createdResult = result as CreatedAtActionResult; Assert.NotNull(createdResult); Assert.Equal(201, createdResult.StatusCode); Assert.IsType <Listinguser>(createdResult.Value); var valueResult = createdResult.Value as Listinguser; Assert.NotNull(valueResult); // Assert.Equal(id, valueResult.Id); Assert.Equal(userid, valueResult.Userid); Assert.Equal(email, valueResult.Email); Assert.Equal(firstname, valueResult.Firstname); Assert.Equal(lastname, valueResult.Lastname); Assert.Equal(regionId, valueResult.RegionId); }