wedroid/wrapper/src/test/java/it/unisannio/ding/ids/wedroid/wrapper/api/UserServiceTest.java

204 lines
8.5 KiB
Java

package it.unisannio.ding.ids.wedroid.wrapper.api;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import it.unisannio.ding.ids.wedroid.wrapper.entity.Service;
import it.unisannio.ding.ids.wedroid.wrapper.entity.User;
import it.unisannio.ding.ids.wedroid.wrapper.entity.UserEmail;
import it.unisannio.ding.ids.wedroid.wrapper.entity.UserProfile;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class UserServiceTest {
private MockWebServer mockWebServer = new MockWebServer();
private UserService service = null;
@Before
public void setUp() {
try {
mockWebServer.start();
service = new Retrofit.Builder()
.baseUrl(mockWebServer.url("/"))
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(UserService.class);
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void teardown() {
try {
mockWebServer.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void getAllUsersTest() {
MockResponse response = new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(
"[{\"_id\":\"Si69gNgkJfQuk6uiJ\",\"username\":\"norangebit\"}," +
"{\"_id\":\"8rsnfEPkfMS49Pv6q\",\"username\":\"noemi\"}," +
"{\"_id\":\"jPdkf3a9bmfZWx3GR\",\"username\":\"umberto\"}]"
);
mockWebServer.enqueue(response);
try {
List<User> users = service.getAllUser().execute().body();
assertNotNull(users);
assertEquals("Si69gNgkJfQuk6uiJ", users.get(0).getId());
assertEquals("norangebit", users.get(0).getUsername());
assertEquals("8rsnfEPkfMS49Pv6q", users.get(1).getId());
assertEquals("noemi", users.get(1).getUsername());
assertEquals("jPdkf3a9bmfZWx3GR", users.get(2).getId());
assertEquals("umberto", users.get(2).getUsername());
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void getUser() {
MockResponse response = new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(user1);
mockWebServer.enqueue(response);
try {
User user = service.getUser("jPdkf3a9bmfZWx3GR").execute().body();
assert user != null;
List<UserEmail> userEmail = user.getEmails();
UserProfile userProfile = user.getProfile();
Service ser = user.getServices();
assertEquals("jPdkf3a9bmfZWx3GR", user.getId());
assertEquals("umberto", user.getUsername());
assertEquals("password", user.getAuthenticationMethod());
assertTrue(user.isAdmin());
//assertEquals("Mon Oct 14 20:14:38 CEST 2019", user.getCreatedAt());
assertFalse(userEmail.isEmpty());
assertEquals("my@email.com", userEmail.get(0).getEmail());
assertFalse(userEmail.get(0).isVerified());
assertEquals("BOARD_VIEW_SWIMLANES", userProfile.getBoardView().toString());
assertEquals("zo82BZYxFTNBpb7jX", userProfile.getCardTemplatesSwimlaneId());
assertEquals("_MODIFIEDAT", userProfile.getListSortBy().toString());
assertEquals(1, ser.getEmail().getVerificationTokens().size());
assertEquals("my@email.com",
ser.getEmail().getVerificationTokens().get(0).getAddress());
assertEquals("$2a$10$CRZrpT4x.VpG2FdJxR3rN.9m0NbQb0OPsSPBDAZukggxrskMtWA8.",
ser.getPassword().getBcrypt());
assertEquals(2, ser.getResume().getLoginTokens().size());
assertEquals("CY/PWeDa3fAkl+k94+GWzCtpB5nPcVxLzzzjXs4kI3A=", ser.getResume().getLoginTokens().get(0).getHashedToken());
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void getCurrentUser() {
MockResponse mockResponse = new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(user2);
mockWebServer.enqueue(mockResponse);
try {
User user = service.getCurrentUser().execute().body();
assert user != null;
UserProfile userProfile = user.getProfile();
assertNotNull(user);
assertEquals("jPdkf3a9bmfZWx3GR", user.getId());
assertEquals("umberto", user.getUsername());
assertEquals("password", user.getAuthenticationMethod());
assertTrue(user.isAdmin());
assertFalse(user.isLoginDisabled());
assertFalse(user.getEmails().isEmpty());
assertEquals(1, user.getEmails().size());
assertEquals("my@email.com", user.getEmails().get(0).getEmail());
assertNotNull(userProfile);
assertEquals("zo82BZYxFTNBpb7jX",
userProfile.getCardTemplatesSwimlaneId());
assertEquals("j6ZuPbwaN9nsCDxyS",
userProfile.getBoardTemplatesSwimlaneId());
} catch (IOException e) {
e.printStackTrace();
}
}
private static final String user1 = "{" +
"\"_id\":\"jPdkf3a9bmfZWx3GR\"," +
"\"username\":\"umberto\"," +
"\"emails\":[{\"address\":\"my@email.com\",\"verified\":false}]," +
"\"authenticationMethod\":\"password\"," +
"\"isAdmin\":\"true\"," +
"\"loginDisabled\":\"true\"," +
"\"profile\":{\"boardView\":\"board-view-swimlanes\",\"templatesBoardId\":\"eLvE8FnqvACfC9Rtb\"," +
"\"cardTemplatesSwimlaneId\":\"zo82BZYxFTNBpb7jX\",\"listTemplatesSwimlaneId\":\"Kja32A85P2HADWKFA\"," +
"\"boardTemplatesSwimlaneId\":\"j6ZuPbwaN9nsCDxyS\",\"listSortBy\":\"-modifiedAt\"}," +
"\"createdAt\":\"2019-10-14T18:14:38.249Z\"," +
"\"modifiedAt\":\"2019-11-09T17:55:36.976Z\"," +
"\"services\":{\n" +
" \"password\":{\n" +
" \"bcrypt\":\"$2a$10$CRZrpT4x.VpG2FdJxR3rN.9m0NbQb0OPsSPBDAZukggxrskMtWA8.\"\n" +
" },\n" +
" \"email\":{\n" +
" \"verificationTokens\":[\n" +
" {\n" +
" \"token\":\"8rzwpq_So2PVYHVSfrcc5f5QZnuV2wEtu7QRQGwOJx8\",\n" +
" \"address\":\"my@email.com\",\n" +
" \"when\":\"2017-09-13T06:45:53.157Z\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"resume\":{\n" +
" \"loginTokens\":[\n" +
" {\n" +
" \"when\":\"2017-09-13T06:45:53.265Z\",\n" +
" \"hashedToken\":\"CY/PWeDa3fAkl+k94+GWzCtpB5nPcVxLzzzjXs4kI3A=\"\n" +
" },\n" +
" {\n" +
" \"when\":\"2017-09-16T06:06:19.741Z\",\n" +
" \"hashedToken\":\"74MQNXfsgjkItx/gpgPb29Y0MSNAvBrsnSGQmr4YGvQ=\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"}\n" +
"}";
private static final String user2 = "{" +
"\"_id\":\"jPdkf3a9bmfZWx3GR\"," +
"\"createdAt\":\"2019-10-14T18:14:38.249Z\"," +
"\"username\":\"umberto\"," +
"\"emails\":[{\"address\":\"my@email.com\"," +
"\"verified\":false}]," +
"\"modifiedAt\":\"2019-11-09T17:55:36.976Z\"," +
"\"profile\":{\"boardView\":\"board-view-swimlanes\"," +
"\"templatesBoardId\":\"eLvE8FnqvACfC9Rtb\"," +
"\"cardTemplatesSwimlaneId\":\"zo82BZYxFTNBpb7jX\"," +
"\"listTemplatesSwimlaneId\":\"Kja32A85P2HADWKFA\"," +
"\"boardTemplatesSwimlaneId\":\"j6ZuPbwaN9nsCDxyS\"," +
"\"listSortBy\":\"-modifiedAt\"}," +
"\"authenticationMethod\":\"password\"," +
"\"isAdmin\":true," +
"\"loginDisabled\":false}";
}