public void Add_New_User()
        {
            User newUser = new User()
            {
                NameIdentifier = "s0Me1De9Tf!Er$tRing",
                FirstName = "SomeFirstName",
                MiddleName = "SomeMiddleName",
                LastName = "SomeLastName",
                IdentityProvider = "Windows Live",
                Organization = "SomeOrganization",
                EmailId = "*****@*****.**",
                CreatedOn = DateTime.Now,
                ModifiedOn = DateTime.Now,
                IsActive = true,
            };

            IUserRepository repository = new UserRepository(testDBContext as IUnitOfWork);
            User addedUser = repository.AddUser(newUser);

            // Check if a non-null user is returned from the call
            Assert.IsNotNull(addedUser);

            // Check id the DbContext cached the user object in its local User collection
            Assert.IsTrue(testDBContext.Users.Local.Count == 1);

            // Check if the locally cached user object has the same data as the new User that was added above
            Assert.AreEqual(testDBContext.Users.Local[0].NameIdentifier, newUser.NameIdentifier);

            // Check if the state of the added user is set to Added
            Assert.AreEqual(testDBContext.GetEntityState(addedUser), EntityState.Added);

            testDBContext.Commit();
            User retrieved = testDBContext.Users.Find(1);
        }
 private void AddUser(User user)
 {
     IUserRepository repository = new UserRepository(testDBContext);
     User addedUser = repository.AddUser(user);
     testDBContext.Commit();
 }
        public void Add_User_Attribute()
        {
            // Prepare
            User newUser = new User()
            {
                NameIdentifier = "s0Me1De9Tf!Er$tRing",
                FirstName = "SomeFirstName",
                MiddleName = "SomeMiddleName",
                LastName = "SomeLastName",
                IdentityProvider = "Windows Live",
                Organization = "SomeOrganization",
                EmailId = "*****@*****.**",
                CreatedOn = DateTime.Now,
                ModifiedOn = DateTime.Now,
                IsActive = true
            };
            AddUser(newUser);
            User addedUser = testDBContext.Users.Find(1);
            UserAttribute userAttribute = new UserAttribute()
            {
                UserId = addedUser.UserId,
                Key = "Key1",
                Value = "Value1"
            };

            // Perform
            IUserRepository repository = new UserRepository(testDBContext as IUnitOfWork);
            UserAttribute addedUserAttribute = repository.AddUserAttribute(userAttribute); // Invalid user id passed

            // Check is a valid user attribute is returned
            Assert.IsNotNull(addedUserAttribute);

            // Check if the count of tracked objects in the data context is 1
            Assert.IsTrue(testDBContext.UserAttributes.Local.Count == 1);

            // Check if the tracked object is the same one as the added user attribute
            Assert.AreEqual(testDBContext.UserAttributes.Local[0].Key, userAttribute.Key);

            // Check the entity state
            Assert.AreEqual(testDBContext.GetEntityState(addedUserAttribute), EntityState.Added);
        }
        public void Return_Null_User_Value_For_Non_Existing_NameIdentifier()
        {
            // Perform get
            IUserRepository repository = new UserRepository(testDBContext as IUnitOfWork);
            User retrievedUser = repository.GetUserByNameIdentifier("some_non_existing_identifier");

            // Check if a valid user is returned
            Assert.IsNull(retrievedUser);
        }
        public void Update_Existing_User()
        {
            // Prepare :
            // 1. Create a new user
            // 2. Fetch the added user
            // 3. Update user data
            User newUser = new User()
            {
                NameIdentifier = "s0Me1De9Tf!Er$tRing",
                FirstName = "SomeFirstName",
                MiddleName = "SomeMiddleName",
                LastName = "SomeLastName",
                IdentityProvider = "Windows Live",
                Organization = "SomeOrganization",
                EmailId = "*****@*****.**",
                CreatedOn = DateTime.Now,
                ModifiedOn = DateTime.Now,
                IsActive = true
            };
            AddUser(newUser);
            User userToUpdate = testDBContext.Users.Find(1);
            userToUpdate.EmailId = "*****@*****.**";
            userToUpdate.Organization = "somethingelseorganization";

            // Perform update
            IUserRepository repository = new UserRepository(testDBContext as IUnitOfWork);
            User updatedUser = repository.UpdateUser(userToUpdate);

            // Assert if the updated user object is valid
            Assert.IsNotNull(updatedUser);

            // Asset that the updated user is added to the tracked object in the context
            Assert.IsTrue(testDBContext.Users.Local.Count == 1);

            // Assert email id before and after update are same
            Assert.AreEqual(testDBContext.Users.Local[0].EmailId, userToUpdate.EmailId);

            // Assert organization before and after is the same
            Assert.AreEqual(testDBContext.Users.Local[0].Organization, userToUpdate.Organization);

            // Assert that the locally tracked user object to update has the modified entity state
            Assert.AreEqual(testDBContext.GetEntityState(updatedUser), EntityState.Modified);
        }
        public void Return_Empty_UserRoles_For_InValid_UserId()
        {
            // Perform
            IUserRepository repository = new UserRepository(testDBContext as IUnitOfWork);
            IEnumerable<UserRole> userRoles = repository.GetUserRolesByUserId(-1); // Invalid user id passed

            // Check is the count is zero
            Assert.AreEqual(userRoles.Count(), 0);
        }
        public void Get_User_Attributes_For_Valid_User()
        {
            // Prepare
            UserAttribute userAttribute1 = new UserAttribute()
            {
                Key = "Key1",
                Value = "Value1"
            };
            UserAttribute userAttribute2 = new UserAttribute()
            {
                Key = "Key2",
                Value = "Value2"
            };
            User newUser = new User()
            {
                NameIdentifier = "s0Me1De9Tf!Er$tRing",
                FirstName = "SomeFirstName",
                MiddleName = "SomeMiddleName",
                LastName = "SomeLastName",
                IdentityProvider = "Windows Live",
                Organization = "SomeOrganization",
                EmailId = "*****@*****.**",
                CreatedOn = DateTime.Now,
                ModifiedOn = DateTime.Now,
                IsActive = true,
                UserAttributes = new List<UserAttribute>() { userAttribute1, userAttribute2 }
            };
            AddUser(newUser);
            User addedUser = testDBContext.Users.Find(1);

            // Perform
            IUserRepository repository = new UserRepository(testDBContext);
            IEnumerable<UserAttribute> attributes = repository.GetUserAttributesByUserId(addedUser.UserId);

            // Check if a valid collection of user attributes is returned
            Assert.IsNotNull(attributes);

            // Check if the count of attributes is 2, since we added 2 attributes in the preparation code
            Assert.AreEqual(attributes.Count(), 2);

            // Check if attribute 1 is returned
            Assert.AreEqual(attributes.Count<UserAttribute>(ua => ua.Key.Equals(userAttribute1.Key)), 1);

            // Check is attribute 2 is returned
            Assert.AreEqual(attributes.Count<UserAttribute>(ua => ua.Key.Equals(userAttribute2.Key)), 1);
        }
        public void Get_UserRoles_For_Valid_UserId()
        {
            // Prepare
            AddDefaultRoles();
            User newUser = new User()
            {
                NameIdentifier = "s0Me1De9Tf!Er$tRing",
                FirstName = "SomeFirstName",
                MiddleName = "SomeMiddleName",
                LastName = "SomeLastName",
                IdentityProvider = "Windows Live",
                Organization = "SomeOrganization",
                EmailId = "*****@*****.**",
                CreatedOn = DateTime.Now,
                ModifiedOn = DateTime.Now,
                IsActive = true
            };
            AddUserWithDefaultRoles(newUser);
            User addedUser = testDBContext.Users.Find(1);

            // Perform
            IUserRepository repository = new UserRepository(testDBContext as IUnitOfWork);
            IEnumerable<UserRole> userRoles = repository.GetUserRolesByUserId(addedUser.UserId);

            // Check if valid user roles collection is returned
            Assert.IsNotNull(userRoles);

            // Check if the count of items in user role collection is 2, since the preparation code
            // added 2 roles to user
            Assert.AreEqual(userRoles.Count(), 2);

            // Check if the administrator role exists
            Assert.AreEqual(userRoles.Count<UserRole>(r => r.RoleId.Equals(GetAdministratorRoleId())), 1);

            // Check if the user role exists
            Assert.AreEqual(userRoles.Count<UserRole>(r => r.RoleId.Equals(GetUserRoleId())), 1);
        }
        public void Get_Existing_User_By_NameIdentifier()
        {
            AddDefaultRoles();
            User newUser = new User()
            {
                NameIdentifier = "s0Me1De9Tf!Er$tRing",
                FirstName = "SomeFirstName",
                MiddleName = "SomeMiddleName",
                LastName = "SomeLastName",
                IdentityProvider = "Windows Live",
                Organization = "SomeOrganization",
                EmailId = "*****@*****.**",
                CreatedOn = DateTime.Now,
                ModifiedOn = DateTime.Now,
                IsActive = true
            };
            AddUser(newUser);
            UserRole userRole = new UserRole()
            {
                UserId = 1,
                RoleId = 1
            };
            testDBContext.UserRoles.Add(userRole);
            testDBContext.Commit();

            // Perform get
            IUserRepository repository = new UserRepository(testDBContext as IUnitOfWork);
            User retrievedUser = repository.GetUserByNameIdentifier(newUser.NameIdentifier);

            // Check if a valid user is returned
            Assert.IsNotNull(retrievedUser);

            // Check if the name identifier matches
            Assert.AreEqual(newUser.NameIdentifier, retrievedUser.NameIdentifier);
        }
        public void Add_User_Role()
        {
            // Prepare
            AddDefaultRoles();

            User newUser = new User()
            {
                NameIdentifier = "s0Me1De9Tf!Er$tRing",
                FirstName = "SomeFirstName",
                MiddleName = "SomeMiddleName",
                LastName = "SomeLastName",
                IdentityProvider = "Windows Live",
                Organization = "SomeOrganization",
                EmailId = "*****@*****.**",
                CreatedOn = DateTime.Now,
                ModifiedOn = DateTime.Now,
                IsActive = true
            };
            AddUser(newUser);
            User addedUser = testDBContext.Users.Find(1);
            UserRole userRole = new UserRole()
            {
                UserId = addedUser.UserId,
                RoleId = GetUserRoleId()
            };

            // Perform
            IUserRepository repository = new UserRepository(testDBContext as IUnitOfWork);
            UserRole addedUserRole = repository.AddUserRole(userRole);

            // Check if a valid user role is returned
            Assert.IsNotNull(addedUserRole);

            // Check if the count of tracked objects is 1
            Assert.IsTrue(testDBContext.UserRoles.Local.Count == 1);

            // Check is the entity state of the added object is set to Added
            Assert.AreEqual(testDBContext.GetEntityState(addedUserRole), EntityState.Added);
        }