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 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);
            }
        }