Example #1
0
        public void LoginUserValid()
        {
            LoginRequest request = new LoginRequest()
            {
                Password = "******",
                Username = "******"
            };

            var facade   = new UsersFacade();
            var response = facade.LoginUser(request);

            Assert.AreEqual(HttpStatusCode.OK, response.Status);
            Assert.IsFalse(string.IsNullOrEmpty(response.Token));
        }
Example #2
0
        public void LoginFakeUser()
        {
            LoginRequest request = new LoginRequest()
            {
                Password = "******",
                Username = "******"
            };

            var facade   = new UsersFacade();
            var response = facade.LoginUser(request);

            Assert.AreEqual(HttpStatusCode.BadRequest, response.Status);
            Assert.IsTrue(string.IsNullOrEmpty(response.Token));
        }
Example #3
0
        public void LoginUserBadPassword()
        {
            LoginRequest request = new LoginRequest()
            {
                Password = "******",
                Username = "******"
            };

            var facade   = new UsersFacade();
            var response = facade.LoginUser(request);

            Assert.AreEqual(HttpStatusCode.BadRequest, response.Status);
            Assert.IsTrue(string.IsNullOrEmpty(response.Token));
        }
Example #4
0
        public IActionResult Login([FromBody] LoginRequest request)
        {
            if (request == null)
            {
                return(BadRequest());
            }

            try
            {
                var facade   = new UsersFacade();
                var response = facade.LoginUser(request);

                switch (response.Status)
                {
                case HttpStatusCode.OK:
                {
                    CookieOptions cookie = new CookieOptions()
                    {
                        Expires     = DateTime.Now.AddHours(4),
                        HttpOnly    = false,
                        Domain      = ".swin.helpdesk.edu.au",
                        IsEssential = true,
                        Path        = "/",
                        Secure      = false,
                        SameSite    = SameSiteMode.Strict,
                    };

                    Response.Cookies.Append("AuthToken", response.Token, cookie);
                    return(Ok(response));
                }

                case HttpStatusCode.Accepted:
                {
                    CookieOptions cookie = new CookieOptions()
                    {
                        Expires     = DateTime.Now.AddHours(4),
                        HttpOnly    = false,
                        Domain      = ".swin.helpdesk.edu.au",
                        IsEssential = true,
                        Path        = "/",
                        Secure      = false,
                        SameSite    = SameSiteMode.Strict,
                    };

                    Response.Cookies.Append("AuthToken", response.Token, cookie);
                    return(Accepted(response));
                }

                case HttpStatusCode.BadRequest:
                    return(BadRequest(BuildBadRequestMessage(response)));

                case HttpStatusCode.InternalServerError:
                    return(StatusCode(StatusCodes.Status500InternalServerError));

                case HttpStatusCode.NotFound:
                    return(NotFound());
                }
                s_logger.Fatal("This code should be unreachable, unknown result has occured.");
            }
            catch (Exception ex)
            {
                s_logger.Error(ex, "Unable to login user.");
            }
            return(StatusCode(StatusCodes.Status500InternalServerError));
        }