Skip to content

Commit 18f5f47

Browse files
committed
Write unit tests
1 parent 126caaa commit 18f5f47

File tree

10 files changed

+479
-0
lines changed

10 files changed

+479
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.pearx.kpastebin.test.helper
2+
3+
import kotlinx.coroutines.CoroutineScope
4+
import kotlinx.coroutines.runBlocking
5+
6+
actual fun runTest(f: suspend CoroutineScope.() -> Unit) = runBlocking(block = f)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package net.pearx.kpastebin.test
2+
3+
import net.pearx.kpastebin.*
4+
import net.pearx.kpastebin.model.ExpireDate
5+
import net.pearx.kpastebin.model.Privacy
6+
import net.pearx.kpastebin.test.helper.createClient
7+
import net.pearx.kpastebin.test.helper.lastCreatedPaste
8+
import net.pearx.kpastebin.test.helper.pastes
9+
import net.pearx.kpastebin.test.helper.runTest
10+
import kotlin.test.Test
11+
import kotlin.test.assertEquals
12+
import kotlin.test.assertFailsWith
13+
14+
class AnonymousTest {
15+
@Test
16+
fun `getting non-existing paste fails with 404`() = runTest {
17+
assertFailsWith<PasteNotFoundException> {
18+
createClient("").getPaste("123")
19+
}
20+
}
21+
22+
@Test
23+
fun `getting paste`() = runTest {
24+
val cl = createClient("")
25+
for ((pasteKey, pasteCode) in pastes) {
26+
assertEquals(pasteCode, cl.getPaste(pasteKey))
27+
}
28+
}
29+
30+
@Test
31+
fun `creating paste with invalid devKey fails`() = runTest {
32+
assertFailsWith<InvalidDevKeyException> {
33+
createClient("").createPaste("SoMeThInG")
34+
}
35+
}
36+
37+
@Test
38+
fun `creating paste with empty body`() = runTest {
39+
assertFailsWith<EmptyPasteException> {
40+
createClient().createPaste("")
41+
}
42+
}
43+
44+
@Test
45+
fun `creating paste with invalid format`() = runTest {
46+
assertFailsWith<InvalidPasteFormatException> {
47+
createClient().createPaste("test", format = "notatext")
48+
}
49+
}
50+
51+
@Test
52+
fun `creating paste with exceeding size limit`() = runTest {
53+
assertFailsWith<PasteSizeException> {
54+
createClient().createPaste("a".repeat(2048))
55+
}
56+
}
57+
58+
@Test
59+
fun `creating paste`() = runTest {
60+
val text = """
61+
class Main {
62+
public static void main() {
63+
System.out.println("hello world");
64+
}
65+
}
66+
""".trimIndent()
67+
val title = "HELLO WORLD!"
68+
createClient().createPaste(text, title, "java", Privacy.UNLISTED, ExpireDate.ONE_WEEK)
69+
assertEquals(text, lastCreatedPaste["code"])
70+
assertEquals(title, lastCreatedPaste["name"])
71+
assertEquals("java", lastCreatedPaste["format"])
72+
assertEquals(ExpireDate.ONE_WEEK.code, lastCreatedPaste["expireDate"])
73+
assertEquals(Privacy.UNLISTED.ordinal.toString(), lastCreatedPaste["private"])
74+
}
75+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package net.pearx.kpastebin.test
2+
3+
import net.pearx.kpastebin.InvalidUserKeyException
4+
import net.pearx.kpastebin.test.helper.createClient
5+
import net.pearx.kpastebin.test.helper.pastes
6+
import net.pearx.kpastebin.test.helper.runTest
7+
import kotlin.test.Test
8+
import kotlin.test.assertFailsWith
9+
10+
class InvalidUserKeyTest {
11+
@Test
12+
fun `creating paste`() = runTest {
13+
assertFailsWith<InvalidUserKeyException> {
14+
createClient(userKey = "invalid").createPaste("TEST")
15+
}
16+
}
17+
18+
@Test
19+
fun `listing pastes`() = runTest {
20+
assertFailsWith<InvalidUserKeyException> {
21+
createClient(userKey = "invalid").listPastes()
22+
}
23+
}
24+
25+
@Test
26+
fun `deleting paste`() = runTest {
27+
assertFailsWith<InvalidUserKeyException> {
28+
createClient(userKey = "invalid").deletePaste(pastes.keys.first())
29+
}
30+
}
31+
32+
@Test
33+
fun `getting user details`() = runTest {
34+
assertFailsWith<InvalidUserKeyException> {
35+
createClient(userKey = "invalid").getUserDetails()
36+
}
37+
}
38+
39+
@Test
40+
fun `getting paste`() = runTest {
41+
assertFailsWith<InvalidUserKeyException> {
42+
createClient(userKey = "invalid").getPaste(pastes.keys.first())
43+
}
44+
}
45+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package net.pearx.kpastebin.test
2+
3+
import net.pearx.kpastebin.PasteNotFoundException
4+
import net.pearx.kpastebin.model.*
5+
import net.pearx.kpastebin.test.helper.createClient
6+
import net.pearx.kpastebin.test.helper.globalUserKey
7+
import net.pearx.kpastebin.test.helper.pastes
8+
import net.pearx.kpastebin.test.helper.runTest
9+
import kotlin.test.Test
10+
import kotlin.test.assertEquals
11+
import kotlin.test.assertFailsWith
12+
13+
class LoggedInTest {
14+
@Test
15+
fun `creating paste`() = runTest {
16+
createClient(userKey = globalUserKey).createPaste("something")
17+
}
18+
19+
@Test
20+
fun `listing pastes with invalid results limit`() = runTest {
21+
assertFailsWith<IllegalArgumentException> {
22+
createClient(userKey = globalUserKey).listPastes(resultsLimit = 0)
23+
}
24+
assertFailsWith<IllegalArgumentException> {
25+
createClient(userKey = globalUserKey).listPastes(resultsLimit = 1001)
26+
}
27+
}
28+
29+
@Test
30+
fun `listing pastes`() = runTest {
31+
assertEquals(listOf(
32+
PasteDetails(
33+
"0b42rwhf",
34+
1297953260,
35+
"javascript test",
36+
15,
37+
1297956860,
38+
Privacy.PUBLIC,
39+
"JavaScript",
40+
"javascript",
41+
"https://pastebin.com/0b42rwhf",
42+
15
43+
),
44+
PasteDetails(
45+
"0C343n0d",
46+
1297694343,
47+
"Welcome To Pastebin V3",
48+
490,
49+
0,
50+
Privacy.PUBLIC,
51+
"None",
52+
"text",
53+
"https://pastebin.com/0C343n0d",
54+
65
55+
)
56+
), createClient(userKey = globalUserKey).listPastes())
57+
}
58+
59+
@Test
60+
fun `deleting non-existing paste`() = runTest {
61+
assertFailsWith<PasteNotFoundException> {
62+
createClient(userKey = globalUserKey).deletePaste("TEST")
63+
}
64+
}
65+
66+
67+
@Test
68+
fun `deleting paste`() = runTest {
69+
createClient(userKey = globalUserKey).deletePaste(pastes.keys.first())
70+
}
71+
72+
@Test
73+
fun `getting non-existing paste`() = runTest {
74+
assertFailsWith<PasteNotFoundException> {
75+
createClient(userKey = globalUserKey).getPaste("TEST")
76+
}
77+
}
78+
79+
@Test
80+
fun `getting user details`() = runTest {
81+
assertEquals(UserDetails(
82+
"wiz_kitty",
83+
"text",
84+
ExpireDate.NEVER,
85+
"https://pastebin.com/cache/a/1.jpg",
86+
Privacy.UNLISTED,
87+
"https://myawesomesite.com",
88+
"oh@dear.com",
89+
"New York",
90+
AccountType.PRO
91+
), createClient(userKey = globalUserKey).getUserDetails())
92+
}
93+
94+
@Test
95+
fun `getting pastes`() = runTest {
96+
for ((key, text) in pastes) {
97+
assertEquals(text, createClient(userKey = globalUserKey).getPaste(key))
98+
}
99+
}
100+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.pearx.kpastebin.test
2+
3+
import net.pearx.kpastebin.AccountNotActiveException
4+
import net.pearx.kpastebin.InvalidLoginException
5+
import net.pearx.kpastebin.test.helper.*
6+
import kotlin.test.Test
7+
import kotlin.test.assertEquals
8+
import kotlin.test.assertFailsWith
9+
10+
class LoginTest {
11+
@Test
12+
fun `logging in with inactive credentials`() = runTest {
13+
assertFailsWith<AccountNotActiveException> {
14+
createClient().login(inactiveCredentials.first, inactiveCredentials.second)
15+
}
16+
}
17+
18+
@Test
19+
fun `logging in with invalid credentials`() = runTest {
20+
assertFailsWith<InvalidLoginException> {
21+
createClient().login("ABC", "DEF")
22+
}
23+
}
24+
25+
@Test
26+
fun `logging in`() = runTest {
27+
val cl = createClient()
28+
cl.login(credentials.first, credentials.second)
29+
assertEquals(globalUserKey, cl.userKey)
30+
}
31+
}

0 commit comments

Comments
 (0)