public static PluginRoleConfig fromJSON(JsonReader jsonReader) { PluginRoleConfig model = new PluginRoleConfig(); if (jsonReader == null) { return model; } jsonReader.readStringIfPresent("auth_config_id", model::setAuthConfigId); model.addConfigurations(ConfigurationPropertyRepresenter.fromJSONArray(jsonReader, "properties")); return model; }
public static PluginRoleConfig fromJSON(JsonReader jsonReader) { PluginRoleConfig model = new PluginRoleConfig(); if (jsonReader == null) { return model; } jsonReader.readStringIfPresent("auth_config_id", model::setAuthConfigId); model.addConfigurations(ConfigurationPropertyRepresenter.fromJSONArray(jsonReader, "properties")); return model; }
@Test public void shouldCareAboutPluginRoleConfigChange() { SecurityConfigChangeListener securityConfigChangeListener = new SecurityConfigChangeListener() { @Override public void onEntityConfigChange(Object entity) { } }; assertThat(securityConfigChangeListener.shouldCareAbout(new PluginRoleConfig()), is(true)); }
@Test public void shouldBeAbleToFetchPluginRolesForAAuthConfig() throws Exception { PluginRoleConfig admin = new PluginRoleConfig("admin", "corporate_ldap"); PluginRoleConfig view = new PluginRoleConfig("view", "corporate_ldap"); PluginRoleConfig operator = new PluginRoleConfig("operator", "internal_ldap"); RolesConfig rolesConfig = new RolesConfig(admin, view, operator, new RoleConfig(new CaseInsensitiveString("committer"))); assertThat(rolesConfig.pluginRoleConfigsFor("corporate_ldap"), hasSize(2)); assertThat(rolesConfig.pluginRoleConfigsFor("corporate_ldap"), containsInAnyOrder(admin, view)); assertThat(rolesConfig.pluginRoleConfigsFor("internal_ldap"), hasSize(1)); assertThat(rolesConfig.pluginRoleConfigsFor("internal_ldap"), containsInAnyOrder(operator)); }
@Test public void getRoleConfigsShouldReturnOnlyNonPluginRoles() { Role admin = new RoleConfig(new CaseInsensitiveString("admin")); Role view = new RoleConfig(new CaseInsensitiveString("view")); Role blackbird = new PluginRoleConfig("blackbird", "foo"); Role spacetiger = new PluginRoleConfig("spacetiger", "foo"); RolesConfig rolesConfig = new RolesConfig(admin, blackbird, view, spacetiger); List<RoleConfig> roles = rolesConfig.getRoleConfigs(); assertThat(roles, hasSize(2)); assertThat(roles, contains(admin, view)); }
@Test public void getPluginRoleConfigsShouldReturnOnlyPluginRoles() { Role admin = new RoleConfig(new CaseInsensitiveString("admin")); Role view = new RoleConfig(new CaseInsensitiveString("view")); Role blackbird = new PluginRoleConfig("blackbird", "foo"); Role spacetiger = new PluginRoleConfig("spacetiger", "foo"); RolesConfig rolesConfig = new RolesConfig(admin, blackbird, view, spacetiger); List<PluginRoleConfig> roles = rolesConfig.getPluginRoleConfigs(); assertThat(roles, hasSize(2)); assertThat(roles, contains(blackbird, spacetiger)); }
@Test public void allRolesShouldReturnAllRoles() { Role admin = new RoleConfig(new CaseInsensitiveString("admin")); Role view = new RoleConfig(new CaseInsensitiveString("view")); Role blackbird = new PluginRoleConfig("blackbird", "foo"); Role spacetiger = new PluginRoleConfig("spacetiger", "foo"); RolesConfig rolesConfig = new RolesConfig(admin, blackbird, view, spacetiger); List<Role> roles = rolesConfig.allRoles(); assertThat(roles, hasSize(4)); assertThat(roles, contains(admin, blackbird, view, spacetiger)); }
@Test public void assignRole_ShouldAssignPluginRoleToAnUser() throws Exception { assertThat(pluginRoleUsersStore.pluginRoles(), hasSize(0)); PluginRoleConfig pluginRoleConfig = new PluginRoleConfig("spacetiger", "ldap"); pluginRoleUsersStore.assignRole("wing-commander", pluginRoleConfig); assertThat(pluginRoleUsersStore.pluginRoles(), hasSize(1)); assertThat(pluginRoleUsersStore.usersInRole(pluginRoleConfig), hasItem(new RoleUser("wing-commander"))); }
public void validatePresenceAuthConfigId(Validator v){ PluginRoleConfig role = new PluginRoleConfig("admin", ""); SecurityConfig securityConfig = new SecurityConfig(); v.validate(role, ValidationContextMother.validationContext(securityConfig)); assertThat(role.errors().size(), is(1)); assertThat(role.errors().get("authConfigId").size(), is(1)); assertThat(role.errors().get("authConfigId").get(0), is("Invalid plugin role authConfigId name ''. This must be alphanumeric and can" + " contain underscores and periods (however, it cannot start with a period). The maximum allowed length is 255 characters.")); }
public void validatePresenceOfAuthConfigIdInSecurityConfig(Validator v) throws Exception { PluginRoleConfig role = new PluginRoleConfig("admin", "auth_config_id"); SecurityConfig securityConfig = new SecurityConfig(); v.validate(role, ValidationContextMother.validationContext(securityConfig)); assertThat(role.errors().size(), is(1)); assertThat(role.errors().get("authConfigId").size(), is(1)); assertThat(role.errors().get("authConfigId").get(0), is("No such security auth configuration present for id: `auth_config_id`")); }
@Test public void getPluginRole_shouldReturnPluginRoleMatchingTheGivenName() throws Exception { PluginRoleConfig role = new PluginRoleConfig("foo", "ldap"); SecurityConfig securityConfig = new SecurityConfig(); securityConfig.addRole(role); assertThat(securityConfig.getPluginRole(new CaseInsensitiveString("FOO")), is(role)); }
@Test public void getProcessRoleConfigsResponseBody() throws Exception { AuthorizationMessageConverterV1 converter = new AuthorizationMessageConverterV1(); String json = converter.getProcessRoleConfigsResponseBody(singletonList(new PluginRoleConfig("blackbird", "ldap", create("foo", false, "bar")))); assertThatJson("[{\"name\":\"blackbird\",\"configuration\":{\"foo\":\"bar\"}}]").isEqualTo(json); } }
@Test public void shouldGetPluginRolesWhichBelogsToSpecifiedPlugin() throws Exception { SecurityConfig securityConfig = new SecurityConfig(); securityConfig.addRole(new PluginRoleConfig("foo", "ldap")); securityConfig.addRole(new PluginRoleConfig("bar", "github")); securityConfig.addRole(new RoleConfig(new CaseInsensitiveString("xyz"))); securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("ldap", "cd.go.ldap")); securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("github", "cd.go.github")); List<PluginRoleConfig> pluginRolesConfig = securityConfig.getPluginRoles("cd.go.ldap"); assertThat(pluginRolesConfig, hasSize(1)); assertThat(pluginRolesConfig, contains(new PluginRoleConfig("foo", "ldap"))); }
@Test public void removePluginRole_ShouldRemovePluginRoleFromStore() throws Exception { PluginRoleConfig pluginRoleConfig = new PluginRoleConfig("spacetiger", "ldap"); pluginRoleUsersStore.assignRole("wing-commander", pluginRoleConfig); assertThat(pluginRoleUsersStore.pluginRoles(), hasSize(1)); assertThat(pluginRoleUsersStore.usersInRole(pluginRoleConfig), hasItem(new RoleUser("wing-commander"))); pluginRoleUsersStore.remove(pluginRoleConfig); assertThat(pluginRoleUsersStore.pluginRoles(), hasSize(0)); }
private void validatePresenceOfRoleName(Validator v) { PluginRoleConfig role = new PluginRoleConfig("", "auth_config_id"); SecurityConfig securityConfig = new SecurityConfig(); securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("auth_config_id", "plugin_id")); v.validate(role, ValidationContextMother.validationContext(securityConfig)); assertTrue(role.hasErrors()); assertThat(role.errors().size(), is(1)); assertThat(role.errors().get("name").get(0), is("Invalid role name name ''. This must be alphanumeric and can" + " contain underscores and periods (however, it cannot start with a period). The maximum allowed length is 255 characters.")); }
private void validateNullRoleName(Validator v) { PluginRoleConfig role = new PluginRoleConfig("", "auth_config_id"); role.setName(null); SecurityConfig securityConfig = new SecurityConfig(); securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("auth_config_id", "plugin_id")); v.validate(role, ValidationContextMother.validationContext(securityConfig)); assertTrue(role.hasErrors()); assertThat(role.errors().size(), is(1)); assertThat(role.errors().get("name").get(0), is("Invalid role name name 'null'. This must be alphanumeric and can" + " contain underscores and periods (however, it cannot start with a period). The maximum allowed length is 255 characters.")); }
@Test public void getPluginRolesConfig_shouldReturnNothingWhenBadPluginIdSpecified() throws Exception { SecurityConfig securityConfig = new SecurityConfig(); securityConfig.addRole(new PluginRoleConfig("foo", "ldap")); securityConfig.addRole(new PluginRoleConfig("bar", "github")); securityConfig.addRole(new RoleConfig(new CaseInsensitiveString("xyz"))); securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("ldap", "cd.go.ldap")); securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("github", "cd.go.github")); List<PluginRoleConfig> pluginRolesConfig = securityConfig.getPluginRoles("non-existant-plugin"); assertThat(pluginRolesConfig, hasSize(0)); }
public void validateUniquenessOfRoleName(Validator v) throws Exception { PluginRoleConfig role = new PluginRoleConfig("admin", "auth_config_id"); SecurityConfig securityConfig = new SecurityConfig(); ValidationContext validationContext = ValidationContextMother.validationContext(securityConfig); securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("auth_config_id", "plugin_id")); securityConfig.getRoles().add(new RoleConfig(new CaseInsensitiveString("admin"))); securityConfig.getRoles().add(role); v.validate(role, validationContext); assertThat(role.errors().size(), is(1)); assertThat(role.errors().get("name").get(0), is("Role names should be unique. Role with the same name exists.")); }
@Test public void hasErrors_shouldBeTrueIfRoleHasErrors() throws Exception { Role role = new PluginRoleConfig("", "auth_config_id"); SecurityConfig securityConfig = new SecurityConfig(); securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("auth_config_id", "plugin_id")); role.validate(ValidationContextMother.validationContext(securityConfig)); assertTrue(role.hasErrors()); }
@Test public void hasErrors_shouldBeTrueIfConfigurationPropertiesHasErrors() throws Exception { ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey("username"), new ConfigurationValue("view")); PluginRoleConfig roleConfig = new PluginRoleConfig("admin", "auth_id", property); property.addError("username", "username format is incorrect"); assertTrue(roleConfig.hasErrors()); assertTrue(roleConfig.errors().isEmpty()); }