Skip to content

Commit

Permalink
Merge pull request #36 from oxctl/mergeusers
Browse files Browse the repository at this point in the history
Users: Ability to merge user accounts.
  • Loading branch information
nicholaswilson100 committed Sep 30, 2021
2 parents 7efb0ff + 2e686d7 commit c2d9c50
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/edu/ksu/canvas/impl/UserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ public List<User> getUsersInAccount(GetUsersInAccountOptions options) throws IOE
return getListFromCanvas(url);
}

@Override
public Optional<User> mergeUsers(String fromUserId, String destinationUserId) throws InvalidOauthTokenException, IOException {
LOG.debug("Merging user {} into {}", fromUserId, destinationUserId);
String mergeUserUrl = buildCanvasUrl(String.format("users/%s/merge_into/%s", fromUserId, destinationUserId), Collections.emptyMap());
LOG.debug("Merge operation URL: {}", mergeUserUrl);
Response response = canvasMessenger.putToCanvas(oauthToken, mergeUserUrl, Collections.emptyMap());
return responseParser.parseToObject(User.class, response);
}

@Override
public Optional<User> mergeUsersIntoAccount(String fromUserId, String destinationAccountId, String destinationUserId) throws InvalidOauthTokenException, IOException {
LOG.debug("Merging user {} into the user {} and the account {}", fromUserId, destinationUserId, destinationAccountId);
String mergeUserUrl = buildCanvasUrl(String.format("users/%s/merge_into/accounts/%s/users/%s", fromUserId, destinationAccountId, destinationUserId), Collections.emptyMap());
LOG.debug("Merge operation URL: {}", mergeUserUrl);
Response response = canvasMessenger.putToCanvas(oauthToken, mergeUserUrl, Collections.emptyMap());
return responseParser.parseToObject(User.class, response);
}

@Override
protected Type listType() {
return new TypeToken<List<User>>() {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/edu/ksu/canvas/interfaces/UserWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,25 @@ public interface UserWriter extends CanvasWriter<User, UserWriter> {
* @throws IOException When there is an error communicating with Canvas
*/
Optional<User> updateUser(User user) throws InvalidOauthTokenException, IOException;

/**
* Merge a user into another user.
* @param fromUserId The fromUserId is the user that was deleted in the user_merge process.
* @param destinationUserId The destinationUserId is the user that remains, that is being split.
* @return An Optional object that may include the merged user
* @throws InvalidOauthTokenException When the supplied OAuth token is not valid
* @throws IOException When there is an error communicating with Canvas
*/
Optional<User> mergeUsers(String fromUserId, String destinationUserId) throws InvalidOauthTokenException, IOException;

/**
* Merge a user into another user in a specific account. When finding users by SIS ids in different accounts the destination_account_id is required.
* @param fromUserId The fromUserId is the user that was deleted in the user_merge process.
* @param destinationAccountId The account can also be identified by passing the domain in destination_account_id.
* @param destinationUserId The destinationUserId is the user that remains, that is being split.
* @return An Optional object that may include the merged user
* @throws InvalidOauthTokenException When the supplied OAuth token is not valid
* @throws IOException When there is an error communicating with Canvas
*/
Optional<User> mergeUsersIntoAccount(String fromUserId, String destinationAccountId, String destinationUserId) throws InvalidOauthTokenException, IOException;
}
28 changes: 28 additions & 0 deletions src/test/java/edu/ksu/canvas/tests/user/UserRetrieverUTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import edu.ksu.canvas.constants.CanvasConstants;
import edu.ksu.canvas.impl.UserImpl;
import edu.ksu.canvas.interfaces.UserReader;
import edu.ksu.canvas.interfaces.UserWriter;
import edu.ksu.canvas.model.User;
import edu.ksu.canvas.net.FakeRestClient;
import edu.ksu.canvas.net.Response;
Expand All @@ -23,13 +24,16 @@ public class UserRetrieverUTest extends CanvasTestBase {
@Autowired
private FakeRestClient fakeRestClient;
private UserReader userReader;
private UserWriter userWriter;

private static final String someCourseId = "123";

@Before
public void setupData() {
userReader = new UserImpl(baseUrl, apiVersion, SOME_OAUTH_TOKEN, fakeRestClient, SOME_CONNECT_TIMEOUT,
SOME_READ_TIMEOUT, DEFAULT_PAGINATION_PAGE_SIZE, false);
userWriter = new UserImpl(baseUrl, apiVersion, SOME_OAUTH_TOKEN, fakeRestClient, SOME_CONNECT_TIMEOUT,
SOME_READ_TIMEOUT, DEFAULT_PAGINATION_PAGE_SIZE, false);
}

@Test
Expand Down Expand Up @@ -154,4 +158,28 @@ public void testGetAllUsersBySearchTermFromAccount() throws Exception {
List<User> result = userReader.getUsersInAccount(options);
Assert.assertEquals(1, result.size());
}

@Test
public void testMergeUsers() throws Exception {
String userId = "1";
String destinationUserId = "2";
String url = baseUrl + String.format("/api/v1/users/%s/merge_into/%s", userId, destinationUserId);
fakeRestClient.addSuccessResponse(url, "SampleJson/user/User2.json");
Optional<User> result = userWriter.mergeUsers(userId, destinationUserId);
User user = result.get();
Assert.assertEquals(destinationUserId, String.valueOf(user.getId()));
}

@Test
public void testMergeUsersIntoAccount() throws Exception {
String userId = "1";
String destinationUserId = "2";
String accountId = "1";
String url = baseUrl + String.format("/api/v1/users/%s/merge_into/accounts/%s/users/%s", userId, accountId, destinationUserId);
fakeRestClient.addSuccessResponse(url, "SampleJson/user/User2.json");
Optional<User> result = userWriter.mergeUsersIntoAccount(userId, accountId, destinationUserId);
User user = result.get();
Assert.assertEquals(destinationUserId, String.valueOf(user.getId()));
}

}

0 comments on commit c2d9c50

Please sign in to comment.