diff --git a/member-service/src/main/java/org/orcid/memberportal/service/member/web/rest/MemberResource.java b/member-service/src/main/java/org/orcid/memberportal/service/member/web/rest/MemberResource.java index d42093334..3a05903ac 100644 --- a/member-service/src/main/java/org/orcid/memberportal/service/member/web/rest/MemberResource.java +++ b/member-service/src/main/java/org/orcid/memberportal/service/member/web/rest/MemberResource.java @@ -196,8 +196,7 @@ public ResponseEntity updateMemberDefaultLanguage(@PathVariable String sal public ResponseEntity updatePublicMemberDetails(@RequestBody MemberUpdateData memberUpdateData, @PathVariable String salesforceId) { LOG.info("REST request to update member public details for salesforce id {}", salesforceId); - if (StringUtils.isBlank(memberUpdateData.getPublicName())) { - LOG.info("Null name in request to update public details"); + if (!memberDetailsUpdateValid(memberUpdateData)) { return ResponseEntity.badRequest().build(); } try { @@ -388,4 +387,18 @@ public ResponseEntity requestRemoveConsortiumMember( memberService.requestRemoveConsortiumMember(removeConsortiumMember); return ResponseEntity.ok().build(); } + + private boolean memberDetailsUpdateValid(MemberUpdateData data) { + if (StringUtils.isBlank(data.getPublicName())) { + LOG.info("Null name in request to update public details"); + return false; + } + + // allow null billing address but if present, country must be specified + if (data.getBillingAddress() != null && StringUtils.isBlank(data.getBillingAddress().getCountry())) { + LOG.info("Null billing country in request to update public details"); + return false; + } + return true; + } } diff --git a/member-service/src/test/java/org/orcid/memberportal/service/member/web/rest/MemberResourceTest.java b/member-service/src/test/java/org/orcid/memberportal/service/member/web/rest/MemberResourceTest.java index 8b11afd4d..5c36f0d54 100644 --- a/member-service/src/test/java/org/orcid/memberportal/service/member/web/rest/MemberResourceTest.java +++ b/member-service/src/test/java/org/orcid/memberportal/service/member/web/rest/MemberResourceTest.java @@ -16,14 +16,7 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.orcid.memberportal.service.member.client.model.Country; -import org.orcid.memberportal.service.member.client.model.MemberContact; -import org.orcid.memberportal.service.member.client.model.MemberContacts; -import org.orcid.memberportal.service.member.client.model.MemberDetails; -import org.orcid.memberportal.service.member.client.model.MemberOrgId; -import org.orcid.memberportal.service.member.client.model.MemberOrgIds; -import org.orcid.memberportal.service.member.client.model.MemberUpdateData; -import org.orcid.memberportal.service.member.client.model.State; +import org.orcid.memberportal.service.member.client.model.*; import org.orcid.memberportal.service.member.domain.Member; import org.orcid.memberportal.service.member.services.MemberService; import org.orcid.memberportal.service.member.validation.MemberValidation; @@ -105,6 +98,24 @@ public void testUpdatePublicMemberDetails() throws UnauthorizedMemberAccessExcep Mockito.verify(memberService).updateMemberData(Mockito.any(MemberUpdateData.class), Mockito.eq("salesforceId")); } + @Test + public void testUpdatePublicMemberDetailsWithBillingAddress() throws UnauthorizedMemberAccessException { + Mockito.when(memberService.updateMemberData(Mockito.any(MemberUpdateData.class), Mockito.eq("salesforceId"))).thenReturn(Boolean.TRUE); + MemberUpdateData memberUpdateData = getPublicMemberDetailsWithBillingAddress(); + ResponseEntity response = memberResource.updatePublicMemberDetails(memberUpdateData, "salesforceId"); + assertEquals(200, response.getStatusCodeValue()); + Mockito.verify(memberService).updateMemberData(Mockito.any(MemberUpdateData.class), Mockito.eq("salesforceId")); + } + + @Test + public void testUpdatePublicMemberDetailsWithBillingAddressAndNullCountry() throws UnauthorizedMemberAccessException { + Mockito.when(memberService.updateMemberData(Mockito.any(MemberUpdateData.class), Mockito.eq("salesforceId"))).thenReturn(Boolean.TRUE); + MemberUpdateData memberUpdateData = getPublicMemberDetailsWithBillingAddressAndNullCountry(); + ResponseEntity response = memberResource.updatePublicMemberDetails(memberUpdateData, "salesforceId"); + assertEquals(400, response.getStatusCodeValue()); + Mockito.verify(memberService, Mockito.never()).updateMemberData(Mockito.any(MemberUpdateData.class), Mockito.eq("salesforceId")); + } + @Test public void testUpdatePublicMemberDetails_unauthorised() throws UnauthorizedMemberAccessException { Mockito.when(memberService.updateMemberData(Mockito.any(MemberUpdateData.class), Mockito.eq("salesforceId"))).thenThrow(new UnauthorizedMemberAccessException("blah", "blah")); @@ -301,6 +312,35 @@ private MemberUpdateData getPublicMemberDetails() { return memberUpdateData; } + private MemberUpdateData getPublicMemberDetailsWithBillingAddress() { + MemberUpdateData memberUpdateData = new MemberUpdateData(); + memberUpdateData.setPublicName("test member details"); + memberUpdateData.setWebsite("https://website.com"); + memberUpdateData.setOrgName("orgName"); + memberUpdateData.setDescription("test"); + memberUpdateData.setEmail("email@orcid.org"); + + BillingAddress billingAddress = new BillingAddress(); + billingAddress.setCity("new york"); + billingAddress.setCountry("USA"); + memberUpdateData.setBillingAddress(billingAddress); + return memberUpdateData; + } + + private MemberUpdateData getPublicMemberDetailsWithBillingAddressAndNullCountry() { + MemberUpdateData memberUpdateData = new MemberUpdateData(); + memberUpdateData.setPublicName("test member details"); + memberUpdateData.setWebsite("https://website.com"); + memberUpdateData.setOrgName("orgName"); + memberUpdateData.setDescription("test"); + memberUpdateData.setEmail("email@orcid.org"); + + BillingAddress billingAddress = new BillingAddress(); + billingAddress.setCity("new york"); + memberUpdateData.setBillingAddress(billingAddress); + return memberUpdateData; + } + private MemberContacts getMemberContacts() { MemberContacts memberContacts = new MemberContacts();