Skip to content

Commit e23619b

Browse files
committed
chore: extends tests of user UserController
1 parent be5d699 commit e23619b

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

iris-client-bff/src/test/java/iris/client_bff/users/web/UserControllerIntegrationTests.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import iris.client_bff.WithMockAdmin;
1515
import iris.client_bff.matchers.IsUuid;
1616
import iris.client_bff.users.UserAccountsRepository;
17+
import iris.client_bff.users.web.dto.UserInsertDTO;
18+
import iris.client_bff.users.web.dto.UserRoleDTO;
19+
import iris.client_bff.users.web.dto.UserUpdateDTO;
1720
import lombok.RequiredArgsConstructor;
1821

1922
import java.util.Locale;
@@ -24,11 +27,18 @@
2427
import org.junit.jupiter.api.DisplayName;
2528
import org.junit.jupiter.api.Tag;
2629
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.params.ParameterizedTest;
31+
import org.junit.jupiter.params.provider.CsvSource;
32+
import org.springframework.http.HttpStatus;
2733
import org.springframework.security.test.context.support.WithAnonymousUser;
2834
import org.springframework.security.test.context.support.WithMockUser;
2935
import org.springframework.test.web.servlet.MockMvc;
3036

37+
import com.fasterxml.jackson.core.JsonProcessingException;
38+
import com.fasterxml.jackson.databind.ObjectMapper;
3139
import com.github.javafaker.Faker;
40+
import com.github.javafaker.service.FakeValuesService;
41+
import com.github.javafaker.service.RandomService;
3242

3343
@IrisWebIntegrationTest
3444
@WithMockAdmin
@@ -41,7 +51,10 @@ class UserControllerIntegrationTests {
4151
private static final String BASE_URL = "/users";
4252
private static final String DETAILS_URL = BASE_URL + "/{id}";
4353

54+
private static final FakeValuesService fake = new FakeValuesService(Locale.GERMAN, new RandomService());
55+
4456
final MockMvc mvc;
57+
final ObjectMapper objectMapper;
4558
final UserAccountsRepository users;
4659

4760
Faker faker = Faker.instance();
@@ -439,4 +452,51 @@ void updateUser_WithInvalidPassword_ReturnsValidationErrors() {
439452

440453
assertThat(users.count()).isEqualTo(count);
441454
}
455+
456+
@ParameterizedTest(name = "{0} characters ⇒ expect = {1}")
457+
@CsvSource({ "0, BAD_REQUEST", "49, CREATED", "50,CREATED", "51, BAD_REQUEST", "1000, BAD_REQUEST" })
458+
@DisplayName("create user: field length validation for: userName")
459+
void createUser_userNameLength(int wordLength, HttpStatus expectation) throws Exception {
460+
461+
var userName = createWord(wordLength);
462+
463+
var dto = new UserInsertDTO().firstName("fn").lastName("ln").userName(userName).password("Password12A_")
464+
.role(UserRoleDTO.USER);
465+
466+
given()
467+
.body(toJson(dto))
468+
469+
.when()
470+
.post(BASE_URL)
471+
472+
.then()
473+
.status(expectation);
474+
}
475+
476+
@ParameterizedTest(name = "{0} characters ⇒ expect = {1}")
477+
@CsvSource({ "0, OK", "199, OK", "200,OK", "201, BAD_REQUEST", "1000, BAD_REQUEST" })
478+
@DisplayName("update user: field length validation for: lastName")
479+
void updateUser_lastNameLength(int wordLength, HttpStatus expectation) throws Exception {
480+
481+
var admin = users.findByUserName("admin").get();
482+
var lastName = createWord(wordLength);
483+
var dto = new UserUpdateDTO().lastName(lastName);
484+
485+
given()
486+
.body(toJson(dto))
487+
488+
.when()
489+
.patch(DETAILS_URL, admin.getId())
490+
491+
.then()
492+
.status(expectation);
493+
}
494+
495+
private String createWord(int wordLength) {
496+
return fake.letterify("?".repeat(wordLength));
497+
}
498+
499+
private String toJson(Object obj) throws JsonProcessingException {
500+
return objectMapper.writeValueAsString(obj);
501+
}
442502
}

0 commit comments

Comments
 (0)