Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AB#78143: Adding restore API call #66

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/edu/ksu/canvas/impl/AccountImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,12 @@ public User deleteUser(String userId, String accountId) throws IOException {
Optional<DeletedUserResponse> responseParsed = responseParser.parseToObject(DeletedUserResponse.class, response);
return responseParsed.map(DeletedUserResponse::getUser).orElse(null);
}

@Override
public Optional<User> restoreUser(String accountId, String userIdentifier) throws IOException {
LOG.debug("Restoring user {}", userIdentifier);
String url = buildCanvasUrl("accounts/" + accountId + "/users/" + userIdentifier + "/restore", Collections.emptyMap());
Response response = canvasMessenger.putToCanvas(oauthToken, url, Collections.emptyMap());
return responseParser.parseToObject(User.class, response);
}
}
11 changes: 11 additions & 0 deletions src/main/java/edu/ksu/canvas/interfaces/AccountWriter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.ksu.canvas.interfaces;

import edu.ksu.canvas.exception.InvalidOauthTokenException;
import edu.ksu.canvas.model.Account;
import edu.ksu.canvas.model.User;

Expand Down Expand Up @@ -39,4 +40,14 @@ public interface AccountWriter extends CanvasWriter<Account, AccountWriter> {
* @throws IOException When there is an error communicating with Canvas
*/
User deleteUser(String userId, String accountId) throws IOException;

/**
* Restore a deleted user in Canvas
* @param accountId account id for restoring user into an account
* @param userId user id for restoring user
* @return The newly restored user
* @throws InvalidOauthTokenException When the supplied OAuth token is not valid
* @throws IOException When there is an error communicating with Canvas
*/
Optional<User> restoreUser (String accountId, String userId) throws InvalidOauthTokenException, IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.Map;
import java.util.Optional;

import edu.ksu.canvas.interfaces.AccountWriter;
import edu.ksu.canvas.model.User;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -22,17 +24,20 @@
import edu.ksu.canvas.requestOptions.ListAccountOptions.Include;
import edu.ksu.canvas.util.CanvasURLBuilder;

public class AccountReaderUTest extends CanvasTestBase {
public class AccountUTest extends CanvasTestBase {
@Autowired
private FakeRestClient fakeRestClient;
private AccountReader accountReader;
private AccountWriter accountWriter;

private static final String ROOT_ACCOUNT_ID = "1";

@Before
public void setupReader() {
accountReader = new AccountImpl(baseUrl, apiVersion, SOME_OAUTH_TOKEN, fakeRestClient, SOME_CONNECT_TIMEOUT,
SOME_READ_TIMEOUT, DEFAULT_PAGINATION_PAGE_SIZE, false);
accountWriter = new AccountImpl(baseUrl, apiVersion, SOME_OAUTH_TOKEN, fakeRestClient, SOME_CONNECT_TIMEOUT,
SOME_READ_TIMEOUT, DEFAULT_PAGINATION_PAGE_SIZE, false);
}

@Test
Expand Down Expand Up @@ -63,4 +68,16 @@ public void listAccountWithLtiGuidInclude() throws Exception {
Assert.assertEquals("f22a4332-3d40-427b-846d-9bc1fa5ab9b4.canvas.example.edu", account.getLtiGuid());
}

@Test
public void testRestoreUserByUserId() throws Exception {
String accountId = "1";
int userId = 20;
String url = baseUrl + "/api/v1/accounts/1/users/" + String.valueOf(userId) + "/restore";
fakeRestClient.addSuccessResponse(url, "SampleJson/user/UserById.json");
Optional<User> result = accountWriter.restoreUser(accountId, String.valueOf(userId));
User user = result.get();
Assert.assertEquals(userId, user.getId());
Assert.assertEquals("2011-05-30T16:45:25Z", user.getCreatedAt().toString());
}

}
Loading