/
UserServiceTest.java
117 lines (96 loc) · 4.46 KB
/
UserServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import com.despegar.http.client.GetMethod;
import com.despegar.http.client.HttpResponse;
import com.despegar.http.client.PostMethod;
import com.despegar.sparkjava.test.SparkServer;
import com.gojek.ApplicationConfiguration;
import com.gojek.Figaro;
import com.google.gson.Gson;
import com.testpyramid.UserService;
import helpers.TestDataHelper;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import spark.servlet.SparkApplication;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class UserServiceTest {
private static final Gson gson = new Gson();
private static final ApplicationConfiguration config = Figaro.configure(null);
public static class UserServiceTestSparkApplication implements SparkApplication {
@Override
public void init() {
UserService.main(new String[]{});
}
}
@ClassRule
public static SparkServer<UserServiceTestSparkApplication> testServer =
new SparkServer<>(UserServiceTestSparkApplication.class, config.getValueAsInt("PORT"));
@Before
public void setUp() {
TestDataHelper.cleanDb();
}
@Test
public void testPing() throws Exception {
GetMethod get = testServer.get("/ping", false);
HttpResponse httpResponse = testServer.execute(get);
assertEquals(200, httpResponse.code());
assertEquals("pong", new String(httpResponse.body()));
}
@Test
public void testLoginSucceedsForValidUserAndReturnsAllUserDetailsExceptPassword() throws Exception {
String name = "Tom";
String email = "tom@test.com";
String active = "true";
String emailVerified = "true";
TestDataHelper.createUser(name, email, "pwd", active, emailVerified);
PostMethod post = testServer.post("/login", "{\"email\":\"tom@test.com\",\"password\":\"pwd\"}", true);
HttpResponse httpResponse = testServer.execute(post);
assertEquals(200, httpResponse.code());
Map responseBody = gson.fromJson(new String(httpResponse.body()), Map.class);
assertEquals(name, responseBody.get("name"));
assertEquals(email, responseBody.get("email"));
assertEquals(active, responseBody.get("active"));
assertEquals(emailVerified, responseBody.get("email_verified"));
assertTrue(responseBody.containsKey("id"));
assertTrue(responseBody.containsKey("auth_token"));
assertFalse(responseBody.containsKey("password"));
}
@Test
public void testLoginFailsIfUserNotFound() throws Exception {
PostMethod post = testServer.post("/login", "{\"email\":\"user\",\"password\":\"abcd\"}", true);
HttpResponse httpResponse = testServer.execute(post);
assertEquals(401, httpResponse.code());
assertEquals("Unauthorized", httpResponse.message());
assertTrue((httpResponse.body()).length == 0);
}
@Test
public void testLoginFailsIfUserNotActive() throws Exception {
TestDataHelper.createUser("Tom", "tom@test.com", "pwd", "false", "true");
PostMethod post = testServer.post("/login", "{\"email\":\"tom@test.com\",\"password\":\"pwd\"}", true);
HttpResponse httpResponse = testServer.execute(post);
assertEquals(401, httpResponse.code());
assertEquals("Unauthorized", httpResponse.message());
assertTrue((httpResponse.body()).length == 0);
}
@Test
public void testLoginSucceedsForActiveButNotEmailVerifiedUser() throws Exception {
String name = "Tom";
String email = "tom@test.com";
String active = "true";
String emailVerified = "false";
TestDataHelper.createUser(name, email, "pwd", active, emailVerified);
PostMethod post = testServer.post("/login", "{\"email\":\"tom@test.com\",\"password\":\"pwd\"}", true);
HttpResponse httpResponse = testServer.execute(post);
assertEquals(200, httpResponse.code());
Map responseBody = gson.fromJson(new String(httpResponse.body()), Map.class);
assertEquals(name, responseBody.get("name"));
assertEquals(email, responseBody.get("email"));
assertEquals(active, responseBody.get("active"));
assertEquals(emailVerified, responseBody.get("email_verified"));
assertTrue(responseBody.containsKey("id"));
assertTrue(responseBody.containsKey("auth_token"));
assertFalse(responseBody.containsKey("password"));
}
}