Skip to content

Commit

Permalink
Merge pull request #66 from growingabit/parent-phone-verification-link
Browse files Browse the repository at this point in the history
fixed link
  • Loading branch information
gianmarcobrescia authored Oct 9, 2017
2 parents 5e3c7fd + 20e646e commit b0f7e25
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public Response parentphone(@Context final HttpServletRequest request, @Context

try {
final ParentPhoneSignupStage stage = new ParentPhoneSignupStage();
stage.setData(new ParentConfirmationPhone(parentConfirmationPhone.getPhoneNumber(), RequestUtils.getOrigin(request), parentConfirmationPhone.getName(), parentConfirmationPhone.getSurname()));
stage.setData(new ParentConfirmationPhone(parentConfirmationPhone.getPhoneNumber(), RequestUtils.getHost(request), parentConfirmationPhone.getName(), parentConfirmationPhone.getSurname()));
stage.exec(new SignupStageExecutor(currentUser));
return Response.ok().entity(currentUser).build();
} catch (final SignupStageExecutionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public class ParentConfirmationPhone {
private String surname;
private transient String verificationCode;
private transient Long tsExpiration;
private transient String origin;
private transient String host;

public ParentConfirmationPhone(final String phoneNumber, final String origin, final String name, final String surname) {
public ParentConfirmationPhone(final String phoneNumber, final String host, final String name, final String surname) {
Preconditions.checkArgument(StringUtils.isNotEmpty(phoneNumber));
Preconditions.checkArgument(StringUtils.isNotEmpty(origin));
Preconditions.checkArgument(StringUtils.isNotEmpty(host));
Preconditions.checkArgument(StringUtils.isNotEmpty(name));
Preconditions.checkArgument(StringUtils.isNotEmpty(surname));
this.origin = origin;
this.host = host;
this.phoneNumber = phoneNumber;
this.name = name;
this.surname = surname;
Expand Down Expand Up @@ -56,8 +56,8 @@ public Long getTsExpiration() {
return this.tsExpiration;
}

public String getOrigin() {
return this.origin;
public String getHost() {
return this.host;
}

private String generateVerificationCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void run() {

final String verficationData = Base64.encodeBase64URLSafeString(GsonFactory.getGsonInstance().toJson(verificationTaskData).getBytes("utf-8"));

final String verificationLink = parentPhoneData.getOrigin() + "/verify/parentphone/" + verficationData;
final String verificationLink = parentPhoneData.getHost() + "/api/v1/verify/parentphone/" + verficationData;

final ImmutableMap<String, String> map = ImmutableMap.of("verificationLink", verificationLink,
"student.firstname", studentData.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@
public class RequestUtils {

public static String getOrigin(final HttpServletRequest req) {
String origin = StringUtils.isNotEmpty(req.getHeader("Origin")) ? req.getHeader("Origin") : req.getHeader("Host");
if (StringUtils.isNotEmpty(origin) && !origin.startsWith("http://") && !origin.startsWith("https://")) {
origin = "https://" + origin;
final String origin = StringUtils.isNotEmpty(req.getHeader("Origin")) ? req.getHeader("Origin") : req.getHeader("Host");
return addSchemeIfMissing(origin);
}

public static String getHost(final HttpServletRequest req) {
final String host = req.getHeader("Host");
return addSchemeIfMissing(host);
}

private static String addSchemeIfMissing(String url) {
if (StringUtils.isNotEmpty(url) && !url.startsWith("http://") && !url.startsWith("https://")) {
url = "https://" + url;
}
return origin;
return url;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ public void parentPhoneDataHasSurnameFieldEmpty() {
public void completeParentPhoneStage() {
final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
PowerMockito.mockStatic(RequestUtils.class);
PowerMockito.when(RequestUtils.getOrigin(req)).thenReturn(HOST);
PowerMockito.when(RequestUtils.getHost(req)).thenReturn(HOST);

final ParentConfirmationPhone data = new ParentConfirmationPhone("+15005550006", HOST, "name", "surname");

Expand All @@ -524,7 +524,7 @@ public void completeParentPhoneStage() {
public void doubleParentPhoneStage() {
final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
PowerMockito.mockStatic(RequestUtils.class);
PowerMockito.when(RequestUtils.getOrigin(req)).thenReturn(HOST);
PowerMockito.when(RequestUtils.getHost(req)).thenReturn(HOST);

final ParentConfirmationPhone data = new ParentConfirmationPhone("+15005550006", HOST, "name", "surname");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void rigthCodeButNotEncoded() {
public void emailTsExpirationExpired() {
try {
final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
Mockito.when(RequestUtils.getOrigin(req)).thenReturn(HOST);
Mockito.when(RequestUtils.getHost(req)).thenReturn(HOST);
final StudentConfirmationEmail data = new StudentConfirmationEmail(EMAIL_EXAMPLE_COM, HOST);
Response response = new MeController().studentemail(req, this.currentUser, data);

Expand All @@ -193,7 +193,7 @@ public void checkPhoneCode() {
try {
final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
PowerMockito.mockStatic(RequestUtils.class);
PowerMockito.when(RequestUtils.getOrigin(req)).thenReturn(HOST);
PowerMockito.when(RequestUtils.getHost(req)).thenReturn(HOST);
final StudentConfirmationPhone data = new StudentConfirmationPhone("+15005550006");
Response response = new MeController().studentphone(req, this.currentUser, data);
final User user = (User) response.getEntity();
Expand All @@ -216,8 +216,8 @@ public void wrongPhoneVerificationCode() {
try {
final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
PowerMockito.mockStatic(RequestUtils.class);
PowerMockito.when(RequestUtils.getOrigin(req)).thenReturn(HOST);
Mockito.when(RequestUtils.getOrigin(req)).thenReturn(HOST);
PowerMockito.when(RequestUtils.getHost(req)).thenReturn(HOST);
Mockito.when(RequestUtils.getHost(req)).thenReturn(HOST);
final StudentConfirmationPhone data = new StudentConfirmationPhone("+15005550006");
Response response = new MeController().studentphone(req, this.currentUser, data);

Expand All @@ -239,7 +239,7 @@ public void phoneTsExpirationExpired() {
try {
final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
PowerMockito.mockStatic(RequestUtils.class);
PowerMockito.when(RequestUtils.getOrigin(req)).thenReturn(HOST);
PowerMockito.when(RequestUtils.getHost(req)).thenReturn(HOST);
final StudentConfirmationPhone data = new StudentConfirmationPhone("+15005550006");
Response response = new MeController().studentphone(req, this.currentUser, data);

Expand All @@ -266,7 +266,7 @@ public void checkCode() {
try {
final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
PowerMockito.mockStatic(RequestUtils.class);
PowerMockito.when(RequestUtils.getOrigin(req)).thenReturn(HOST);
PowerMockito.when(RequestUtils.getHost(req)).thenReturn(HOST);
final ParentConfirmationPhone data = new ParentConfirmationPhone("+15005550006", HOST, NAME, SURNAME);
Response response = new MeController().parentphone(req, this.currentUser, data);
final User user = (User) response.getEntity();
Expand Down Expand Up @@ -296,7 +296,7 @@ public void wrongVerificationCode() {
try {
final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
PowerMockito.mockStatic(RequestUtils.class);
PowerMockito.when(RequestUtils.getOrigin(req)).thenReturn(HOST);
PowerMockito.when(RequestUtils.getHost(req)).thenReturn(HOST);
final ParentConfirmationPhone data = new ParentConfirmationPhone("+15005550006", HOST, NAME, SURNAME);
Response response = new MeController().parentphone(req, this.currentUser, data);

Expand Down Expand Up @@ -325,7 +325,7 @@ public void wrongVerificationCode() {
public void tsExpirationExpired() {
try {
final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
Mockito.when(RequestUtils.getOrigin(req)).thenReturn(HOST);
Mockito.when(RequestUtils.getHost(req)).thenReturn(HOST);
final ParentConfirmationPhone data = new ParentConfirmationPhone("+15005550006", HOST, NAME, SURNAME);
Response response = new MeController().parentphone(req, this.currentUser, data);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void useHostIfOriginIsEmpty() {
}

@Test
public void doNotDuplicateScheme() {
public void doNotDuplicateOriginScheme() {
final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getHeader("Origin")).thenReturn(ORIGIN);
final String origin = RequestUtils.getOrigin(request);
Expand All @@ -49,7 +49,7 @@ public void doNotDuplicateScheme() {
}

@Test
public void alwaysAddSchemeifMissing() {
public void alwaysAddOriginSchemeifMissing() {
final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getHeader("Origin")).thenReturn(null);
Mockito.when(request.getHeader("Host")).thenReturn(HOST);
Expand All @@ -58,4 +58,22 @@ public void alwaysAddSchemeifMissing() {
assertThat(StringUtils.countMatches(origin, "https")).isAtMost(1);
}

@Test
public void doNotDuplicateHostScheme() {
final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getHeader("Host")).thenReturn("https://" + HOST);
final String host = RequestUtils.getHost(request);
assertThat(host).startsWith("https");
assertThat(StringUtils.countMatches(host, "https")).isAtMost(1);
}

@Test
public void alwaysAddHostSchemeifMissing() {
final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getHeader("Host")).thenReturn(HOST);
final String host = RequestUtils.getHost(request);
assertThat(host).startsWith("https");
assertThat(StringUtils.countMatches(host, "https")).isAtMost(1);
}

}

0 comments on commit b0f7e25

Please sign in to comment.