Token response

This commit is contained in:
Filip Izydorczyk 2020-12-07 21:39:47 +01:00
parent b412ee2a5b
commit 3061fb1edf
2 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package com.plannaplan.responses.models;
import com.plannaplan.entities.User;
public class TokenResponse {
private String token;
private Long id;
private String authorityRole;
private String email;
public TokenResponse(User user) {
this.id = user.getId();
this.authorityRole = user.getRole().toString();
this.email = user.getEmail();
this.token = user.getToken();
}
public String getEmail() {
return email;
}
public String getAuthorityRole() {
return authorityRole;
}
public Long getId() {
return id;
}
public String getToken() {
return token;
}
}

View File

@ -0,0 +1,69 @@
package com.plannaplan.responses.models;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Field;
import com.plannaplan.entities.User;
import com.plannaplan.types.UserRoles;
import org.junit.Test;
public class TokenResponseTest {
public TokenResponse testMapUserEntiutyToTokenResponse(UserRoles role)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
final String token = "totalnie-prawdziwy-token";
final String mail = "shouldMapUserEntiutyToTokenResponse@TokenResponse.test";
final User userToMap = new User("Franek", "Kimono", mail, null, role);
Field reader = User.class.getDeclaredField("id");
reader.setAccessible(true);
reader.set(userToMap, Long.valueOf(12));
reader = User.class.getDeclaredField("token");
reader.setAccessible(true);
reader.set(userToMap, token);
final TokenResponse response = new TokenResponse(userToMap);
assertTrue(response.getId() == 12);
assertTrue(response.getToken().equals(token));
assertTrue(response.getEmail().equals(mail));
return response;
}
@Test
public void shouldMapAdminToTokenResonse()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
final TokenResponse response = testMapUserEntiutyToTokenResponse(UserRoles.ADMIN);
assertTrue(response.getAuthorityRole().equals("ADMIN"));
}
@Test
public void shouldMapStudentToTokenResonse()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
final TokenResponse response = testMapUserEntiutyToTokenResponse(UserRoles.STUDENT);
assertTrue(response.getAuthorityRole().equals("STUDENT"));
}
@Test
public void shouldMapDeaneryToTokenResonse()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
final TokenResponse response = testMapUserEntiutyToTokenResponse(UserRoles.DEANERY);
assertTrue(response.getAuthorityRole().equals("DEANERY"));
}
@Test
public void shouldMapTestUserToTokenResonse()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
final TokenResponse response = testMapUserEntiutyToTokenResponse(UserRoles.TEST_USER);
assertTrue(response.getAuthorityRole().equals("TEST_USER"));
}
}