Skip to content

Commit

Permalink
Bugfix for backward compatible new mobile API
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Apr 8, 2024
1 parent 5a1e1c0 commit 77e91b7
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion account-gui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.openconext</groupId>
<artifactId>myconext</artifactId>
<version>7.2.9</version>
<version>7.2.10</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>account-gui</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion myconext-gui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.openconext</groupId>
<artifactId>myconext</artifactId>
<version>7.2.9</version>
<version>7.2.10</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>myconext-gui</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion myconext-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.openconext</groupId>
<artifactId>myconext</artifactId>
<version>7.2.9</version>
<version>7.2.10</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>myconext-server</artifactId>
Expand Down
15 changes: 13 additions & 2 deletions myconext-server/src/main/java/myconext/api/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,22 @@ public ResponseEntity<StatusResponse> createEduIDAccount(@Valid @RequestBody Cre
@PutMapping("/sp/update")
public ResponseEntity<UserResponse> updateUserProfile(Authentication authentication, @Valid @RequestBody UpdateUserNameRequest deltaUser) {
User user = userFromAuthentication(authentication);
if (StringUtils.hasText(deltaUser.getGivenName())) {
user.setChosenName(deltaUser.getGivenName());
}
//New API allows for update chosen name, override previous update
if (StringUtils.hasText(deltaUser.getChosenName())) {
user.setChosenName(deltaUser.getChosenName());
}
user.setGivenName(deltaUser.getGivenName());
user.setFamilyName(deltaUser.getFamilyName());
//Only if there is not validated name, we allow for updates
if (CollectionUtils.isEmpty(user.getLinkedAccounts())) {
if (StringUtils.hasText(deltaUser.getGivenName())) {
user.setGivenName(deltaUser.getGivenName());
}
if (StringUtils.hasText(deltaUser.getFamilyName())) {
user.setFamilyName(deltaUser.getFamilyName());
}
}
user.validate();

userRepository.save(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ public class UpdateUserNameRequest implements Serializable {

private String chosenName;

@NotBlank
private String givenName;

@NotBlank
private String familyName;
}
24 changes: 6 additions & 18 deletions myconext-server/src/test/java/myconext/api/UserControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,20 @@ public void relayState() throws IOException {

@Test
public void updateUser() {
User user = userRepository.findOneUserByEmail("[email protected]");
user.setGivenName("Mary");
user.setFamilyName("Poppins");
UpdateUserNameRequest updateUserNameRequest = new UpdateUserNameRequest("chosenName", "Mary", "Poppins");
given()
.when()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.body(user)
.body(updateUserNameRequest)
.put("/myconext/api/sp/update")
.then()
.statusCode(HttpStatus.CREATED.value());

User userFromDB = userRepository.findOneUserByEmail("[email protected]");

assertEquals(user.getGivenName(), userFromDB.getGivenName());
assertEquals(user.getFamilyName(), userFromDB.getFamilyName());
//Has linked accounts, so no update
assertEquals(userFromDB.getGivenName(), "John");
assertEquals(userFromDB.getFamilyName(), "Doe");
assertEquals(userFromDB.getChosenName(), updateUserNameRequest.getChosenName());
}

@Test
Expand Down Expand Up @@ -362,17 +361,6 @@ public void removeUserService() {
assertFalse(userFromDB.getEduIDS().stream().anyMatch(val -> val.getServiceProviderEntityId().equals(("http://mock-sp"))));
}

@Test
public void updateUser403() {
given()
.when()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.body(new User())
.put("/myconext/api/sp/update")
.then()
.statusCode(400);
}

@Test
public void updateUserWeakPassword() {
User user = userRepository.findOneUserByEmail("[email protected]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,40 @@ public void me() throws IOException {

@Test
public void updateUserProfile() throws IOException {
UpdateUserNameRequest userNameRequest = new UpdateUserNameRequest("CallName", "Mary", "Winters");
UpdateUserNameRequest userNameRequest = new UpdateUserNameRequest("Annie", "Anna", "Winters");
given()
.when()
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.auth().oauth2(opaqueAccessToken(true, "eduid.nl/mobile"))
.auth().oauth2(doOpaqueAccessToken(true, new String[] {"eduid.nl/mobile"}, "introspect_no_linked_accounts"))
.body(userNameRequest)
.put("/mobile/api/sp/update")
.then()
.statusCode(201);
User user = userRepository.findUserByEmail("jdoe@example.com").get();
User user = userRepository.findUserByEmail("mdoe@example.com").get();

assertEquals(userNameRequest.getChosenName(), user.getChosenName());
assertEquals(userNameRequest.getGivenName(), user.getGivenName());
assertEquals(userNameRequest.getFamilyName(), user.getFamilyName());
}

@Test
public void updateUserProfileOldAPI() throws IOException {
UpdateUserNameRequest userNameRequest = new UpdateUserNameRequest(null, "Anna", null);
given()
.when()
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.auth().oauth2(doOpaqueAccessToken(true, new String[] {"eduid.nl/mobile"}, "introspect_no_linked_accounts"))
.body(userNameRequest)
.put("/mobile/api/sp/update")
.then()
.statusCode(201);
User user = userRepository.findUserByEmail("[email protected]").get();

assertEquals(userNameRequest.getGivenName(), user.getChosenName());
}

@Test
public void institutionNames() throws IOException {
Map names = given()
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.openconext</groupId>
<artifactId>myconext</artifactId>
<version>7.2.9</version>
<version>7.2.10</version>
<packaging>pom</packaging>
<name>myconext</name>
<description>My OpenConext</description>
Expand Down
2 changes: 1 addition & 1 deletion tiqr-mock/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.openconext</groupId>
<artifactId>myconext</artifactId>
<version>7.2.9</version>
<version>7.2.10</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>tiqr-mock</artifactId>
Expand Down

0 comments on commit 77e91b7

Please sign in to comment.