Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
sergi-Jr committed Jun 21, 2024
1 parent 6d1d894 commit 63b2d3b
Showing 1 changed file with 28 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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();
Expand All @@ -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))
Expand All @@ -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())
Expand All @@ -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))
Expand All @@ -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());
Expand All @@ -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());
Expand Down

0 comments on commit 63b2d3b

Please sign in to comment.