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
        }
Example #5
0
        public void TestInsertNewUserReturnsExpectedValue1()
        {
            #region Arrange
            const string applicationName = "Name2";
            ControllerRecordFakes.FakeUsers(3, UserRepository);
            AutomapperConfig.Configure();
            var serviceUser = CreateValidEntities.ServiceUser(4);
            ControllerRecordFakes.FakeApplications(3, ApplicationRepository);
            ControllerRecordFakes.FakeRoles(3, RoleRepository);
            ControllerRecordFakes.FakeUnits(3, UnitRepository);
            ControllerRecordFakes.FakePermissions(3, PermissionRepository);
            ControllerRecordFakes.FakeUnitAssociations(3, UnitAssociationRepository);
            #endregion Arrange

            #region Act
            var result = Controller.InsertNewUser(applicationName, serviceUser, 3, 2)
                         .AssertResultIs <JsonResult>();
            #endregion Act

            #region Assert
            Assert.IsNotNull(result);
            var result2 = (ServiceUser)result.Data;
            Assert.IsNotNull(result2);
            Assert.AreEqual("*****@*****.**", result2.Email);
            Assert.AreEqual("FirstName4", result2.FirstName);
            Assert.AreEqual("LastName4", result2.LastName);
            Assert.AreEqual("LoginId4", result2.Login);
            Assert.AreEqual("+1 530 755 7777", result2.Phone);

            UserRepository.AssertWasCalled(a => a.EnsurePersistent(Arg <User> .Is.Anything));
            var userArgs = (User)UserRepository.GetArgumentsForCallsMadeOn(a => a.EnsurePersistent(Arg <User> .Is.Anything))[0][0];
            Assert.AreEqual("*****@*****.**", userArgs.Email);
            Assert.AreEqual("FirstName4", userArgs.FirstName);
            Assert.IsFalse(userArgs.Inactive);
            Assert.AreEqual("LastName4", userArgs.LastName);
            Assert.AreEqual("LoginId4", userArgs.LoginId);
            Assert.AreEqual("+1 530 755 7777", userArgs.Phone);

            PermissionRepository.AssertWasCalled(a => a.EnsurePersistent(Arg <Permission> .Is.Anything));
            var permissionArgs = (Permission)PermissionRepository.GetArgumentsForCallsMadeOn(a => a.EnsurePersistent(Arg <Permission> .Is.Anything))[0][0];
            Assert.IsNotNull(permissionArgs);
            Assert.AreEqual(applicationName, permissionArgs.Application.ToString());
            Assert.AreEqual("Name3", permissionArgs.Role.Name);
            Assert.IsFalse(permissionArgs.Inactive);
            Assert.AreEqual(userArgs.LoginId, permissionArgs.User.LoginId);

            UnitAssociationRepository.AssertWasCalled(a => a.EnsurePersistent(Arg <UnitAssociation> .Is.Anything));
            var unitAssociationArgs = (UnitAssociation)UnitAssociationRepository.GetArgumentsForCallsMadeOn(a => a.EnsurePersistent(Arg <UnitAssociation> .Is.Anything))[0][0];
            Assert.IsNotNull(unitAssociationArgs);
            Assert.AreEqual(applicationName, unitAssociationArgs.Application.ToString());
            Assert.AreEqual("ShortName2", unitAssociationArgs.Unit.ShortName);
            Assert.IsFalse(unitAssociationArgs.Inactive);
            Assert.AreEqual(userArgs.LoginId, unitAssociationArgs.User.LoginId);

            #endregion Assert
        }
Example #6
0
        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;
            }
        }
Example #7
0
        public void TestRemoveUnitThrowsExceptionIfCurrentUserDoesNotHaveAccess1()
        {
            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);
                }
                ControllerRecordFakes.FakeUnitAssociations(2, UnitAssociationRepository, unitAssociations);
                UserService.Expect(a => a.CanUserManageGivenLogin("Name2", "UserName", "LoginId1")).Return(false).Repeat.Any();
                #endregion Arrange

                #region Act
                Controller.RemoveUnit("Name2", "LoginId1", 2);
                #endregion Act
            }
            catch (Exception ex)
            {
                #region Assert
                Assert.IsNotNull(ex);
                Assert.AreEqual("UserName does not have access to manage LoginId1 within the Name2 application", ex.Message);
                UserService.AssertWasCalled(a => a.CanUserManageGivenLogin("Name2", "UserName", "LoginId1"));
                UnitAssociationRepository.AssertWasNotCalled(a => a.Remove(Arg <UnitAssociation> .Is.Anything));
                #endregion Assert
                throw;
            }
        }
Example #8
0
        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
        }
Example #9
0
        public void TestInsertNewUserReturnsExpectedValue3()
        {
            #region Arrange
            const string applicationName = "Name2";
            var          users           = new List <User>();
            users.Add(CreateValidEntities.User(4));
            ControllerRecordFakes.FakeUsers(0, UserRepository, users);

            AutomapperConfig.Configure();
            var serviceUser = CreateValidEntities.ServiceUser(4);

            ControllerRecordFakes.FakeApplications(3, ApplicationRepository);
            ControllerRecordFakes.FakeRoles(3, RoleRepository);
            ControllerRecordFakes.FakeUnits(3, UnitRepository);

            var permissions = new List <Permission>();
            permissions.Add(CreateValidEntities.Permission(1));
            permissions[0].Application = ApplicationRepository.GetNullableById(2);
            permissions[0].User        = users[0];
            permissions[0].Role        = RoleRepository.GetNullableById(3);
            permissions.Add(CreateValidEntities.Permission(2));
            permissions[1].Application = ApplicationRepository.GetNullableById(2);
            permissions[1].User        = users[0];
            permissions[1].Role        = RoleRepository.GetNullableById(1);
            ControllerRecordFakes.FakePermissions(0, PermissionRepository, permissions);

            var unitAssociations = new List <UnitAssociation>();
            unitAssociations.Add(CreateValidEntities.UnitAssociation(1));
            unitAssociations[0].Application = ApplicationRepository.GetNullableById(2);
            unitAssociations[0].User        = users[0];
            unitAssociations[0].Unit        = UnitRepository.GetNullableById(2);
            unitAssociations.Add(CreateValidEntities.UnitAssociation(2));
            unitAssociations[1].Application = ApplicationRepository.GetNullableById(2);
            unitAssociations[1].User        = users[0];
            unitAssociations[1].Unit        = UnitRepository.GetNullableById(1);
            ControllerRecordFakes.FakeUnitAssociations(0, UnitAssociationRepository, unitAssociations);
            #endregion Arrange

            #region Act
            var result = Controller.InsertNewUser(applicationName, serviceUser, 3, 2)
                         .AssertResultIs <JsonResult>();
            #endregion Act

            #region Assert
            Assert.IsNotNull(result);
            var result2 = (ServiceUser)result.Data;
            Assert.IsNotNull(result2);
            Assert.AreEqual("*****@*****.**", result2.Email);
            Assert.AreEqual("FirstName4", result2.FirstName);
            Assert.AreEqual("LastName4", result2.LastName);
            Assert.AreEqual("LoginId4", result2.Login);
            Assert.AreEqual("+1 530 755 7777", result2.Phone);
            Assert.AreEqual("Name1, Name3", result2.Roles);
            Assert.AreEqual("ShortName1, ShortName2", result2.Units);

            UserRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg <User> .Is.Anything));
            PermissionRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg <Permission> .Is.Anything));
            UnitAssociationRepository.AssertWasNotCalled(a => a.EnsurePersistent(Arg <UnitAssociation> .Is.Anything));


            #endregion Assert
        }