public void RemoveUncommitedRoleTest()
        {
            ISecurityRepository client = GetClient();

            var account = new Account() { UserName = "******" };
            var role = new Role() { Name = "testRole" };

            var roleAssignment = new RoleAssignment
            {
                Account = account,
                AccountId = account.AccountId,
                Role = role,
                RoleId = role.RoleId
            };

            client.Attach(account);

            // add role
            account.RoleAssignments.Add(roleAssignment);
            Assert.True(client.IsAttachedTo(roleAssignment));
 
            // remove uncommited role
            account.RoleAssignments.Remove(roleAssignment);
            client.Remove(roleAssignment);
            Assert.False(client.IsAttachedTo(roleAssignment));
        }
        public void CreateAccountWithNewRoleTest()
        {
            const string roleName = "testRole";
            const string userName = "******";
            ISecurityRepository client = GetClient();

            var role = new Role() { Name = roleName };

            var account = new Account() { UserName = userName };

            var roleAssignment = new RoleAssignment
            {
                Account = account,
                AccountId = account.AccountId,
                Role = role,
                RoleId = role.RoleId
            };


            // add role to account
            account.RoleAssignments.Add(roleAssignment);

            client.Add(role);
            client.Add(account);
            client.UnitOfWork.Commit();
            string accountId = account.AccountId;
            string roleId = role.RoleId;
            EndActionClearAccount(userName);
            EndActionClearRole(roleName);

            client = GetClient();
            int count = client.Accounts.Where(x => x.AccountId == accountId).Count();
            Assert.Equal(count, 1);
            count = client.Roles.Where(x => x.RoleId == roleId).Count();
            Assert.Equal(count, 1);
            count = client.RoleAssignments.Where(x => x.RoleId == roleId && x.AccountId == accountId).Count();
            Assert.Equal(count, 1);

        }
예제 #3
0
		public void Can_remove_add_the_same_role()
		{
			var accountName = "Account remove_add_the_same_role";
			var roleName = "Role remove_add_the_same_role";
			
			// create one account with one role
			var accountId = AddAccount(accountName);
			var roleId = AddRole(roleName);
			AddRoleAssignment(roleId, accountId);


			var client = GetRepository();
			
			var aviableRole = client.Roles.Where(x => x.Name == roleName).SingleOrDefault();
			Assert.NotNull(aviableRole);

			var account = client.Accounts.Expand("RoleAssignments/Role").Where(x => x.AccountId == accountId).SingleOrDefault();
			Assert.NotNull(account);

			var checkRole = account.RoleAssignments.SingleOrDefault(x => x.RoleId == roleId).Role;
			Assert.NotNull(checkRole);

			// remove existing role from account
            var item = account.RoleAssignments.First(x => x.RoleId == roleId);
            account.RoleAssignments.Remove(item);

			// add the same role again
			item = new RoleAssignment {AccountId = accountId, Role = aviableRole, RoleId = aviableRole.RoleId};
			account.RoleAssignments.Add(item);


			client.UnitOfWork.Commit();

			client = GetRepository();
			account = client.Accounts.Expand("RoleAssignments/Role").Where(x => x.UserName == accountName).SingleOrDefault();
			Assert.NotNull(account.RoleAssignments.Where(x => x.Role.Name == roleName).SingleOrDefault());
		}
예제 #4
0
		private void AddRoleAssignment(string roleId, int accountId)
		{
			var client = GetRepository();
			var account = client.Accounts.Expand("RoleAssignments/Role").Where(x => x.AccountId == accountId).SingleOrDefault();
			var rolePermission = new RoleAssignment { RoleId = roleId, AccountId = accountId };
			account.RoleAssignments.Add(rolePermission);
			client.UnitOfWork.Commit();
		}
예제 #5
0
        public void Can_add_remove_add()
        {
            var accountName = "Account add_remove_add";
            var roleName = "Role create_role_graph";
            var accountId = AddAccount(accountName);
            var roleId = AddRole(roleName);


            var client = GetRepository();
            var role = client.Roles.Where(x => x.Name == roleName).SingleOrDefault();
            Assert.NotNull(role);

            var account = client.Accounts.Expand("RoleAssignments/Role").Where(x => x.AccountId == accountId).SingleOrDefault();
            Assert.NotNull(account);

            //var newRole = client.Roles.Where(x => x.Name == roleName).SingleOrDefault(); // need to reload role in new repository

            //var roleAssignment = new RoleAssignment { RoleId = roleId, AccountId = accountId};// worked
            var roleAssignment = new RoleAssignment { RoleId = role.RoleId, AccountId = accountId, Role = role };

            account.RoleAssignments.Add(roleAssignment);
            account.RoleAssignments.Remove(roleAssignment);
            //Deattach here
            account.RoleAssignments.Add(roleAssignment);
            
            client.UnitOfWork.Commit();

            client = GetRepository();
            account = client.Accounts.Expand("RoleAssignments/Role").Where(x => x.UserName == accountName).SingleOrDefault();
            Assert.NotNull(account.RoleAssignments.Where(x => x.Role.Name == roleName).SingleOrDefault());
        }