-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a static app to test usage from CDN (#446)
- Loading branch information
1 parent
af8f21d
commit 34afcbb
Showing
8 changed files
with
217 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
node_modules/ | ||
.yalc | ||
*.log | ||
*.log | ||
public/*.js | ||
public/*.map |
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,111 @@ | ||
<html> | ||
<head> | ||
<!-- https://global.oktacdn.com/okta-auth-js/3.2.3/okta-auth-js.polyfill.js --> | ||
<script src="/okta-auth-js.polyfill.js" type="text/javascript"></script> | ||
|
||
<!-- https://global.oktacdn.com/okta-auth-js/3.2.3/okta-auth-js.min.js --> | ||
<script src="/okta-auth-js.min.js" type="text/javascript"></script> | ||
</head> | ||
<body> | ||
<div id="unauth" style="display: none"> | ||
<b>Please login</b><hr/> | ||
<a id="login" href="#" onclick="login(event)">Login</a> | ||
</div> | ||
<div id="auth" style="display: none"> | ||
<b>Welcome back</b><hr/> | ||
<div id="userinfo"></div><hr/> | ||
<a id="logout" href=#" onclick="logout(event)">Logout</a> | ||
</div> | ||
<div id="error" style="color: red; padding-top: 20px"> | ||
</div> | ||
<hr/> | ||
<a href="/">Return Home</a> | ||
<script type="text/javascript"> | ||
var authClient; | ||
var issuer; | ||
var clientId; | ||
var redirectUri = window.location.origin + window.location.pathname; // http://localhost:8080/static/ | ||
|
||
var url = new URL(window.location.href); | ||
var state = url.searchParams.get('state'); | ||
if (state) { | ||
state = JSON.parse(state); | ||
issuer = state.issuer; | ||
clientId = state.clientId; | ||
} else { | ||
issuer = url.searchParams.get('issuer'); | ||
clientId = url.searchParams.get('clientId'); | ||
} | ||
|
||
function main() { | ||
try { | ||
authClient = new OktaAuth({ | ||
issuer: issuer, | ||
clientId: clientId, | ||
redirectUri: redirectUri | ||
}); | ||
} catch (error) { | ||
return showError(error); | ||
} | ||
|
||
if (authClient.token.isLoginRedirect()) { | ||
return handleLoginRedirect(); | ||
} | ||
|
||
// normal app startup. | ||
showUserInfo(); | ||
} | ||
|
||
function handleLoginRedirect() { | ||
authClient.token.parseFromUrl().then(function(res) { | ||
var tokens = res.tokens; | ||
if (tokens.idToken) { | ||
authClient.tokenManager.add('idToken', tokens.idToken); | ||
} | ||
if (tokens.accessToken) { | ||
authClient.tokenManager.add('accessToken', tokens.accessToken); | ||
} | ||
showUserInfo(); | ||
}); | ||
} | ||
|
||
function showError(error) { | ||
console.error(error); | ||
var node = document.createElement('DIV'); | ||
node.innerText = JSON.stringify(error, null, 2); | ||
document.getElementById('error').appendChild(node); | ||
} | ||
|
||
function showUserInfo() { | ||
authClient.token.getUserInfo().then(function(userInfo) { | ||
document.getElementById('auth').style.display = 'block'; | ||
document.getElementById('userinfo').innerText = JSON.stringify(userInfo, null, 2); | ||
}).catch(function(error) { | ||
showError(error); | ||
showLogin(); | ||
}); | ||
} | ||
|
||
function showLogin() { | ||
document.getElementById('unauth').style.display = 'block'; | ||
} | ||
|
||
function login(e) { | ||
e.preventDefault(); | ||
authClient.token.getWithRedirect({ | ||
state: JSON.stringify({ | ||
issuer: issuer, | ||
clientId: clientId | ||
}) | ||
}); | ||
}; | ||
|
||
function logout(e) { | ||
e.preventDefault(); | ||
authClient.signOut() | ||
} | ||
|
||
main(); | ||
</script> | ||
</body> | ||
</html> |
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,60 @@ | ||
import assert from 'assert'; | ||
import toQueryParams from '../util/toQueryParams'; | ||
|
||
class StaticApp { | ||
get unauth() { return $('#unauth'); } | ||
get auth() { return $('#auth'); } | ||
get userinfo() { return $('#userinfo'); } | ||
get error() { return $('#error'); } | ||
get login() { return $('#login'); } | ||
get logout() { return $('#logout'); } | ||
|
||
async open() { | ||
const ISSUER = process.env.ISSUER; | ||
const CLIENT_ID = process.env.CLIENT_ID; | ||
const params = toQueryParams({ | ||
issuer: ISSUER, | ||
clientId: CLIENT_ID | ||
}); | ||
await browser.url('/static' + params); | ||
await browser.waitUntil(async () => this.error.then(el => el.isExisting()), 5000, 'wait for error selector'); | ||
} | ||
|
||
async assertNoError() { | ||
await this.error.then(el => el.getText()).then(txt => { | ||
assert(txt === ''); | ||
}); | ||
} | ||
|
||
async waitForLoginBtn() { | ||
return browser.waitUntil(async () => this.login.then(el => el.isDisplayed()), 5000, 'wait for login button'); | ||
} | ||
|
||
async clickLogin() { | ||
await this.waitForLoginBtn(); | ||
await this.login.then(el => el.click()); | ||
} | ||
|
||
async waitForUserInfo() { | ||
return browser.waitUntil(async () => this.userinfo.then(el => el.isDisplayed()), 5000, 'wait for userinfo'); | ||
} | ||
|
||
async assertUserInfo() { | ||
await this.waitForUserInfo(); | ||
await this.userinfo.then(el => el.getText()).then(txt => { | ||
assert(txt !== ''); | ||
}); | ||
} | ||
|
||
async waitForLogoutBtn() { | ||
return browser.waitUntil(async () => this.logout.then(el => el.isDisplayed()), 5000, 'wait for logout button'); | ||
} | ||
|
||
async clickLogout() { | ||
await this.waitForLogoutBtn(); | ||
await this.logout.then(el => el.click()); | ||
} | ||
|
||
} | ||
|
||
export default new StaticApp(); |
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,23 @@ | ||
import StaticApp from '../pageobjects/StaticApp'; | ||
import OktaLogin from '../pageobjects/OktaLogin'; | ||
import TestApp from '../pageobjects/TestApp'; | ||
|
||
const USERNAME = process.env.USERNAME; | ||
const PASSWORD = process.env.PASSWORD; | ||
|
||
describe('Static App', () => { | ||
|
||
beforeEach(async () => { | ||
await StaticApp.open(); | ||
}); | ||
|
||
it('can login, display userinfo, and logout', async () => { | ||
await StaticApp.clickLogin(); | ||
await OktaLogin.signin(USERNAME, PASSWORD); | ||
await StaticApp.assertUserInfo(); | ||
await StaticApp.assertNoError(); | ||
await StaticApp.clickLogout(); | ||
await TestApp.waitForLoginBtn(); | ||
}); | ||
|
||
}); |