Пример #1
0
        public async Task Post_Register_ReturnsNewlyCreatedUser()
        {
            // Arrange
            var testUsername = "******";
            var dbContext    = DbContextMocker.GetLandmarkingContext(nameof(Post_Register_ReturnsNewlyCreatedUser), false);
            var repository   = new AuthRepository(dbContext);
            var mapper       = IMapperMocker.getMapper();
            var iConfig      = IConfigMocker.getIconfiguration();
            var controller   = new AuthController(repository, mapper, iConfig);

            var newUser = new UserRegisterDto()
            {
                Username = testUsername,
                Password = "******",
                Email    = "*****@*****.**"
            };

            // Act

            var response = await controller.Register(newUser);

            // Assert
            var CreatedAtRouteResult = Assert.IsType <CreatedAtRouteResult>(response);
            var createdUser          = Assert.IsType <UserToReturnDto>(CreatedAtRouteResult.Value);

            Assert.Equal(testUsername.ToLower(), createdUser.Username);


            //cleanup
            dbContext.Dispose();
        }
Пример #2
0
        public async Task Post_Login_returnsJWTTokenAndUserObject()
        {
            // Arrange
            var testUsername = "******";
            var dbContext    = DbContextMocker.GetLandmarkingContext(nameof(Post_Login_returnsJWTTokenAndUserObject));
            var repository   = new AuthRepository(dbContext);
            var mapper       = IMapperMocker.getMapper();
            var iConfig      = IConfigMocker.getIconfiguration();
            var controller   = new AuthController(repository, mapper, iConfig);

            var userLogin = new UserLoginDto()
            {
                Username = testUsername,
                Password = "******"
            };

            // Act
            var response = await controller.Login(userLogin);

            // Assert
            var OkObjectResult = Assert.IsType <OkObjectResult>(response);

            //// retrieve & convert anonymous response object with Reflection
            dynamic         responseObject     = OkObjectResult.Value;
            var             tokenProperty      = responseObject.GetType().GetProperty("token");
            string          encodedToken       = tokenProperty.GetValue(responseObject, null);
            var             userObjectProperty = responseObject.GetType().GetProperty("userObject");
            UserToReturnDto userObject         = userObjectProperty.GetValue(responseObject, null);


            Assert.Equal(testUsername.ToLower(), userObject.Username);

            // decode verifyJWT
            var decodedToken = new JwtSecurityToken(encodedToken);
            var jwtUsername  = decodedToken.Claims.First(claim => claim.Type == "unique_name").Value;

            Assert.Equal(testUsername.ToLower(), jwtUsername);

            //cleanup
            dbContext.Dispose();
        }
Пример #3
0
        public async Task Get_ReturnsUserDetails()
        {
            // Arrange
            var dbContext  = DbContextMocker.GetLandmarkingContext(nameof(Get_ReturnsUserDetails));
            var repository = new LandmarkingRepository(dbContext);
            var mapper     = IMapperMocker.getMapper();
            var controller = new UsersController(repository, mapper);
            int userId     = 3;

            // Act
            var response = await controller.GetUser(userId) as OkObjectResult;

            // Assert
            var user = Assert.IsType <UserToReturnDto>(response.Value);

            Assert.Equal(userId, user.Id);


            //cleanup
            dbContext.Dispose();
        }