Exemplo n.º 1
0
        public void TestDeactivateUser()
        {
            IConfiguration config = getTestConfiguration();

            using (var context = new SGContext(config))
            {
                var token = quickLogin(context, "admin@localhost", "password");
                Assert.IsTrue(token != null, "Unable to log in as admin user.  Test can't run");

                var testUser  = TryCreateUser(context, token, "deactiveUser@localhost", "password2");
                var userToken = quickLogin(context, "deactiveUser@localhost", "password2");
                Assert.IsTrue(userToken != null, "Unable to log in as activation test user.  Test can't run");

                UserTasks.SetUserActiveAsync(context, token, "deactiveUser@localhost", false).Wait();

                var task = LoginTokenTasks.GetLoginTokenAsync(context, userToken.Id);
                task.Wait();
                var userToken2 = task.Result;
                Assert.IsNull(userToken2, "A user's tokens should not be available after deactivation.");

                userToken = quickLogin(context, "deactiveUser@localhost", "password2");
                Assert.IsNull(userToken2, "A user's should not be able to login after deactivation.");

                UserTasks.SetUserActiveAsync(context, token, "deactiveUser@localhost", true).Wait();

                task = LoginTokenTasks.GetLoginTokenAsync(context, userToken.Id);
                task.Wait();
                var userToken3 = task.Result;
                Assert.IsNotNull(userToken3, "User failed to login after reactivation.");
            }
        }
Exemplo n.º 2
0
        public void TestBasicLogin()
        {
            IConfiguration config = getTestConfiguration();

            using (var context = new SGContext(config))
            {
                var tokenTask = LoginTokenTasks.LoginAsync(context, "admin@localhost", "password");
                tokenTask.Wait();
                var token = tokenTask.Result;
                Assert.IsTrue(token != null, "Login token returned null");
                Assert.IsFalse(token.UserId == 0, "I've broken the EF key link somehow");
                Assert.AreEqual(token.UserId, token.User.Id, "I've broken the EF key link somehow");

                var token2Task = LoginTokenTasks.GetLoginTokenAsync(context, token.Id);
                token2Task.Wait();
                var token2 = token2Task.Result;
                Assert.AreEqual(token2.UserId, token2.User.Id, "I'ev broken the EF key link somehow");

                Assert.IsNotNull(token2, "A valid login token has come back as null");
                Assert.AreEqual(token.Id, token2.Id, "The token requested is not the token retrieved.");

                LoginTokenTasks.LogoutAsync(context, token2).Wait();

                var token3Task = LoginTokenTasks.GetLoginTokenAsync(context, token.Id);
                token3Task.Wait();
                var token3 = token3Task.Result;

                Assert.IsNull(token3, "After logout the token should return as null.");
            }

            Assert.Pass();
        }
Exemplo n.º 3
0
        public async Task logout(Int64 tokenId, string email)
        {
            var token = await LoginTokenTasks.GetLoginTokenAsync(_context, tokenId);

            if (token == null)
            {
                throw AutoApiError.NotFound();
            }
            if (email != null)
            {
                if (string.IsNullOrWhiteSpace(email))
                {
                    throw AutoApiError.InvalidParam("email");
                }
                var userRole = new UserRole(token.User.RawRole);
                if (!userRole.IsAdmin)
                {
                    throw AutoApiError.Unauthorised();
                }
                await LoginTokenTasks.LogoutAsync(_context, token, email);
            }
            else
            {
                await LoginTokenTasks.LogoutAsync(_context, token);
            }
        }
Exemplo n.º 4
0
        private async Task <LoginToken> quickGetToken(ApiCall apiCall, bool allowAnon = false)
        {
            Int64 tokenId;
            bool  parsed = Int64.TryParse(apiCall.Parameters.OrNull("tokenId"), out tokenId);

            if (!parsed)
            {
                if (allowAnon)
                {
                    tokenId = 0;
                }
                else
                {
                    return(null);
                }
            }
            if (tokenId == 0 && allowAnon)
            {
                return(LoginTokenTasks.GetAnonmymousToken());
            }
            var token = await LoginTokenTasks.GetLoginTokenAsync(_context, tokenId);

            if (token.User.Active)
            {
                return(token);
            }
            return(null);
        }
Exemplo n.º 5
0
        private LoginToken quickGetToken(SGContext context, Int64 tokenId)
        {
            var task = LoginTokenTasks.GetLoginTokenAsync(context, tokenId);

            task.Wait();
            var token = task.Result;

            return(token);
        }
Exemplo n.º 6
0
        private async Task <LoginToken> quickGetToken(Int64 tokenId, bool allowAnon = false)
        {
            if (tokenId == LoginToken.AnonymousLoginId && allowAnon)
            {
                return(LoginTokenTasks.GetAnonmymousToken());
            }
            var token = await LoginTokenTasks.GetLoginTokenAsync(_context, tokenId);

            if (token.User.Active)
            {
                return(token);
            }
            throw AutoApiError.InvalidToken();
        }
Exemplo n.º 7
0
        public async Task <LoginToken> getToken(Int64 tokenId)
        {
            LoginToken token;

            if (tokenId == LoginToken.AnonymousLoginId)
            {
                token = LoginTokenTasks.GetAnonmymousToken();
            }
            else
            {
                token = await LoginTokenTasks.GetLoginTokenAsync(_context, tokenId);
            }

            if (token == null)
            {
                throw AutoApiError.InvalidToken();
            }

            return(token.CloneForExport());
        }