Exemplo n.º 1
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.º 2
0
        public void TestBasicAddRemoveUser()
        {
            IConfiguration config = getTestConfiguration();

            using (var context = new SGContext(config))
            {
                var adminTask = LoginTokenTasks.LoginAsync(context, Defaults.UserAdmin, Defaults.UserAdminPassword);
                adminTask.Wait();
                var admin = adminTask.Result;

                var userA = TryCreateUser(context, admin, "user@localhost", "pwd");
                Assert.IsNotNull(userA, "Failed to create a valid user user@localhost");
                var userB = TryCreateUser(context, admin, "user@localhost", "pwd");
                Assert.IsNull(userB, "Incorrectly succeeded in creating a user that already exists.");

                var userC = TryCreateUser(context, admin, "user2@localhost", "pwd");
                Assert.IsNotNull(userA, "Failed to create a valid user user2@localhost");

                TryChangeUserDisplayEmailAndName(context, admin, userA, "userA@localhost", "User A new name");

                var userTask = UserTasks.QuickGetUserNoAuthCheckAsync(context, "userA@localhost");
                userTask.Wait();
                var userA_2 = userTask.Result;

                Assert.IsNotNull(userA_2, "User failed to be retrieved after email update");
                Assert.AreEqual(userA_2.Id, userA.Id, "User that came back after email change had a different ID.");
                Assert.AreEqual(userA_2.DisplayName, "User A new name", "Displayname update failed.");
            }
        }
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
        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.º 5
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.º 6
0
        private LoginToken quickLogin(SGContext context, string username, string password)
        {
            var tokenTask = LoginTokenTasks.LoginAsync(context, username, password);

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

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

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

            return(token);
        }
Exemplo n.º 8
0
        public async Task <LoginToken> login(string email, string password)
        {
            var token = await LoginTokenTasks.LoginAsync(_context, email, password);

            if (token == null)
            {
                throw AutoApiError.AuthenticationFailure();
            }
            return(token);
        }
Exemplo n.º 9
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.º 10
0
        public void TestGetAnonymousToken()
        {
            IConfiguration config = getTestConfiguration();

            using (var context = new SGContext(config))
            {
                var loginToken = LoginTokenTasks.GetAnonmymousToken();
                Assert.IsTrue(loginToken.IsAnonymous(), "Anonymous token is not anonymous");
                Assert.AreEqual(loginToken.Id, 0, "The anonymous token ID must be Zero.");
                Assert.AreEqual(loginToken.UserId, -1, "Anonymous user id must be -1");
                Assert.AreEqual(loginToken.User.Id, -1, "Anonymous user id must be -1");
                var role = new UserRole(loginToken.User.RawRole);
                Assert.IsTrue(role.IsGuest, "The guest token must report as role guest");
                Assert.IsFalse(role.IsAdmin, "The guest token must not report as role admin");
                Assert.IsFalse(role.IsUser, "The guest token must not report as role user");
            }
        }
Exemplo n.º 11
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());
        }