Skip to content

Commit 88faa85

Browse files
committed
Setting up test environment and fixing failing test
1 parent ca0db06 commit 88faa85

File tree

10 files changed

+69
-35
lines changed

10 files changed

+69
-35
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ build/
33
!.gitignore
44
!.github/workflows/build.yml
55
src/main/resources/application.conf
6+
/src/test/resources/application.conf

build.gradle.kts

+9
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@ val koin_version: String by project
66
plugins {
77
kotlin("jvm") version "1.6.10"
88
kotlin("plugin.serialization") version "1.6.10"
9+
id("com.github.johnrengelman.shadow") version "7.0.0"
910
application
1011
}
1112

1213
group = "com.runetopic.website"
1314
version = "1.0.0-SNAPSHOT"
1415

16+
tasks{
17+
shadowJar {
18+
manifest {
19+
attributes(Pair("Main-Class", "com.runetopic.ApplicationKt"))
20+
}
21+
}
22+
}
23+
1524
application {
1625
mainClass.set("com.runetopic.ApplicationKt")
1726
}

src/main/kotlin/com/runetopic/Application.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.runetopic
22

33
import com.runetopic.plugins.*
44
import com.runetopic.plugins.routing.configureRouting
5-
import installCors
5+
import com.runetopic.plugins.installCors
66
import io.ktor.application.*
77
import io.ktor.server.netty.*
88

src/main/kotlin/com/runetopic/plugins/Cors.kt

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package com.runetopic.plugins
2+
13
import io.ktor.application.*
24
import io.ktor.features.*
35
import io.ktor.http.*

src/main/kotlin/com/runetopic/plugins/Security.kt

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import java.util.*
1010
import javax.crypto.SecretKey
1111

