ScriptPrivilegeApiResource extends PrivilegeApiResourceSupport implements Resource, ScriptPrivilegeApiResourceDoc { @Override @POST @RequiresAuthentication @RequiresPermissions("nexus:privileges:create") @Path("script") public Response createPrivilege(final ApiPrivilegeScriptRequest privilege) { return doCreate(ScriptPrivilegeDescriptor.TYPE, privilege); } @Inject  ScriptPrivilegeApiResource(final SecuritySystem securitySystem,
                                    final Map<String, PrivilegeDescriptor> privilegeDescriptors); @Override @POST @RequiresAuthentication @RequiresPermissions("nexus:privileges:create") @Path("script") Response createPrivilege(final ApiPrivilegeScriptRequest privilege); @Override @PUT @RequiresAuthentication @RequiresPermissions("nexus:privileges:update") @Path("script/{privilegeId}") void updatePrivilege(@PathParam("privilegeId") final String privilegeId,
                              final ApiPrivilegeScriptRequest privilege);  }
@Test public void testCreatePrivilege_script() { when(authorizationManager.getPrivilege("name")).thenThrow(new NoSuchPrivilegeException("name")); ApiPrivilegeScriptRequest apiPrivilege = new ApiPrivilegeScriptRequest("name", "description", "scriptName", Arrays .asList(PrivilegeAction.BROWSE, PrivilegeAction.READ, PrivilegeAction.DELETE, PrivilegeAction.EDIT, PrivilegeAction.ADD, PrivilegeAction.RUN)); underTest.createPrivilege(apiPrivilege); ArgumentCaptor<Privilege> argument = ArgumentCaptor.forClass(Privilege.class); verify(authorizationManager).addPrivilege(argument.capture()); assertPrivilege(argument.getValue(), "name", "description", SCRIPT_KEY, "scriptName", ACTIONS_KEY, "browse,read,delete,edit,add,run"); }
@Test public void testCreatePrivilege_scriptWithAllAction() { when(authorizationManager.getPrivilege("name")).thenThrow(new NoSuchPrivilegeException("name")); ApiPrivilegeScriptRequest apiPrivilege = new ApiPrivilegeScriptRequest("name", "description", "scriptName", Collections.singleton(PrivilegeAction.ALL)); underTest.createPrivilege(apiPrivilege); ArgumentCaptor<Privilege> argument = ArgumentCaptor.forClass(Privilege.class); verify(authorizationManager).addPrivilege(argument.capture()); assertPrivilege(argument.getValue(), "name", "description", SCRIPT_KEY, "scriptName", ACTIONS_KEY, "*"); }
@Test public void testCreatePrivilege_invalidScript() { when(authorizationManager.getPrivilege("name")).thenThrow(new NoSuchPrivilegeException("name")); ApiPrivilegeScriptRequest apiPrivilege = new ApiPrivilegeScriptRequest("name", "description", "invalid", Collections.singleton(PrivilegeAction.ALL)); try { underTest.createPrivilege(apiPrivilege); } catch (WebApplicationMessageException e) { assertThat(e.getResponse().getStatus(), is(400)); assertThat(e.getResponse().getMediaType(), is(MediaType.APPLICATION_JSON_TYPE)); assertThat(e.getResponse().getEntity().toString(), is("\"Invalid script 'invalid' supplied.\"")); } }