-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,29 +3,61 @@ package io.github.nomisrev.routes | |
import arrow.core.flatMap | ||
import io.github.nefilim.kjwt.JWSHMAC512Algorithm | ||
import io.github.nefilim.kjwt.JWT | ||
import io.github.nomisrev.KotestProject | ||
import io.github.nomisrev.auth.JwtToken | ||
import io.github.nomisrev.repo.UserId | ||
import io.github.nomisrev.service.CreateArticle | ||
import io.github.nomisrev.service.Login | ||
import io.github.nomisrev.service.RegisterUser | ||
import io.github.nomisrev.withServer | ||
import io.kotest.assertions.arrow.core.shouldBeRight | ||
import io.kotest.assertions.arrow.core.shouldBeSome | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.ktor.client.call.body | ||
import io.ktor.client.plugins.resources.get | ||
import io.ktor.client.request.bearerAuth | ||
import io.ktor.http.HttpStatusCode | ||
import kotlin.properties.Delegates | ||
|
||
class ArticlesRouteSpec : | ||
StringSpec({ | ||
// User | ||
val validUsername = "username2" | ||
val validEmail = "[email protected]" | ||
val validPw = "123456789" | ||
// User 3 | ||
val validUsername3 = "username3" | ||
val validEmail3 = "[email protected]" | ||
val validPw3 = "123456789" | ||
|
||
// Article | ||
val validTags = setOf("arrow", "kotlin", "ktor", "sqldelight") | ||
val validTitle = "Fake Article Arrow " | ||
val validDescription = "This is a fake article description." | ||
val validBody = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." | ||
|
||
var token: JwtToken by Delegates.notNull() | ||
var userId: UserId by Delegates.notNull() | ||
|
||
beforeAny { | ||
KotestProject.dependencies | ||
.get() | ||
.userService | ||
.register(RegisterUser(validUsername3, validEmail3, validPw3)) | ||
.shouldBeRight() | ||
} | ||
|
||
beforeTest { | ||
token = | ||
KotestProject.dependencies | ||
.get() | ||
.userService | ||
.login(Login(validEmail3, validPw3)) | ||
.shouldBeRight() | ||
.first | ||
userId = KotestProject.dependencies.get().jwtService.verifyJwtToken(token).shouldBeRight() | ||
} | ||
|
||
"Article by slug not found" { | ||
withServer { | ||
val response = get(ArticlesResource.Slug(slug = "slug")) | ||
|
@@ -59,4 +91,36 @@ class ArticlesRouteSpec : | |
assert(response.body<SingleArticleResponse>().article == article) | ||
} | ||
} | ||
|
||
"can get comments for an article by slug when authenticated" { | ||
withServer { dependencies -> | ||
val article = | ||
dependencies.articleService | ||
.createArticle( | ||
CreateArticle(userId, validTitle, validDescription, validBody, validTags) | ||
) | ||
.shouldBeRight() | ||
|
||
val response = | ||
get(ArticlesResource.Comments(slug = article.slug)) { bearerAuth(token.value) } | ||
|
||
assert(response.status == HttpStatusCode.OK) | ||
assert(response.body<MultipleCommentsResponse>().comments == emptyList<Comment>()) | ||
} | ||
} | ||
|
||
"can not get comments for an article when not authenticated" { | ||
withServer { dependencies -> | ||
val article = | ||
dependencies.articleService | ||
.createArticle( | ||
CreateArticle(userId, validTitle, validDescription, validBody, validTags) | ||
) | ||
.shouldBeRight() | ||
|
||
val response = get(ArticlesResource.Comments(slug = article.slug)) | ||
|
||
assert(response.status == HttpStatusCode.Unauthorized) | ||
} | ||
} | ||
}) |