Skip to content

Commit

Permalink
Merge pull request #88 from Sprokof/add-resetPasswordApiTest
Browse files Browse the repository at this point in the history
add test for resetPassword endpoint
  • Loading branch information
devondragon authored May 16, 2024
2 parents bfe43ba + 441b9ab commit 97cb1ac
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.digitalsanctuary.spring.user.api;

import com.digitalsanctuary.spring.user.api.data.RegistrationResponse;
import com.digitalsanctuary.spring.user.api.data.ApiTestData;
import com.digitalsanctuary.spring.user.api.data.Response;
import com.digitalsanctuary.spring.user.api.provider.ApiTestRegistrationArgumentsHolder;
import com.digitalsanctuary.spring.user.api.provider.ApiTestRegistrationArgumentsProvider;
import com.digitalsanctuary.spring.user.dto.UserDto;
import com.digitalsanctuary.spring.user.json.JsonUtil;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.springframework.http.MediaType;
Expand All @@ -18,9 +20,9 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class UserApiTest extends BaseApiTest {
private static final String URL = "/user/registration";
private static final String URL = "/user";

private static UserDto testUser;
private static final UserDto testUser = ApiTestData.getUserDto();

@AfterAll
public static void deleteTestUser() {
Expand All @@ -36,10 +38,9 @@ public static void deleteTestUser() {
@ParameterizedTest
@ArgumentsSource(ApiTestRegistrationArgumentsProvider.class)
public void registerUserAccount(ApiTestRegistrationArgumentsHolder argumentsHolder) throws Exception {
testUser = argumentsHolder.getUserDto();
ResultActions action = perform(MockMvcRequestBuilders.post(URL)
ResultActions action = perform(MockMvcRequestBuilders.post(URL + "/registration")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(buildUrlEncodedFormEntity(testUser)));
.content(buildUrlEncodedFormEntity(argumentsHolder.getUserDto())));

if (argumentsHolder.getStatus() == ApiTestRegistrationArgumentsHolder.DataStatus.NEW) {
action.andExpect(status().isOk());
Expand All @@ -51,10 +52,23 @@ public void registerUserAccount(ApiTestRegistrationArgumentsHolder argumentsHold
action.andExpect(status().is5xxServerError());
}

RegistrationResponse actual = JsonUtil.readValue(action.andReturn()
.getResponse().getContentAsString(), RegistrationResponse.class);
RegistrationResponse excepted = argumentsHolder.getResponse();
Response actual = JsonUtil.readValue(action.andReturn()
.getResponse().getContentAsString(), Response.class);
Response excepted = argumentsHolder.getResponse();
Assertions.assertEquals(excepted, actual);
}

@Test
public void resetPassword() throws Exception {
ResultActions action = perform(MockMvcRequestBuilders.post(URL + "/resetPassword")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(buildUrlEncodedFormEntity(testUser)))
.andExpect(status().isOk());

Response actual = JsonUtil.readValue(action.andReturn().getResponse()
.getContentAsString(), Response.class);
Response expected = ApiTestData.resetPassword();
Assertions.assertEquals(actual, expected);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,32 @@ public static UserDto getEmptyUserDto() {
return new UserDto();
}

public static RegistrationResponse successRegistration() {
return new RegistrationResponse(
public static Response successRegistration() {
return new Response(
true, 0,
"/user/registration-pending-verification.html",
new String[]{"Registration Successful!"}, null
);
}

public static RegistrationResponse userAlreadyExist() {
return new RegistrationResponse(
public static Response userAlreadyExist() {
return new Response(
false, 2, null,
new String[]{"An account already exists for the email address"}, null
);
}

public static RegistrationResponse systemError() {
return new RegistrationResponse(
public static Response systemError() {
return new Response(
false, 5, null,
new String[]{"System Error!"}, null
);
}

public static Response resetPassword() {
return new Response(true, null, "/user/forgot-password-pending-verification.html",
new String[]{"If account exists, password reset email has been sent!"}, null);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import java.util.Arrays;
import java.util.Objects;

public class RegistrationResponse {
public class Response {
private boolean success;
private String redirectUrl;
private int code;
private Integer code;
private String[] messages;
private Object data;

public RegistrationResponse() {}
public Response() {}

public RegistrationResponse(boolean success, int code, String redirectUrl, String[] messages, Object data) {
public Response(boolean success, Integer code, String redirectUrl, String[] messages, Object data) {
this.success = success;
this.code = code;
this.redirectUrl = redirectUrl;
Expand All @@ -37,7 +37,7 @@ public void setRedirectUrl(String redirectUrl) {
this.redirectUrl = redirectUrl;
}

public int getCode() {
public Integer getCode() {
return code;
}

Expand All @@ -64,7 +64,7 @@ public void setData(Object data) {
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof RegistrationResponse response)) return false;
if (!(obj instanceof Response response)) return false;
return response.success == this.success &&
Objects.equals(response.redirectUrl, this.redirectUrl) &&
response.code == this.code &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.digitalsanctuary.spring.user.api.provider;

import com.digitalsanctuary.spring.user.api.data.RegistrationResponse;
import com.digitalsanctuary.spring.user.api.data.Response;
import com.digitalsanctuary.spring.user.dto.UserDto;

public class ApiTestRegistrationArgumentsHolder {
Expand All @@ -13,10 +13,10 @@ public enum DataStatus {

private final UserDto userDto;
private final DataStatus status;
private final RegistrationResponse response;
private final Response response;


public ApiTestRegistrationArgumentsHolder(UserDto userDto, DataStatus status, RegistrationResponse response) {
public ApiTestRegistrationArgumentsHolder(UserDto userDto, DataStatus status, Response response) {
this.userDto = userDto;
this.status = status;
this.response = response;
Expand All @@ -30,7 +30,7 @@ public DataStatus getStatus() {
return status;
}

public RegistrationResponse getResponse() {
public Response getResponse() {
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext extensionCo
ApiTestData.getUserDto(),
ApiTestRegistrationArgumentsHolder.DataStatus.EXIST,
ApiTestData.userAlreadyExist()
)
)
).map(Arguments::of);
}
}

0 comments on commit 97cb1ac

Please sign in to comment.