1212
fun Application.installJWT() {
13-
14-
1513
install(Authentication) {
1614
jwt(Authentications.LOGGED_IN) {
1715
verifier(JWT.require(Algorithm.HMAC256(environment.config.property("jwt.secret").getString())).build())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.runetopic
2+
3+
import io.ktor.application.*
4+
import io.ktor.config.*
5+
6+
object TestEnvironment: (Application) -> Unit {
7+
const val JWT_PROPERTY = "jwt.secret"
8+
const val TEST_KEY = "testSecretKey"
9+
10+
override fun invoke(application: Application) {
11+
(application.environment.config as MapApplicationConfig).apply {
12+
put(JWT_PROPERTY, TEST_KEY)
13+
}
14+
application.module()
15+
}
16+
}

src/test/kotlin/com/runetopic/login/LoginControllerTest.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.runetopic.login
22

33
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
4+
import com.runetopic.TestEnvironment
45
import com.runetopic.api.login.LoginCredentials
56
import com.runetopic.module
67
import io.ktor.application.*
@@ -17,7 +18,7 @@ import kotlin.test.assertEquals
1718
class LoginControllerTest {
1819

1920
@Test
20-
fun `test login user does not exist`() = withTestApplication(Application::module) {
21+
fun `test login user does not exist`() = withTestApplication(TestEnvironment) {
2122
val credentials = mockk<LoginCredentials>()
2223
every { credentials.username } returns "Jordan"
2324
every { credentials.password } returns "passwordtest"
@@ -30,4 +31,4 @@ class LoginControllerTest {
3031
assertEquals(response.content, "Invalid username and/or password.")
3132
}
3233
}
33-
}
34+
}

src/test/kotlin/com/runetopic/tools/npc/NpcControllerTest.kt

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.runetopic.tools.npc
22

33
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
4+
import com.runetopic.TestEnvironment
5+
import com.runetopic.TestEnvironment.TEST_KEY
46
import com.runetopic.api.tools.npc.Npc
57
import com.runetopic.api.tools.npc.NpcStorage
68
import com.runetopic.module
@@ -23,34 +25,34 @@ import kotlin.test.assertEquals
2325
class NpcControllerTest {
2426

2527
@BeforeTest
26-
fun `clear npc storage`() = withTestApplication(Application::module) {
28+
fun `clear npc storage`() = withTestApplication(TestEnvironment) {
2729
with(application.inject<NpcStorage>()) { value.storage.clear() }
2830
}
2931

3032
@Test
31-
fun `test not authorized`() = withTestApplication(Application::module) {
33+
fun `test not authorized`() = withTestApplication(TestEnvironment) {
3234
with(handleRequest(HttpMethod.Get, "/api/tools/npcs")) {
3335
assertEquals(HttpStatusCode.Unauthorized, response.status())
3436
}
3537
}
3638

3739
@Test
38-
fun `test get npcs empty`() = withTestApplication(Application::module) {
40+
fun `test get npcs empty`() = withTestApplication(TestEnvironment) {
3941
with(handleRequest(HttpMethod.Get, "/api/tools/npcs") {
40-
addHeader("Authorization", "Bearer ${loginToken("test")}")
42+
addHeader("Authorization", "Bearer ${loginToken("test", TEST_KEY)}")
4143
}) {
4244
assertEquals(HttpStatusCode.NotFound, response.status())
4345
}
4446
}
4547

4648
@Test
47-
fun `test post npc`() = withTestApplication(Application::module) {
49+
fun `test post npc`() = withTestApplication(TestEnvironment) {
4850
val npc = mockk<Npc>()
4951
every { npc.id } returns 1
5052
every { npc.name } returns "Hans"
5153

5254
with(handleRequest(HttpMethod.Post, "/api/tools/npcs") {
53-
addHeader("Authorization", "Bearer ${loginToken("test")}")
55+
addHeader("Authorization", "Bearer ${loginToken("test", TEST_KEY)}")
5456
addHeader(HttpHeaders.ContentType, "application/json")
5557
setBody(jacksonObjectMapper().writeValueAsString(npc))
5658
}) {
@@ -61,21 +63,21 @@ class NpcControllerTest {
6163
}
6264

6365
@Test
64-
fun `test get npcs`() = withTestApplication(Application::module) {
66+
fun `test get npcs`() = withTestApplication(TestEnvironment) {
6567
val npc = mockk<Npc>()
6668
every { npc.id } returns 1
6769
every { npc.name } returns "Hans"
6870

6971
with(handleRequest(HttpMethod.Post, "/api/tools/npcs") {
70-
addHeader("Authorization", "Bearer ${loginToken("test")}")
72+
addHeader("Authorization", "Bearer ${loginToken("test", TEST_KEY)}")
7173
addHeader(HttpHeaders.ContentType, "application/json")
7274
setBody(jacksonObjectMapper().writeValueAsString(npc))
7375
}) {
7476
assertEquals(HttpStatusCode.Created, response.status())
7577
}
7678

7779
with(handleRequest(HttpMethod.Get, "/api/tools/npcs") {
78-
addHeader("Authorization", "Bearer ${loginToken("test")}")
80+
addHeader("Authorization", "Bearer ${loginToken("test", TEST_KEY)}")
7981
}) {
8082
with(jacksonObjectMapper().readValue(response.content, Array<Npc>::class.java).first()) {
8183
assertEquals(npc.id, id)

src/test/kotlin/com/runetopic/tools/obj/ObjControllerTest.kt

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.runetopic.tools.obj
22

33
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
4+
import com.runetopic.TestEnvironment
5+
import com.runetopic.TestEnvironment.TEST_KEY
46
import com.runetopic.api.tools.obj.Obj
57
import com.runetopic.api.tools.obj.ObjStorage
6-
import com.runetopic.module
78
import com.runetopic.plugins.loginToken
89
import io.ktor.application.*
10+
import io.ktor.config.*
911
import io.ktor.http.*
1012
import io.ktor.server.testing.*
1113
import io.mockk.*
@@ -18,36 +20,35 @@ import kotlin.test.assertEquals
1820
* @author Jordan Abraham
1921
*/
2022
class ObjControllerTest {
21-
2223
@BeforeTest
23-
fun `clear obj storage`() = withTestApplication(Application::module) {
24+
fun `clear obj storage`() = withTestApplication(TestEnvironment) {
2425
with(application.inject<ObjStorage>()) { value.storage.clear() }
2526
}
2627

2728
@Test
28-
fun `test not authorized`() = withTestApplication(Application::module) {
29+
fun `test not authorized`() = withTestApplication(TestEnvironment) {
2930
with(handleRequest(HttpMethod.Get, "/api/tools/objs")) {
3031
assertEquals(HttpStatusCode.Unauthorized, response.status())
3132
}
3233
}
3334

3435
@Test
35-
fun `test get objs empty`() = withTestApplication(Application::module) {
36+
fun `test get objs empty`() = withTestApplication(TestEnvironment) {
3637
with(handleRequest(HttpMethod.Get, "/api/tools/objs") {
37-
addHeader("Authorization", "Bearer ${loginToken("test")}")
38+
addHeader("Authorization", "Bearer ${loginToken("test", TEST_KEY)}")
3839
}) {
3940
assertEquals(HttpStatusCode.NotFound, response.status())
4041
}
4142
}
4243

4344
@Test
44-
fun `test post obj`() = withTestApplication(Application::module) {
45+
fun `test post obj`() = withTestApplication(TestEnvironment) {
4546
val obj = mockk<Obj>()
4647
every { obj.id } returns 4151
4748
every { obj.name } returns "Abyssal Whip"
4849

4950
with(handleRequest(HttpMethod.Post, "/api/tools/objs") {
50-
addHeader("Authorization", "Bearer ${loginToken("test")}")
51+
addHeader("Authorization", "Bearer ${loginToken("test", TEST_KEY)}")
5152
addHeader(HttpHeaders.ContentType, "application/json")
5253
setBody(jacksonObjectMapper().writeValueAsString(obj))
5354
}) {
@@ -58,21 +59,21 @@ class ObjControllerTest {
5859
}
5960

6061
@Test
61-
fun `test get objs`() = withTestApplication(Application::module) {
62+
fun `test get objs`() = withTestApplication(TestEnvironment) {
6263
val obj = mockk<Obj>()
6364
every { obj.id } returns 4151
6465
every { obj.name } returns "Abyssal Whip"
6566

6667
with(handleRequest(HttpMethod.Post, "/api/tools/objs") {
67-
addHeader("Authorization", "Bearer ${loginToken("test")}")
68+
addHeader("Authorization", "Bearer ${loginToken("test", TEST_KEY)}")
6869
addHeader(HttpHeaders.ContentType, "application/json")
6970
setBody(jacksonObjectMapper().writeValueAsString(obj))
7071
}) {
7172
assertEquals(HttpStatusCode.Created, response.status())
7273
}
7374

7475
with(handleRequest(HttpMethod.Get, "/api/tools/objs") {
75-
addHeader("Authorization", "Bearer ${loginToken("test")}")
76+
addHeader("Authorization", "Bearer ${loginToken("test", TEST_KEY)}")
7677
}) {
7778
with(jacksonObjectMapper().readValue(response.content, Array<Obj>::class.java).first()) {
7879
assertEquals(obj.id, id)
@@ -87,3 +88,5 @@ class ObjControllerTest {
8788
confirmVerified()
8889
}
8990
}
91+
92+

src/test/kotlin/com/runetopic/topics/TopicControllerTest.kt

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.runetopic.topics
22

33
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
4+
import com.runetopic.TestEnvironment
5+
import com.runetopic.TestEnvironment.TEST_KEY
46
import com.runetopic.api.topics.Topic
57
import com.runetopic.api.topics.TopicStorage
68
import com.runetopic.module
@@ -25,28 +27,28 @@ import kotlin.test.assertNotEquals
2527
class TopicControllerTest {
2628

2729
@BeforeTest
28-
fun `clear topic storage`() = withTestApplication(Application::module) {
30+
fun `clear topic storage`() = withTestApplication(TestEnvironment) {
2931
with(application.inject<TopicStorage>()) { value.storage.clear() }
3032
}
3133

3234
@Test
33-
fun `test not authorized`() = withTestApplication(Application::module) {
35+
fun `test not authorized`() = withTestApplication(TestEnvironment) {
3436
with(handleRequest(HttpMethod.Get, "/api/topics")) {
3537
assertEquals(HttpStatusCode.Unauthorized, response.status())
3638
}
3739
}
3840

3941
@Test
40-
fun `test get topics empty`() = withTestApplication(Application::module) {
42+
fun `test get topics empty`() = withTestApplication(TestEnvironment) {
4143
with(handleRequest(HttpMethod.Get, "/api/topics") {
42-
addHeader("Authorization", "Bearer ${loginToken("test")}")
44+
addHeader("Authorization", "Bearer ${loginToken("test", TEST_KEY)}")
4345
}) {
4446
assertEquals(HttpStatusCode.NotFound, response.status())
4547
}
4648
}
4749

4850
@Test
49-
fun `test post npc`() = withTestApplication(Application::module) {
51+
fun `test post npc`() = withTestApplication(TestEnvironment) {
5052
val topic = mockk<Topic>()
5153
every { topic.id } returns UUID.randomUUID()
5254
every { topic.title } returns "Test Title"
@@ -55,7 +57,7 @@ class TopicControllerTest {
5557
every { topic.private } returns false
5658

5759
with(handleRequest(HttpMethod.Post, "/api/topics") {
58-
addHeader("Authorization", "Bearer ${loginToken("test")}")
60+
addHeader("Authorization", "Bearer ${loginToken("test", TEST_KEY)}")
5961
addHeader(HttpHeaders.ContentType, "application/json")
6062
setBody(jacksonObjectMapper().writeValueAsString(topic))
6163
}) {
@@ -66,7 +68,7 @@ class TopicControllerTest {
6668
}
6769

6870
@Test
69-
fun `test get topics`() = withTestApplication(Application::module) {
71+
fun `test get topics`() = withTestApplication(TestEnvironment) {
7072
val topic = mockk<Topic>()
7173
every { topic.id } returns UUID.randomUUID()
7274
every { topic.title } returns "Test Title"
@@ -75,7 +77,7 @@ class TopicControllerTest {
7577
every { topic.private } returns false
7678

7779
with(handleRequest(HttpMethod.Post, "/api/topics") {
78-
addHeader("Authorization", "Bearer ${loginToken("test")}")
80+
addHeader("Authorization", "Bearer ${loginToken("test", TEST_KEY)}")
7981
addHeader(HttpHeaders.ContentType, "application/json")
8082
setBody(jacksonObjectMapper().writeValueAsString(topic))
8183
}) {
@@ -85,7 +87,7 @@ class TopicControllerTest {
8587
confirmVerified()
8688

8789
with(handleRequest(HttpMethod.Get, "/api/topics/${topic.id}") {
88-
addHeader("Authorization", "Bearer ${loginToken("test")}")
90+
addHeader("Authorization", "Bearer ${loginToken("test", TEST_KEY)}")
8991
}) {
9092
assertEquals(jacksonObjectMapper().readValue(response.content, Topic::class.java), topic)
9193
assertEquals(HttpStatusCode.OK, response.status())
@@ -95,7 +97,7 @@ class TopicControllerTest {
9597
confirmVerified()
9698

9799
with(handleRequest(HttpMethod.Put, "/api/topics/${topic.id}") {
98-
addHeader("Authorization", "Bearer ${loginToken("test")}")
100+
addHeader("Authorization", "Bearer ${loginToken("test", TEST_KEY)}")
99101
addHeader(HttpHeaders.ContentType, "application/json")
100102
setBody(
101103
jacksonObjectMapper().writeValueAsString(

0 commit comments

Comments
 (0)