Skip to content

Commit

Permalink
Refactor updating status of Sync Destinations and Opt Attributes (#1035)
Browse files Browse the repository at this point in the history
  • Loading branch information
RuiChen12 authored Oct 31, 2024
1 parent fbfe435 commit 71bac76
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -569,33 +569,22 @@ public ResponseEntity<String> updateDescription(
return httpRequestService.makeApiRequestWithBody(currentUid, uri, description, HttpMethod.PUT);
}

/**
* Allow an owner of a Grouping to enable that a Grouping connected to a given sync destination.
*/
@PostMapping(value = "/groupings/{path}/syncDests/{syncDestId}/enable")
public ResponseEntity<String> enableSyncDest(
@PathVariable String path,
@PathVariable String syncDestId) {
logger.info("Entered REST enableSyncDest...");
String currentUid = policy.sanitize(userContextService.getCurrentUid());
String safeGrouping = policy.sanitize(path);
String safeSyncDestId = policy.sanitize(syncDestId);
return changeSyncDest(safeGrouping, currentUid, safeSyncDestId, true);
}

/**
* This allows an owner of a Grouping to disable that a Grouping connected to a given sync
* This allows an owner of a Grouping to enable/disable that a Grouping connected to a given sync
* destination.
*/
@PostMapping(value = "/groupings/{path}/syncDests/{syncDestId}/disable")
public ResponseEntity<String> disableSyncDest(
@PostMapping(value = "/groupings/{path}/syncDests/{syncDestId}/{status}")
public ResponseEntity<String> updateSyncDest(
@PathVariable String path,
@PathVariable String syncDestId) {
logger.info("Entered REST disableSyncDest...");
@PathVariable String syncDestId,
@PathVariable boolean status) {
logger.info("Entered REST updateSyncDest...");
String currentUid = policy.sanitize(userContextService.getCurrentUid());
String safeGrouping = policy.sanitize(path);
String safeSyncDestId = policy.sanitize(syncDestId);
return changeSyncDest(safeGrouping, currentUid, safeSyncDestId, false);
String uri = String.format(API_2_1_BASE + "/groupings/%s/sync-destination/%s/%s", safeGrouping, safeSyncDestId, status);
return httpRequestService.makeApiRequest(currentUid, uri, HttpMethod.PUT);
}

/**
Expand Down Expand Up @@ -708,15 +697,6 @@ private ResponseEntity<String> changePreference(String grouping, String uhIdenti
return httpRequestService.makeApiRequest(uhIdentifier, uri, HttpMethod.PUT);
}

private ResponseEntity<String> changeSyncDest(String grouping, String uhIdentifier, String syncDest, Boolean isOn) {
String ending = "disable";
if (isOn) {
ending = "enable";
}
String uri = String.format(API_2_1_BASE + "/groupings/%s/sync-destination/%s/%s", grouping, syncDest, ending);
return httpRequestService.makeApiRequest(uhIdentifier, uri, HttpMethod.PUT);
}

protected Boolean shouldDoApiHandshake() {
if (!API_HANDSHAKE_ENABLED) {
logger.info("API handshake disabled.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1607,7 +1607,7 @@
$scope.updateSingleSyncDest = (syncDestName) => {
const groupingPath = $scope.selectedGrouping.path;
const syncDestOn = $scope.getSyncDestValueInArray(syncDestName);
groupingsService.setSyncDest(groupingPath, syncDestName, syncDestOn, handleSuccessfulPreferenceToggle, handleUnsuccessfulRequest);
groupingsService.updateSyncDest(groupingPath, syncDestName, syncDestOn, handleSuccessfulPreferenceToggle, handleUnsuccessfulRequest);
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,8 @@
/**
* Toggle the given sync destination.
*/
setSyncDest(path, syncDestId, turnOn, onSuccess, onError) {
let endpoint = BASE_URL + "groupings/" + path + "/syncDests/" + syncDestId;
endpoint = (turnOn) ? endpoint.concat("/enable") : endpoint.concat("/disable");
updateSyncDest(path, syncDestId, status, onSuccess, onError) {
let endpoint = `${BASE_URL}groupings/${path}/syncDests/${syncDestId}/${status}`;
dataProvider.updateData(endpoint, onSuccess, onError);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,36 +669,30 @@ public void updateDescriptionTest() throws Exception {

@Test
@WithMockUhUser
public void enableSyncDestTest() throws Exception {
String uri = REST_CONTROLLER_BASE + "groupings/" + GROUPING + "/syncDests/listserv/enable";
public void updateSyncDestTest() throws Exception {
String uri = REST_CONTROLLER_BASE + "groupings/" + GROUPING + "/syncDests/listserv/";

String enableUri = uri + "true";
given(httpRequestService.makeApiRequest(eq(UID), anyString(), eq(HttpMethod.PUT)))
.willReturn(new ResponseEntity(HttpStatus.OK));
.willReturn(new ResponseEntity<>(HttpStatus.OK));

assertNotNull(mockMvc.perform(post(uri).with(csrf()))
assertNotNull(mockMvc.perform(post(enableUri).with(csrf()))
.andExpect(status().isOk())
.andReturn());

verify(httpRequestService, times(1))
.makeApiRequest(eq(UID), anyString(), eq(HttpMethod.PUT));
}

@Test
@WithMockUhUser
public void disableSyncDestTest() throws Exception {
String uri = REST_CONTROLLER_BASE + "groupings/" + GROUPING + "/syncDests/listserv/disable";

String disableUri = uri + "false";
given(httpRequestService.makeApiRequest(eq(UID), anyString(), eq(HttpMethod.PUT)))
.willReturn(new ResponseEntity(HttpStatus.OK));
.willReturn(new ResponseEntity<>(HttpStatus.OK));

assertNotNull(mockMvc.perform(post(uri).with(csrf()))
assertNotNull(mockMvc.perform(post(disableUri).with(csrf()))
.andExpect(status().isOk())
.andReturn());

verify(httpRequestService, times(1))
verify(httpRequestService, times(2))
.makeApiRequest(eq(UID), anyString(), eq(HttpMethod.PUT));
}

@Test
@WithMockUhUser
public void setOptInTrueTest() throws Exception {
Expand Down
6 changes: 3 additions & 3 deletions src/test/javascript/grouping.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2508,11 +2508,11 @@ describe("GroupingController", () => {
};
});

it("should call the setSyncDest", () => {
spyOn(gs, "setSyncDest");
it("should call the updateSyncDest", () => {
spyOn(gs, "updateSyncDest");
scope.syncDestArray.push(testSync);
scope.updateSingleSyncDest(testSync.name);
expect(gs.setSyncDest).toHaveBeenCalled();
expect(gs.updateSyncDest).toHaveBeenCalled();
});

it("should call getSyncDestValueInArray", () => {
Expand Down
14 changes: 7 additions & 7 deletions src/test/javascript/groupings.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,24 +574,24 @@ describe("GroupingsService", () => {
});
});

describe("setSyncDest", () => {
describe("updateSyncDest", () => {
let syncDestId;
let turnOn;
it("should call dataProvider.updateData", () => {
spyOn(dp, "updateData");
gs.setSyncDest(groupingPath, syncDestId, turnOn, onSuccess, onError);
gs.updateSyncDest(groupingPath, syncDestId, turnOn, onSuccess, onError);
expect(dp.updateData).toHaveBeenCalled();
});

it("should use the correct path for enable", () => {
gs.setSyncDest(groupingPath, syncDestId, true, onSuccess, onError);
httpBackend.expectPOST(BASE_URL + "groupings/" + groupingPath + "/syncDests/" + syncDestId + "/enable").respond(200);
gs.updateSyncDest(groupingPath, syncDestId, true, onSuccess, onError);
httpBackend.expectPOST(BASE_URL + "groupings/" + groupingPath + "/syncDests/" + syncDestId + "/true").respond(200);
expect(httpBackend.flush).not.toThrow();
});

it("should use the correct path for disable", () => {
gs.setSyncDest(groupingPath, syncDestId, false, onSuccess, onError);
httpBackend.expectPOST(BASE_URL + "groupings/" + groupingPath + "/syncDests/" + syncDestId + "/disable").respond(200);
gs.updateSyncDest(groupingPath, syncDestId, false, onSuccess, onError);
httpBackend.expectPOST(BASE_URL + "groupings/" + groupingPath + "/syncDests/" + syncDestId + "/false").respond(200);
expect(httpBackend.flush).not.toThrow();
});
});
Expand Down

0 comments on commit 71bac76

Please sign in to comment.