Skip to content

Commit 63ed8ae

Browse files
authored
Integration test (#51)
Basic integration testing to verify all frameworks integration
1 parent 52a4052 commit 63ed8ae

File tree

12 files changed

+437
-24
lines changed

12 files changed

+437
-24
lines changed

oauth2-server-core/pom.xml

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,4 @@
1010
<modelVersion>4.0.0</modelVersion>
1111

1212
<artifactId>oauth2-server-core</artifactId>
13-
14-
<dependencies>
15-
<dependency>
16-
<groupId>org.junit.jupiter</groupId>
17-
<artifactId>junit-jupiter-engine</artifactId>
18-
<version>5.2.0</version>
19-
<scope>test</scope>
20-
</dependency>
21-
<dependency>
22-
<groupId>io.mockk</groupId>
23-
<artifactId>mockk</artifactId>
24-
<version>1.8.12.kotlin13</version>
25-
<scope>test</scope>
26-
</dependency>
27-
<dependency>
28-
<groupId>org.hamcrest</groupId>
29-
<artifactId>hamcrest-library</artifactId>
30-
<version>1.3</version>
31-
<scope>test</scope>
32-
</dependency>
33-
</dependencies>
3413
</project>

oauth2-server-http4k/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
<version>3.37.1</version>
1919
<scope>provided</scope>
2020
</dependency>
21+
<dependency>
22+
<groupId>org.http4k</groupId>
23+
<artifactId>http4k-server-jetty</artifactId>
24+
<version>3.37.1</version>
25+
<scope>test</scope>
26+
</dependency>
2127
<dependency>
2228
<groupId>nl.myndocs</groupId>
2329
<artifactId>oauth2-server-core</artifactId>
@@ -30,6 +36,13 @@
3036
<version>${project.version}</version>
3137
<classifier>shaded</classifier>
3238
</dependency>
39+
40+
<dependency>
41+
<groupId>nl.myndocs</groupId>
42+
<artifactId>oauth2-server-integration-base</artifactId>
43+
<version>${project.version}</version>
44+
<scope>test</scope>
45+
</dependency>
3346
</dependencies>
3447

3548
<repositories>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package nl.myndocs.oauth2.http4k.integration
2+
3+
import nl.myndocs.oauth2.http4k.`enable oauth2`
4+
import nl.myndocs.oauth2.integration.BaseIntegrationTest
5+
import org.http4k.routing.RoutingHttpHandler
6+
import org.http4k.routing.routes
7+
import org.http4k.server.Jetty
8+
import org.http4k.server.asServer
9+
import org.junit.jupiter.api.AfterEach
10+
import org.junit.jupiter.api.BeforeEach
11+
12+
class Http4kIntegrationTest : BaseIntegrationTest() {
13+
val server = routes(*emptyArray<RoutingHttpHandler>())
14+
.let { it `enable oauth2` { configBuilder(this) } }
15+
.let { it.asServer(Jetty(0)) }
16+
17+
@BeforeEach
18+
fun before() {
19+
server.start()
20+
21+
localPort = server.port()
22+
}
23+
24+
@AfterEach
25+
fun after() {
26+
server.stop()
27+
}
28+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>kotlin-oauth2-server</artifactId>
7+
<groupId>nl.myndocs</groupId>
8+
<version>0.5.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>oauth2-server-integration-base</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.squareup.okhttp3</groupId>
17+
<artifactId>okhttp</artifactId>
18+
<version>3.12.1</version>
19+
</dependency>
20+
21+
<dependency>
22+
<groupId>org.junit.jupiter</groupId>
23+
<artifactId>junit-jupiter-engine</artifactId>
24+
<version>5.2.0</version>
25+
<scope>provided</scope>
26+
</dependency>
27+
28+
<!-- In memory dependencies -->
29+
<dependency>
30+
<groupId>nl.myndocs</groupId>
31+
<artifactId>oauth2-server-client-inmemory</artifactId>
32+
<version>${project.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>nl.myndocs</groupId>
36+
<artifactId>oauth2-server-identity-inmemory</artifactId>
37+
<version>${project.version}</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>nl.myndocs</groupId>
41+
<artifactId>oauth2-server-token-store-inmemory</artifactId>
42+
<version>${project.version}</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>nl.myndocs</groupId>
46+
<artifactId>oauth2-server-core</artifactId>
47+
<version>${project.version}</version>
48+
</dependency>
49+
50+
<!-- Jackson -->
51+
<dependency>
52+
<groupId>com.fasterxml.jackson.core</groupId>
53+
<artifactId>jackson-core</artifactId>
54+
<version>2.9.8</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.fasterxml.jackson.core</groupId>
58+
<artifactId>jackson-databind</artifactId>
59+
<version>2.9.8</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.fasterxml.jackson.module</groupId>
63+
<artifactId>jackson-module-kotlin</artifactId>
64+
<version>2.9.8</version>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.hamcrest</groupId>
68+
<artifactId>hamcrest-all</artifactId>
69+
<version>1.3</version>
70+
</dependency>
71+
</dependencies>
72+
</project>
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package nl.myndocs.oauth2.integration
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
5+
import nl.myndocs.oauth2.client.AuthorizedGrantType
6+
import nl.myndocs.oauth2.client.inmemory.InMemoryClient
7+
import nl.myndocs.oauth2.config.ConfigurationBuilder
8+
import nl.myndocs.oauth2.identity.inmemory.InMemoryIdentity
9+
import nl.myndocs.oauth2.tokenstore.inmemory.InMemoryTokenStore
10+
import okhttp3.*
11+
import org.hamcrest.CoreMatchers.*
12+
import org.hamcrest.MatcherAssert.assertThat
13+
import org.junit.jupiter.api.Test
14+
import java.util.*
15+
16+
abstract class BaseIntegrationTest {
17+
var localPort: Int? = null
18+
val configBuilder: ConfigurationBuilder.Configuration.() -> Unit = {
19+
identityService = InMemoryIdentity()
20+
.identity {
21+
username = "foo"
22+
password = "bar"
23+
}
24+
clientService = InMemoryClient()
25+
.client {
26+
clientId = "testapp"
27+
clientSecret = "testpass"
28+
scopes = setOf("trusted")
29+
redirectUris = setOf("http://localhost:8080/callback")
30+
authorizedGrantTypes = setOf(
31+
AuthorizedGrantType.AUTHORIZATION_CODE,
32+
AuthorizedGrantType.PASSWORD,
33+
AuthorizedGrantType.IMPLICIT,
34+
AuthorizedGrantType.REFRESH_TOKEN
35+
)
36+
}
37+
tokenStore = InMemoryTokenStore()
38+
39+
}
40+
41+
private val objectMapper = ObjectMapper().registerKotlinModule()
42+
43+
@Test
44+
fun `test password grant flow`() {
45+
val client = OkHttpClient()
46+
val body = FormBody.Builder()
47+
.add("grant_type", "password")
48+
.add("username", "foo")
49+
.add("password", "bar")
50+
.add("client_id", "testapp")
51+
.add("client_secret", "testpass")
52+
.build()
53+
54+
val url = buildOauthTokenUri()
55+
56+
val request = Request.Builder()
57+
.url(url)
58+
.post(body)
59+
.build()
60+
61+
val response = client.newCall(request)
62+
.execute()
63+
64+
val values = objectMapper.readMap(response.body()!!.string())
65+
66+
assertThat(values["access_token"], `is`(notNullValue()))
67+
assertThat(UUID.fromString(values["access_token"] as String), `is`(instanceOf(UUID::class.java)))
68+
69+
response.close()
70+
}
71+
72+
@Test
73+
fun `test authorization grant flow`() {
74+
75+
val client = OkHttpClient.Builder()
76+
.followRedirects(false)
77+
.build()
78+
79+
val url = HttpUrl.Builder()
80+
.scheme("http")
81+
.host("localhost")
82+
.port(localPort!!)
83+
.addPathSegment("oauth")
84+
.addPathSegment("authorize")
85+
.setQueryParameter("response_type", "code")
86+
.setQueryParameter("client_id", "testapp")
87+
.setQueryParameter("redirect_uri", "http://localhost:$localPort/callback")
88+
.build()
89+
90+
val request = Request.Builder()
91+
.addHeader("Authorization", Credentials.basic("foo", "bar"))
92+
.url(url)
93+
.get()
94+
.build()
95+
96+
val response = client.newCall(request)
97+
.execute()
98+
99+
response.close()
100+
101+
val body = FormBody.Builder()
102+
.add("grant_type", "authorization_code")
103+
.add("code", response.header("location")!!.asQueryParameters()["code"])
104+
.add("redirect_uri", "http://localhost:$localPort/callback")
105+
.add("client_id", "testapp")
106+
.add("client_secret", "testpass")
107+
.build()
108+
109+
val tokenUrl = buildOauthTokenUri()
110+
111+
val tokenRequest = Request.Builder()
112+
.url(tokenUrl)
113+
.post(body)
114+
.build()
115+
116+
val tokenResponse = client.newCall(tokenRequest)
117+
.execute()
118+
119+
val values = objectMapper.readMap(tokenResponse.body()!!.string())
120+
assertThat(values["access_token"], `is`(notNullValue()))
121+
assertThat(UUID.fromString(values["access_token"] as String), `is`(instanceOf(UUID::class.java)))
122+
123+
tokenResponse.close()
124+
}
125+
126+
@Test
127+
fun `test client credentials flow`() {
128+
val client = OkHttpClient()
129+
val body = FormBody.Builder()
130+
.add("grant_type", "client_credentials")
131+
.add("client_id", "testapp")
132+
.add("client_secret", "testpass")
133+
.build()
134+
135+
val tokenRequest = Request.Builder()
136+
.url(buildOauthTokenUri())
137+
.post(body)
138+
.build()
139+
140+
val tokenResponse = client.newCall(tokenRequest)
141+
.execute()
142+
143+
val values = objectMapper.readMap(tokenResponse.body()!!.string())
144+
assertThat(values["access_token"], `is`(notNullValue()))
145+
assertThat(UUID.fromString(values["access_token"] as String), `is`(instanceOf(UUID::class.java)))
146+
147+
tokenResponse.close()
148+
149+
}
150+
151+
private fun buildOauthTokenUri() =
152+
HttpUrl.Builder()
153+
.scheme("http")
154+
.host("localhost")
155+
.port(localPort!!)
156+
.addPathSegment("oauth")
157+
.addPathSegment("token")
158+
.build()
159+
}
160+
161+
fun ObjectMapper.readMap(content: String) = this.readValue(content, Map::class.java)
162+
163+
fun String.asQueryParameters() =
164+
split("?")[1]
165+
.split("&")
166+
.map { it.split("=") }
167+
.map { Pair(it[0], it[1]) }
168+
.toMap()

oauth2-server-javalin/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
<artifactId>oauth2-server-javalin</artifactId>
1313

14-
1514
<dependencies>
1615
<dependency>
1716
<groupId>nl.myndocs</groupId>
@@ -25,5 +24,12 @@
2524
<version>2.0.0</version>
2625
<scope>provided</scope>
2726
</dependency>
27+
28+
<dependency>
29+
<groupId>nl.myndocs</groupId>
30+
<artifactId>oauth2-server-integration-base</artifactId>
31+
<version>${project.version}</version>
32+
<scope>test</scope>
33+
</dependency>
2834
</dependencies>
2935
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package nl.myndocs.oauth2.javalin.integration
2+
3+
import io.javalin.Javalin
4+
import nl.myndocs.oauth2.integration.BaseIntegrationTest
5+
import nl.myndocs.oauth2.javalin.enableOauthServer
6+
import org.junit.jupiter.api.AfterEach
7+
import org.junit.jupiter.api.BeforeEach
8+
9+
class JavalinIntegrationTest : BaseIntegrationTest() {
10+
11+
val server = Javalin.create()
12+
.apply {
13+
enableOauthServer {
14+
configBuilder(this)
15+
}
16+
}
17+
.port(0)
18+
19+
@BeforeEach
20+
fun before() {
21+
server.start()
22+
23+
localPort = server.port()
24+
}
25+
26+
@AfterEach
27+
fun after() {
28+
server.stop()
29+
}
30+
}

0 commit comments

Comments
 (0)