public async void ReturnUser_WhenTokenIsValid()
 {
     MockSimpleIdentityDbContext dbContext = new MockSimpleIdentityDbContext(MockData.Seed());
     UserManager userManager = new UserManager(dbContext);
     User user = await userManager.FindUserByToken("token1");
     Assert.True(user != null);
     Assert.True(user.Id == 1);
 }
        public async void ReturnUser_WhenUserNameMatch()
        {
            var mockDbContext = new MockSimpleIdentityDbContext(MockData.Seed());
            UserManager userManager = new UserManager(mockDbContext);

            var rand = new Random(Guid.NewGuid().GetHashCode());
            User user = mockDbContext.Data.Users[rand.Next(0, mockDbContext.Data.Users.Count - 1)];
            User found = await userManager.FindUserByName(user.Name);

            Assert.True(found.Id == user.Id);
            Assert.True(found.Name == user.Name);
        }
        public async void ThrowException_WhenUserNotFound()
        {
            Exception ex = null;
            try
            {
                var mockDbContext = new MockSimpleIdentityDbContext();
                UserManager userManager = new UserManager(mockDbContext);
                await userManager.FindUserByName("Some Name");
            }
            catch (SimpleIdentityDataNotFoundException e) { ex = e; }

            Assert.True(ex != null);
        }
        public async void ThrowDataException_WhenUserNotProvided()
        {
            Exception ex = null;
            try
            {
                var mockDbContext = new MockSimpleIdentityDbContext();
                UserManager userManager = new UserManager(mockDbContext);
                await userManager.CreateUser(null);
            }
            catch (SimpleIdentityDataException e) { ex = e; }

            Assert.True(ex != null);
        }
        public async void ThrowDataException_WhenMandatoryElementIsMissing()
        {
            Exception ex = null;
            try
            {
                var mockDbContext = new MockSimpleIdentityDbContext();
                UserManager userManager = new UserManager(mockDbContext);

                await userManager.CreateUser(new User());
            }
            catch (SimpleIdentityDataException e) { ex = e; }

            Assert.True(ex != null);
        }
        public async void ReturnUser_WhenSucessful()
        {
            var mockDbContext = new MockSimpleIdentityDbContext();
            UserManager userManager = new UserManager(mockDbContext);
            AuthorizationManager authManager = new AuthorizationManager(mockDbContext);

            User user = new User();
            user.Name = "foo";
            user.Email = "*****@*****.**";
            user.IsActive = true;
            user.Id = 1;

            var createdUser = await userManager.CreateUser(user);

            Assert.True(createdUser.Name == user.Name);
            Assert.True(createdUser.Email == user.Email);
            Assert.True(createdUser.Authentication.Secret.Length > 0);
            Assert.True(await authManager.IsUserAuthorized(1, Authorization.AuthResourceType.User, 1, Authorization.AuthScopes.Full));

        }
        public async void ReturnUser_WhenSuccessful()
        {
            var mockDbContext = new MockSimpleIdentityDbContext(MockData.Seed());
            UserManager userManager = new UserManager(mockDbContext);

            var rand = new Random(Guid.NewGuid().GetHashCode());
            User user = mockDbContext.Data.Users[rand.Next(0, mockDbContext.Data.Users.Count - 1)];
            User modifiedUser = new User();
            modifiedUser.Id = user.Id;
            modifiedUser.IsActive = user.IsActive;
            modifiedUser.Email = "*****@*****.**";
            modifiedUser.Name = user.Name;

            user = await userManager.UpdateUser(modifiedUser);

            Assert.Equal(1, mockDbContext.SaveCount);
            Assert.True(user.Id == modifiedUser.Id);
            Assert.True(user.IsActive == modifiedUser.IsActive);
            Assert.True(user.Email == modifiedUser.Email);
            Assert.True(user.Name == modifiedUser.Name);
        }
        public async void ThrowException_WhenMandatoryElementIsMissing()
        {
            Exception ex = null;
            try
            {
                var mockDbContext = new MockSimpleIdentityDbContext(MockData.Seed());
                UserManager userManager = new UserManager(mockDbContext);

                var rand = new Random(Guid.NewGuid().GetHashCode());
                User user = mockDbContext.Data.Users[rand.Next(0, mockDbContext.Data.Users.Count - 1)];
                User modifiedUser = new User();
                modifiedUser.Id = user.Id;
                modifiedUser.IsActive = user.IsActive;
                modifiedUser.Name = user.Name;

                await userManager.UpdateUser(modifiedUser);
            }
            catch (SimpleIdentityDataException e) { ex = e; }

            Assert.True(ex != null);
        }
 public SimpleIdentityRepository(ISimpleIdentityDbContext dbContext)
 {
     UserManager = new UserManager(dbContext);
     AuthorizationManager = new AuthorizationManager(dbContext);
     TokenManager = new TokenManager(dbContext);
 }