PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/nexus/nexus-core-plugins/nexus-ldap-plugin-parent/nexus-ldap-realm-plugin/src/test/java/org/sonatype/nexus/security/ldap/realms/api/LdapUserGroupConfNotConfiguredTest.java

https://github.com/stadelma/nexus
Java | 168 lines | 116 code | 25 blank | 27 comment | 0 complexity | 76a756eb10353b7a8a0e2f63c4508e31 MD5 | raw file
  1. /**
  2. * Copyright (c) 2008-2011 Sonatype, Inc.
  3. * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions
  4. *
  5. * This program is free software: you can redistribute it and/or modify it only under the terms of the GNU Affero General
  6. * Public License Version 3 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  9. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License Version 3
  10. * for more details.
  11. *
  12. * You should have received a copy of the GNU Affero General Public License Version 3 along with this program. If not, see
  13. * http://www.gnu.org/licenses.
  14. *
  15. * Sonatype Nexus (TM) Open Source Version is available from Sonatype, Inc. Sonatype and Sonatype Nexus are trademarks of
  16. * Sonatype, Inc. Apache Maven is a trademark of the Apache Foundation. M2Eclipse is a trademark of the Eclipse Foundation.
  17. * All other trademarks are the property of their respective owners.
  18. */
  19. package org.sonatype.nexus.security.ldap.realms.api;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.FileNotFoundException;
  23. import java.io.IOException;
  24. import java.io.InputStreamReader;
  25. import org.junit.Assert;
  26. import org.junit.Test;
  27. import org.codehaus.plexus.context.Context;
  28. import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
  29. import org.sonatype.nexus.AbstractNexusLdapTestCase;
  30. import org.sonatype.nexus.security.ldap.realms.api.dto.LdapUserAndGroupConfigurationDTO;
  31. import org.sonatype.nexus.security.ldap.realms.api.dto.LdapUserAndGroupConfigurationResponse;
  32. import org.sonatype.plexus.rest.resource.PlexusResource;
  33. import org.sonatype.security.ldap.realms.persist.model.CUserAndGroupAuthConfiguration;
  34. import org.sonatype.security.ldap.realms.persist.model.Configuration;
  35. import org.sonatype.security.ldap.realms.persist.model.io.xpp3.LdapConfigurationXpp3Reader;
  36. public class LdapUserGroupConfNotConfiguredTest
  37. extends AbstractNexusLdapTestCase
  38. {
  39. private PlexusResource getResource()
  40. throws Exception
  41. {
  42. return this.lookup( PlexusResource.class, "LdapUserAndGroupsConfigurationPlexusResource" );
  43. }
  44. @Test
  45. public void testGetNotConfigured()
  46. throws Exception
  47. {
  48. PlexusResource resource = getResource();
  49. // none of these args are used, but if they start being used, we will need to change this.
  50. LdapUserAndGroupConfigurationResponse response =
  51. (LdapUserAndGroupConfigurationResponse) resource.get( null, null, null, null );
  52. // the default configuration is returned.
  53. LdapUserAndGroupConfigurationDTO dto = response.getData();
  54. Assert.assertNotNull( dto );
  55. Assert.assertEquals( "ou=groups", dto.getGroupBaseDn() );
  56. Assert.assertEquals( "cn", dto.getGroupIdAttribute() );
  57. Assert.assertEquals( "uniqueMember", dto.getGroupMemberAttribute() );
  58. Assert.assertEquals( "${username}", dto.getGroupMemberFormat() );
  59. Assert.assertEquals( "groupOfUniqueNames", dto.getGroupObjectClass() );
  60. Assert.assertEquals( "ou=people", dto.getUserBaseDn() );
  61. Assert.assertEquals( "uid", dto.getUserIdAttribute() );
  62. Assert.assertEquals( "inetOrgPerson", dto.getUserObjectClass() );
  63. Assert.assertNull( dto.getUserPasswordAttribute() );
  64. // Assert.assertEquals("userPassword", dto.getUserPasswordAttribute());
  65. Assert.assertEquals( "cn", dto.getUserRealNameAttribute() );
  66. Assert.assertEquals( "mail", dto.getEmailAddressAttribute() );
  67. Assert.assertNull( dto.getUserMemberOfAttribute() );
  68. Assert.assertTrue( dto.isLdapGroupsAsRoles() );
  69. Assert.assertFalse( dto.isGroupSubtree() );
  70. Assert.assertFalse( dto.isUserSubtree() );
  71. }
  72. private void validateConfigFile( LdapUserAndGroupConfigurationDTO dto )
  73. throws Exception
  74. {
  75. String configFileName = CONF_HOME.getAbsolutePath() + "/no-conf" + "/ldap.xml";// this.getNexusLdapConfiguration();
  76. LdapConfigurationXpp3Reader reader = new LdapConfigurationXpp3Reader();
  77. FileInputStream fis = new FileInputStream( configFileName );
  78. Configuration config = reader.read( fis );
  79. CUserAndGroupAuthConfiguration userGroupConfig = config.getUserAndGroupConfig();
  80. Assert.assertEquals( dto.getGroupBaseDn(), userGroupConfig.getGroupBaseDn() );
  81. Assert.assertEquals( dto.getGroupIdAttribute(), userGroupConfig.getGroupIdAttribute() );
  82. Assert.assertEquals( dto.getGroupMemberAttribute(), userGroupConfig.getGroupMemberAttribute() );
  83. Assert.assertEquals( dto.getGroupMemberFormat(), userGroupConfig.getGroupMemberFormat() );
  84. Assert.assertEquals( dto.getGroupObjectClass(), userGroupConfig.getGroupObjectClass() );
  85. Assert.assertEquals( dto.getUserBaseDn(), userGroupConfig.getUserBaseDn() );
  86. Assert.assertEquals( dto.getUserIdAttribute(), userGroupConfig.getUserIdAttribute() );
  87. Assert.assertEquals( dto.getUserObjectClass(), userGroupConfig.getUserObjectClass() );
  88. Assert.assertEquals( dto.getUserPasswordAttribute(), userGroupConfig.getUserPasswordAttribute() );
  89. Assert.assertEquals( dto.getUserRealNameAttribute(), userGroupConfig.getUserRealNameAttribute() );
  90. Assert.assertEquals( dto.getEmailAddressAttribute(), userGroupConfig.getEmailAddressAttribute() );
  91. Assert.assertEquals( dto.getUserMemberOfAttribute(), userGroupConfig.getUserMemberOfAttribute() );
  92. Assert.assertEquals( dto.isLdapGroupsAsRoles(), userGroupConfig.isLdapGroupsAsRoles() );
  93. Assert.assertEquals( dto.isGroupSubtree(), userGroupConfig.isGroupSubtree() );
  94. Assert.assertEquals( dto.isUserSubtree(), userGroupConfig.isUserSubtree() );
  95. }
  96. @Test
  97. public void testPutNotConfigured()
  98. throws Exception
  99. {
  100. PlexusResource resource = getResource();
  101. LdapUserAndGroupConfigurationResponse response = new LdapUserAndGroupConfigurationResponse();
  102. LdapUserAndGroupConfigurationDTO userGroupConf = new LdapUserAndGroupConfigurationDTO();
  103. response.setData( userGroupConf );
  104. userGroupConf.setGroupMemberFormat( "uid=${username},ou=people,o=sonatype" );
  105. userGroupConf.setGroupObjectClass( "groupOfUniqueNames" );
  106. userGroupConf.setGroupBaseDn( "ou=groups" );
  107. userGroupConf.setGroupIdAttribute( "cn" );
  108. userGroupConf.setGroupMemberAttribute( "uniqueMember" );
  109. userGroupConf.setUserObjectClass( "inetOrgPerson" );
  110. userGroupConf.setUserBaseDn( "ou=people" );
  111. userGroupConf.setUserIdAttribute( "uid" );
  112. userGroupConf.setUserPasswordAttribute( "userPassword" );
  113. userGroupConf.setUserRealNameAttribute( "cn" );
  114. userGroupConf.setEmailAddressAttribute( "mail" );
  115. userGroupConf.setGroupSubtree( false );
  116. userGroupConf.setUserSubtree( true );
  117. LdapUserAndGroupConfigurationResponse result =
  118. (LdapUserAndGroupConfigurationResponse) resource.put( null, null, null, response );
  119. Assert.assertEquals( userGroupConf, result.getData() );
  120. // now how about that get
  121. result = (LdapUserAndGroupConfigurationResponse) resource.get( null, null, null, null );
  122. Assert.assertEquals( userGroupConf, result.getData() );
  123. this.validateConfigFile( userGroupConf );
  124. }
  125. /*
  126. * (non-Javadoc)
  127. * @see com.sonatype.nexus.AbstractNexusTestCase#customizeContext(org.codehaus.plexus.context.Context)
  128. */
  129. @Override
  130. protected void customizeContext( Context ctx )
  131. {
  132. super.customizeContext( ctx );
  133. ctx.put( LDAP_CONFIGURATION_KEY, CONF_HOME.getAbsolutePath() + "/no-conf/" );
  134. }
  135. public void tearDown()
  136. throws Exception
  137. {
  138. super.tearDown();
  139. // delete the ldap.xml file
  140. File confFile = new File( CONF_HOME.getAbsolutePath() + "/no-conf/", "ldap.xml" );
  141. confFile.delete();
  142. }
  143. }