-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsersControllerTest.kt
More file actions
113 lines (90 loc) · 4.15 KB
/
Copy pathUsersControllerTest.kt
File metadata and controls
113 lines (90 loc) · 4.15 KB
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
package org.genspectrum.dashboardsbackend.controller
import org.genspectrum.dashboardsbackend.api.UserSyncRequest
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.notNullValue
import org.hamcrest.Matchers.nullValue
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Import
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import java.util.UUID
@SpringBootTest
@AutoConfigureMockMvc
@Import(UsersClient::class)
class UsersControllerTest(@param:Autowired private val usersClient: UsersClient) {
@Test
fun `WHEN syncing a new user THEN creates user and returns it with internal ID`() {
val githubId = UUID.randomUUID().toString()
val request = UserSyncRequest(githubId = githubId, name = "Alice", email = "alice@example.com")
val user = usersClient.syncUser(request)
assertThat(user.id, notNullValue())
assertThat(user.githubId, equalTo(githubId))
assertThat(user.name, equalTo("Alice"))
assertThat(user.email, equalTo("alice@example.com"))
}
@Test
fun `WHEN syncing the same github ID twice THEN returns same internal ID`() {
val githubId = UUID.randomUUID().toString()
val request = UserSyncRequest(githubId = githubId, name = "Bob", email = null)
val first = usersClient.syncUser(request)
val second = usersClient.syncUser(request)
assertThat(first.id, equalTo(second.id))
}
@Test
fun `WHEN syncing with updated name THEN name is updated`() {
val githubId = UUID.randomUUID().toString()
usersClient.syncUser(UserSyncRequest(githubId = githubId, name = "Old Name", email = null))
val updated = usersClient.syncUser(UserSyncRequest(githubId = githubId, name = "New Name", email = null))
assertThat(updated.name, equalTo("New Name"))
}
@Test
fun `WHEN syncing without email THEN email is null`() {
val githubId = UUID.randomUUID().toString()
val user = usersClient.syncUser(UserSyncRequest(githubId = githubId, name = "Charlie", email = null))
assertThat(user.email, nullValue())
}
@Test
fun `WHEN getting user by ID THEN returns public user info`() {
val created = usersClient.syncUser(
UserSyncRequest(githubId = UUID.randomUUID().toString(), name = "Dave", email = null),
)
val fetched = usersClient.getUser(created.id)
assertThat(fetched.id, equalTo(created.id))
assertThat(fetched.name, equalTo("Dave"))
}
@Test
fun `WHEN getting user with nonexistent ID THEN returns 404`() {
usersClient.getUserRaw(999999999L)
.andExpect(status().isNotFound)
.andExpect(jsonPath("$.detail").value("User 999999999 not found"))
}
@Test
fun `WHEN getting me THEN returns own public user info`() {
val created = usersClient.syncUser(
UserSyncRequest(githubId = UUID.randomUUID().toString(), name = "Eve", email = null),
)
val me = usersClient.getMe(created.id)
assertThat(me.id, equalTo(created.id))
assertThat(me.name, equalTo("Eve"))
}
@Test
fun `WHEN getting me with nonexistent user ID THEN returns 404`() {
usersClient.getMeRaw(999999999L)
.andExpect(status().isNotFound)
.andExpect(jsonPath("$.detail").value("User 999999999 not found"))
}
@Test
fun `WHEN syncing two different github IDs THEN they get different internal IDs`() {
val first = usersClient.syncUser(
UserSyncRequest(githubId = UUID.randomUUID().toString(), name = "User1", email = null),
)
val second = usersClient.syncUser(
UserSyncRequest(githubId = UUID.randomUUID().toString(), name = "User2", email = null),
)
assertThat(first.id == second.id, equalTo(false))
}
}