Skip to content

Commit

Permalink
Fix more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amontenegro committed Dec 11, 2024
1 parent 82a2a66 commit 2f55433
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ public Response viewRecord(String orcid) {
ActivityUtils.cleanEmptyFields(record.getActivitiesSummary());
sourceUtils.setSourceName(record.getActivitiesSummary());
}
if(record.getPerson() != null && record.getPerson().getEmails() != null) {
processProfessionalEmails(record.getPerson().getEmails());
}
ElementUtils.setPathToRecord(record, orcid);
Api2_0_LastModifiedDatesHelper.calculateLastModified(record);
return Response.ok(record).build();
Expand Down Expand Up @@ -1066,6 +1069,7 @@ public Response viewPersonalDetails(String orcid) {
public Response viewPerson(String orcid) {
Person person = personDetailsManagerReadOnly.getPersonDetails(orcid);
orcidSecurityManager.checkAndFilter(orcid, person);
processProfessionalEmails(person.getEmails());
ElementUtils.setPathToPerson(person, orcid);
Api2_0_LastModifiedDatesHelper.calculateLastModified(person);
sourceUtils.setSourceName(person);
Expand Down Expand Up @@ -1114,6 +1118,9 @@ private void validateSearchParams(Map<String, List<String>> queryMap) {
}

private void processProfessionalEmails(Emails emails) {
if(emails == null || emails.getEmails() == null) {
return;
}
for (Email email : emails.getEmails()) {
if (email.isVerified()) {
String domain = email.getEmail().split("@")[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -69,13 +70,51 @@ public void testViewEmailsWrongToken() {
}

@Test
public void testViewEmailsReadPublic() {
public void testViewEmailsReadPublic_withSourceClient() {
SecurityContextTestUtils.setUpSecurityContextForClientOnly("APP-5555555555555555", ScopePathType.READ_PUBLIC);
Response r = serviceDelegator.viewEmails(ORCID);
Emails element = (Emails) r.getEntity();
assertNotNull(element);
assertEquals("/0000-0000-0000-0003/email", element.getPath());
Utils.assertIsPublicOrSource(element, "APP-5555555555555555");
assertEquals(4, element.getEmails().size());

List<String> emails = new ArrayList<>();
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");

for(Email e : element.getEmails()) {
if(!emails.contains(e.getEmail())) {
fail(e.getEmail() + " is not in the email list");
}
emails.remove(e.getEmail());
}

assertTrue(emails.isEmpty());
}

@Test
public void testViewEmailsReadPublic_withOtherClient() {
SecurityContextTestUtils.setUpSecurityContextForClientOnly("APP-5555555555555556", ScopePathType.READ_PUBLIC);
Response r = serviceDelegator.viewEmails(ORCID);
Emails element = (Emails) r.getEntity();
assertNotNull(element);
assertEquals("/0000-0000-0000-0003/email", element.getPath());
assertEquals(2, element.getEmails().size());

List<String> emails = new ArrayList<>();
emails.add("[email protected]");
emails.add("[email protected]");

for(Email e : element.getEmails()) {
if(!emails.contains(e.getEmail())) {
fail(e.getEmail() + " is not in the email list");
}
emails.remove(e.getEmail());
}

assertTrue(emails.isEmpty());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void testViewPersonReadPublic() {
assertNotNull(element);
assertEquals("/0000-0000-0000-0003/person", element.getPath());
Utils.assertIsPublicOrSource(element, "APP-5555555555555555");
fail("TEST EMAILS");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void testViewRecordWrongScope() {
Utils.assertIsPublicOrSource(record.getActivitiesSummary(), SecurityContextTestUtils.DEFAULT_CLIENT_ID);
assertEquals("/0000-0000-0000-0003/person", record.getPerson().getPath());
Utils.assertIsPublicOrSource(record.getPerson(), SecurityContextTestUtils.DEFAULT_CLIENT_ID);

}

@Test
Expand All @@ -115,6 +116,7 @@ public void testViewRecordReadPublic() {
Utils.assertIsPublicOrSource(record.getActivitiesSummary(), "APP-5555555555555555");
assertEquals("/0000-0000-0000-0003/person", record.getPerson().getPath());
Utils.assertIsPublicOrSource(record.getPerson(), "APP-5555555555555555");
fail("TEST EMAILS");
}

@Test(expected = OrcidUnauthorizedException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ public static void assertIsPublicOrSource(Person p, String sourceId) {
}
assertIsPublicOrSource(p.getAddresses(), sourceId);
assertIsPublicOrSource(p.getBiography(), sourceId);
assertIsPublicOrSource(p.getEmails(), sourceId);
assertIsPublicOrSource(p.getExternalIdentifiers(), sourceId);
assertIsPublicOrSource(p.getKeywords(), sourceId);
assertIsPublicOrSource(p.getName(), sourceId);
assertIsPublicOrSource(p.getOtherNames(), sourceId);
assertIsPublicOrSource(p.getResearcherUrls(), sourceId);
// EMAILS ARE NOT TESTED! Source on professional emails is overwritten, so, you must verify the email on each test!
}

public static void verifyLastModified(LastModifiedDate l) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ public Person getPersonDetails(String orcid) {
if (emails.getEmails() != null) {
Emails filteredEmails = new Emails();
filteredEmails.setEmails(new ArrayList<Email>(emails.getEmails().stream().filter(e -> e.isVerified()).collect(Collectors.toList())));
processProfessionalEmails(filteredEmails);
person.setEmails(filteredEmails);
}
return person;
Expand Down Expand Up @@ -201,35 +200,9 @@ public Person getPublicPersonDetails(String orcid) {
if (emails.getEmails() != null) {
Emails filteredEmails = new Emails();
filteredEmails.setEmails(new ArrayList<Email>(emails.getEmails()));
processProfessionalEmails(filteredEmails);
person.setEmails(filteredEmails);
}

return person;
}

private void processProfessionalEmails(Emails emails) {
for (Email email : emails.getEmails()) {
if (email.isVerified()) {
String domain = email.getEmail().split("@")[1];
List<EmailDomainEntity> domainsInfo = emailDomainManager.findByEmailDomain(domain);
String category = EmailDomainEntity.DomainCategory.UNDEFINED.name();
// Set appropriate source name and source id for professional emails
if (domainsInfo != null) {
for(EmailDomainEntity domainInfo: domainsInfo) {
category = domainInfo.getCategory().name();
if(StringUtils.equalsIgnoreCase(category, EmailDomainEntity.DomainCategory.PROFESSIONAL.name())) {
break;
}
}
if(StringUtils.equalsIgnoreCase(category, EmailDomainEntity.DomainCategory.PROFESSIONAL.name())) {
if(email.getSource() == null) {
email.setSource(new Source());
}
email.setSource(sourceEntityUtils.convertEmailSourceToOrcidValidator(email.getSource()));
}
}
}
}
}
}

0 comments on commit 2f55433

Please sign in to comment.