PageRenderTime 30ms CodeModel.GetById 22ms app.highlight 5ms RepoModel.GetById 1ms 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/galak/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 */
 19package org.sonatype.nexus.security.ldap.realms.api;
 20
 21import java.io.File;
 22import java.io.FileInputStream;
 23import java.io.FileNotFoundException;
 24import java.io.IOException;
 25import java.io.InputStreamReader;
 26
 27import org.junit.Assert;
 28import org.junit.Test;
 29
 30import org.codehaus.plexus.context.Context;
 31import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 32import org.sonatype.nexus.AbstractNexusLdapTestCase;
 33import org.sonatype.nexus.security.ldap.realms.api.dto.LdapUserAndGroupConfigurationDTO;
 34import org.sonatype.nexus.security.ldap.realms.api.dto.LdapUserAndGroupConfigurationResponse;
 35import org.sonatype.plexus.rest.resource.PlexusResource;
 36
 37import org.sonatype.security.ldap.realms.persist.model.CUserAndGroupAuthConfiguration;
 38import org.sonatype.security.ldap.realms.persist.model.Configuration;
 39import org.sonatype.security.ldap.realms.persist.model.io.xpp3.LdapConfigurationXpp3Reader;
 40
 41public class LdapUserGroupConfNotConfiguredTest
 42    extends AbstractNexusLdapTestCase
 43{
 44
 45    private PlexusResource getResource()
 46        throws Exception
 47    {
 48        return this.lookup( PlexusResource.class, "LdapUserAndGroupsConfigurationPlexusResource" );
 49    }
 50
 51    @Test
 52    public void testGetNotConfigured()
 53        throws Exception
 54    {
 55        PlexusResource resource = getResource();
 56
 57        // none of these args are used, but if they start being used, we will need to change this.
 58        LdapUserAndGroupConfigurationResponse response =
 59            (LdapUserAndGroupConfigurationResponse) resource.get( null, null, null, null );
 60
 61        // the default configuration is returned.
 62        LdapUserAndGroupConfigurationDTO dto = response.getData();
 63        Assert.assertNotNull( dto );
 64
 65        Assert.assertEquals( "ou=groups", dto.getGroupBaseDn() );
 66        Assert.assertEquals( "cn", dto.getGroupIdAttribute() );
 67        Assert.assertEquals( "uniqueMember", dto.getGroupMemberAttribute() );
 68        Assert.assertEquals( "${username}", dto.getGroupMemberFormat() );
 69        Assert.assertEquals( "groupOfUniqueNames", dto.getGroupObjectClass() );
 70        Assert.assertEquals( "ou=people", dto.getUserBaseDn() );
 71        Assert.assertEquals( "uid", dto.getUserIdAttribute() );
 72        Assert.assertEquals( "inetOrgPerson", dto.getUserObjectClass() );
 73        Assert.assertNull( dto.getUserPasswordAttribute() );
 74        // Assert.assertEquals("userPassword", dto.getUserPasswordAttribute());
 75        Assert.assertEquals( "cn", dto.getUserRealNameAttribute() );
 76        Assert.assertEquals( "mail", dto.getEmailAddressAttribute() );
 77        Assert.assertNull( dto.getUserMemberOfAttribute() );
 78        Assert.assertTrue( dto.isLdapGroupsAsRoles() );
 79        Assert.assertFalse( dto.isGroupSubtree() );
 80        Assert.assertFalse( dto.isUserSubtree() );
 81    }
 82
 83    private void validateConfigFile( LdapUserAndGroupConfigurationDTO dto )
 84        throws Exception
 85    {
 86        String configFileName = CONF_HOME.getAbsolutePath() + "/no-conf" + "/ldap.xml";// this.getNexusLdapConfiguration();
 87
 88        LdapConfigurationXpp3Reader reader = new LdapConfigurationXpp3Reader();
 89        FileInputStream fis = new FileInputStream( configFileName );
 90        Configuration config = reader.read( fis );
 91
 92        CUserAndGroupAuthConfiguration userGroupConfig = config.getUserAndGroupConfig();
 93
 94        Assert.assertEquals( dto.getGroupBaseDn(), userGroupConfig.getGroupBaseDn() );
 95        Assert.assertEquals( dto.getGroupIdAttribute(), userGroupConfig.getGroupIdAttribute() );
 96        Assert.assertEquals( dto.getGroupMemberAttribute(), userGroupConfig.getGroupMemberAttribute() );
 97        Assert.assertEquals( dto.getGroupMemberFormat(), userGroupConfig.getGroupMemberFormat() );
 98        Assert.assertEquals( dto.getGroupObjectClass(), userGroupConfig.getGroupObjectClass() );
 99        Assert.assertEquals( dto.getUserBaseDn(), userGroupConfig.getUserBaseDn() );
100        Assert.assertEquals( dto.getUserIdAttribute(), userGroupConfig.getUserIdAttribute() );
101        Assert.assertEquals( dto.getUserObjectClass(), userGroupConfig.getUserObjectClass() );
102        Assert.assertEquals( dto.getUserPasswordAttribute(), userGroupConfig.getUserPasswordAttribute() );
103        Assert.assertEquals( dto.getUserRealNameAttribute(), userGroupConfig.getUserRealNameAttribute() );
104        Assert.assertEquals( dto.getEmailAddressAttribute(), userGroupConfig.getEmailAddressAttribute() );
105        Assert.assertEquals( dto.getUserMemberOfAttribute(), userGroupConfig.getUserMemberOfAttribute() );
106        Assert.assertEquals( dto.isLdapGroupsAsRoles(), userGroupConfig.isLdapGroupsAsRoles() );
107        Assert.assertEquals( dto.isGroupSubtree(), userGroupConfig.isGroupSubtree() );
108        Assert.assertEquals( dto.isUserSubtree(), userGroupConfig.isUserSubtree() );
109
110    }
111
112    @Test
113    public void testPutNotConfigured()
114        throws Exception
115    {
116        PlexusResource resource = getResource();
117
118        LdapUserAndGroupConfigurationResponse response = new LdapUserAndGroupConfigurationResponse();
119        LdapUserAndGroupConfigurationDTO userGroupConf = new LdapUserAndGroupConfigurationDTO();
120        response.setData( userGroupConf );
121        userGroupConf.setGroupMemberFormat( "uid=${username},ou=people,o=sonatype" );
122        userGroupConf.setGroupObjectClass( "groupOfUniqueNames" );
123        userGroupConf.setGroupBaseDn( "ou=groups" );
124        userGroupConf.setGroupIdAttribute( "cn" );
125        userGroupConf.setGroupMemberAttribute( "uniqueMember" );
126        userGroupConf.setUserObjectClass( "inetOrgPerson" );
127        userGroupConf.setUserBaseDn( "ou=people" );
128        userGroupConf.setUserIdAttribute( "uid" );
129        userGroupConf.setUserPasswordAttribute( "userPassword" );
130        userGroupConf.setUserRealNameAttribute( "cn" );
131        userGroupConf.setEmailAddressAttribute( "mail" );
132        userGroupConf.setGroupSubtree( false );
133        userGroupConf.setUserSubtree( true );
134
135        LdapUserAndGroupConfigurationResponse result =
136            (LdapUserAndGroupConfigurationResponse) resource.put( null, null, null, response );
137        Assert.assertEquals( userGroupConf, result.getData() );
138
139        // now how about that get
140        result = (LdapUserAndGroupConfigurationResponse) resource.get( null, null, null, null );
141        Assert.assertEquals( userGroupConf, result.getData() );
142
143        this.validateConfigFile( userGroupConf );
144    }
145
146    /*
147     * (non-Javadoc)
148     * @see com.sonatype.nexus.AbstractNexusTestCase#customizeContext(org.codehaus.plexus.context.Context)
149     */
150    @Override
151    protected void customizeContext( Context ctx )
152    {
153        super.customizeContext( ctx );
154
155        ctx.put( LDAP_CONFIGURATION_KEY, CONF_HOME.getAbsolutePath() + "/no-conf/" );
156    }
157
158    public void tearDown()
159        throws Exception
160    {
161        super.tearDown();
162
163        // delete the ldap.xml file
164        File confFile = new File( CONF_HOME.getAbsolutePath() + "/no-conf/", "ldap.xml" );
165        confFile.delete();
166    }
167
168}