From 8ea92dbc9d7ecd83bc832419e810461363273ed4 Mon Sep 17 00:00:00 2001 From: arios Date: Sun, 7 Jan 2024 12:57:06 -0300 Subject: [PATCH] selectBySlug test implemented --- .../nomisrev/routes/ArticlesRouteSpec.kt | 68 ++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/src/test/kotlin/io/github/nomisrev/routes/ArticlesRouteSpec.kt b/src/test/kotlin/io/github/nomisrev/routes/ArticlesRouteSpec.kt index 9de77e6..c0b30de 100644 --- a/src/test/kotlin/io/github/nomisrev/routes/ArticlesRouteSpec.kt +++ b/src/test/kotlin/io/github/nomisrev/routes/ArticlesRouteSpec.kt @@ -3,8 +3,11 @@ 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 @@ -12,7 +15,9 @@ 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({ @@ -20,12 +25,39 @@ class ArticlesRouteSpec : val validUsername = "username2" val validEmail = "valid2@domain.com" val validPw = "123456789" + // User 3 + val validUsername3 = "username3" + val validEmail3 = "valid3@domain.com" + 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")) @@ -39,7 +71,7 @@ class ArticlesRouteSpec : "Can get an article by slug" { withServer { dependencies -> - val userId = + val user1Id = dependencies.userService .register(RegisterUser(validUsername, validEmail, validPw)) .flatMap { JWT.decodeT(it.value, JWSHMAC512Algorithm) } @@ -49,7 +81,7 @@ class ArticlesRouteSpec : val article = dependencies.articleService .createArticle( - CreateArticle(UserId(userId), validTitle, validDescription, validBody, validTags) + CreateArticle(UserId(user1Id), validTitle, validDescription, validBody, validTags) ) .shouldBeRight() @@ -59,4 +91,36 @@ class ArticlesRouteSpec : assert(response.body().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().comments == emptyList()) + } + } + + "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) + } + } })