Skip to content

Commit

Permalink
Add test for interacting with the API
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Sep 20, 2024
1 parent f77cd71 commit fb515bc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
1 change: 0 additions & 1 deletion app/src/test/kotlin/env/TestSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ val TestSettings = Environment.from(
"AWS_SECRET_ACCESS_KEY" to "secretKey",
"AWS_REGION" to US_EAST_1.value,
"AWS_BUCKET" to "http4kbox",
"OAUTH_CALLBACK_URI" to "http://localhost:8000/oauth/callback",
"OAUTH_CLIENT" to "user:password",
"API_KEY" to "http4kbox"
)
35 changes: 27 additions & 8 deletions app/src/test/kotlin/functional/Http4kboxTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,24 @@ import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.NOT_FOUND
import org.http4k.core.Status.Companion.OK
import org.http4k.core.Status.Companion.SEE_OTHER
import org.http4k.core.Uri
import org.http4k.core.then
import org.http4k.core.with
import org.http4k.filter.ClientFilters
import org.http4k.filter.RequestFilters
import org.http4k.hamkrest.hasBody
import org.http4k.hamkrest.hasStatus
import org.http4k.lens.Header.CONTENT_TYPE
import org.http4k.lens.MultipartFormFile
import org.http4k.routing.reverseProxy
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.time.Clock

class Http4kboxTest {

private val clock = Clock.systemUTC()

private val github = FakeGitHub(clock)

private val s3 = FakeS3().apply {
Expand All @@ -43,12 +46,21 @@ class Http4kboxTest {

private val http4kbox = Http4kBox(TestSettings, reverseProxy("s3" to s3, "github" to github), clock)

private val client = ClientFilters.FollowRedirects()
.then(ClientFilters.Cookies(clock))
.then(reverseProxy(
"github" to github,
"http4kbox" to http4kbox
))
private val client =
ClientFilters.SetBaseUriFrom(Uri.of("http://http4kbox"))
.then(ClientFilters.FollowRedirects())
.then(ClientFilters.Cookies(clock))
.then(
reverseProxy(
"github" to github,
"http4kbox" to http4kbox
)
)

@BeforeEach
fun login() {
client(Request(GET, "/"))
}

@Test
fun `can list files`() {
Expand Down Expand Up @@ -91,6 +103,13 @@ class Http4kboxTest {
assertThat(listFiles().countFiles(), equalTo(0))
}

@Test
fun `can list files through the API`() {
uploadFile("bob.txt", "content")
val apiClient = RequestFilters.SetHeader("http4k-api-key", "http4kbox").then(client)
assertThat(apiClient(Request(GET, "/api/list")).bodyString(), equalTo("""["bob.txt"]"""))
}

private fun Response.countFiles() = bodyString().split("""id="file-""").size - 1
private fun Response.containsFile(name: String) = bodyString().contains("""id="file-$name""")

Expand All @@ -107,6 +126,6 @@ class Http4kboxTest {
.body(body)
)

assertThat(upload, hasStatus(SEE_OTHER))
assertThat(upload, hasStatus(OK))
}
}

0 comments on commit fb515bc

Please sign in to comment.