Skip to content

Commit

Permalink
first pass at v2 migration
Browse files Browse the repository at this point in the history
  • Loading branch information
rushtong committed Jan 10, 2024
1 parent ed14836 commit 8f59df8
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class ServicesConfiguration {
public static final String REGISTER_SELF_DIAGNOSTICS_PATH = "register/user/v2/self/diagnostics";
public static final String REGISTER_SELF_PATH = "register/user/v2/self";
public static final String TOS_TEXT_PATH = "tos/text";
public static final String REGISTER_TOS_PATH = "register/user/v1/termsofservice";
public static final String ACCEPT_TOS_PATH = "api/termsOfService/v1/user/self/accept";
public static final String REJECT_TOS_PATH = "api/termsOfService/v1/user/self/reject";
public static final String SAM_V1_USER_EMAIL = "api/users/v1";

@NotNull
Expand Down Expand Up @@ -98,8 +99,12 @@ public String getToSTextUrl() {
return getSamUrl() + TOS_TEXT_PATH;
}

public String tosRegistrationUrl() {
return getSamUrl() + REGISTER_TOS_PATH;
public String acceptTosUrl() {
return getSamUrl() + ACCEPT_TOS_PATH;
}

public String rejectTosUrl() {
return getSamUrl() + REJECT_TOS_PATH;
}

public String getV1UserUrl(String email) {
Expand Down
23 changes: 8 additions & 15 deletions src/main/java/org/broadinstitute/consent/http/db/SamDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpStatusCodes;
import com.google.api.client.http.json.JsonHttpContent;
import com.google.api.client.json.gson.GsonFactory;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
Expand All @@ -26,7 +24,6 @@
import org.broadinstitute.consent.http.models.AuthUser;
import org.broadinstitute.consent.http.models.sam.EmailResponse;
import org.broadinstitute.consent.http.models.sam.ResourceType;
import org.broadinstitute.consent.http.models.sam.TosResponse;
import org.broadinstitute.consent.http.models.sam.UserStatus;
import org.broadinstitute.consent.http.models.sam.UserStatusDiagnostics;
import org.broadinstitute.consent.http.models.sam.UserStatusInfo;
Expand Down Expand Up @@ -141,31 +138,27 @@ public String getToSText() throws Exception {
return response.parseAsString();
}

public TosResponse postTosAcceptedStatus(AuthUser authUser) throws Exception {
GenericUrl genericUrl = new GenericUrl(configuration.tosRegistrationUrl());
JsonHttpContent content = new JsonHttpContent(new GsonFactory(),
"app.terra.bio/#terms-of-service");
HttpRequest request = clientUtil.buildPostRequest(genericUrl, content, authUser);
public int acceptTosStatus(AuthUser authUser) throws Exception {
GenericUrl genericUrl = new GenericUrl(configuration.acceptTosUrl());
HttpRequest request = clientUtil.buildPutRequest(genericUrl, new EmptyContent(), authUser);
HttpResponse response = clientUtil.handleHttpRequest(request);
if (!response.isSuccessStatusCode()) {
logException("Error accepting Terms of Service through Sam: " + response.getStatusMessage(),
new ServerErrorException(response.getStatusMessage(), response.getStatusCode()));
}
String body = response.parseAsString();
return new Gson().fromJson(body, TosResponse.class);
return response.getStatusCode();
}

public TosResponse removeTosAcceptedStatus(AuthUser authUser) throws Exception {
GenericUrl genericUrl = new GenericUrl(configuration.tosRegistrationUrl());
HttpRequest request = clientUtil.buildDeleteRequest(genericUrl, authUser);
public int rejectTosStatus(AuthUser authUser) throws Exception {
GenericUrl genericUrl = new GenericUrl(configuration.rejectTosUrl());
HttpRequest request = clientUtil.buildPutRequest(genericUrl, new EmptyContent(), authUser);
HttpResponse response = clientUtil.handleHttpRequest(request);
if (!response.isSuccessStatusCode()) {
logException(
"Error removing Terms of Service acceptance through Sam: " + response.getStatusMessage(),
new ServerErrorException(response.getStatusMessage(), response.getStatusCode()));
}
String body = response.parseAsString();
return new Gson().fromJson(body, TosResponse.class);
return response.getStatusCode();
}

public EmailResponse getV1UserByEmail(AuthUser authUser, String email) throws Exception {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.broadinstitute.consent.http.resources;

import com.google.api.client.http.HttpStatusCodes;
import com.google.inject.Inject;
import io.dropwizard.auth.Auth;
import jakarta.annotation.security.PermitAll;
Expand All @@ -17,7 +18,6 @@
import org.broadinstitute.consent.http.models.AuthUser;
import org.broadinstitute.consent.http.models.User;
import org.broadinstitute.consent.http.models.sam.ResourceType;
import org.broadinstitute.consent.http.models.sam.TosResponse;
import org.broadinstitute.consent.http.models.sam.UserStatus;
import org.broadinstitute.consent.http.models.sam.UserStatusDiagnostics;
import org.broadinstitute.consent.http.models.sam.UserStatusInfo;
Expand Down Expand Up @@ -104,8 +104,12 @@ public Response postSelfTos(@Auth AuthUser authUser) {
user.setDisplayName(authUser.getName());
userService.createUser(user);
}
TosResponse tosResponse = samService.postTosAcceptedStatus(authUser);
return Response.ok().entity(tosResponse).build();
int status = samService.postTosAcceptedStatus(authUser);
if (HttpStatusCodes.isSuccess(status)) {
return Response.ok().build();
} else {
return Response.status(status).build();
}
} catch (Exception e) {
return createExceptionResponse(e);
}
Expand All @@ -118,8 +122,12 @@ public Response postSelfTos(@Auth AuthUser authUser) {
@PermitAll
public Response removeTos(@Auth AuthUser authUser) {
try {
TosResponse tosResponse = samService.removeTosAcceptedStatus(authUser);
return Response.ok().entity(tosResponse).build();
int status = samService.removeTosAcceptedStatus(authUser);
if (HttpStatusCodes.isSuccess(status)) {
return Response.ok().build();
} else {
return Response.status(status).build();
}
} catch (Exception e) {
return createExceptionResponse(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.broadinstitute.consent.http.db.SamDAO;
import org.broadinstitute.consent.http.models.AuthUser;
import org.broadinstitute.consent.http.models.sam.ResourceType;
import org.broadinstitute.consent.http.models.sam.TosResponse;
import org.broadinstitute.consent.http.models.sam.UserStatus;
import org.broadinstitute.consent.http.models.sam.UserStatusDiagnostics;
import org.broadinstitute.consent.http.models.sam.UserStatusInfo;
Expand Down Expand Up @@ -43,11 +42,11 @@ public String getToSText() throws Exception {
return samDAO.getToSText();
}

public TosResponse postTosAcceptedStatus(AuthUser authUser) throws Exception {
return samDAO.postTosAcceptedStatus(authUser);
public int postTosAcceptedStatus(AuthUser authUser) throws Exception {
return samDAO.acceptTosStatus(authUser);
}

public TosResponse removeTosAcceptedStatus(AuthUser authUser) throws Exception {
return samDAO.removeTosAcceptedStatus(authUser);
public int removeTosAcceptedStatus(AuthUser authUser) throws Exception {
return samDAO.rejectTosStatus(authUser);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,19 @@ public HttpRequest buildPostRequest(GenericUrl genericUrl, HttpContent content,
return request;
}

public HttpRequest buildUnAuthedPostRequest(GenericUrl genericUrl, HttpContent content)
public HttpRequest buildPutRequest(GenericUrl genericUrl, HttpContent content, AuthUser authUser)
throws Exception {
HttpTransport transport = new NetHttpTransport();
HttpRequest request = transport.createRequestFactory().buildPostRequest(genericUrl, content);
request.setHeaders(new HttpHeaders().set("X-App-ID", "DUOS"));
HttpRequest request = transport.createRequestFactory().buildPutRequest(genericUrl, content);
request.setHeaders(buildHeaders(authUser));
return request;
}

public HttpRequest buildDeleteRequest(GenericUrl genericUrl, AuthUser authUser)
public HttpRequest buildUnAuthedPostRequest(GenericUrl genericUrl, HttpContent content)
throws Exception {
HttpTransport transport = new NetHttpTransport();
HttpRequest request = transport.createRequestFactory().buildDeleteRequest(genericUrl);
request.setHeaders(buildHeaders(authUser));
HttpRequest request = transport.createRequestFactory().buildPostRequest(genericUrl, content);
request.setHeaders(new HttpHeaders().set("X-App-ID", "DUOS"));
return request;
}

Expand Down
8 changes: 0 additions & 8 deletions src/main/resources/assets/paths/samRegisterSelfTos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ post:
responses:
200:
description: Accept ToS
content:
application/json:
schema:
$ref: '../schemas/SamTosResponse.yaml'
500:
description: Internal Server Error
delete:
Expand All @@ -20,9 +16,5 @@ delete:
responses:
200:
description: Reject ToS
content:
application/json:
schema:
$ref: '../schemas/SamTosResponse.yaml'
500:
description: Internal Server Error
33 changes: 0 additions & 33 deletions src/main/resources/assets/schemas/SamTosResponse.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.broadinstitute.consent.http.models.sam.ActionPattern;
import org.broadinstitute.consent.http.models.sam.ResourceType;
import org.broadinstitute.consent.http.models.sam.ResourceTypeRole;
import org.broadinstitute.consent.http.models.sam.TosResponse;
import org.broadinstitute.consent.http.models.sam.UserStatus;
import org.broadinstitute.consent.http.models.sam.UserStatusDiagnostics;
import org.broadinstitute.consent.http.models.sam.UserStatusInfo;
Expand Down Expand Up @@ -121,28 +120,16 @@ public void testGetRegistrationInfo() throws Exception {

@Test
public void testPostSelfTos() throws Exception {
TosResponse.Enabled enabled = new TosResponse.Enabled()
.setAdminEnabled(true).setTosAccepted(true).setGoogle(true).setAllUsersGroup(true)
.setLdap(true);
UserStatus.UserInfo info = new UserStatus.UserInfo().setUserEmail("[email protected]")
.setUserSubjectId("subjectId");
TosResponse tosResponse = new TosResponse().setEnabled(enabled).setUserInfo(info);
when(samService.postTosAcceptedStatus(any())).thenReturn(tosResponse);
when(samService.postTosAcceptedStatus(any())).thenReturn(200);
initResource();
Response response = resource.postSelfTos(authUser);
assertEquals(HttpStatusCodes.STATUS_CODE_OK, response.getStatus());
}

@Test
public void testPostSelfTos_NoConsentUser() throws Exception {
TosResponse.Enabled enabled = new TosResponse.Enabled()
.setAdminEnabled(true).setTosAccepted(true).setGoogle(true).setAllUsersGroup(true)
.setLdap(true);
UserStatus.UserInfo info = new UserStatus.UserInfo().setUserEmail("[email protected]")
.setUserSubjectId("subjectId");
TosResponse tosResponse = new TosResponse().setEnabled(enabled).setUserInfo(info);
doThrow(new NotFoundException()).when(userService).findUserByEmail(any());
when(samService.postTosAcceptedStatus(any())).thenReturn(tosResponse);
when(samService.postTosAcceptedStatus(any())).thenReturn(200);
initResource();

Response response = resource.postSelfTos(authUser);
Expand All @@ -152,14 +139,8 @@ public void testPostSelfTos_NoConsentUser() throws Exception {

@Test
public void testPostSelfTos_ExistingSamUser() throws Exception {
TosResponse.Enabled enabled = new TosResponse.Enabled()
.setAdminEnabled(true).setTosAccepted(true).setGoogle(true).setAllUsersGroup(true)
.setLdap(true);
UserStatus.UserInfo info = new UserStatus.UserInfo().setUserEmail("[email protected]")
.setUserSubjectId("subjectId");
TosResponse tosResponse = new TosResponse().setEnabled(enabled).setUserInfo(info);
doThrow(new ConsentConflictException()).when(samService).postRegistrationInfo(any());
when(samService.postTosAcceptedStatus(any())).thenReturn(tosResponse);
when(samService.postTosAcceptedStatus(any())).thenReturn(409);
initResource();

Response response = resource.postSelfTos(authUser);
Expand All @@ -168,13 +149,7 @@ public void testPostSelfTos_ExistingSamUser() throws Exception {

@Test
public void testRemoveSelfTos() throws Exception {
TosResponse.Enabled enabled = new TosResponse.Enabled()
.setAdminEnabled(true).setTosAccepted(false).setGoogle(true).setAllUsersGroup(true)
.setLdap(true);
UserStatus.UserInfo info = new UserStatus.UserInfo().setUserEmail("[email protected]")
.setUserSubjectId("subjectId");
TosResponse tosResponse = new TosResponse().setEnabled(enabled).setUserInfo(info);
when(samService.removeTosAcceptedStatus(any())).thenReturn(tosResponse);
when(samService.removeTosAcceptedStatus(any())).thenReturn(200);
initResource();
Response response = resource.removeTos(authUser);
assertEquals(HttpStatusCodes.STATUS_CODE_OK, response.getStatus());
Expand Down
Loading

0 comments on commit 8f59df8

Please sign in to comment.