示例#1
0
  @Test
  public void testPutChangeConfig() throws Exception {
    // the test config starts off being setup for simple auth scheme, this test will update it for
    // DIGEST-MD5
    PlexusResource resource = getResource();

    LdapConnectionInfoResponse response = new LdapConnectionInfoResponse();
    LdapConnectionInfoDTO connectionInfo = new LdapConnectionInfoDTO();
    response.setData(connectionInfo);
    connectionInfo.setHost("localhost");
    connectionInfo.setPort(this.getLdapPort());
    connectionInfo.setSearchBase("o=sonatype");
    connectionInfo.setSystemPassword("secret");
    connectionInfo.setSystemUsername("admin");
    connectionInfo.setProtocol("ldap");
    connectionInfo.setAuthScheme("DIGEST-MD5");
    connectionInfo.setRealm("localhost");

    LdapConnectionInfoResponse result =
        (LdapConnectionInfoResponse) resource.put(null, null, null, response);
    this.validateConnectionDTO(connectionInfo, result.getData());

    // now how about that get
    result = (LdapConnectionInfoResponse) resource.get(null, null, null, null);
    this.validateConnectionDTO(connectionInfo, result.getData());
  }
  public void testSearchUsers() throws Exception {
    // now we should have two jcoders, one from the mock and one from the XML realm

    PlexusResource userSearchResource =
        this.lookup(PlexusResource.class, "UserSearchPlexusResource");

    Request request = this.buildRequest();
    request.getAttributes().put(UserSearchPlexusResource.USER_SOURCE_KEY, "all");
    request.getAttributes().put(UserSearchPlexusResource.USER_ID_KEY, "jcoder");

    List<PlexusUserResource> result =
        ((PlexusUserListResourceResponse) userSearchResource.get(null, request, null, null))
            .getData();

    // now make sure we have 2 jcoders
    PlexusUserResource jcoderXML = null;
    PlexusUserResource jcoderMock = null;

    for (PlexusUserResource plexusUserResource : result) {
      if (plexusUserResource.getUserId().equals("jcoder")) {
        if (plexusUserResource.getSource().endsWith(SecurityXmlUserManager.SOURCE)) {
          jcoderXML = plexusUserResource;
        } else if (plexusUserResource.getSource().endsWith(MockUserManager.SOURCE)) {
          jcoderMock = plexusUserResource;
        } else {
          Assert.fail(
              "found a jcoder with an unknown source of: " + plexusUserResource.getSource());
        }
      }
    }

    Assert.assertNotNull("jcoderXML is null", jcoderXML);
    Assert.assertNotNull("jcoderMock is null", jcoderMock);
  }
  public void updateUserRoleTest() throws Exception {
    // first get the list of the previous users roles so we can make sure they change
    User jcoderXML = securitySystem.getUser("jcoder", SecurityXmlUserManager.SOURCE);
    User jcoderMock = securitySystem.getUser("jcoder", MockUserManager.SOURCE);

    List<String> jcoderXMLOriginalRoles = new ArrayList<String>();
    for (RoleIdentifier role : jcoderXML.getRoles()) {
      jcoderXMLOriginalRoles.add(role.getRoleId());
    }

    List<String> jcoderMockOriginalRoles = new ArrayList<String>();
    for (RoleIdentifier role : jcoderMock.getRoles()) {
      jcoderMockOriginalRoles.add(role.getRoleId());
    }

    // now update one... and check the other

    Request request = this.buildRequest();
    Response response = new Response(request);
    request.getAttributes().put(UserToRolePlexusResource.USER_ID_KEY, "jcoder");
    request.getAttributes().put(UserToRolePlexusResource.SOURCE_ID_KEY, MockUserManager.SOURCE);

    PlexusResource userToRoleResource =
        this.lookup(PlexusResource.class, "UserSearchPlexusResource");

    UserToRoleResourceRequest payload = new UserToRoleResourceRequest();
    payload.setData(new UserToRoleResource());
    payload.getData().setUserId("jcoder");
    payload.getData().setSource(MockUserManager.SOURCE);
    payload.getData().getRoles().add("admin");

    userToRoleResource.put(null, request, response, null);

    // the xml user should have the original roles the mock users should only have admin.

    jcoderXML = securitySystem.getUser("jcoder", SecurityXmlUserManager.SOURCE);
    jcoderMock = securitySystem.getUser("jcoder", MockUserManager.SOURCE);

    List<String> jcoderXMLNewRoles = new ArrayList<String>();
    for (RoleIdentifier role : jcoderXML.getRoles()) {
      jcoderXMLNewRoles.add(role.getRoleId());
    }

    List<String> jcoderMockNewRoles = new ArrayList<String>();
    for (RoleIdentifier role : jcoderMock.getRoles()) {
      jcoderMockNewRoles.add(role.getRoleId());
    }

    Assert.assertEquals(jcoderXMLOriginalRoles, jcoderXMLNewRoles);

    Assert.assertEquals(1, jcoderMockNewRoles.size());
    Assert.assertTrue(jcoderMockNewRoles.contains("admin"));
  }
示例#4
0
  @Test
  public void testRestPassword() throws Exception {

    this.copyDefaultConfigToPlace();
    this.setupEmailConfig();

    NexusConfiguration nexusConfig = this.lookup(NexusConfiguration.class);
    nexusConfig.loadConfiguration(true);

    String username = "******";

    PlexusResource resetEmailPR = this.lookup(PlexusResource.class, "UserResetPlexusResource");

    Request request = new Request();
    Response response = new Response(request);
    request.getAttributes().put(UserResetPlexusResource.USER_ID_KEY, username);
    resetEmailPR.delete(null, request, response);

    // Need 1 message
    server.waitForIncomingEmail(5000, 1);

    MimeMessage[] msgs = server.getReceivedMessages();
    Assert.assertTrue("Expected email.", msgs != null && msgs.length > 0);
    MimeMessage msg = msgs[0];

    String password = null;
    // Sample body: Your password has been reset. Your new password is: c1r6g4p8l7
    String body = GreenMailUtil.getBody(msg);

    int index = body.indexOf("Your new password is: ");
    int passwordStartIndex = index + "Your new password is: ".length();
    if (index != -1) {
      password = body.substring(passwordStartIndex, body.indexOf('\n', passwordStartIndex)).trim();
    }

    Assert.assertNotNull(password);
  }