-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
746cabf
commit 94266ce
Showing
4 changed files
with
112 additions
and
1 deletion.
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
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,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"); | ||
} | ||
} |
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,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> |