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