-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
28 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import net.datafaker.Faker; | ||
import org.instancio.Instancio; | ||
import org.instancio.Select; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
|
@@ -46,8 +47,12 @@ class UserControllerTest { | |
@Autowired | ||
private UserMapper userMapper; | ||
|
||
private User wrong; | ||
|
||
private User user; | ||
|
||
private User generateUser() { | ||
User user = Instancio.of(User.class) | ||
User generatedUser = Instancio.of(User.class) | ||
.ignore(Select.field(User::getId)) | ||
.ignore(Select.field(User::getCreatedAt)) | ||
.ignore(Select.field(User::getUpdatedAt)) | ||
|
@@ -58,8 +63,17 @@ private User generateUser() { | |
.supply(Select.field(User::getEmail), () -> faker.internet().emailAddress()) | ||
.supply(Select.field(User::getPassword), () -> faker.internet().password(3, 10)) | ||
.create(); | ||
user.addUserRole(UserRoleType.ADMIN); | ||
return user; | ||
generatedUser.addUserRole(UserRoleType.ADMIN); | ||
return generatedUser; | ||
} | ||
|
||
@BeforeEach | ||
void setup() { | ||
wrong = generateUser(); | ||
wrong.removeUserRole(UserRoleType.ADMIN); | ||
user = generateUser(); | ||
repository.save(user); | ||
repository.save(wrong); | ||
} | ||
|
||
@Test | ||
|
@@ -85,17 +99,12 @@ public void testIndexNoAuth() throws Exception { | |
|
||
@Test | ||
public void testIndexForbidden() throws Exception { | ||
User wrong = generateUser(); | ||
wrong.removeUserRole(UserRoleType.ADMIN); | ||
mockMvc.perform(get("/api/users").with(user(wrong))) | ||
.andExpect(status().isForbidden()); | ||
} | ||
|
||
@Test | ||
public void testShow() throws Exception { | ||
User user = generateUser(); | ||
repository.save(user); | ||
|
||
MvcResult result = mockMvc.perform(get("/api/users/" + user.getId()).with(user(user))) | ||
.andExpect(status().isOk()) | ||
.andReturn(); | ||
|
@@ -110,74 +119,58 @@ public void testShow() throws Exception { | |
|
||
@Test | ||
public void testShowNoAuth() throws Exception { | ||
User user = generateUser(); | ||
repository.save(user); | ||
|
||
mockMvc.perform(get("/api/users/" + user.getId())) | ||
.andExpect(status().isUnauthorized()); | ||
} | ||
|
||
@Test | ||
public void testShowForbidden() throws Exception { | ||
User user = generateUser(); | ||
User wrong = generateUser(); | ||
wrong.removeUserRole(UserRoleType.ADMIN); | ||
repository.save(user); | ||
repository.save(wrong); | ||
|
||
mockMvc.perform(get("/api/users/" + user.getId()).with(user(wrong))) | ||
.andExpect(status().isForbidden()); | ||
} | ||
|
||
@Test | ||
void testCreate() throws Exception { | ||
User user = generateUser(); | ||
User admin = generateUser(); | ||
repository.save(admin); | ||
User user1 = generateUser(); | ||
|
||
var request = post("/api/users").with(user(admin)) | ||
var request = post("/api/users").with(user(user)) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(mapper.writeValueAsString(userMapper.mapToCreateDTO(user))); | ||
.content(mapper.writeValueAsString(userMapper.mapToCreateDTO(user1))); | ||
mockMvc.perform(request) | ||
.andExpect(status().isCreated()); | ||
Optional<User> opActual = repository.findByEmail(user.getEmail()); | ||
Optional<User> opActual = repository.findByEmail(user1.getEmail()); | ||
|
||
assertThat(opActual).isNotNull(); | ||
User actual = opActual.get(); | ||
assertThat(user.getEmail()).isEqualTo(actual.getEmail()); | ||
assertThat(user.getLastName()).isEqualTo(actual.getLastName()); | ||
assertThat(user.getFirstName()).isEqualTo(actual.getFirstName()); | ||
assertThat(user1.getEmail()).isEqualTo(actual.getEmail()); | ||
assertThat(user1.getLastName()).isEqualTo(actual.getLastName()); | ||
assertThat(user1.getFirstName()).isEqualTo(actual.getFirstName()); | ||
} | ||
|
||
@Test | ||
void testCreateForbidden() throws Exception { | ||
User user = generateUser(); | ||
User wrong = generateUser(); | ||
wrong.removeUserRole(UserRoleType.ADMIN); | ||
repository.save(wrong); | ||
User user1 = generateUser(); | ||
|
||
var request = post("/api/users").with(user(wrong)) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(mapper.writeValueAsString(userMapper.mapToCreateDTO(user))); | ||
.content(mapper.writeValueAsString(userMapper.mapToCreateDTO(user1))); | ||
mockMvc.perform(request) | ||
.andExpect(status().isForbidden()); | ||
} | ||
|
||
@Test | ||
void testCreateNoAuth() throws Exception { | ||
User user = generateUser(); | ||
User user1 = generateUser(); | ||
|
||
var request = post("/api/users") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(mapper.writeValueAsString(userMapper.mapToCreateDTO(user))); | ||
.content(mapper.writeValueAsString(userMapper.mapToCreateDTO(user1))); | ||
mockMvc.perform(request) | ||
.andExpect(status().isUnauthorized()); | ||
} | ||
|
||
@Test | ||
public void testUpdate() throws Exception { | ||
User user = generateUser(); | ||
repository.save(user); | ||
Map<String, String> data = Map.of("email", "[email protected]", "firstName", "John"); | ||
|
||
var request = put("/api/users/" + user.getId()).with(user(user)) | ||
|
@@ -196,8 +189,6 @@ public void testUpdate() throws Exception { | |
|
||
@Test | ||
public void testUpdateNoAuth() throws Exception { | ||
User user = generateUser(); | ||
repository.save(user); | ||
Map<String, String> data = Map.of("email", "[email protected]", "firstName", "John"); | ||
|
||
var request = put("/api/users/" + user.getId()) | ||
|
@@ -209,10 +200,6 @@ public void testUpdateNoAuth() throws Exception { | |
|
||
@Test | ||
public void testUpdateForbidden() throws Exception { | ||
User user = generateUser(); | ||
User wrong = generateUser(); | ||
repository.save(wrong); | ||
repository.save(user); | ||
Map<String, String> data = Map.of("email", "[email protected]", "firstName", "John"); | ||
|
||
var request = put("/api/users/" + user.getId()).with(user(wrong)) | ||
|
@@ -224,9 +211,6 @@ public void testUpdateForbidden() throws Exception { | |
|
||
@Test | ||
public void testDelete() throws Exception { | ||
User user = generateUser(); | ||
repository.save(user); | ||
|
||
var request = delete("/api/users/" + user.getId()).with(user(user)); | ||
mockMvc.perform(request) | ||
.andExpect(status().isNoContent()); | ||
|
@@ -237,21 +221,13 @@ public void testDelete() throws Exception { | |
|
||
@Test | ||
public void testDeleteNoAuth() throws Exception { | ||
User user = generateUser(); | ||
repository.save(user); | ||
|
||
var request = delete("/api/users/" + user.getId()); | ||
mockMvc.perform(request) | ||
.andExpect(status().isUnauthorized()); | ||
} | ||
|
||
@Test | ||
public void testDeleteForbidden() throws Exception { | ||
User user = generateUser(); | ||
User wrong = generateUser(); | ||
repository.save(wrong); | ||
repository.save(user); | ||
|
||
var request = delete("/api/users/" + user.getId()).with(user(wrong)); | ||
mockMvc.perform(request) | ||
.andExpect(status().isForbidden()); | ||
|