public void TestCanUserManageGivenLogin3() { #region Arrange UserService = new UserService(UnitService, UnitAssociationRepository, PermissionRepository); ControllerRecordFakes.FakeUnits(6, UnitRepository); ControllerRecordFakes.FakeRoles(6, RoleRepository); ControllerRecordFakes.FakeApplications(3, ApplicationRepository); ControllerRecordFakes.FakeUsers(6, UserRepository); //These are the units that the current user has if he has the correct access UnitService.Expect(a => a.GetVisibleByUser("Name1", "UserName")).Return(UnitRepository.Queryable.Where(a => a.Id == 2 || a.Id == 4)).Repeat.Any(); var unitAssociations = new List <UnitAssociation>(); for (int i = 0; i < 5; i++) { unitAssociations.Add(CreateValidEntities.UnitAssociation(i + 1)); unitAssociations[i].User = UserRepository.GetNullableById(3); //Different LoginId unitAssociations[i].Application = ApplicationRepository.Queryable.First(); unitAssociations[i].Unit = UnitRepository.GetNullableById(i + 1); } ControllerRecordFakes.FakeUnitAssociations(0, UnitAssociationRepository, unitAssociations); #endregion Arrange #region Act var result = UserService.CanUserManageGivenLogin("Name1", "UserName", "LoginId2"); #endregion Act #region Assert Assert.IsFalse(result); #endregion Assert }
public void TestAddUnitThrowsExceptionIfCurrentUserDoesNotHaveRights2() { try { #region Arrange Controller.ControllerContext.HttpContext = new MockHttpContext(1, new[] { "" }); ControllerRecordFakes.FakeApplications(3, ApplicationRepository); ControllerRecordFakes.FakeUnits(5, UnitRepository); ControllerRecordFakes.FakeUsers(3, UserRepository); ControllerRecordFakes.FakeUnitAssociations(1, UnitAssociationRepository); UnitService.Expect(a => a.GetVisibleByUser("Name2", "UserName")).Return(UnitRepository.Queryable.Where(a => a.Id >= 4)).Repeat.Any(); UserService.Expect(a => a.CanUserManageGivenLogin("Name2", "UserName", "LoginId1")).Return(true).Repeat.Any(); UnitAssociationRepository.Expect(a => a.EnsurePersistent(Arg <UnitAssociation> .Is.Anything)).Repeat.Any(); #endregion Arrange #region Act Controller.AddUnit("Name2", "LoginId1", 3); #endregion Act } catch (Exception ex) { #region Assert Assert.IsNotNull(ex); Assert.AreEqual("UserName does not have access to manage the given unit", ex.Message); UserService.AssertWasCalled(a => a.CanUserManageGivenLogin("Name2", "UserName", "LoginId1")); UnitAssociationRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg <UnitAssociation> .Is.Anything)); #endregion Assert throw; } }
public void TestAddUnitWhereUnitAssociationAlreadyExists() { #region Arrange Controller.ControllerContext.HttpContext = new MockHttpContext(1, new[] { "" }); ControllerRecordFakes.FakeApplications(3, ApplicationRepository); ControllerRecordFakes.FakeUnits(3, UnitRepository); ControllerRecordFakes.FakeUsers(3, UserRepository); var unitAssociations = new List <UnitAssociation>(); unitAssociations.Add(CreateValidEntities.UnitAssociation(1)); unitAssociations[0].Application = ApplicationRepository.GetNullableById(2); unitAssociations[0].User = UserRepository.GetNullableById(1); unitAssociations[0].Unit = UnitRepository.GetNullableById(3); ControllerRecordFakes.FakeUnitAssociations(1, UnitAssociationRepository, unitAssociations); UnitService.Expect(a => a.GetVisibleByUser("Name2", "UserName")).Return(UnitRepository.Queryable).Repeat.Any(); UserService.Expect(a => a.CanUserManageGivenLogin("Name2", "UserName", "LoginId1")).Return(true).Repeat.Any(); UnitAssociationRepository.Expect(a => a.EnsurePersistent(Arg <UnitAssociation> .Is.Anything)).Repeat.Any(); #endregion Arrange #region Act Controller.AddUnit("Name2", "LoginId1", 3); #endregion Act #region Assert UserService.AssertWasCalled(a => a.CanUserManageGivenLogin("Name2", "UserName", "LoginId1")); UnitAssociationRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg <UnitAssociation> .Is.Anything)); UnitService.AssertWasCalled(a => a.GetVisibleByUser("Name2", "UserName")); #endregion Assert }
public void TestAddUnitThrowsExceptionIfloginIdDuplicate() { try { #region Arrange Controller.ControllerContext.HttpContext = new MockHttpContext(1, new[] { "" }); ControllerRecordFakes.FakeApplications(3, ApplicationRepository); ControllerRecordFakes.FakeUnits(3, UnitRepository); var users = new List <User>(); users.Add(CreateValidEntities.User(1)); users.Add(CreateValidEntities.User(1)); ControllerRecordFakes.FakeUsers(3, UserRepository, users); ControllerRecordFakes.FakeUnitAssociations(1, UnitAssociationRepository); UnitService.Expect(a => a.GetVisibleByUser("Name2", "UserName")).Return(UnitRepository.Queryable).Repeat.Any(); UserService.Expect(a => a.CanUserManageGivenLogin("Name2", "UserName", "LoginId1")).Return(true).Repeat.Any(); UnitAssociationRepository.Expect(a => a.EnsurePersistent(Arg <UnitAssociation> .Is.Anything)).Repeat.Any(); #endregion Arrange #region Act Controller.AddUnit("Name2", "LoginId1", 3); #endregion Act } catch (Exception ex) { #region Assert Assert.IsNotNull(ex); Assert.AreEqual("Sequence contains more than one element", ex.Message); UserService.AssertWasCalled(a => a.CanUserManageGivenLogin("Name2", "UserName", "LoginId1")); UnitService.AssertWasCalled(a => a.GetVisibleByUser("Name2", "UserName")); UnitAssociationRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg <UnitAssociation> .Is.Anything)); #endregion Assert throw; } }
public void TestAddUnit() { #region Arrange Controller.ControllerContext.HttpContext = new MockHttpContext(1, new[] { "" }); ControllerRecordFakes.FakeApplications(3, ApplicationRepository); ControllerRecordFakes.FakeUnits(3, UnitRepository); ControllerRecordFakes.FakeUsers(3, UserRepository); ControllerRecordFakes.FakeUnitAssociations(1, UnitAssociationRepository); UserService.Expect(a => a.CanUserManageGivenLogin("Name2", "UserName", "LoginId1")).Return(true).Repeat.Any(); UnitService.Expect(a => a.GetVisibleByUser("Name2", "UserName")).Return(UnitRepository.Queryable).Repeat.Any(); UnitAssociationRepository.Expect(a => a.EnsurePersistent(Arg <UnitAssociation> .Is.Anything)).Repeat.Any(); #endregion Arrange #region Act Controller.AddUnit("Name2", "LoginId1", 3); #endregion Act #region Assert UserService.AssertWasCalled(a => a.CanUserManageGivenLogin("Name2", "UserName", "LoginId1")); UnitService.AssertWasCalled(a => a.GetVisibleByUser("Name2", "UserName")); UnitAssociationRepository.AssertWasCalled(a => a.EnsurePersistent(Arg <UnitAssociation> .Is.Anything)); var args = (UnitAssociation)UnitAssociationRepository.GetArgumentsForCallsMadeOn(a => a.EnsurePersistent(Arg <UnitAssociation> .Is.Anything))[0][0]; Assert.IsNotNull(args); Assert.AreEqual("Name2", args.Application.ToString()); Assert.AreEqual("LoginId1", args.User.LoginId); Assert.AreEqual("ShortName3", args.Unit.ShortName); Assert.IsFalse(args.Inactive); #endregion Assert }
public void TestGetByApplication1() { #region Arrange UserService = new UserService(UnitService, UnitAssociationRepository, PermissionRepository); ControllerRecordFakes.FakeUnits(6, UnitRepository); ControllerRecordFakes.FakeRoles(6, RoleRepository); UnitService.Expect(a => a.GetVisibleByUser("Name1", "UserName")).Return(UnitRepository.Queryable.Where(a => a.Id == 2 || a.Id == 4)).Repeat.Any(); ControllerRecordFakes.FakeApplications(3, ApplicationRepository); var users = new List <User>(); for (int i = 0; i < 5; i++) { users.Add(CreateValidEntities.User(i + 1)); users[i].UnitAssociations.Add(CreateValidEntities.UnitAssociation(i + 1)); users[i].UnitAssociations[0].Unit = UnitRepository.GetNullableById(i + 1); } ControllerRecordFakes.FakeUsers(0, UserRepository, users); var permissions = new List <Permission>(); for (int i = 0; i < 5; i++) { permissions.Add(CreateValidEntities.Permission(i + 1)); permissions[i].User = UserRepository.GetNullableById(i + 1); permissions[i].Application = ApplicationRepository.Queryable.First(); permissions[i].Role = RoleRepository.GetNullableById(i + 1); } ControllerRecordFakes.FakePermissions(0, PermissionRepository, permissions); var unitAssociations = new List <UnitAssociation>(); for (int i = 0; i < 5; i++) { unitAssociations.Add(CreateValidEntities.UnitAssociation(i + 1)); unitAssociations[i].User = UserRepository.GetNullableById(i + 1); unitAssociations[i].Application = ApplicationRepository.Queryable.First(); unitAssociations[i].Unit = UnitRepository.GetNullableById(i + 1); } ControllerRecordFakes.FakeUnitAssociations(0, UnitAssociationRepository, unitAssociations); #endregion Arrange #region Act var result = UserService.GetByApplication("Name1", "UserName"); //var allowedUnitIds = UnitService.GetVisibleByUser("Name1", "UserName").ToList().Select(x => x.Id).ToList(); ////Get everyone with perms, possibly filtered by role and unit //var usersWithPermissions = from p in PermissionRepository.Queryable // join u in UnitAssociationRepository.Queryable on // new { User = p.User.Id, App = p.Application.Id } // equals new { User = u.User.Id, App = u.Application.Id } // where p.Application.Name == "Name1" // where p.User.UnitAssociations.Any(a => allowedUnitIds.Contains(a.Unit.Id)) // select new { Permissions = p, UnitAssociations = u }; #endregion Act #region Assert Assert.IsNotNull(result); Assert.AreEqual(2, result.Count()); Assert.AreEqual("LoginId2", result.ElementAt(0).LoginId); Assert.AreEqual("LoginId4", result.ElementAt(1).LoginId); #endregion Assert }
public void TestManageReturnsView4() { #region Arrange Controller.ControllerContext.HttpContext = new MockHttpContext(1, new[] { "" }); ControllerRecordFakes.FakeUnits(3, UnitRepository); UnitService.Expect(a => a.GetVisibleByUser("appName", "UserName")).Return(UnitRepository.GetAll().AsQueryable()).Repeat.Any(); ControllerRecordFakes.FakeRoles(3, RoleRepository); RoleService.Expect(a => a.GetVisibleByUser("appName", "UserName")).Return(RoleRepository.GetAll().AsQueryable()).Repeat.Any(); ControllerRecordFakes.FakeUsers(3, UserRepository); UserService.Expect(a => a.GetByApplication("appName", "UserName")).Return(UserRepository.Queryable).Repeat.Any(); var application = CreateValidEntities.Application(1); application.Name = "appName"; var permissions = new List <Permission>(); for (int i = 0; i < 4; i++) { permissions.Add(CreateValidEntities.Permission(i + 1)); permissions[i].Application = application; permissions[i].User = UserRepository.Queryable.First(); permissions[i].Role = RoleRepository.GetNullableById(i + 1); } permissions[1].User = UserRepository.GetNullableById(2); permissions[2].User = UserRepository.GetNullableById(3); permissions[3].Role = RoleRepository.GetNullableById(1); permissions[3].Application = CreateValidEntities.Application(9); // This will cause these permissions to be filtered out ControllerRecordFakes.FakePermissions(0, PermissionRepository, permissions); var unitAssociations = new List <UnitAssociation>(); for (int i = 0; i < 4; i++) { unitAssociations.Add(CreateValidEntities.UnitAssociation(i + 1)); unitAssociations[i].Application = application; unitAssociations[i].User = UserRepository.Queryable.First(); unitAssociations[i].Unit = UnitRepository.GetNullableById(i + 1); } unitAssociations[1].User = UserRepository.GetNullableById(2); unitAssociations[2].User = UserRepository.GetNullableById(3); unitAssociations[2].Application = CreateValidEntities.Application(9); //This one is filtered out unitAssociations[3].Unit = UnitRepository.GetNullableById(1); ControllerRecordFakes.FakeUnitAssociations(0, UnitAssociationRepository, unitAssociations); #endregion Arrange #region Act Controller.Manage("appName") .AssertViewRendered() .WithViewData <UserManagementViewModel>(); #endregion Act #region Assert UnitService.AssertWasCalled(a => a.GetVisibleByUser("appName", "UserName")); RoleService.AssertWasCalled(a => a.GetVisibleByUser("appName", "UserName")); UserService.AssertWasCalled(a => a.GetByApplication("appName", "UserName")); #endregion Assert }
public void TestRemoveUnitThrowsExceptionIfRecordNotFound4() { try { #region Arrange Controller.ControllerContext.HttpContext = new MockHttpContext(1, new[] { "" }); ControllerRecordFakes.FakeApplications(3, ApplicationRepository); ControllerRecordFakes.FakeUnits(3, UnitRepository); ControllerRecordFakes.FakeUsers(3, UserRepository); var unitAssociations = new List <UnitAssociation>(); for (int i = 0; i < 3; i++) { unitAssociations.Add(CreateValidEntities.UnitAssociation(i + 1)); unitAssociations[i].Application = ApplicationRepository.GetNullableById(2); unitAssociations[i].User = UserRepository.GetNullableById(1); unitAssociations[i].Unit = UnitRepository.GetNullableById(i + 1); } unitAssociations.Add(CreateValidEntities.UnitAssociation(4)); unitAssociations[3].Application = ApplicationRepository.GetNullableById(2); unitAssociations[3].User = UserRepository.GetNullableById(1); unitAssociations[3].Unit = UnitRepository.GetNullableById(2); //Duplicate ControllerRecordFakes.FakeUnitAssociations(2, UnitAssociationRepository, unitAssociations); UserService.Expect(a => a.CanUserManageGivenLogin("Name2", "UserName", "LoginId1")).Return(true).Repeat.Any(); UnitService.Expect(a => a.GetVisibleByUser("Name2", "UserName")).Return(UnitRepository.Queryable).Repeat.Any(); #endregion Arrange #region Act Controller.RemoveUnit("Name2", "LoginId1", 2); #endregion Act } catch (Exception ex) { #region Assert Assert.IsNotNull(ex); Assert.AreEqual("Sequence contains more than one element", ex.Message); UserService.AssertWasCalled(a => a.CanUserManageGivenLogin("Name2", "UserName", "LoginId1")); UnitService.AssertWasCalled(a => a.GetVisibleByUser("Name2", "UserName")); UnitAssociationRepository.AssertWasNotCalled(a => a.Remove(Arg <UnitAssociation> .Is.Anything)); #endregion Assert throw; } }
public void TestCanUserManageGivenLogin1() { #region Arrange UserService = new UserService(UnitService, UnitAssociationRepository, PermissionRepository); ControllerRecordFakes.FakeUnits(6, UnitRepository); ControllerRecordFakes.FakeRoles(6, RoleRepository); ControllerRecordFakes.FakeApplications(3, ApplicationRepository); ControllerRecordFakes.FakeUsers(6, UserRepository); //These are the units that the current user has if he has the correct access UnitService.Expect(a => a.GetVisibleByUser("Name1", "UserName")).Return(UnitRepository.Queryable.Where(a => a.Id == 2 || a.Id == 4)).Repeat.Any(); var unitAssociations = new List <UnitAssociation>(); for (int i = 0; i < 5; i++) { unitAssociations.Add(CreateValidEntities.UnitAssociation(i + 1)); unitAssociations[i].User = UserRepository.GetNullableById(i + 1); unitAssociations[i].Application = ApplicationRepository.Queryable.First(); unitAssociations[i].Unit = UnitRepository.GetNullableById(i + 1); } ControllerRecordFakes.FakeUnitAssociations(0, UnitAssociationRepository, unitAssociations); #endregion Arrange #region Act var result = UserService.CanUserManageGivenLogin("Name1", "UserName", "LoginId2"); //var unitsForLoginToManage = from u in UnitAssociationRepository.Queryable // where u.Application.Name == "Name1" // && u.User.LoginId == "LoginId2" // select u.Unit; //var numIntersectingUnits1 = unitsForLoginToManage.ToFuture(); ////The linq provider can't handle Intersect() so we need to turn them into Enumerable first. ////Using to future does this and makes the queries more efficient by batching them //var numIntersectingUnits = unitsForLoginToManage.ToFuture().Intersect(UnitRepository.Queryable.Where(a => a.Id == 2 || a.Id == 4).ToFuture()).Count(); #endregion Act #region Assert Assert.IsTrue(result); #endregion Assert }
public void TestRemoveUnit() { #region Arrange Controller.ControllerContext.HttpContext = new MockHttpContext(1, new[] { "" }); ControllerRecordFakes.FakeApplications(3, ApplicationRepository); ControllerRecordFakes.FakeUnits(3, UnitRepository); ControllerRecordFakes.FakeUsers(3, UserRepository); var unitAssociations = new List <UnitAssociation>(); for (int i = 0; i < 3; i++) { unitAssociations.Add(CreateValidEntities.UnitAssociation(i + 1)); unitAssociations[i].Application = ApplicationRepository.GetNullableById(2); unitAssociations[i].User = UserRepository.GetNullableById(1); unitAssociations[i].Unit = UnitRepository.GetNullableById(i + 1); } ControllerRecordFakes.FakeUnitAssociations(2, UnitAssociationRepository, unitAssociations); UserService.Expect(a => a.CanUserManageGivenLogin("Name2", "UserName", "LoginId1")).Return(true).Repeat.Any(); UnitService.Expect(a => a.GetVisibleByUser("Name2", "UserName")).Return(UnitRepository.Queryable).Repeat.Any(); #endregion Arrange #region Act Controller.RemoveUnit("Name2", "LoginId1", 2); #endregion Act #region Assert UserService.AssertWasCalled(a => a.CanUserManageGivenLogin("Name2", "UserName", "LoginId1")); UnitService.AssertWasCalled(a => a.GetVisibleByUser("Name2", "UserName")); UnitAssociationRepository.AssertWasCalled(a => a.Remove(Arg <UnitAssociation> .Is.Anything)); var args = (UnitAssociation)UnitAssociationRepository.GetArgumentsForCallsMadeOn(a => a.Remove(Arg <UnitAssociation> .Is.Anything))[0][0]; Assert.AreEqual(2, args.Id); Assert.AreEqual("ShortName2", args.Unit.ShortName); Assert.AreEqual("LoginId1", args.User.LoginId); Assert.AreEqual("Name2", args.Application.ToString()); #endregion Assert }
public void TestManageReturnsView2() { #region Arrange Controller.ControllerContext.HttpContext = new MockHttpContext(1, new[] { "" }); ControllerRecordFakes.FakeUnits(3, UnitRepository); UnitService.Expect(a => a.GetVisibleByUser("appName", "UserName")).Return(UnitRepository.GetAll().AsQueryable()).Repeat.Any(); ControllerRecordFakes.FakeRoles(3, RoleRepository); RoleService.Expect(a => a.GetVisibleByUser("appName", "UserName")).Return(RoleRepository.GetAll().AsQueryable()).Repeat.Any(); ControllerRecordFakes.FakeUsers(3, UserRepository); UserService.Expect(a => a.GetByApplication("appName", "UserName")).Return(UserRepository.Queryable).Repeat.Any(); var application = CreateValidEntities.Application(1); application.Name = "appName"; var permissions = new List <Permission>(); for (int i = 0; i < 4; i++) { permissions.Add(CreateValidEntities.Permission(i + 1)); permissions[i].Application = application; permissions[i].User = UserRepository.Queryable.First(); permissions[i].Role = RoleRepository.GetNullableById(i + 1); } permissions[1].User = UserRepository.GetNullableById(2); permissions[2].User = UserRepository.GetNullableById(3); permissions[3].Role = RoleRepository.GetNullableById(1); ControllerRecordFakes.FakePermissions(0, PermissionRepository, permissions); var unitAssociations = new List <UnitAssociation>(); for (int i = 0; i < 4; i++) { unitAssociations.Add(CreateValidEntities.UnitAssociation(i + 1)); unitAssociations[i].Application = application; unitAssociations[i].User = UserRepository.Queryable.First(); unitAssociations[i].Unit = UnitRepository.GetNullableById(i + 1); } unitAssociations[1].User = UserRepository.GetNullableById(2); unitAssociations[2].User = UserRepository.GetNullableById(3); unitAssociations[3].Unit = UnitRepository.GetNullableById(1); ControllerRecordFakes.FakeUnitAssociations(0, UnitAssociationRepository, unitAssociations); #endregion Arrange #region Act var result = Controller.Manage("appName") .AssertViewRendered() .WithViewData <UserManagementViewModel>(); #endregion Act #region Assert Assert.IsNotNull(result); Assert.AreEqual("appName", result.Application); Assert.AreEqual(3, result.Roles.Count); Assert.AreEqual("Name2", result.Roles[1].Value); Assert.AreEqual(3, result.Units.Count); Assert.AreEqual("ShortName2", result.Units[1].Value); Assert.AreEqual(3, result.UserShowModel.Count); Assert.AreEqual("FirstName2", result.UserShowModel[1].FirstName); Assert.AreEqual("LastName2", result.UserShowModel[1].LastName); Assert.AreEqual("*****@*****.**", result.UserShowModel[1].Email); Assert.AreEqual("LoginId2", result.UserShowModel[1].Login); Assert.AreEqual(null, result.UserShowModel[1].Phone); Assert.AreEqual(2, result.UserShowModel[0].Permissions.Count); Assert.AreEqual("Name1", result.UserShowModel[0].Permissions[1].RoleName); Assert.AreEqual("Name1", result.UserShowModel[0].Permissions[0].RoleName); Assert.AreEqual(2, result.UserShowModel[0].UnitAssociations.Count); Assert.AreEqual("ShortName1", result.UserShowModel[0].UnitAssociations[1].UnitName); Assert.AreEqual("ShortName1", result.UserShowModel[0].UnitAssociations[0].UnitName); Assert.AreEqual(1, result.UserShowModel[1].Permissions.Count); Assert.AreEqual("Name2", result.UserShowModel[1].Permissions[0].RoleName); Assert.AreEqual(1, result.UserShowModel[1].UnitAssociations.Count); Assert.AreEqual("ShortName2", result.UserShowModel[1].UnitAssociations[0].UnitName); Assert.AreEqual(1, result.UserShowModel[2].Permissions.Count); Assert.AreEqual("Name3", result.UserShowModel[2].Permissions[0].RoleName); Assert.AreEqual(1, result.UserShowModel[2].UnitAssociations.Count); Assert.AreEqual("ShortName3", result.UserShowModel[2].UnitAssociations[0].UnitName); #endregion Assert }
public void TestGetByApplication2() { #region Arrange UserService = new UserService(UnitService, UnitAssociationRepository, PermissionRepository); ControllerRecordFakes.FakeUnits(6, UnitRepository); ControllerRecordFakes.FakeRoles(6, RoleRepository); ControllerRecordFakes.FakeApplications(3, ApplicationRepository); //These are the units that the current user has if he has the correct access UnitService.Expect(a => a.GetVisibleByUser("Name1", "UserName")).Return(UnitRepository.Queryable.Where(a => a.Id == 2 || a.Id == 4)).Repeat.Any(); var users = new List <User>(); //User 1 has Unit 1, unit 3 -- no match users.Add(CreateValidEntities.User(1)); users[0].UnitAssociations.Add(CreateValidEntities.UnitAssociation(1)); users[0].UnitAssociations.Add(CreateValidEntities.UnitAssociation(2)); users[0].UnitAssociations[0].Unit = UnitRepository.GetNullableById(1); users[0].UnitAssociations[1].Unit = UnitRepository.GetNullableById(3); //User2 has unit1, 2, 3, and 4 //Match users.Add(CreateValidEntities.User(2)); users[1].UnitAssociations.Add(CreateValidEntities.UnitAssociation(3)); users[1].UnitAssociations.Add(CreateValidEntities.UnitAssociation(4)); users[1].UnitAssociations.Add(CreateValidEntities.UnitAssociation(5)); users[1].UnitAssociations.Add(CreateValidEntities.UnitAssociation(6)); users[1].UnitAssociations[0].Unit = UnitRepository.GetNullableById(1); users[1].UnitAssociations[1].Unit = UnitRepository.GetNullableById(2); users[1].UnitAssociations[2].Unit = UnitRepository.GetNullableById(3); users[1].UnitAssociations[3].Unit = UnitRepository.GetNullableById(4); //User3 has unit 4 //Match users.Add(CreateValidEntities.User(3)); users[2].UnitAssociations.Add(CreateValidEntities.UnitAssociation(7)); users[2].UnitAssociations[0].Unit = UnitRepository.GetNullableById(4); //user4 has unit2 //match users.Add(CreateValidEntities.User(4)); users[3].UnitAssociations.Add(CreateValidEntities.UnitAssociation(8)); users[3].UnitAssociations[0].Unit = UnitRepository.GetNullableById(2); //user5 has unit 5 and 1, no match users.Add(CreateValidEntities.User(5)); users[4].UnitAssociations.Add(CreateValidEntities.UnitAssociation(9)); users[4].UnitAssociations.Add(CreateValidEntities.UnitAssociation(10)); users[4].UnitAssociations[0].Unit = UnitRepository.GetNullableById(5); users[4].UnitAssociations[1].Unit = UnitRepository.GetNullableById(1); ControllerRecordFakes.FakeUsers(0, UserRepository, users); var permissions = new List <Permission>(); var count = 0; for (int i = 0; i < 5; i++) { for (int x = 0; x < 5; x++) { count++; permissions.Add(CreateValidEntities.Permission(count)); permissions[count - 1].User = UserRepository.GetNullableById(x + 1); permissions[count - 1].Application = ApplicationRepository.Queryable.First(); permissions[count - 1].Role = RoleRepository.GetNullableById(x + 1); } } ControllerRecordFakes.FakePermissions(0, PermissionRepository, permissions); var unitAssociations = new List <UnitAssociation>(); for (int i = 0; i < 10; i++) { unitAssociations.Add(CreateValidEntities.UnitAssociation(i + 1)); } unitAssociations[0].User = UserRepository.GetNullableById(1); unitAssociations[0].Application = ApplicationRepository.Queryable.First(); unitAssociations[0].Unit = UnitRepository.GetNullableById(1); unitAssociations[1].User = UserRepository.GetNullableById(1); unitAssociations[1].Application = ApplicationRepository.Queryable.First(); unitAssociations[1].Unit = UnitRepository.GetNullableById(3); unitAssociations[2].User = UserRepository.GetNullableById(2); unitAssociations[2].Application = ApplicationRepository.Queryable.First(); unitAssociations[2].Unit = UnitRepository.GetNullableById(1); unitAssociations[3].User = UserRepository.GetNullableById(2); unitAssociations[3].Application = ApplicationRepository.Queryable.First(); unitAssociations[3].Unit = UnitRepository.GetNullableById(2); unitAssociations[4].User = UserRepository.GetNullableById(2); unitAssociations[4].Application = ApplicationRepository.Queryable.First(); unitAssociations[4].Unit = UnitRepository.GetNullableById(3); unitAssociations[5].User = UserRepository.GetNullableById(2); unitAssociations[5].Application = ApplicationRepository.Queryable.First(); unitAssociations[5].Unit = UnitRepository.GetNullableById(4); unitAssociations[6].User = UserRepository.GetNullableById(3); unitAssociations[6].Application = ApplicationRepository.Queryable.First(); unitAssociations[6].Unit = UnitRepository.GetNullableById(4); unitAssociations[7].User = UserRepository.GetNullableById(4); unitAssociations[7].Application = ApplicationRepository.Queryable.First(); unitAssociations[7].Unit = UnitRepository.GetNullableById(2); unitAssociations[8].User = UserRepository.GetNullableById(5); unitAssociations[8].Application = ApplicationRepository.Queryable.First(); unitAssociations[8].Unit = UnitRepository.GetNullableById(5); unitAssociations[9].User = UserRepository.GetNullableById(5); unitAssociations[9].Application = ApplicationRepository.Queryable.First(); unitAssociations[9].Unit = UnitRepository.GetNullableById(1); ControllerRecordFakes.FakeUnitAssociations(0, UnitAssociationRepository, unitAssociations); #endregion Arrange #region Act var result = UserService.GetByApplication("Name1", "UserName"); //var allowedUnitIds = UnitService.GetVisibleByUser("Name1", "UserName").ToList().Select(x => x.Id).ToList(); ////Get everyone with perms, possibly filtered by role and unit //var usersWithPermissions = from p in PermissionRepository.Queryable // join u in UnitAssociationRepository.Queryable on // new { User = p.User.Id, App = p.Application.Id } // equals new { User = u.User.Id, App = u.Application.Id } // where p.Application.Name == "Name1" // where p.User.UnitAssociations.Any(a => allowedUnitIds.Contains(a.Unit.Id)) // select new { Permissions = p, UnitAssociations = u }; #endregion Act #region Assert Assert.IsNotNull(result); Assert.AreEqual(3, result.Count()); Assert.AreEqual("LoginId2", result.ElementAt(0).LoginId); Assert.AreEqual("LoginId3", result.ElementAt(1).LoginId); Assert.AreEqual("LoginId4", result.ElementAt(2).LoginId); #endregion Assert }