Skip to content

Commit

Permalink
UP-5002: Update setup (to include PersonFactory) for failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drewwills committed Mar 8, 2018
1 parent 4eab96c commit f7e77c2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apereo.portal.events.PortalEventFactoryImpl;
import org.apereo.portal.security.IPerson;
import org.apereo.portal.security.IPersonManager;
import org.apereo.portal.security.PersonFactory;
import org.apereo.portal.security.provider.PersonImpl;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -47,6 +48,9 @@ public class SessionRESTControllerTest {

@Before
public void setup() {
PersonFactory fac = new PersonFactory();
fac.init();

sessionRESTController = new SessionRESTController();
res = new MockHttpServletResponse();
req = new MockHttpServletRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,28 @@
import org.springframework.stereotype.Component;

/**
* Creates a person.
* Responsible for creating {@link IPerson} instances. Historically, the capabilities of this class
* were accessed through static methods and constants, but with uP5 (and beyond) we need to
* configure this class through the <code>PropertySourcesPlaceholderConfigurer</code>. At present
* this class brideges both worlds, but in the future it would be good to move away from static
* methods.
*
* <p>Can create representations of a <i>system</i> user and a <i>guest</i> user.
* <p>Can create representations of a <i>system</i> as well as <i>guest</i> users.
*
* <p><i>system</i> users have an ID of 0
* <p>The <i>system</i> user has an ID of 0
*
* <p><i>guest</i> users have both of the following characteristics<br>
* <p><i>guest</i> users exhibit both of the following characteristics<br>
*
* <ol>
* <li>User is not successfully authenticated with the portal.
* <li>User name matches the value of the property <code>
* org.apereo.portal.security.PersonFactory.guest_user_name</code> in <code>portal.properties
* </code>.
* <li>User is not (successfully) authenticated with the portal.
* <li>Username is included in the list specified by the property <code>
* org.apereo.portal.security.PersonFactory.guest_user_names</code>.
* </ol>
*/
@Component
public class PersonFactory {

@Value("${org.apereo.portal.security.PersonFactory.guest_user_names:guest}")
private String guestUsernamesProperty;
private String guestUsernamesProperty = "guest"; // default; for unit tests

private static List<String> guestUsernames = null;

Expand Down Expand Up @@ -98,4 +100,15 @@ public static RestrictedPerson createRestrictedPerson() {
IPerson person = createPerson();
return new RestrictedPerson(person);
}

/**
* In addition to supporting the Spring context, this setter allows unit tests to bootstrap this
* class so that downstream features won't break.
*
* @since 5.0
*/
@Value("${org.apereo.portal.security.PersonFactory.guest_user_names:guest}")
public void setGuestUsernamesProperty(String guestUsernamesProperty) {
this.guestUsernamesProperty = guestUsernamesProperty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apereo.portal.groups.pags.testers.ValueExistsTester;
import org.apereo.portal.groups.pags.testers.ValueMissingTester;
import org.apereo.portal.security.IPerson;
import org.apereo.portal.security.PersonFactory;
import org.apereo.portal.security.provider.PersonImpl;

/** Tests the PAGS testers. */
Expand Down Expand Up @@ -86,6 +87,10 @@ private static void print(String msg) {
}

protected void setUp() {

PersonFactory fac = new PersonFactory();
fac.init();

try {
if (IPERSON_CLASS == null) {
IPERSON_CLASS = Class.forName("org.apereo.portal.security.IPerson");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@

import org.apereo.portal.groups.pags.TestPersonAttributesGroupTestDefinition;
import org.apereo.portal.security.IPerson;
import org.apereo.portal.security.PersonFactory;
import org.apereo.portal.security.provider.PersonImpl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class GuestUserTesterTest {

@Before
public void setUp() {
PersonFactory fac = new PersonFactory();
fac.init();
}

@Test
public void testGuestTrue() throws Exception {
GuestUserTester tester =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import java.util.List;
import java.util.Map;
import org.apereo.portal.security.IPerson;
import org.apereo.portal.security.PersonFactory;
import org.apereo.portal.security.provider.PersonImpl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
Expand All @@ -29,14 +31,20 @@
@RunWith(BlockJUnit4ClassRunner.class)
public class ServerNameGuestChainingProfileMapperTest {

private Map<String, String> configMap = new HashMap<String, String>();
private Map<String, String> configMap = new HashMap<>();

{
configMap.put("test-lycee.portail.ent", "lycees");
configMap.put("test-cfa.portail.ent", "cfa");
configMap.put("test.college.ent", "clg37");
}

@Before
public void setUp() {
PersonFactory fac = new PersonFactory();
fac.init();
}

@Test
public void testGuestAuthorizedDomain() throws Exception {
ServerNameGuestChainingProfileMapper profileMapper =
Expand Down Expand Up @@ -106,12 +114,12 @@ public void testNonGuestWithSubMapperMatching() throws Exception {
ServerNameGuestChainingProfileMapper profileMapper =
new ServerNameGuestChainingProfileMapper();
profileMapper.setAuthorizedServerNames(configMap);
List<IProfileMapper> subMappers = new ArrayList<IProfileMapper>();
List<IProfileMapper> subMappers = new ArrayList<>();
SessionAttributeProfileMapperImpl subMapper = new SessionAttributeProfileMapperImpl();
subMappers.add(subMapper);
subMapper.setDefaultProfileName("submapper_default");
subMapper.setAttributeName("session_attr");
Map<String, String> mapping = new HashMap<String, String>();
Map<String, String> mapping = new HashMap<>();
mapping.put("session_attr", "sub_fname");
subMapper.setMappings(mapping);
profileMapper.setSubMappers(subMappers);
Expand All @@ -132,12 +140,12 @@ public void testNonGuestWithSubMapperNotMatching() throws Exception {
ServerNameGuestChainingProfileMapper profileMapper =
new ServerNameGuestChainingProfileMapper();
profileMapper.setAuthorizedServerNames(configMap);
List<IProfileMapper> subMappers = new ArrayList<IProfileMapper>();
List<IProfileMapper> subMappers = new ArrayList<>();
SessionAttributeProfileMapperImpl subMapper = new SessionAttributeProfileMapperImpl();
subMappers.add(subMapper);
subMapper.setDefaultProfileName("submapper_default");
subMapper.setAttributeName("session_attr");
Map<String, String> mapping = new HashMap<String, String>();
Map<String, String> mapping = new HashMap<>();
mapping.put("session_attr", "sub_fname");
subMapper.setMappings(mapping);
profileMapper.setSubMappers(subMappers);
Expand All @@ -158,12 +166,12 @@ public void testGuestNonAuthorizedDomainWithSubMapperMatching() throws Exception
ServerNameGuestChainingProfileMapper profileMapper =
new ServerNameGuestChainingProfileMapper();
profileMapper.setAuthorizedServerNames(configMap);
List<IProfileMapper> subMappers = new ArrayList<IProfileMapper>();
List<IProfileMapper> subMappers = new ArrayList<>();
SessionAttributeProfileMapperImpl subMapper = new SessionAttributeProfileMapperImpl();
subMappers.add(subMapper);
subMapper.setDefaultProfileName("submapper_default");
subMapper.setAttributeName("session_attr");
Map<String, String> mapping = new HashMap<String, String>();
Map<String, String> mapping = new HashMap<>();
mapping.put("session_attr", "sub_fname");
subMapper.setMappings(mapping);
profileMapper.setSubMappers(subMappers);
Expand All @@ -184,12 +192,12 @@ public void testGuestAuthorizedDomainWithSubMapperMatching() throws Exception {
ServerNameGuestChainingProfileMapper profileMapper =
new ServerNameGuestChainingProfileMapper();
profileMapper.setAuthorizedServerNames(configMap);
List<IProfileMapper> subMappers = new ArrayList<IProfileMapper>();
List<IProfileMapper> subMappers = new ArrayList<>();
SessionAttributeProfileMapperImpl subMapper = new SessionAttributeProfileMapperImpl();
subMappers.add(subMapper);
subMapper.setDefaultProfileName("submapper_default");
subMapper.setAttributeName("session_attr");
Map<String, String> mapping = new HashMap<String, String>();
Map<String, String> mapping = new HashMap<>();
mapping.put("session_attr", "sub_fname");
subMapper.setMappings(mapping);
profileMapper.setSubMappers(subMappers);
Expand All @@ -210,12 +218,12 @@ public void testGuestAuthorizedDomainWithSubMapperNotMatchingWithSubDefault() th
ServerNameGuestChainingProfileMapper profileMapper =
new ServerNameGuestChainingProfileMapper();
profileMapper.setAuthorizedServerNames(configMap);
List<IProfileMapper> subMappers = new ArrayList<IProfileMapper>();
List<IProfileMapper> subMappers = new ArrayList<>();
SessionAttributeProfileMapperImpl subMapper = new SessionAttributeProfileMapperImpl();
subMappers.add(subMapper);
subMapper.setDefaultProfileName("submapper_default");
subMapper.setAttributeName("session_attr");
Map<String, String> mapping = new HashMap<String, String>();
Map<String, String> mapping = new HashMap<>();
mapping.put("session_attr", "sub_fname");
subMapper.setMappings(mapping);
profileMapper.setSubMappers(subMappers);
Expand All @@ -237,13 +245,13 @@ public void testGuestAuthorizedDomainWithSubMapperNotMatchingWithoutSubDefault()
ServerNameGuestChainingProfileMapper profileMapper =
new ServerNameGuestChainingProfileMapper();
profileMapper.setAuthorizedServerNames(configMap);
List<IProfileMapper> subMappers = new ArrayList<IProfileMapper>();
List<IProfileMapper> subMappers = new ArrayList<>();
SessionAttributeProfileMapperImpl subMapper = new SessionAttributeProfileMapperImpl();
subMappers.add(subMapper);
subMapper.setDefaultProfileName("submapper_default");
subMapper.setAttributeName("session_attr");
subMapper.setDefaultProfileName("");
Map<String, String> mapping = new HashMap<String, String>();
Map<String, String> mapping = new HashMap<>();
mapping.put("session_attr", "sub_fname");
subMapper.setMappings(mapping);
profileMapper.setSubMappers(subMappers);
Expand All @@ -259,14 +267,14 @@ public void testGuestAuthorizedDomainWithSubMapperNotMatchingWithoutSubDefault()
Assert.assertEquals("guest-clg37-default", fname);
}

protected static IPerson createGuestPerson() throws Exception {
private static IPerson createGuestPerson() throws Exception {
IPerson person = new PersonImpl();
person.setAttribute(IPerson.USERNAME, "guest");

return person;
}

protected static IPerson createPerson() throws Exception {
private static IPerson createPerson() throws Exception {
IPerson person = new PersonImpl();
person.setAttribute(IPerson.USERNAME, "non_guest");

Expand Down

0 comments on commit f7e77c2

Please sign in to comment.