dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #27103
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13443: Moved and generalized test support method for injecting user into security context
------------------------------------------------------------
revno: 13443
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-12-27 13:39:33 +0100
message:
Moved and generalized test support method for injecting user into security context
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java
dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/NoRegistrationSingleEventServiceTest.java
dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/RegistrationMultiEventsServiceTest.java
dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/RegistrationSingleEventServiceTest.java
dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java
dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisTest.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java 2013-10-30 12:51:03 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java 2013-12-27 12:39:33 +0000
@@ -42,6 +42,8 @@
*/
public interface IdentifiableObjectManager
{
+ final String ID = IdentifiableObjectManager.class.getName();
+
void save( IdentifiableObject object );
void update( IdentifiableObject object );
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/NoRegistrationSingleEventServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/NoRegistrationSingleEventServiceTest.java 2013-10-07 18:38:10 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/NoRegistrationSingleEventServiceTest.java 2013-12-27 12:39:33 +0000
@@ -56,6 +56,7 @@
import org.hisp.dhis.program.ProgramStageDataElementService;
import org.hisp.dhis.program.ProgramStageInstance;
import org.hisp.dhis.program.ProgramStageInstanceService;
+import org.hisp.dhis.user.UserService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -77,9 +78,6 @@
@Autowired
private ProgramStageInstanceService programStageInstanceService;
- @Autowired
- private IdentifiableObjectManager manager;
-
private OrganisationUnit organisationUnitA;
private DataElement dataElementA;
private Program programA;
@@ -88,19 +86,22 @@
@Override
protected void setUpTest() throws Exception
{
+ identifiableObjectManager = (IdentifiableObjectManager) getBean( IdentifiableObjectManager.ID );
+ userService = (UserService) getBean( UserService.ID );
+
organisationUnitA = createOrganisationUnit( 'A' );
- manager.save( organisationUnitA );
+ identifiableObjectManager.save( organisationUnitA );
dataElementA = createDataElement( 'A' );
dataElementA.setType( DataElement.VALUE_TYPE_INT );
- manager.save( dataElementA );
+ identifiableObjectManager.save( dataElementA );
programStageA = createProgramStage( 'A', 0 );
- manager.save( programStageA );
+ identifiableObjectManager.save( programStageA );
programA = createProgram( 'A', new HashSet<ProgramStage>(), organisationUnitA );
programA.setType( Program.SINGLE_EVENT_WITHOUT_REGISTRATION );
- manager.save( programA );
+ identifiableObjectManager.save( programA );
ProgramStageDataElement programStageDataElement = new ProgramStageDataElement();
programStageDataElement.setDataElement( dataElementA );
@@ -111,8 +112,8 @@
programStageA.setProgram( programA );
programA.getProgramStages().add( programStageA );
- manager.update( programStageA );
- manager.update( programA );
+ identifiableObjectManager.update( programStageA );
+ identifiableObjectManager.update( programA );
ProgramInstance programInstance = new ProgramInstance();
programInstance.setProgram( programA );
@@ -121,9 +122,9 @@
programInstanceService.addProgramInstance( programInstance );
programA.getProgramInstances().add( programInstance );
- manager.update( programA );
+ identifiableObjectManager.update( programA );
- createSuperuserAndInjectSecurityContext( 'A' );
+ createUserAndInjectSecurityContext( 'A', true );
// mocked format
I18nFormat mockFormat = mock( I18nFormat.class );
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/RegistrationMultiEventsServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/RegistrationMultiEventsServiceTest.java 2013-12-16 04:27:26 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/RegistrationMultiEventsServiceTest.java 2013-12-27 12:39:33 +0000
@@ -57,6 +57,7 @@
import org.hisp.dhis.program.ProgramStage;
import org.hisp.dhis.program.ProgramStageDataElement;
import org.hisp.dhis.program.ProgramStageDataElementService;
+import org.hisp.dhis.user.UserService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -78,9 +79,6 @@
@Autowired
private EnrollmentService enrollmentService;
- @Autowired
- private IdentifiableObjectManager manager;
-
private Patient maleA;
private Patient maleB;
@@ -115,20 +113,23 @@
protected void setUpTest()
throws Exception
{
+ identifiableObjectManager = (IdentifiableObjectManager) getBean( IdentifiableObjectManager.ID );
+ userService = (UserService) getBean( UserService.ID );
+
organisationUnitA = createOrganisationUnit( 'A' );
organisationUnitB = createOrganisationUnit( 'B' );
- manager.save( organisationUnitA );
- manager.save( organisationUnitB );
+ identifiableObjectManager.save( organisationUnitA );
+ identifiableObjectManager.save( organisationUnitB );
maleA = createPatient( 'A', organisationUnitA );
maleB = createPatient( 'B', organisationUnitB );
femaleA = createPatient( 'C', organisationUnitA );
femaleB = createPatient( 'D', organisationUnitB );
- manager.save( maleA );
- manager.save( maleB );
- manager.save( femaleA );
- manager.save( femaleB );
+ identifiableObjectManager.save( maleA );
+ identifiableObjectManager.save( maleB );
+ identifiableObjectManager.save( femaleA );
+ identifiableObjectManager.save( femaleB );
personMaleA = personService.getPerson( maleA );
personMaleB = personService.getPerson( maleB );
@@ -140,19 +141,19 @@
dataElementA.setType( DataElement.VALUE_TYPE_INT );
dataElementB.setType( DataElement.VALUE_TYPE_INT );
- manager.save( dataElementA );
- manager.save( dataElementB );
+ identifiableObjectManager.save( dataElementA );
+ identifiableObjectManager.save( dataElementB );
programStageA = createProgramStage( 'A', 0 );
programStageB = createProgramStage( 'B', 0 );
programStageB.setIrregular( true );
- manager.save( programStageA );
- manager.save( programStageB );
+ identifiableObjectManager.save( programStageA );
+ identifiableObjectManager.save( programStageB );
programA = createProgram( 'A', new HashSet<ProgramStage>(), organisationUnitA );
programA.setType( Program.MULTIPLE_EVENTS_WITH_REGISTRATION );
- manager.save( programA );
+ identifiableObjectManager.save( programA );
ProgramStageDataElement programStageDataElement = new ProgramStageDataElement();
programStageDataElement.setDataElement( dataElementA );
@@ -174,11 +175,11 @@
programA.getProgramStages().add( programStageA );
programA.getProgramStages().add( programStageB );
- manager.update( programStageA );
- manager.update( programStageB );
- manager.update( programA );
+ identifiableObjectManager.update( programStageA );
+ identifiableObjectManager.update( programStageB );
+ identifiableObjectManager.update( programA );
- createSuperuserAndInjectSecurityContext( 'A' );
+ createUserAndInjectSecurityContext( 'A', true );
// mocked format
I18nFormat mockFormat = mock( I18nFormat.class );
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/RegistrationSingleEventServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/RegistrationSingleEventServiceTest.java 2013-12-16 04:27:26 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/RegistrationSingleEventServiceTest.java 2013-12-27 12:39:33 +0000
@@ -59,6 +59,7 @@
import org.hisp.dhis.program.ProgramStage;
import org.hisp.dhis.program.ProgramStageDataElement;
import org.hisp.dhis.program.ProgramStageDataElementService;
+import org.hisp.dhis.user.UserService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -80,9 +81,6 @@
@Autowired
private EnrollmentService enrollmentService;
- @Autowired
- private IdentifiableObjectManager manager;
-
private Patient maleA;
private Patient maleB;
private Patient femaleA;
@@ -102,20 +100,23 @@
@Override
protected void setUpTest() throws Exception
{
+ identifiableObjectManager = (IdentifiableObjectManager) getBean( IdentifiableObjectManager.ID );
+ userService = (UserService) getBean( UserService.ID );
+
organisationUnitA = createOrganisationUnit( 'A' );
organisationUnitB = createOrganisationUnit( 'B' );
- manager.save( organisationUnitA );
- manager.save( organisationUnitB );
+ identifiableObjectManager.save( organisationUnitA );
+ identifiableObjectManager.save( organisationUnitB );
maleA = createPatient( 'A', organisationUnitA );
maleB = createPatient( 'B', organisationUnitB );
femaleA = createPatient( 'C', organisationUnitA );
femaleB = createPatient( 'D', organisationUnitB );
- manager.save( maleA );
- manager.save( maleB );
- manager.save( femaleA );
- manager.save( femaleB );
+ identifiableObjectManager.save( maleA );
+ identifiableObjectManager.save( maleB );
+ identifiableObjectManager.save( femaleA );
+ identifiableObjectManager.save( femaleB );
personMaleA = personService.getPerson( maleA );
personMaleB = personService.getPerson( maleB );
@@ -124,14 +125,14 @@
dataElementA = createDataElement( 'A' );
dataElementA.setType( DataElement.VALUE_TYPE_INT );
- manager.save( dataElementA );
+ identifiableObjectManager.save( dataElementA );
programStageA = createProgramStage( 'A', 0 );
- manager.save( programStageA );
+ identifiableObjectManager.save( programStageA );
programA = createProgram( 'A', new HashSet<ProgramStage>(), organisationUnitA );
programA.setType( Program.SINGLE_EVENT_WITH_REGISTRATION );
- manager.save( programA );
+ identifiableObjectManager.save( programA );
ProgramStageDataElement programStageDataElement = new ProgramStageDataElement();
programStageDataElement.setDataElement( dataElementA );
@@ -142,10 +143,10 @@
programStageA.setProgram( programA );
programA.getProgramStages().add( programStageA );
- manager.update( programStageA );
- manager.update( programA );
+ identifiableObjectManager.update( programStageA );
+ identifiableObjectManager.update( programA );
- createSuperuserAndInjectSecurityContext( 'A' );
+ createUserAndInjectSecurityContext( 'A', true );
// mocked format
I18nFormat mockFormat = mock( I18nFormat.class );
=== modified file 'dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java'
--- dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java 2013-12-20 22:53:53 +0000
+++ dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java 2013-12-27 12:39:33 +0000
@@ -31,6 +31,7 @@
import java.io.File;
import java.io.StringReader;
import java.lang.reflect.Method;
+import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
@@ -52,6 +53,7 @@
import org.hisp.dhis.aggregation.AggregatedOrgUnitDataValueService;
import org.hisp.dhis.chart.Chart;
import org.hisp.dhis.common.DimensionalObject;
+import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.concept.Concept;
import org.hisp.dhis.constant.Constant;
import org.hisp.dhis.constant.ConstantService;
@@ -110,6 +112,7 @@
import org.hisp.dhis.resourcetable.ResourceTableService;
import org.hisp.dhis.sqlview.SqlView;
import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserAuthorityGroup;
import org.hisp.dhis.user.UserCredentials;
import org.hisp.dhis.user.UserGroup;
import org.hisp.dhis.user.UserService;
@@ -120,6 +123,13 @@
import org.hisp.dhis.validation.ValidationRuleService;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.util.Assert;
import org.xml.sax.InputSource;
/**
@@ -195,6 +205,8 @@
protected UserService userService;
protected MessageService messageService;
+
+ protected IdentifiableObjectManager identifiableObjectManager;
static
{
@@ -1318,4 +1330,45 @@
return null;
}
}
+
+ // -------------------------------------------------------------------------
+ // Allow xpath testing of DXF2
+ // -------------------------------------------------------------------------
+
+ /**
+ * Creates a user and injects into the security context. Requires
+ * <code>identifiableObjectManager</code> and <code>userService</code> to be
+ * injected into the test.
+ *
+ * @param uniqueCharacter uniquey character for username.
+ * @param allAuth whether to grant ALL authority to user.
+ * @param auths authorities to grant to user.
+ * @return the user.
+ */
+ public User createUserAndInjectSecurityContext( char uniqueCharacter, boolean allAuth, String... auths )
+ {
+ Assert.notNull( identifiableObjectManager );
+ Assert.notNull( userService );
+
+ UserAuthorityGroup userAuthorityGroup = new UserAuthorityGroup();
+ userAuthorityGroup.setName( "Superuser" );
+ userAuthorityGroup.getAuthorities().add( "ALL" );
+ identifiableObjectManager.save( userAuthorityGroup );
+
+ User user = createUser( 'A' );
+ user.getUserCredentials().getUserAuthorityGroups().add( userAuthorityGroup );
+ userService.addUser( user );
+ user.getUserCredentials().setUser( user );
+ userService.addUserCredentials( user.getUserCredentials() );
+
+ List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
+ authorities.add( new SimpleGrantedAuthority( "ALL" ) );
+
+ UserDetails userDetails = new org.springframework.security.core.userdetails.User( "username", "password", authorities );
+
+ Authentication authentication = new UsernamePasswordAuthenticationToken( userDetails, "", authorities );
+ SecurityContextHolder.getContext().setAuthentication( authentication );
+
+ return user;
+ }
}
=== modified file 'dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisTest.java'
--- dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisTest.java 2013-09-30 12:00:52 +0000
+++ dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisTest.java 2013-12-27 12:39:33 +0000
@@ -29,16 +29,10 @@
*/
import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
-import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.dbms.DbmsManager;
-import org.hisp.dhis.user.User;
-import org.hisp.dhis.user.UserAuthorityGroup;
-import org.hisp.dhis.user.UserService;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
@@ -47,12 +41,6 @@
import org.springframework.context.ApplicationContextAware;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.orm.hibernate4.SessionHolder;
-import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
-import org.springframework.security.core.Authentication;
-import org.springframework.security.core.GrantedAuthority;
-import org.springframework.security.core.authority.SimpleGrantedAuthority;
-import org.springframework.security.core.context.SecurityContextHolder;
-import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@@ -187,36 +175,4 @@
SessionFactoryUtils.closeSession( sessionHolder.getSession() );
}
-
- private UserService _userService;
-
- private IdentifiableObjectManager _manager;
-
- public User createSuperuserAndInjectSecurityContext( char uniqueCharacter )
- {
- _userService = (UserService) getBean( "org.hisp.dhis.user.UserService" );
- _manager = (IdentifiableObjectManager) getBean( "org.hisp.dhis.common.IdentifiableObjectManager" );
-
- UserAuthorityGroup userAuthorityGroup = new UserAuthorityGroup();
- userAuthorityGroup.setName( "Superuser" );
- userAuthorityGroup.getAuthorities().add( "ALL" );
- _manager.save( userAuthorityGroup );
-
- User user = createUser( 'A' );
- user.getUserCredentials().getUserAuthorityGroups().add( userAuthorityGroup );
- _userService.addUser( user );
- user.getUserCredentials().setUser( user );
- _userService.addUserCredentials( user.getUserCredentials() );
-
- List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
- authorities.add( new SimpleGrantedAuthority( "ALL" ) );
-
- UserDetails userDetails = new org.springframework.security.core.userdetails.User( "username", "password", authorities );
-
- Authentication authentication = new UsernamePasswordAuthenticationToken( userDetails, "", authorities );
- SecurityContextHolder.getContext().setAuthentication( authentication );
-
- return user;
- }
-
}