aspectNames = person.getAspectNames();
assertTrue(aspectNames.contains("cm:auditable"));
// Newly added aspects
assertTrue(aspectNames.contains("cm:dublincore"));
assertTrue(aspectNames.contains("cm:summarizable"));
}
// Remove a property by setting it to null
{
Person person = createTestUpdatePerson();
person = people.update(person.getId(), qjson("{`properties`: {`cm:title`: null}}"), 200);
assertFalse(person.getProperties().containsKey("cm:title"));
// The aspect will still be there, I don't think we can easily remove the aspect automatically
// just because the associated properties have all been removed.
assertTrue(person.getAspectNames().contains("cm:titled"));
}
}
/**
* Simple helper to make JSON literals a little easier to read in test code,
* by allowing values that would normally be quoted with double quotes, to be
* quoted with backticks instead.
*
* Double and single quotes may still be used as normal, if required.
*
* @param raw The untreated JSON string to munge
* @return JSON String with backticks replaced with double quotes.
*/
private String qjson(String raw)
{
return raw.replace("`", "\"");
}
public static class PersonJSONSerializer implements JSONAble
{
private final Person personUpdate;
public PersonJSONSerializer(Person personUpdate)
{
this.personUpdate = personUpdate;
}
@Override
public JSONObject toJSON()
{
JSONObject personJson = new JSONObject();
if (personUpdate.getUserName() != null)
{
personJson.put("id", personUpdate.getUserName());
}
personJson.put("firstName", personUpdate.getFirstName());
personJson.put("lastName", personUpdate.getLastName());
personJson.put("description", personUpdate.getDescription());
personJson.put("email", personUpdate.getEmail());
personJson.put("skypeId", personUpdate.getSkypeId());
personJson.put("googleId", personUpdate.getGoogleId());
personJson.put("instantMessageId", personUpdate.getInstantMessageId());
personJson.put("jobTitle", personUpdate.getJobTitle());
personJson.put("location", personUpdate.getLocation());
org.alfresco.rest.api.model.Company co = personUpdate.getCompany();
if (co == null)
{
co = new org.alfresco.rest.api.model.Company();
}
personJson.put("company", new Company(co).toJSON());
personJson.put("mobile", personUpdate.getMobile());
personJson.put("telephone", personUpdate.getTelephone());
personJson.put("userStatus", personUpdate.getUserStatus());
personJson.put("enabled", personUpdate.isEnabled());
personJson.put("emailNotificationsEnabled", personUpdate.isEmailNotificationsEnabled());
personJson.put("password", personUpdate.getPassword());
personJson.put("properties", personUpdate.getProperties());
personJson.put("aspectNames", personUpdate.getAspectNames());
return personJson;
}
}
@Test
public void testUpdatePersonAuthenticationFailed() throws PublicApiException
{
final String personId = account2PersonIt.next();
publicApiClient.setRequestContext(new RequestContext(account1.getId(), personId));
people.update("people", personId, null, null, "{\n" + " \"firstName\": \"Updated firstName\"\n" + "}", null, "Expected 401 response when updating " + personId, 401);
}
@Test
public void testUpdatePersonNonSelfAndNonAdminDisallowed() throws PublicApiException
{
// TODO: this is bad, it seems that the test fixture isn't unique per test!?
final String personId = account1PersonIt.next();
final String personToUpdateId = account1PersonIt.next();
publicApiClient.setRequestContext(new RequestContext(account1.getId(), personId));
people.update(personToUpdateId, qjson("{ `firstName`:`Updated firstName` }"), 403);
// TODO: temp fix, set back to orig firstName
publicApiClient.setRequestContext(new RequestContext(account1.getId(), account1Admin, "admin"));
people.update(personToUpdateId, qjson("{ `firstName`:`Bob` }"), 200);
}
@Test
public void testUpdatePersonCanUpdateThemself() throws PublicApiException
{
final String personId = account1PersonIt.next();
publicApiClient.setRequestContext(new RequestContext(account1.getId(), personId));
Person updatedPerson = people.update(personId, qjson("{ `firstName`: `Updated firstName` }"), 200);
assertEquals("Updated firstName", updatedPerson.getFirstName());
// TODO: temp fix, set back to orig firstName
publicApiClient.setRequestContext(new RequestContext(account1.getId(), account1Admin, "admin"));
people.update(personId, qjson("{ `firstName`:`Bill` }"), 200);
}
@Test
public void testUpdatePersonNonexistentPerson() throws PublicApiException
{
final String personId = "non-existent";
publicApiClient.setRequestContext(new RequestContext(account3.getId(), account3Admin, "admin"));
people.update("people", personId, null, null, "{\n" + " \"firstName\": \"Updated firstName\"\n" + "}", null, "Expected 404 response when updating " + personId, 404);
}
@Test
public void testUpdatePersonUsingPartialUpdate() throws PublicApiException
{
final String personId = account3PersonIt.next();
publicApiClient.setRequestContext(new RequestContext(account3.getId(), account3Admin, "admin"));
String updatedFirstName = "Updated firstName";
HttpResponse response = people.update("people", personId, null, null, "{\n" + " \"firstName\": \"" + updatedFirstName + "\"\n" + "}", null,
"Expected 200 response when updating " + personId, 200);
Person updatedPerson = Person.parsePerson((JSONObject) response.getJsonResponse().get("entry"));
assertEquals(updatedFirstName, updatedPerson.getFirstName());
}
@Test
public void testUpdatePersonWithRestrictedResponseFields() throws PublicApiException
{
final String personId = account3PersonIt.next();
publicApiClient.setRequestContext(new RequestContext(account3.getId(), account3Admin, "admin"));
String updatedFirstName = "Updated firstName";
Map params = new HashMap<>();
params.put("fields", "id,firstName");
HttpResponse response = people.update("people", personId, null, null, "{\n" + " \"firstName\": \"" + updatedFirstName + "\"\n" + "}", params,
"Expected 200 response when updating " + personId, 200);
Person updatedPerson = Person.parsePerson((JSONObject) response.getJsonResponse().get("entry"));
assertNotNull(updatedPerson.getId());
assertEquals(updatedFirstName, updatedPerson.getFirstName());
assertNull(updatedPerson.getEmail());
}
@Test
public void testUpdatePersonUpdate() throws Exception
{
final String personId = account3PersonIt.next();
publicApiClient.setRequestContext(new RequestContext(account3.getId(), account3Admin, "admin"));
String firstName = "updatedFirstName";
String lastName = "updatedLastName";
String description = "updatedDescription";
String email = "updated@example.com";
String skypeId = "updated.skype.id";
String googleId = "googleId";
String instantMessageId = "updated.user@example.com";
String jobTitle = "updatedJobTitle";
String location = "updatedLocation";
Company company = new Company("updatedOrganization", "updatedAddress1", "updatedAddress2", "updatedAddress3", "updatedPostcode", "updatedTelephone", "updatedFax", "updatedEmail");
String mobile = "mobile";
String telephone = "telephone";
String userStatus = "userStatus";
Boolean enabled = true;
Boolean emailNotificationsEnabled = false;
Map params = new HashMap<>();
params.put("fields", "id,firstName,lastName,description,avatarId,email,skypeId,googleId,instantMessageId,jobTitle,location,mobile,telephone,userStatus,emailNotificationsEnabled,enabled,company");
HttpResponse response = people.update("people", personId, null, null,
"{\n"
+ " \"firstName\": \"" + firstName + "\",\n"
+ " \"lastName\": \"" + lastName + "\",\n"
+ " \"description\": \"" + description + "\",\n"
+ " \"email\": \"" + email + "\",\n"
+ " \"skypeId\": \"" + skypeId + "\",\n"
+ " \"googleId\": \"" + googleId + "\",\n"
+ " \"instantMessageId\": \"" + instantMessageId + "\",\n"
+ " \"jobTitle\": \"" + jobTitle + "\",\n"
+ " \"location\": \"" + location + "\",\n"
+ " \"company\": {\n"
+ " \"organization\": \"" + company.getOrganization() + "\",\n"
+ " \"address1\": \"" + company.getAddress1() + "\",\n"
+ " \"address2\": \"" + company.getAddress2() + "\",\n"
+ " \"address3\": \"" + company.getAddress3() + "\",\n"
+ " \"postcode\": \"" + company.getPostcode() + "\",\n"
+ " \"telephone\": \"" + company.getTelephone() + "\",\n"
+ " \"fax\": \"" + company.getFax() + "\",\n"
+ " \"email\": \"" + company.getEmail() + "\"\n"
+ " },\n"
+ " \"mobile\": \"" + mobile + "\",\n"
+ " \"telephone\": \"" + telephone + "\",\n"
+ " \"userStatus\": \"" + userStatus + "\",\n"
+ " \"emailNotificationsEnabled\": \"" + emailNotificationsEnabled + "\",\n"
+ " \"enabled\": \"" + enabled + "\"\n"
+ "}", params,
"Expected 200 response when updating " + personId, 200);
Person updatedPerson = Person.parsePerson((JSONObject) response.getJsonResponse().get("entry"));
assertNotNull(updatedPerson.getId());
assertEquals(firstName, updatedPerson.getFirstName());
assertEquals(lastName, updatedPerson.getLastName());
assertEquals(description, updatedPerson.getDescription());
assertEquals(email, updatedPerson.getEmail());
assertEquals(skypeId, updatedPerson.getSkypeId());
assertEquals(googleId, updatedPerson.getGoogleId());
assertEquals(instantMessageId, updatedPerson.getInstantMessageId());
assertEquals(jobTitle, updatedPerson.getJobTitle());
assertEquals(location, updatedPerson.getLocation());
assertNotNull(updatedPerson.getCompany());
company.expected(updatedPerson.getCompany());
assertEquals(mobile, updatedPerson.getMobile());
assertEquals(telephone, updatedPerson.getTelephone());
assertEquals(userStatus, updatedPerson.getUserStatus());
assertEquals(emailNotificationsEnabled, updatedPerson.isEmailNotificationsEnabled());
assertEquals(enabled, updatedPerson.isEnabled());
}
@Test
public void testUpdatePersonEnabledNonAdminNotAllowed() throws PublicApiException
{
final String personId = account3PersonIt.next();
publicApiClient.setRequestContext(new RequestContext(account3.getId(), personId));
people.update("people", personId, null, null, "{\n" + " \"enabled\": \"false\"\n" + "}", null, "Expected 403 response when updating " + personId, 403);
}
@Test
public void testUpdatePersonEnabled() throws PublicApiException
{
// Non-admin user ID
final String personId = account3PersonIt.next();
// Use admin user credentials
publicApiClient.setRequestContext(new RequestContext(account3.getId(), account3Admin, "admin"));
// Admin can toggle enabled flag: false
{
Boolean enabled = false;
Map params = Collections.singletonMap("fields", "enabled");
Person updatedPerson = people.update(personId, qjson("{`enabled`:"+enabled+"}"), params, 200);
assertEquals(enabled, updatedPerson.isEnabled());
}
// Admin can toggle enabled flag: true
{
Boolean enabled = true;
Map params = Collections.singletonMap("fields", "enabled");
Person updatedPerson = people.update(personId, qjson("{`enabled`:"+enabled+"}"), params, 200);
assertEquals(enabled, updatedPerson.isEnabled());
}
// Use non-admin user's own credentials
publicApiClient.setRequestContext(new RequestContext(account3.getId(), personId, "password"));
// Non-admin cannot set enabled flag
{
boolean origEnabled = people.getPerson(personId).isEnabled();
Boolean enabled = false;
// The test should change that we can't change this, otherwise it isn't effective
assertNotEquals(origEnabled, enabled);
Map params = Collections.singletonMap("fields", "enabled");
people.update(personId, qjson("{`enabled`:"+enabled+"}"), params, 403);
Person me = people.getPerson(personId);
assertEquals("Enabled state shouldn't have changed, but did", origEnabled, me.isEnabled());
}
}
@Test
public void testUpdatePersonAdminCannotBeDisabled() throws PublicApiException
{
publicApiClient.setRequestContext(new RequestContext(account3.getId(), account3Admin, "admin"));
Map params = new HashMap<>();
params.put("fields", "enabled");
people.update("people", account3Admin, null, null, "{\n" + " \"enabled\": \"" + false + "\"\n" + "}", params, "Expected 403 response when updating " + account3Admin, 403);
}
@Test
public void testUpdatePersonPasswordByThemself() throws PublicApiException
{
publicApiClient.setRequestContext(new RequestContext(account1.getId(), account1Admin, "admin"));
Person me = new Person();
me.setId(UUID.randomUUID().toString()+"@"+account1.getId());
me.setUserName(me.getId());
me.setFirstName("Jo");
me.setEmail(me.getId());
me.setEnabled(true);
me.setPassword("password123");
me = people.create(me);
publicApiClient.setRequestContext(new RequestContext(account1.getId(), me.getId(), "password123"));
// update with correct oldPassword
people.update(me.getId(), qjson("{ `oldPassword`:`password123`, `password`:`newpassword456` }"), 200);
// The old password should no longer work - therefore they are "unauthorized".
publicApiClient.setRequestContext(new RequestContext(account1.getId(), me.getId(), "password123"));
people.getPerson(me.getId(), 401);
// The new password should work.
publicApiClient.setRequestContext(new RequestContext(account1.getId(), me.getId(), "newpassword456"));
people.getPerson(me.getId());
// update with wrong oldPassword
people.update(me.getId(), qjson("{ `oldPassword`:`password123`, `password`:`newpassword456` }"), 403);
// update with no oldPassword
people.update(me.getId(), qjson("{ `password`:`newpassword456` }"), 403);
}
@Test
public void testUpdatePersonPasswordByAdmin() throws PublicApiException
{
final String personId = account3PersonIt.next();
final String networkId = account3.getId();
publicApiClient.setRequestContext(new RequestContext(networkId, account3Admin, "admin"));
String invalidPassword = "invalidPassword";
String updatedPassword = "newPassword";
people.update("people", personId, null, null, "{\n" + " \"password\": \"" + updatedPassword + "\"\n" + "}", null,
"Expected 200 response when updating " + personId, 200);
publicApiClient.setRequestContext(new RequestContext(networkId, personId, invalidPassword));
try
{
this.people.getPerson(personId);
fail("");
}
catch (PublicApiException e)
{
assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
}
publicApiClient.setRequestContext(new RequestContext(networkId, personId, updatedPassword));
this.people.getPerson(personId);
}
@Test
public void testUpdatePersonWithNotUpdatableFields() throws PublicApiException
{
final String personId = account3PersonIt.next();
publicApiClient.setRequestContext(new RequestContext(account3.getId(), account3Admin, "admin"));
List> notUpdatableFields = new ArrayList<>();
notUpdatableFields.add(new Pair("userName", "userName"));
notUpdatableFields.add(new Pair("avatarId", "avatarId"));
notUpdatableFields.add(new Pair("statusUpdatedAt", "statusUpdatedAt"));
notUpdatableFields.add(new Pair("quota", "quota"));
notUpdatableFields.add(new Pair("quotaUsed", "quotaUsed"));
for (Pair notUpdatableField : notUpdatableFields)
{
people.update("people", personId, null, null, "{\n" + "\"" + notUpdatableField.getFirst() + "\": \"" + notUpdatableField.getSecond() + "\"\n" + "}", null,
"Expected 400 response when updating " + personId, 400);
}
}
private PublicApiClient.ListResponse listPeople(final PublicApiClient.Paging paging, String sortColumn, boolean asc, int statusCode) throws Exception
{
// sort params
final Map params = new HashMap<>();
if (sortColumn != null)
{
params.put("orderBy", sortColumn + " " + (asc ? "ASC" : "DESC"));
}
return listPeople(createParams(paging, params), statusCode);
}
private PublicApiClient.ListResponse listPeople(Map parameters, int expectedStatusCode) throws PublicApiException
{
HttpResponse response = people.getAll("people", null, null, null, parameters, "Failed to get people", expectedStatusCode);
JSONObject jsonList = (JSONObject) response.getJsonResponse().get("list");
if (jsonList == null)
{
return null;
}
return Person.parsePeople(response.getJsonResponse());
}
@Test
public void testListPeopleWithAspectNamesAndProperties() throws PublicApiException
{
initializeContextForGetPeople();
// Are aspectNames and properties left absent when not required?
{
PublicApiClient.ListResponse resp = listPeople(Collections.emptyMap(), 200);
assertNull(resp.getList().get(0).getAspectNames());
assertNull(resp.getList().get(0).getProperties());
}
// Are aspectNames and properties populated when requested?
{
Map parameters = Collections.singletonMap("include", "aspectNames,properties");
PublicApiClient.ListResponse resp = listPeople(parameters, 200);
Person alice = resp.getList().stream().
filter(p -> p.getUserName().equals("alice@"+account4.getId()))
.findFirst().get();
assertNotNull(alice.getAspectNames());
assertTrue(alice.getAspectNames().contains("cm:titled"));
assertNotNull(alice.getProperties());
assertEquals("Alice through the REST API", alice.getProperties().get("cm:title"));
}
}
/**
* Tests the capability to sort and paginate the list of people orderBy =
* firstName ASC skip = 1, count = 3
*
* @throws Exception
*/
@Test
public void testPagingAndSortingByFirstNameAsc() throws Exception
{
initializeContextForGetPeople();
// paging
int skipCount = 1;
int maxItems = 3;
int totalResults = 5;
PublicApiClient.Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults);
// orderBy=firstName ASC
PublicApiClient.ListResponse resp = listPeople(paging, "firstName", true, 200);
List expectedList = new LinkedList<>();
expectedList.add(personAlice);
expectedList.add(personAliceD);
expectedList.add(personBen);
checkList(expectedList, paging.getExpectedPaging(), resp);
}
/**
* Tests the capability to sort and paginate the list of people orderBy =
* firstName DESC skip = 1, count = 3
*
* @throws Exception
*/
@Test
public void testPagingAndSortingByFirstNameDesc() throws Exception
{
initializeContextForGetPeople();
// paging
int skipCount = 1;
int maxItems = 3;
int totalResults = 5;
PublicApiClient.Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults);
// orderBy=firstName DESC
PublicApiClient.ListResponse resp = listPeople(paging, "firstName", false, 200);
List expectedList = new LinkedList<>();
expectedList.add((Person) personBen);
expectedList.add((Person) personAlice);
expectedList.add((Person) personAliceD);
checkList(expectedList, paging.getExpectedPaging(), resp);
}
/**
* Tests the capability paginate the list of people verifies default
* sorting, skip = 1, count = 3
*
* @throws Exception
*/
@Test
public void testPagingAndDefaultSorting() throws Exception
{
initializeContextForGetPeople();
// paging
int skipCount = 1;
int maxItems = 3;
int totalResults = 5;
PublicApiClient.Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults);
PublicApiClient.ListResponse resp = listPeople(paging, null, false, 200);
List expectedList = new LinkedList<>();
expectedList.add((Person) personAlice);
expectedList.add((Person) personAliceD);
expectedList.add((Person) personBen);
checkList(expectedList, paging.getExpectedPaging(), resp);
}
/**
* Tests the capability to sort and paginate the list of people orderBy =
* username DESC skip = 1, count = 3
*
* @throws Exception
*/
@Test
public void testPagingAndSortingByIdDesc() throws Exception
{
initializeContextForGetPeople();
// paging
int skipCount = 1;
int maxItems = 3;
int totalResults = 5;
PublicApiClient.Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults);
// orderBy=userName DESC
PublicApiClient.ListResponse resp = listPeople(paging, "id", false, 200);
List expectedList = new LinkedList<>();
expectedList.add((Person) personBen);
expectedList.add((Person) personAliceD);
expectedList.add((Person) personAlice);
checkList(expectedList, paging.getExpectedPaging(), resp);
}
/**
* Tests the capability to sort and paginate the list of people orderBy =
* invalid sort key ASC skip = 1, count = 3
*
* @throws Exception
*/
@Test
public void testPagingAndSortingByInvalidSortKey() throws Exception
{
initializeContextForGetPeople();
// paging
int skipCount = 1;
int maxItems = 3;
int totalResults = 5;
PublicApiClient.Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults);
// orderBy=invalidSortKey ASC
listPeople(paging, "invalidSortKey", true, 400);
}
/**
* Tests the capability to sort and paginate the list of people orderBy =
* lastName ASC skip = 2, count = 3
*
* @throws Exception
*/
@Test
public void testPagingAndSortingByLastName() throws Exception
{
initializeContextForGetPeople();
// paging
int skipCount = 2;
int maxItems = 3;
int totalResults = 5;
PublicApiClient.Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults);
// orderBy=lastName ASC
PublicApiClient.ListResponse resp = listPeople(paging, "lastName", true, 200);
List expectedList = new LinkedList<>();
expectedList.add((Person) personBen);
expectedList.add((Person) personAliceD);
expectedList.add((Person) personAlice);
checkList(expectedList, paging.getExpectedPaging(), resp);
}
/**
* Tests the capability to sort and paginate the list of people orderBy =
* both firstName and lastName ASC skip = 1, count = 3
*
* @throws Exception
*/
@Test
public void testPagingAndSortingByFirstNameAndLastName() throws Exception
{
initializeContextForGetPeople();
// paging
int skipCount = 1;
int maxItems = 3;
int totalResults = 5;
PublicApiClient.Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults);
// orderBy=firstName,lastName ASC
PublicApiClient.ListResponse resp = listPeople(paging, "firstName,lastName", true, 200);
List expectedList = new LinkedList<>();
expectedList.add((Person) personAliceD);
expectedList.add((Person) personAlice);
expectedList.add((Person) personBen);
checkList(expectedList, paging.getExpectedPaging(), resp);
}
private void initializeContextForGetPeople() throws PublicApiException
{
publicApiClient.setRequestContext(new RequestContext(account4.getId(), account4Admin, "admin"));
personAlice = new Person();
personAlice.setUserName("alice@" + account4.getId());
personAlice.setId("alice@" + account4.getId());
personAlice.setFirstName("Alice");
personAlice.setLastName("Smith");
personAlice.setEmail("alison.smith@example.com");
personAlice.setPassword("password");
personAlice.setEnabled(true);
personAlice.setProperties(Collections.singletonMap("cm:title", "Alice through the REST API"));
people.create(personAlice);
publicApiClient.setRequestContext(new RequestContext(account4.getId(), account4Admin, "admin"));
personAliceD = new Person();
personAliceD.setUserName("aliced@" + account4.getId());
personAliceD.setId("aliced@" + account4.getId());
personAliceD.setFirstName("Alice");
personAliceD.setLastName("Davis");
personAliceD.setEmail("alison.davis@example.com");
personAliceD.setPassword("password");
personAliceD.setEnabled(true);
people.create(personAliceD);
personBen = new Person();
personBen.setUserName("ben@" + account4.getId());
personBen.setId("ben@" + account4.getId());
personBen.setFirstName("Ben");
personBen.setLastName("Carson");
personBen.setEmail("ben.smythe@example.com");
personBen.setPassword("password");
personBen.setEnabled(true);
people.create(personBen);
}
}