Ejemplo n.º 1
0
        /// <summary>
        /// Login with a user and return the token as string.
        /// </summary>
        /// <param name="user">A user to login</param>
        /// <returns>A token as string</returns>
        private async Task <string> GetToken(ApplicationUser user)
        {
            Assert.NotNull(user);
            Assert.NotNull(user.Email);
            var body = JsonStringBuilder.LoginJsonBody(
                user.Email, MockApplicationUsers.UniversalPassword);
            var content  = new StringContent(body);
            var response = await _endSystems.Post(Routes.AccountRoute + "/login", content);

            Assert.NotNull(response);
            var token = JToken.Parse(response.Body).ToString();

            Assert.NotNull(token);
            return(token);
        }
Ejemplo n.º 2
0
        public async Task PostAccountLogin_NonExistingUser_Unauthorized()
        {
            // Arrange
            var body    = JsonStringBuilder.LoginJsonBody("*****@*****.**", "J0hn_Carmack");
            var content = new StringContent(body);
            var path    = $"{Routes.AccountRoute}/login";

            // Act
            var response = await _endSystems.Post(path, content);

            // Assert
            Assert.Equal(HttpStatusCode.Unauthorized, response.Code);

            // Tear down
            _endSystems.Dispose();
        }
Ejemplo n.º 3
0
        public async Task PostAccountLogin_ExistingUserWrongPassword_Unauthorized()
        {
            // Arrange
            var user    = MockApplicationUsers.Get(6);
            var body    = JsonStringBuilder.LoginJsonBody(user.Email, "A-Am-N0t-C0rrect");
            var content = new StringContent(body);
            var path    = $"{Routes.AccountRoute}/login";

            // Act
            var response = await _endSystems.Post(path, content);

            // Assert
            Assert.Equal(HttpStatusCode.Unauthorized, response.Code);

            // Tear down
            _endSystems.Dispose();
        }
Ejemplo n.º 4
0
        public async Task PostAccountRegiser_ValidNewUser_CreatedAndToken()
        {
            // Arrange
            var user = new ApplicationUser
            {
                Name     = "The Clash",
                Email    = "*****@*****.**",
                UserName = "******"
            };
            var registerBody = JsonStringBuilder.RegisterJsonBody
                               (
                user.Name,
                user.Email,
                MockApplicationUsers.UniversalPassword
                               );
            var loginBody = JsonStringBuilder.LoginJsonBody
                            (
                user.Email,
                MockApplicationUsers.UniversalPassword
                            );
            var registerContent = new StringContent(registerBody);
            var loginContent    = new StringContent(loginBody);
            var registerPath    = $"{Routes.AccountRoute}/register";
            var loginPath       = $"{Routes.AccountRoute}/login";

            // Act
            var registerResponse = await _endSystems.Post(registerPath, registerContent);

            var loginResponse = await _endSystems.Post(loginPath, loginContent);

            var registerToken = JToken.Parse(registerResponse.Body).ToString();
            var loginToken    = JToken.Parse(loginResponse.Body).ToString();

            // Assert
            Assert.Equal(HttpStatusCode.Created, registerResponse.Code);
            Assert.Equal(HttpStatusCode.OK, loginResponse.Code);
            CheckToken(MockRoles.User, user, registerToken, false);
            CheckToken(MockRoles.User, user, loginToken, false);

            // Tear down
            _endSystems.Dispose();
        }
Ejemplo n.º 5
0
        public async Task PostAccountLogin_ExistingUser_OkWithToken()
        {
            // Arrange
            var user    = MockApplicationUsers.Get(6);
            var path    = $"{Routes.AccountRoute}/login";
            var body    = JsonStringBuilder.LoginJsonBody(user.Email, MockApplicationUsers.UniversalPassword);
            var content = new StringContent(body);
            var role    = MockRoles.Admin.Id == MockUserRoles.GetUserRoleForUser(user.Id).RoleId
                ? MockRoles.Admin : MockRoles.User;

            // Act
            var response = await _endSystems.Post(path, content);

            var token = JToken.Parse(response.Body).ToString();

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.Code);
            CheckToken(role, user, token);

            // Tear down
            _endSystems.Dispose();
        }