Skip to content

Commit

Permalink
Added a WireMock OAuth2 login test
Browse files Browse the repository at this point in the history
  • Loading branch information
tomakehurst committed Mar 10, 2020
1 parent 746cabf commit 94266ce
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("org.seleniumhq.selenium:selenium-api")
testCompile 'org.seleniumhq.selenium:selenium-chrome-driver'
testCompile('com.github.tomakehurst:wiremock:2.8.0')
testCompile('com.github.tomakehurst:wiremock:2.26.3')
testCompile("junit:junit")
testCompile('org.assertj:assertj-core:3.8.0')
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ spring:
user-name-attribute: sub
jwk-set-uri: https://oauth.mocklab.io/.well-known/jwks.json

wiremock:
authorization-uri: http://localhost:8077/oauth/authorize
token-uri: http://localhost:8077/oauth/token
user-info-uri: http://localhost:8077/userinfo
user-name-attribute: sub

registration:
google:
clientId: 677624187256-vcp2r9ccsjjhu5diej8a89nu1aac4q4j.apps.googleusercontent.com
Expand All @@ -36,6 +42,14 @@ spring:
clientId: mocklab_oidc
clientSecret: whatever

wiremock:
provider: wiremock
authorization-grant-type: authorization_code
scope: email
redirect-uri: "{baseUrl}/{action}/oauth2/code/{registrationId}"
clientId: wm
clientSecret: whatever

spring.mustache.expose-request-attributes: true

mockapi.baseurl: http://live-demo.mocklab.io
Expand Down
82 changes: 82 additions & 0 deletions src/test/java/mocklab/demo/OAuth2LoginTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package mocklab.demo;

import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
classes = MockLabDemoApp.class)
public class OAuth2LoginTest {

static final String APP_BASE_URL = "http://localhost:9000";

private WebDriver webDriver;

@Rule
public WireMockRule mockOAuth2Provider = new WireMockRule(wireMockConfig()
.port(8077)
.extensions(new ResponseTemplateTransformer(true)));

@Before
public void setUp() {
webDriver = new ChromeDriver();

mockOAuth2Provider.stubFor(get(urlPathEqualTo("/favicon.ico")).willReturn(notFound()));

mockOAuth2Provider.stubFor(get(urlPathMatching("/oauth/authorize?.*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/html")
.withBodyFile("login.html")));

mockOAuth2Provider.stubFor(post(urlPathEqualTo("/login"))
.willReturn(temporaryRedirect("{{formData request.body 'form' urlDecode=true}}{{{form.redirectUri}}}?code={{{randomValue length=30 type='ALPHANUMERIC'}}}&state={{{form.state}}}")));

mockOAuth2Provider.stubFor(post(urlPathEqualTo("/oauth/token"))
.willReturn(okJson("{\"token_type\": \"Bearer\",\"access_token\":\"{{randomValue length=20 type='ALPHANUMERIC'}}\"}")));

mockOAuth2Provider.stubFor(get(urlPathEqualTo("/userinfo"))
.willReturn(okJson("{\"sub\":\"my-id\",\"email\":\"[email protected]\"}")));
}

@After
public void reset() {
if (webDriver != null) {
webDriver.close();
}
}

@Test
public void logs_in_via_wiremock_sso() throws Exception {
webDriver.get(APP_BASE_URL + "/oauth2/authorization/wiremock");
assertThat(webDriver.getCurrentUrl()).startsWith("http://localhost:8077/oauth/authorize");

webDriver.findElement(By.name("username")).sendKeys("[email protected]");
webDriver.findElement(By.name("password")).sendKeys("pass123");
webDriver.findElement(By.id("submit")).click();

Thread.sleep(2000);

assertThat(webDriver.getCurrentUrl()).contains("/user");
assertThat(webDriver.findElement(By.tagName("h1")).getText()).contains("Hello [email protected]!");
}

static {
System.setProperty("webdriver.chrome.driver", "lib/chromedriver");
}
}
15 changes: 15 additions & 0 deletions src/test/resources/__files/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<body>
Welcome to the login page!
<br />
<form method="POST" action="/login">
<input type="hidden" name="state" value="{{request.query.state}}"/>
<input type="hidden" name="redirectUri" value="{{request.query.redirect_uri}}"/>

<input type="text" name="username" />
<input type="password" name="password" />

<input id="submit" type="submit" value="Login" />
</form>
</body>
</html>

0 comments on commit 94266ce

Please sign in to comment.