-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthControllerTest.kt
More file actions
117 lines (92 loc) · 3.72 KB
/
AuthControllerTest.kt
File metadata and controls
117 lines (92 loc) · 3.72 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
114
115
116
117
package dev.robothanzo.werewolf.controller
import dev.robothanzo.werewolf.WerewolfApplication
import dev.robothanzo.werewolf.controller.dto.AuthData
import dev.robothanzo.werewolf.controller.dto.AuthResponse
import dev.robothanzo.werewolf.database.documents.AuthSession
import dev.robothanzo.werewolf.database.documents.UserRole
import jakarta.servlet.http.HttpSession
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.entities.Member
import net.dv8tion.jda.api.entities.User
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Mockito.*
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.whenever
import org.springframework.http.HttpStatus
import java.util.*
class AuthControllerTest {
@InjectMocks
private lateinit var authController: AuthController
@Mock
private lateinit var session: HttpSession
@Mock
private lateinit var jda: JDA
@BeforeEach
fun setup() {
MockitoAnnotations.openMocks(this)
WerewolfApplication.jda = jda
}
@Test
fun testSelectGuild_Success() {
val user = AuthSession(userId = "user1", guildId = null, role = UserRole.PENDING)
whenever(session.getAttribute("user")).thenReturn(user)
val guildId = "123"
val guildMock = mock(Guild::class.java)
val memberMock = mock(Member::class.java)
whenever(jda.getGuildById(123L)).thenReturn(guildMock)
whenever(guildMock.getMemberById("user1")).thenReturn(memberMock)
val response = authController.selectGuild(guildId, session)
assertEquals(HttpStatus.OK, response.statusCode)
val body = response.body as AuthResponse
assertEquals(guildId, body.data.user.guildId)
verify(session).setAttribute(eq("user"), any())
}
@Test
fun testSelectGuild_Unauthenticated() {
whenever(session.getAttribute("user")).thenReturn(null)
val response = authController.selectGuild("123", session)
assertEquals(HttpStatus.UNAUTHORIZED, response.statusCode)
}
@Test
fun testSelectGuild_GuildNotFound() {
val user = AuthSession(userId = "user1")
whenever(session.getAttribute("user")).thenReturn(user)
whenever(jda.getGuildById(123L)).thenReturn(null)
val response = authController.selectGuild("123", session)
assertEquals(HttpStatus.BAD_REQUEST, response.statusCode)
}
@Test
fun testMe_Authenticated() {
val user = AuthSession(userId = "user1", guildId = "123")
whenever(session.getAttribute("user")).thenReturn(user)
val discordUser = mock(User::class.java)
whenever(discordUser.name).thenReturn("TestUser")
whenever(discordUser.effectiveAvatarUrl).thenReturn("avatar.png")
whenever(jda.getUserById("user1")).thenReturn(discordUser)
val response = authController.me(session)
assertEquals(HttpStatus.OK, response.statusCode)
val body = response.body as AuthResponse
assertEquals("user1", body.data.user.userId)
assertEquals("TestUser", body.data.username)
}
@Test
fun testMe_Unauthenticated() {
whenever(session.getAttribute("user")).thenReturn(null)
val response = authController.me(session)
assertEquals(HttpStatus.UNAUTHORIZED, response.statusCode)
}
@Test
fun testLogout() {
val response = authController.logout(session)
assertEquals(HttpStatus.OK, response.statusCode)
verify(session).invalidate()
}
}