Skip to content

Commit

Permalink
Refactor updating status of Sync Destinations and Opt Attributes (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
RuiChen12 authored Nov 28, 2024
1 parent 198fb9e commit 256114a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -590,27 +590,29 @@ public ResponseEntity<String> updateSyncDest(
/**
* Enable or disable a users ability to optIn to a grouping at groupingPath.
*/
@PostMapping(value = "/{groupingPath}/{optInOn}/setOptIn")
public ResponseEntity<String> setOptIn(
@PathVariable String groupingPath,
@PathVariable boolean optInOn) {
logger.info("Entered REST setOptIn...");
@PostMapping(value = "/groupings/{path}/opt-attribute/IN/{status}")
public ResponseEntity<String> updateOptIn(
@PathVariable String path,
@PathVariable boolean status) {
logger.info("Entered REST updateOptIn...");
String currentUid = policy.sanitize(userContextService.getCurrentUid());
String safeGroupingPath = policy.sanitize(groupingPath);
return changePreference(safeGroupingPath, currentUid, OPT_IN, optInOn);
String safeGroupingPath = policy.sanitize(path);
String uri = String.format(API_2_1_BASE + "/groupings/%s/opt-attribute/%s/%s", safeGroupingPath, OPT_IN, status);
return httpRequestService.makeApiRequest(currentUid, uri, HttpMethod.PUT);
}

/**
* Enable or disable a users ability to opt out of grouping at groupingPath.
*/
@PostMapping(value = "/{grouping}/{optOutOn}/setOptOut")
public ResponseEntity<String> setOptOut(
@PathVariable String grouping,
@PathVariable boolean optOutOn) {
logger.info("Entered REST setOptOut...");
@PostMapping(value = "/groupings/{path}/opt-attribute/OUT/{status}")
public ResponseEntity<String> updateOptOut(
@PathVariable String path,
@PathVariable boolean status) {
logger.info("Entered REST updateOptOut...");
String currentUid = policy.sanitize(userContextService.getCurrentUid());
String safeGrouping = policy.sanitize(grouping);
return changePreference(safeGrouping, currentUid, OPT_OUT, optOutOn);
String safeGroupingPath = policy.sanitize(path);
String uri = String.format(API_2_1_BASE + "/groupings/%s/opt-attribute/%s/%s", safeGroupingPath, OPT_OUT, status);
return httpRequestService.makeApiRequest(currentUid, uri, HttpMethod.PUT);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,7 @@
const groupingPath = $scope.selectedGrouping.path;
const allowOptOut = $scope.allowOptOut;

groupingsService.setOptOut(groupingPath, allowOptOut, handleSuccessfulPreferenceToggle, handleUnsuccessfulRequest);
groupingsService.updateOptOut(groupingPath, allowOptOut, handleSuccessfulPreferenceToggle, handleUnsuccessfulRequest);
};

/**
Expand All @@ -1569,7 +1569,7 @@
const groupingPath = $scope.selectedGrouping.path;
const allowOptIn = $scope.allowOptIn;

groupingsService.setOptIn(groupingPath, allowOptIn, handleSuccessfulPreferenceToggle, handleUnsuccessfulRequest);
groupingsService.updateOptIn(groupingPath, allowOptIn, handleSuccessfulPreferenceToggle, handleUnsuccessfulRequest);
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,16 @@
/**
* Toggle the preference option to allow users to opt into a grouping.
*/
setOptIn(path, optInOn, onSuccess, onError) {
let endpoint = BASE_URL + path + "/" + optInOn + "/setOptIn";
updateOptIn(path, status, onSuccess, onError) {
let endpoint = `${BASE_URL}groupings/${path}/opt-attribute/IN/${status}`;
dataProvider.updateData(endpoint, onSuccess, onError);
},

/**
* Toggle the preference option to allow users to opt out of a grouping.
*/
setOptOut(path, optOutOn, onSuccess, onError) {
let endpoint = BASE_URL + path + "/" + optOutOn + "/setOptOut";
updateOptOut(path, status, onSuccess, onError) {
let endpoint = `${BASE_URL}groupings/${path}/opt-attribute/OUT/${status}`;
dataProvider.updateData(endpoint, onSuccess, onError);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,40 +695,40 @@ public void updateSyncDestTest() throws Exception {
}
@Test
@WithMockUhUser
public void setOptInTrueTest() throws Exception {
String uri_true = REST_CONTROLLER_BASE + GROUPING + "/true/setOptIn";
public void updateOptInTrueTest() throws Exception {
String uri = REST_CONTROLLER_BASE +"groupings/"+ GROUPING + "/opt-attribute/IN/true";

given(httpRequestService.makeApiRequest(eq(UID), anyString(), eq(HttpMethod.PUT)))
.willReturn(new ResponseEntity(HttpStatus.OK));

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

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

}

@Test
@WithMockUhUser
public void setOptInFalseTest() throws Exception {
String uri_false = REST_CONTROLLER_BASE + GROUPING + "/false/setOptIn";
public void updateOptInFalseTest() throws Exception {
String uri = REST_CONTROLLER_BASE +"groupings/"+ GROUPING + "/opt-attribute/IN/false";

given(httpRequestService.makeApiRequest(eq(UID), anyString(), eq(HttpMethod.PUT)))
.willReturn(new ResponseEntity(HttpStatus.OK));

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

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

}

@Test
@WithMockUhUser
public void setOptOut() throws Exception {
String uri = REST_CONTROLLER_BASE + GROUPING + "/true/setOptOut";
public void updateOptOutTrueTest() throws Exception {
String uri = REST_CONTROLLER_BASE +"groupings/"+ GROUPING + "/opt-attribute/OUT/true";

given(httpRequestService.makeApiRequest(eq(UID), anyString(), eq(HttpMethod.PUT)))
.willReturn(new ResponseEntity(HttpStatus.OK));
Expand All @@ -743,34 +743,34 @@ public void setOptOut() throws Exception {

@Test
@WithMockUhUser
public void getNumberOfOwners() throws Exception {
String uri = REST_CONTROLLER_BASE + GROUPING + "/owners/" + UID + "/count";
public void updateOptOutFalseTest() throws Exception {
String uri = REST_CONTROLLER_BASE +"groupings/"+ GROUPING + "/opt-attribute/OUT/false";

given(httpRequestService.makeApiRequest(eq(UID), anyString(), eq(HttpMethod.GET)))
given(httpRequestService.makeApiRequest(eq(UID), anyString(), eq(HttpMethod.PUT)))
.willReturn(new ResponseEntity(HttpStatus.OK));

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

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

@Test
@WithMockUhUser
public void setOptOutFalseTest() throws Exception {
String uri = REST_CONTROLLER_BASE + GROUPING + "/false/setOptOut";
public void getNumberOfOwners() throws Exception {
String uri = REST_CONTROLLER_BASE + GROUPING + "/owners/" + UID + "/count";

given(httpRequestService.makeApiRequest(eq(UID), anyString(), eq(HttpMethod.PUT)))
given(httpRequestService.makeApiRequest(eq(UID), anyString(), eq(HttpMethod.GET)))
.willReturn(new ResponseEntity(HttpStatus.OK));

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

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

@Test
Expand Down
8 changes: 4 additions & 4 deletions src/test/javascript/grouping.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2394,17 +2394,17 @@ describe("GroupingController", () => {

describe("updateAllowOptOut", () => {
it("should call groupings service", () => {
spyOn(gs, "setOptOut").and.callThrough();
spyOn(gs, "updateOptOut").and.callThrough();
scope.updateAllowOptOut();
expect(gs.setOptOut).toHaveBeenCalled();
expect(gs.updateOptOut).toHaveBeenCalled();
});
});

describe("updateAllowOptIn", () => {
it("should call groupings service setOptIn", () => {
spyOn(gs, "setOptIn").and.callThrough();
spyOn(gs, "updateOptIn").and.callThrough();
scope.updateAllowOptIn();
expect(gs.setOptIn).toHaveBeenCalled();
expect(gs.updateOptIn).toHaveBeenCalled();
});
});

Expand Down
16 changes: 8 additions & 8 deletions src/test/javascript/groupings.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,30 +488,30 @@ describe("GroupingsService", () => {
});
});

describe("setOptIn", () => {
describe("updateOptIn", () => {
let optInOn;
it("should call dataProvider.updateData", () => {
spyOn(dp, "updateData");
gs.setOptIn(groupingPath, optInOn, onSuccess, onError);
gs.updateOptIn(groupingPath, optInOn, onSuccess, onError);
expect(dp.updateData).toHaveBeenCalled();
});
it("should use the correct path", () => {
gs.setOptIn(groupingPath, optInOn, onSuccess, onError);
httpBackend.expectPOST(BASE_URL + groupingPath + "/" + optInOn + "/setOptIn").respond(200);
gs.updateOptIn(groupingPath, optInOn, onSuccess, onError);
httpBackend.expectPOST(BASE_URL + "groupings/"+groupingPath + "/opt-attribute/IN/" + optInOn).respond(200);
expect(httpBackend.flush).not.toThrow();
});
});

describe("setOptOut", () => {
describe("updateOptOut", () => {
let optOutOn;
it("should call dataProvider.updateData", () => {
spyOn(dp, "updateData");
gs.setOptOut(groupingPath, optOutOn, onSuccess, onError);
gs.updateOptOut(groupingPath, optOutOn, onSuccess, onError);
expect(dp.updateData).toHaveBeenCalled();
});
it("should use the correct path", () => {
gs.setOptOut(groupingPath, optOutOn, onSuccess, onError);
httpBackend.expectPOST(BASE_URL + groupingPath + "/" + optOutOn + "/setOptOut").respond(200);
gs.updateOptOut(groupingPath, optOutOn, onSuccess, onError);
httpBackend.expectPOST(BASE_URL + "groupings/"+groupingPath + "/opt-attribute/OUT/" + optOutOn).respond(200);
expect(httpBackend.flush).not.toThrow();
});
});
Expand Down

0 comments on commit 256114a

Please sign in to comment.