Esempio n. 1
0
        public async void UpdateRoleAsync_Success()
        {
            // Arrange
            var user       = PrincipalHelper.CreateForRole("admin-users");
            var helper     = new TestHelper();
            var controller = helper.CreateController <RoleController>(user);

            var mapper  = helper.GetService <IMapper>();
            var service = helper.GetService <Mock <IPimsKeycloakService> >();
            var erole   = new Entity.Role(Guid.NewGuid(), "test")
            {
                Description = "description"
            };

            service.Setup(m => m.UpdateRoleAsync(It.IsAny <Entity.Role>())).Returns(Task.FromResult(erole));
            var model = mapper.Map <Model.Update.RoleModel>(erole);

            // Act
            var result = await controller.UpdateRoleAsync(erole.Id, model);

            // Assert
            var actionResult   = Assert.IsType <JsonResult>(result);
            var actualResult   = Assert.IsType <Model.RoleModel>(actionResult.Value);
            var expectedResult = mapper.Map <Model.RoleModel>(erole);

            Assert.Equal(expectedResult.Id, actualResult.Id);
            Assert.Equal(expectedResult.Name, actualResult.Name);
            Assert.Equal(expectedResult.Description, actualResult.Description);
            service.Verify(m => m.UpdateRoleAsync(It.IsAny <Entity.Role>()), Times.Once());
        }
Esempio n. 2
0
        public void GetRoleCodes()
        {
            // Arrange
            var helper     = new TestHelper();
            var controller = helper.CreateController <LookupController>(Permissions.PropertyView);

            var mapper  = helper.GetService <IMapper>();
            var service = helper.GetService <Mock <IPimsService> >();
            var role    = new Entity.Role
            {
                Id          = Guid.NewGuid(),
                Name        = "Ministry of Health",
                Description = "The Ministry of Health"
            };

            service.Setup(m => m.Lookup.GetRoles()).Returns(new[] { role });

            // Act
            var result = controller.GetRoles();

            // Assert
            var actionResult = Assert.IsType <JsonResult>(result);
            var actualResult = Assert.IsType <Model.RoleModel[]>(actionResult.Value);

            Assert.Equal(new[] { mapper.Map <Model.RoleModel>(role) }, actualResult, new DeepPropertyCompare());
            service.Verify(m => m.Lookup.GetRoles(), Times.Once());
        }
Esempio n. 3
0
        public void GetRoles_Filtered_Success()
        {
            // Arrange
            var helper     = new TestHelper();
            var controller = helper.CreateController <RoleController>(Permissions.AdminRoles);

            var mapper  = helper.GetService <IMapper>();
            var service = helper.GetService <Mock <IPimsAdminService> >();
            var roles   = new Entity.Role[] { EntityHelper.CreateRole("role1"), EntityHelper.CreateRole("role2") };
            var paged   = new Entity.Models.Paged <Entity.Role>(roles);

            service.Setup(m => m.Role.Get(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(paged);

            // Act
            var result = controller.GetRoles(1, 10, "test");

            // Assert
            var actionResult = Assert.IsType <JsonResult>(result);

            Assert.Null(actionResult.StatusCode);
            var actualResult = Assert.IsType <Pims.Api.Models.PageModel <Model.RoleModel> >(actionResult.Value);

            Assert.Equal(mapper.Map <Model.RoleModel[]>(roles), actualResult.Items, new DeepPropertyCompare());
            service.Verify(m => m.Role.Get(1, 10, "test"), Times.Once());
        }
Esempio n. 4
0
        /// <summary>
        /// Update the specified role in keycloak and PIMS.
        /// This will add the role to PIMS if it does not current exist.
        /// </summary>
        /// <param name="role"></param>
        /// <exception type="KeyNotFoundException">User does not exist in keycloak or PIMS.</exception>
        /// <returns></returns>
        public async Task <Entity.Role> UpdateRoleAsync(Entity.Role role)
        {
            if (await _keycloakService.GetGroupAsync(role.Id) == null)
            {
                throw new KeyNotFoundException();
            }

            // Role does not exist in PIMS, it needs to be added.
            if (_pimsAdminService.Role.Find(role.Id) == null)
            {
                _pimsAdminService.Role.Add(role);
            }
            else
            {
                _pimsAdminService.Role.Update(role);
            }

            var kmodel = _mapper.Map <KModel.GroupModel>(role);
            await _keycloakService.UpdateGroupAsync(kmodel);

            return(role);
        }
Esempio n. 5
0
        public async void GetRoleAsync_Success()
        {
            // Arrange
            var user       = PrincipalHelper.CreateForRole("admin-users");
            var helper     = new TestHelper();
            var controller = helper.CreateController <RoleController>(user);

            var mapper  = helper.GetService <IMapper>();
            var service = helper.GetService <Mock <IPimsKeycloakService> >();
            var erole   = new Entity.Role(Guid.NewGuid(), "test");

            service.Setup(m => m.GetRoleAsync(It.IsAny <Guid>())).Returns(Task.FromResult(erole));

            // Act
            var result = await controller.GetRoleAsync(erole.Id);

            // Assert
            var actionResult = Assert.IsType <JsonResult>(result);
            var actualResult = Assert.IsType <Model.RoleModel>(actionResult.Value);

            Assert.Equal(mapper.Map <Model.RoleModel>(erole), actualResult, new DeepPropertyCompare());
            service.Verify(m => m.GetRoleAsync(It.IsAny <Guid>()), Times.Once());
        }
Esempio n. 6
0
        public async void SyncRolesAsync_Success()
        {
            // Arrange
            var user       = PrincipalHelper.CreateForRole("admin-users");
            var helper     = new TestHelper();
            var controller = helper.CreateController <RoleController>(user);

            var mapper  = helper.GetService <IMapper>();
            var service = helper.GetService <Mock <IPimsKeycloakService> >();
            var erole   = new Entity.Role(Guid.NewGuid(), "test");
            var eroles  = new[] { erole };

            service.Setup(m => m.SyncRolesAsync()).Returns(Task.FromResult((IEnumerable <Entity.Role>)eroles));

            // Act
            var result = await controller.SyncRolesAsync();

            // Assert
            var actionResult = Assert.IsType <JsonResult>(result);
            var data         = Assert.IsType <Model.RoleModel[]>(actionResult.Value);

            Assert.Equal(mapper.Map <Model.RoleModel[]>(eroles), data, new DeepPropertyCompare());
            service.Verify(m => m.SyncRolesAsync(), Times.Once());
        }
Esempio n. 7
0
        /// <summary>
        /// Create a new instance of an AccessRequest for a default user.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="username"></param>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="role"></param>
        /// <param name="agency"></param>
        /// <returns></returns>
        public static Entity.User CreateUser(Guid id, string username, string firstName = "given name", string lastName = "surname", Entity.Role role = null, Entity.Agency agency = null)
        {
            var user = new Entity.User(id, username, $"{firstName}.{lastName}@email.com")
            {
                DisplayName = $"{lastName}, {firstName}",
                RowVersion  = new byte[] { 12, 13, 14 }
            };

            user.Roles.Add(new Entity.UserRole(user, role ?? EntityHelper.CreateRole("Real Estate Manager")));
            user.Agencies.Add(new Entity.UserAgency(user, agency ?? EntityHelper.CreateAgency()));

            return(user);
        }