Пример #1
0
        public ActionResult RegisterUser(ApiUser newUser)
        {
            CvApiResponse response = new CvApiResponse();

            // create application user...
            IdentityUser identityUser = new IdentityUser()
            {
                Email          = newUser.Email,
                EmailConfirmed = false,
                UserName       = newUser.Username
            };

            // ensure unique
            if (_userManager.Users.Where(u => u.Email == newUser.Email).ToList().Count == 0)
            {
                IdentityResult result = _userManager.CreateAsync(identityUser, newUser.Password).Result;
                response.Success = result.Succeeded;
                if (!result.Succeeded)
                {
                    response.Message = result.Errors.ToList()[0].Description;
                }
            }
            else
            {
                response.Message = "Email exists";
            }

            return(Ok(response));
        }
        [TestCase("*****@*****.**", "Pass12345!", "User123", true, true)]  // should pass
        public async Task TestRegister(string email, string pass, string user, bool rememberMe, bool shouldPass)
        {
            ApiUser userObj = new ApiUser()
            {
                Email      = email,
                Password   = pass,
                Username   = user,
                RememberMe = rememberMe
            };

            var json = JsonConvert.SerializeObject(userObj);

            StringContent strContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");

            // The endpoint or route of the controller action.
            var httpResponse = await _client.PostAsync("auth/register", strContent);

            var result = httpResponse.Content;

            string content = result.ReadAsStringAsync().Result;

            CvApiResponse response = JsonConvert.DeserializeObject <CvApiResponse>(content);

            // make sure that the content was returned.
            Assert.AreEqual(shouldPass, response.Success);
        }
Пример #3
0
        public async Task <ActionResult> Logout()
        {
            CvApiResponse response = new CvApiResponse();
            await _signInManager.SignOutAsync();

            response.Success = true;
            return(Ok(response));
        }
Пример #4
0
        public async Task <ActionResult> LogIn(ApiUser user)
        {
            CvApiResponse response     = new CvApiResponse();
            IdentityUser  identityUser = await _userManager.FindByEmailAsync(user.Email);

            if (identityUser != null)
            {
                Microsoft.AspNetCore.Identity.SignInResult result = await _signInManager.PasswordSignInAsync(identityUser, user.Password, user.RememberMe, false);

                if (result.Succeeded)
                {
                    response.Success = true;
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid Password";
                }
            }
            return(Ok(response));
        }
        public async Task TestLogin(bool correctPassword)
        {
            // Register a new user.
            ApiUser userObj = new ApiUser()
            {
                Email    = "*****@*****.**",
                Password = "******",
                Username = "******",
            };
            var           json         = JsonConvert.SerializeObject(userObj);
            StringContent strContent   = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
            var           httpResponse = await _client.PostAsync("auth/register", strContent);


            // get the password to use
            string password = correctPassword ? "Testing123!" : "WrongPassword";

            userObj.Password = password;
            // serialise the new object
            json       = JsonConvert.SerializeObject(userObj);
            strContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");

            // The endpoint or route of the controller action.
            httpResponse = await _client.PostAsync("auth/login", strContent);

            var    result  = httpResponse.Content;
            string content = result.ReadAsStringAsync().Result;

            // deserialise the result
            CvApiResponse response = JsonConvert.DeserializeObject <CvApiResponse>(content);

            Assert.AreEqual(correctPassword, response.Success);

            if (correctPassword)
            {
                IEnumerable <string> cookies = httpResponse.Headers.SingleOrDefault(header => header.Key == "Set-Cookie").Value;
                bool authCookieFound         = cookies.SingleOrDefault(s => s.Contains(".AspNetCore.Identity.Application")) != null;
                Assert.IsTrue(authCookieFound);
            }
        }
        public async Task TestEndPointReturnsContent(string endpoint)
        {
            // Arrange
            ApiUser userObj = new ApiUser()
            {
                Email    = "*****@*****.**",
                Password = "******",
                Username = "******",
            };
            var           json         = JsonConvert.SerializeObject(userObj);
            StringContent strContent   = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
            var           httpResponse = await _client.PostAsync("auth/register", strContent);

            // The endpoint or route of the controller action.
            httpResponse = await _client.PostAsync("auth/login", strContent);

            // ensure that we are logged in correctly.
            var result = httpResponse.Content;

            string content = result.ReadAsStringAsync().Result;

            CvApiResponse response = JsonConvert.DeserializeObject <CvApiResponse>(content);

            Assert.AreEqual(true, response.Success);
            IEnumerable <string> cookies = httpResponse.Headers.SingleOrDefault(header => header.Key == "Set-Cookie").Value;
            bool authCookieFound         = cookies.SingleOrDefault(s => s.Contains(".AspNetCore.Identity.Application")) != null;

            Assert.IsTrue(authCookieFound);

            // The endpoint or route of the controller action.
            httpResponse = await _client.GetAsync(endpoint);

            result = httpResponse.Content;

            content = result.ReadAsStringAsync().Result;
            // make sure that the content was returned.
            Assert.IsFalse(content.IsNullOrEmpty());
        }
        public async Task TestLogout()
        {
            // Arrange
            ApiUser userObj = new ApiUser()
            {
                Email    = "*****@*****.**",
                Password = "******",
                Username = "******",
            };
            var           json         = JsonConvert.SerializeObject(userObj);
            StringContent strContent   = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
            var           httpResponse = await _client.PostAsync("auth/register", strContent);

            // The endpoint or route of the controller action.
            httpResponse = await _client.PostAsync("auth/login", strContent);

            // ensure that we are logged in correctly.
            var    result  = httpResponse.Content;
            string content = result.ReadAsStringAsync().Result;

            CvApiResponse response = JsonConvert.DeserializeObject <CvApiResponse>(content);

            Assert.AreEqual(true, response.Success);
            IEnumerable <string> cookies = httpResponse.Headers.SingleOrDefault(header => header.Key == "Set-Cookie").Value;
            bool authCookieFound         = cookies.SingleOrDefault(s => s.Contains(".AspNetCore.Identity.Application")) != null;

            Assert.IsTrue(authCookieFound);

            // Act
            httpResponse = await _client.PostAsync("auth/logout", strContent);

            // Assert
            cookies = httpResponse.Headers.SingleOrDefault(header => header.Key == "Set-Cookie").Value;
            string cookie = cookies.SingleOrDefault(s => s.Contains(".AspNetCore.Identity.Application"));

            if (cookie != null)
            {
                string[] components = cookie.Split(";");

                foreach (string component in components)
                {
                    string[] values = component.Split("=");

                    switch (values[0])
                    {
                    case ".AspNetCore.Identity.Application":
                    {
                        Assert.IsEmpty(values[1]);
                    }
                    break;

                    case "expires":
                    {
                        Assert.AreEqual(values[1], "Thu, 01 Jan 1970 00:00:00 GMT");
                    }
                    break;

                    default:
                        break;
                    }
                }
            }
        }