Skip to content

Commit

Permalink
[Version 1.0.3] Add preauthorization
Browse files Browse the repository at this point in the history
  • Loading branch information
rgryta committed Nov 1, 2024
1 parent 2cb280e commit 57dcea4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 52 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeSimulatorTest
import java.io.ByteArrayOutputStream
import java.util.*
import java.util.Properties

plugins {
alias(libs.plugins.kotlinMultiplatform)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.wellmate.api.client

import io.wellmate.api.client.dataclasses.auth.Token
import kotlin.jvm.JvmStatic

class ApiInstance {
var token: Token? = null

init {
instance = this
}

companion object {
@JvmStatic
lateinit var instance: ApiInstance
private set
}
}
21 changes: 21 additions & 0 deletions library/src/commonMain/kotlin/io/wellmate/api/client/Endpoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.ktor.client.request.setBody
import io.ktor.client.statement.HttpResponse
import io.ktor.http.ContentType
import io.ktor.http.HeadersBuilder
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
import io.ktor.http.ParametersBuilder
import io.ktor.http.contentType
Expand Down Expand Up @@ -43,6 +44,11 @@ class Endpoint(val client: HttpClient, val url: String) {
val response: HttpResponse = client.get(url) {
headers {
headers()

// Authorize if available
this[HttpHeaders.Authorization] ?: ApiInstance.instance.token?.let { token ->
append(HttpHeaders.Authorization, token.authorizationHeader)
}
}
}
return ResponseWrapper(response, typeInfo<T>())
Expand All @@ -58,6 +64,11 @@ class Endpoint(val client: HttpClient, val url: String) {
}) {
headers {
headers()

// Authorize if available
this[HttpHeaders.Authorization] ?: ApiInstance.instance.token?.let { token ->
append(HttpHeaders.Authorization, token.authorizationHeader)
}
}
}
return ResponseWrapper(response, typeInfo<T>())
Expand All @@ -71,6 +82,11 @@ class Endpoint(val client: HttpClient, val url: String) {
val response: HttpResponse = client.post(url) {
headers {
headers()

// Authorize if available
this[HttpHeaders.Authorization] ?: ApiInstance.instance.token?.let { token ->
append(HttpHeaders.Authorization, token.authorizationHeader)
}
}
contentType(contentType)
setBody(body)
Expand All @@ -84,6 +100,11 @@ class Endpoint(val client: HttpClient, val url: String) {
val response: HttpResponse = client.delete(url) {
headers {
headers()

// Authorize if available
this[HttpHeaders.Authorization] ?: ApiInstance.instance.token?.let { token ->
append(HttpHeaders.Authorization, token.authorizationHeader)
}
}
}
return ResponseWrapper(response, typeInfo<T>())
Expand Down
65 changes: 15 additions & 50 deletions library/src/commonTest/kotlin/ApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.wellmate.api.client
import io.ktor.http.HttpHeaders
import io.ktor.http.isSuccess
import io.wellmate.api.client.dataclasses.auth.EmailPassword
import io.wellmate.api.client.dataclasses.auth.Token
import io.wellmate.api.client.dataclasses.entry.Meal
import io.wellmate.api.client.dataclasses.entry.MealFieldsClient
import io.wellmate.api.client.dataclasses.entry.Timer
Expand All @@ -26,7 +25,8 @@ class ApiTest {

private val testDispatcher = StandardTestDispatcher()

private lateinit var token: Token
private val apiInstance = ApiInstance()

private lateinit var me: Me

@BeforeTest
Expand All @@ -39,35 +39,25 @@ class ApiTest {
val resultCreate = WellMateClient.Api.User.post(body = emailPassword)
assertTrue(resultCreate.status.isSuccess())

token = resultCreate.body()
apiInstance.token = resultCreate.body()

val resultMe = WellMateClient.Api.User.Me.get {
append(
HttpHeaders.Authorization,
token.authorizationHeader,
)
}
val resultMe = WellMateClient.Api.User.Me.get()
assertTrue(resultMe.status.isSuccess())
me = resultMe.body()
}

@AfterTest
fun `delete the user used for testing`() = runTest(testDispatcher) {
val deleteEndpoint = WellMateClient.Api.User.UserId(userId = me.id)
val resultDelete = deleteEndpoint.delete {
append(
HttpHeaders.Authorization,
token.authorizationHeader,
)
}
val resultDelete = deleteEndpoint.delete()
assertTrue(resultDelete.status.isSuccess())
}

@Test
fun `api-user-me get fails with no headers`() = runTest(testDispatcher) {
val endpoint = WellMateClient.Api.User.Me
assertFalse(
endpoint.get { }.status.isSuccess(),
endpoint.get { append(HttpHeaders.Authorization, "") }.status.isSuccess(),
"/api/user/me:get should not be successful when no header is provided"
)
}
Expand All @@ -88,39 +78,24 @@ class ApiTest {
note = "Evening meditation",
)

class Operations(val token: Token) {
class Operations {
suspend fun postMeal(): Meal {
val mealEndpoint = WellMateClient.Api.Entry.Meal
val mealResponse = mealEndpoint.post(body = MEAL) {
append(
HttpHeaders.Authorization,
token.authorizationHeader,
)
}
val mealResponse = mealEndpoint.post(body = MEAL)
assertTrue(mealResponse.status.isSuccess())
assertIs<Instant>(mealResponse.body().added)
return mealResponse.body()
}

suspend fun deleteMeal(mealId: Int) {
val mealEndpoint = WellMateClient.Api.Entry.Meal.MealId(mealId)
val mealResponse = mealEndpoint.delete {
append(
HttpHeaders.Authorization,
token.authorizationHeader,
)
}
val mealResponse = mealEndpoint.delete()
assertTrue(mealResponse.status.isSuccess())
}

suspend fun postTimer(): Timer {
val timerEndpoint = WellMateClient.Api.Entry.Timer
val timerResponse = timerEndpoint.post(body = TIMER) {
append(
HttpHeaders.Authorization,
token.authorizationHeader,
)
}
val timerResponse = timerEndpoint.post(body = TIMER)
assertTrue(timerResponse.status.isSuccess())
assertIs<Instant>(timerResponse.body().added)
assertEquals(expected = TIMER.note, actual = timerResponse.body().note)
Expand All @@ -129,45 +104,35 @@ class ApiTest {

suspend fun deleteTimer(timerId: Int) {
val timerEndpoint = WellMateClient.Api.Entry.Timer.TimerId(timerId)
val timerResponse = timerEndpoint.delete {
append(
HttpHeaders.Authorization,
token.authorizationHeader,
)
}
val timerResponse = timerEndpoint.delete()
assertTrue(timerResponse.status.isSuccess())
}
}
}

@Test
fun `api-entry post meal and delete afterwards`() = runTest(testDispatcher) {
val operations = Entries.Operations(token)
val operations = Entries.Operations()
val meal = operations.postMeal()
operations.deleteMeal(meal.id)
}

@Test
fun `api-entry post timer and delete afterwards`() = runTest(testDispatcher) {
val operations = Entries.Operations(token)
val operations = Entries.Operations()
val timer = operations.postTimer()
operations.deleteTimer(timer.id)
}


@Test
fun `api-entry post entry + timer and return both`() = runTest(testDispatcher) {
val operations = Entries.Operations(token)
val operations = Entries.Operations()

val meal = operations.postMeal()
val timer = operations.postTimer()

val entriesEndpoint = WellMateClient.Api.Entry.get {
append(
HttpHeaders.Authorization,
token.authorizationHeader,
)
}
val entriesEndpoint = WellMateClient.Api.Entry.get()

assertTrue(entriesEndpoint.status.isSuccess())
assertEquals(2, entriesEndpoint.body().size)
Expand Down
2 changes: 1 addition & 1 deletion library/version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.0.2
version=1.0.3

0 comments on commit 57dcea4

Please sign in to comment.