-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ES-2395: CordaRestClient to support non-static credentials (#6255)
- Loading branch information
1 parent
c73b7c3
commit 43cb92f
Showing
5 changed files
with
145 additions
and
16 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
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
97 changes: 97 additions & 0 deletions
97
...erated-rest-client/src/test/kotlin/net/corda/restclient/generated/TestClientAuthHeader.kt
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package net.corda.restclient.generated | ||
|
||
import io.javalin.Javalin | ||
import io.javalin.http.ContentType | ||
import net.corda.restclient.CordaRestClient | ||
import net.corda.restclient.generated.infrastructure.ApiClient | ||
import org.assertj.core.api.AssertionsForClassTypes.assertThat | ||
import org.junit.jupiter.api.AfterAll | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import java.net.URI | ||
|
||
class TestClientAuthHeader { | ||
|
||
companion object { | ||
private lateinit var app: Javalin | ||
|
||
@BeforeAll | ||
@JvmStatic | ||
fun setup() { | ||
app = Javalin.create().start(0) | ||
app.get("api/v5_3/mgm/1234/info") { ctx -> | ||
|
||
assertThat(ctx.basicAuthCredentialsExist()).isTrue() | ||
|
||
// Respond with the credentials from the basic auth header | ||
val (user, password) = ctx.basicAuthCredentials() | ||
ctx.contentType(ContentType.APPLICATION_JSON) | ||
.result("$user:$password") | ||
} | ||
} | ||
|
||
@AfterAll | ||
@JvmStatic | ||
fun teardown() { app.stop() } | ||
} | ||
|
||
private val localhost = URI.create("http:localhost:${app.port()}") | ||
|
||
private fun CordaRestClient.echoCredentials(): String { | ||
return this.mgmClient.getMgmHoldingidentityshorthashInfo("1234") | ||
} | ||
|
||
@BeforeEach | ||
fun resetStaticCredentials() { | ||
ApiClient.username = null | ||
ApiClient.password = null | ||
} | ||
|
||
private fun assertThatStaticCredsAreNull() { | ||
assertThat(ApiClient.username).isNull() | ||
assertThat(ApiClient.password).isNull() | ||
} | ||
|
||
@Test | ||
fun defaultCredentialsAreCorrect() { | ||
val defaultClient = CordaRestClient.createHttpClient(localhost) | ||
assertThat(defaultClient.echoCredentials()).isEqualTo("admin:admin") | ||
|
||
assertThatStaticCredsAreNull() | ||
} | ||
|
||
@Test | ||
fun staticCredentialsAreIgnored() { | ||
val defaultClient = CordaRestClient.createHttpClient(localhost, "foo", "bar") | ||
assertThat(defaultClient.echoCredentials()).isEqualTo("foo:bar") | ||
|
||
assertThatStaticCredsAreNull() | ||
|
||
ApiClient.apply { | ||
username = "foo-static" | ||
password = "bar-static" | ||
} | ||
assertThat(defaultClient.echoCredentials()).isEqualTo("foo:bar") | ||
|
||
assertThat(ApiClient.username).isEqualTo("foo-static") | ||
assertThat(ApiClient.password).isEqualTo("bar-static") | ||
} | ||
|
||
@Test | ||
fun twoClientsUseUniqueCredentials() { | ||
val client1 = CordaRestClient.createHttpClient(localhost, "user-one", "password-one") | ||
val client2 = CordaRestClient.createHttpClient(localhost, "user-two", "password-two") | ||
ApiClient.apply { | ||
username = "user-static" | ||
password = "password-static" | ||
} | ||
assertThat(client1.echoCredentials()).isEqualTo("user-one:password-one") | ||
assertThat(client2.echoCredentials()).isEqualTo("user-two:password-two") | ||
|
||
resetStaticCredentials() | ||
|
||
assertThat(client1.echoCredentials()).isEqualTo("user-one:password-one") | ||
assertThat(client2.echoCredentials()).isEqualTo("user-two:password-two") | ||
} | ||
} |
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
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