Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/e2e testgroup multiclient #659

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 45 additions & 91 deletions waltid-services/waltid-e2e-tests/src/test/kotlin/AuthApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,69 @@ import E2ETestWebService.test
import id.walt.webwallet.db.models.Account
import id.walt.webwallet.db.models.AccountWalletListing
import id.walt.webwallet.web.model.AccountRequest
import id.walt.webwallet.web.model.KeycloakLogoutRequest
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.uuid.UUID
import kotlin.test.assertNotNull

class AuthApi(private val client: HttpClient) {
suspend fun userInfo(expectedStatus: HttpStatusCode, output: ((Account) -> Unit)? = null) =
test("/wallet-api/auth/user-info - wallet-api user-info") {
client.get("/wallet-api/auth/user-info").apply {
assert(status == expectedStatus) { "Expected status: $expectedStatus, but had $status" }
output?.invoke(body<Account>())
}

suspend fun userInfo(expectedStatus: HttpStatusCode): Account? =
client.get("/wallet-api/auth/user-info").run {
assert(status == expectedStatus) { "Expected status: $expectedStatus, but had $status" }
if (status.isSuccess()) runCatching { body<Account>() }.getOrNull() else null
}

suspend fun create(request: AccountRequest) = create(
client = client,
name = "/wallet-api/auth/create - wallet-api create",
url = "/wallet-api/auth/create",
request = request
)
suspend fun create(request: AccountRequest) = test(name = "/wallet-api/auth/create - wallet-api create") {
client.post(urlString = "/wallet-api/auth/create") {
setBody(
body = request
)
}.expectSuccess()
}

suspend fun login(request: AccountRequest, output: ((JsonObject) -> Unit)? = null) = login(
client = client,
name = "/wallet-api/auth/login - wallet-api login",
url = "/wallet-api/auth/login",
request = request,
output = output
)
suspend fun login(request: AccountRequest) = client.post("/wallet-api/auth/login") {
setBody(body = request)
}.expectSuccess().run {
body<JsonObject>().let { result ->
val token = result["token"]
assertNotNull(token)
val tokenContent = result["token"]!!.jsonPrimitive.content
tokenContent.expectLooksLikeJwt()
tokenContent
}
}

suspend fun logout() = test("/wallet-api/auth/logout - wallet-api logout") {
suspend fun logout() =
client.post("/wallet-api/auth/logout").expectSuccess()
}

suspend fun userSession() = test("/wallet-api/auth/session - logged in after login") {
suspend fun userSession() =
client.get("/wallet-api/auth/session").expectSuccess()
}

suspend fun userWallets(expectedAccountId: UUID, output: ((AccountWalletListing) -> Unit)? = null) =
test("/wallet-api/wallet/accounts/wallets - get wallets") {
client.get("/wallet-api/wallet/accounts/wallets").expectSuccess().apply {
val listing = body<AccountWalletListing>()
assert(expectedAccountId == listing.account) { "Wallet listing is for wrong account!" }
assert(listing.wallets.isNotEmpty()) { "No wallets available!" }
output?.invoke(listing)
}
suspend fun userWallets(expectedAccountId: UUID) =
client.get("/wallet-api/wallet/accounts/wallets").expectSuccess().run {
val listing = body<AccountWalletListing>()
assert(expectedAccountId == listing.account) { "Wallet listing is for wrong account!" }
assert(listing.wallets.isNotEmpty()) { "No wallets available!" }
listing.wallets
}

class Oidc(private val client: HttpClient) {
suspend fun oidcToken() = test("/wallet-api/auth/oidc-token - wallet-api oidc token") {
client.get("/wallet-api/auth/oidc-token").expectSuccess()
}
/*class Oidc(private val client: HttpClient) {
suspend fun oidcToken() = client.get("/wallet-api/auth/oidc-token").expectSuccess()

suspend fun oidcLogin() = test("/wallet-api/auth/oidc-login - wallet-api oidc login") {
client.get("/wallet-api/auth/oidc-login").expectSuccess()
}
suspend fun oidcLogin() = client.get("/wallet-api/auth/oidc-login").expectSuccess()

suspend fun oidcLogout() = test("/wallet-api/auth/logout-oidc - wallet-api oidc logout") {
client.get("/wallet-api/auth/oidc-logout").expectSuccess()
}
}
suspend fun oidcLogout() = client.get("/wallet-api/auth/oidc-logout").expectSuccess()
}*/

class Keycloak(private val client: HttpClient) {
suspend fun token(output: ((String) -> Unit)? = null) =
test("/wallet-api/auth/keycloak/token - wallet-api keycloak token") {
client.get("/wallet-api/auth/keycloak/token").expectSuccess().apply {
output?.invoke(bodyAsText())
}
}
/*class Keycloak(private val client: HttpClient) {
suspend fun token() = client.get("/wallet-api/auth/keycloak/token").expectSuccess().run {
bodyAsText()
}

suspend fun create(request: AccountRequest) = create(
client = client,
Expand All @@ -84,50 +73,15 @@ class AuthApi(private val client: HttpClient) {
request = request
)

suspend fun login(request: AccountRequest, output: ((JsonObject) -> Unit)? = null) = login(
suspend fun login(request: AccountRequest) = login(
client = client,
name = "/wallet-api/auth/keycloak/login - wallet-api keycloak login",
url = "/wallet-api/auth/keycloak/login",
request = request,
output = output
)

suspend fun logout(request: KeycloakLogoutRequest) =
test("/wallet-api/auth/keycloak/logout - wallet-api keycloak logout") {
client.post("/wallet-api/auth/keycloak/logout") {
suspend fun logout(request: KeycloakLogoutRequest) = client.post("/wallet-api/auth/keycloak/logout") {
setBody(request)
}.expectSuccess()
}
}

private companion object {
suspend fun create(
client: HttpClient,
name: String,
url: String,
request: AccountRequest,
) = test(name) {
client.post(url) {
setBody(request)
}.expectSuccess()
}

suspend fun login(
client: HttpClient,
name: String,
url: String,
request: AccountRequest,
output: ((JsonObject) -> Unit)? = null,
) = test(name) {
client.post(url) {
setBody(request)
}.expectSuccess().apply {
body<JsonObject>().let { result ->
assertNotNull(result["token"])
result["token"]!!.jsonPrimitive.content.expectLooksLikeJwt()
output?.invoke(result)
}
}
}
}
}
}.expectSuccess()
}*/
}
47 changes: 19 additions & 28 deletions waltid-services/waltid-e2e-tests/src/test/kotlin/CategoryApi.kt
Original file line number Diff line number Diff line change
@@ -1,39 +1,30 @@
import E2ETestWebService.test
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import kotlinx.serialization.json.JsonObject
import kotlinx.uuid.UUID
import java.net.URLEncoder

class CategoryApi(private val client: HttpClient) {
suspend fun list(wallet: UUID, expectedSize: Int, output: ((List<JsonObject>) -> Unit)? = null) =
test("/wallet-api/wallet/{wallet}/categories - list categories") {
client.get("/wallet-api/wallet/$wallet/categories").expectSuccess().apply {
val result = body<List<JsonObject>>()
assert(result.size == expectedSize)
output?.invoke(result)
}
class CategoryApi(private val client: HttpClient, val wallet: UUID) {
suspend fun list(expectedSize: Int): List<JsonObject> =
client.get("/wallet-api/wallet/$wallet/categories").expectSuccess().run {
val result = body<List<JsonObject>>()
assert(result.size == expectedSize)
result
}

suspend fun add(wallet: UUID, name: String) =
test("/wallet-api/wallet/{wallet}/categories/{name}/add - add category") {
client.post("/wallet-api/wallet/$wallet/categories/${URLEncoder.encode(name, "utf-8")}/add").expectSuccess()
}
suspend fun add(name: String) =
client.post("/wallet-api/wallet/$wallet/categories/${URLEncoder.encode(name, "utf-8")}/add").expectSuccess()

suspend fun delete(wallet: UUID, name: String) =
test("/wallet-api/wallet/{wallet}/categories/{name} - delete category") {
client.delete("/wallet-api/wallet/$wallet/categories/${URLEncoder.encode(name, "utf-8")}").expectSuccess()
}
suspend fun delete(name: String) =
client.delete("/wallet-api/wallet/$wallet/categories/${URLEncoder.encode(name, "utf-8")}").expectSuccess()

suspend fun rename(wallet: UUID, name: String, newName: String) =
test("/wallet-api/wallet/{wallet}/categories/{name}/rename/{newName} - rename category") {
client.put(
"/wallet-api/wallet/$wallet/categories/${URLEncoder.encode(name, "utf-8")}/rename/${
URLEncoder.encode(
newName, "utf-8"
)
}"
).expectSuccess()
}
}
suspend fun rename(name: String, newName: String) =
client.put(
"/wallet-api/wallet/$wallet/categories/${URLEncoder.encode(name, "utf-8")}/rename/${
URLEncoder.encode(
newName, "utf-8"
)
}"
).expectSuccess()
}
103 changes: 41 additions & 62 deletions waltid-services/waltid-e2e-tests/src/test/kotlin/CredentialsApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,88 +11,67 @@ import kotlinx.serialization.json.jsonObject
import kotlinx.uuid.UUID
import kotlin.test.assertNotNull

class CredentialsApi(private val client: HttpClient) {
class CredentialsApi(private val client: HttpClient, val wallet: UUID) {
suspend fun list(
wallet: UUID,
filter: CredentialFilterObject = CredentialFilterObject.default,
expectedSize: Int = 0,
vararg expectedCredential: String
) = test("/wallet-api/wallet/{wallet}/credentials - list credentials") {
client.get("/wallet-api/wallet/$wallet/credentials") {
url {
filter.toMap().onEach {
parameters.append(it.key, it.value.toString())
}
vararg expectedCredential: String,
) = client.get("/wallet-api/wallet/$wallet/credentials") {
url {
filter.toMap().onEach {
parameters.append(it.key, it.value.toString())
}
}.expectSuccess().apply {
val credentials = body<List<WalletCredential>>()
assert(credentials.size == expectedSize) { "should have $expectedSize credentials, but has ${credentials.size}" }
expectedCredential.onEach { cid -> assertNotNull(credentials.single { it.id == cid }) { "credential not found for id: $cid" } }
}
}.expectSuccess().apply {
val credentials = body<List<WalletCredential>>()
assert(credentials.size == expectedSize) { "should have $expectedSize credentials, but has ${credentials.size}" }
expectedCredential.onEach { cid -> assertNotNull(credentials.single { it.id == cid }) { "credential not found for id: $cid" } }
}

suspend fun get(wallet: UUID, credential: String, output: ((WalletCredential) -> Unit)? = null) =
test("/wallet-api/wallet/{wallet}/credentials/{credentialId} - get credential") {
client.get("/wallet-api/wallet/$wallet/credentials/$credential").expectSuccess().apply {
val credential = body<WalletCredential>()
output?.invoke(credential)
}
}

suspend fun delete(wallet: UUID, credential: String, permanent: Boolean = false) =
test("/wallet-api/wallet/{wallet}/credentials/{credentialId} - delete credential") {
client.delete("/wallet-api/wallet/$wallet/credentials/$credential") {
url {
parameters.append("permanent", "$permanent")
}
}.expectSuccess()
suspend fun get(credential: String) =
client.get("/wallet-api/wallet/$wallet/credentials/$credential").expectSuccess().run {
body<WalletCredential>()
}

suspend fun restore(wallet: UUID, credential: String, output: ((WalletCredential) -> Unit)? = null) =
test("/wallet-api/wallet/{wallet}/credentials/{credentialId}/restore - restore credential") {
client.post("/wallet-api/wallet/$wallet/credentials/$credential/restore").expectSuccess().apply {
output?.invoke(body<WalletCredential>())
suspend fun delete(credential: String, permanent: Boolean = false) =
client.delete("/wallet-api/wallet/$wallet/credentials/$credential") {
url {
parameters.append("permanent", "$permanent")
}
}
}.expectSuccess()

suspend fun accept(wallet: UUID, credential: String) =
test("/wallet-api/wallet/{wallet}/credentials/{credentialId}/accept - accept credential") {
client.post("/wallet-api/wallet/$wallet/credentials/$credential/accept").expectSuccess()
suspend fun restore(credential: String) =
client.post("/wallet-api/wallet/$wallet/credentials/$credential/restore").expectSuccess().run {
body<WalletCredential>()
}

suspend fun reject(wallet: UUID, credential: String, note: String? = null) =
test("/wallet-api/wallet/{wallet}/credentials/{credentialId}/reject - reject credential") {
client.post("/wallet-api/wallet/$wallet/credentials/$credential/reject") {
setBody(mapOf("note" to note))
}.expectSuccess()
}
suspend fun accept(credential: String) =
client.post("/wallet-api/wallet/$wallet/credentials/$credential/accept").expectSuccess()

suspend fun status(wallet: UUID, credential: String, output: ((List<CredentialStatusResult>) -> Unit)? = null) =
test("/wallet-api/wallet/{wallet}/credentials/{credentialId}/status - get credential status") {
client.get("/wallet-api/wallet/$wallet/credentials/$credential/status").expectSuccess().apply {
val result = body<List<CredentialStatusResult>>()
output?.invoke(result)
}
}
suspend fun reject(credential: String, note: String? = null) =
client.post("/wallet-api/wallet/$wallet/credentials/$credential/reject") {
setBody(mapOf("note" to note))
}.expectSuccess()

suspend fun attachCategory(wallet: UUID, credential: String, vararg categories: String) =
test("/wallet-api/wallet/{wallet}/credentials/{credentialId}/category - attach category") {
client.put("/wallet-api/wallet/$wallet/credentials/$credential/category") {
setBody(categories.toList())
}.expectSuccess()
suspend fun status(credential: String) =
client.get("/wallet-api/wallet/$wallet/credentials/$credential/status").expectSuccess().run {
body<List<CredentialStatusResult>>()
}

suspend fun detachCategory(wallet: UUID, credential: String, vararg categories: String) =
test("/wallet-api/wallet/{wallet}/credentials/{credentialId}/category - detach category") {
client.delete("/wallet-api/wallet/$wallet/credentials/$credential/category") {
setBody(categories.toList())
}.expectSuccess()
}
suspend fun attachCategory(credential: String, vararg categories: String) =
client.put("/wallet-api/wallet/$wallet/credentials/$credential/category") {
setBody(categories.toList())
}.expectSuccess()

suspend fun detachCategory(credential: String, vararg categories: String) =
client.delete("/wallet-api/wallet/$wallet/credentials/$credential/category") {
setBody(categories.toList())
}.expectSuccess()

suspend fun store(wallet: UUID, credential: String) =
suspend fun store(credential: String) =
test("/wallet-api/wallet/{wallet}/credentials - store credential") {
TODO("Not implemented")
}

private fun CredentialFilterObject.toMap() = Json.encodeToJsonElement(this).jsonObject.toMap()
}
}
Loading
Loading