Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
Support overriding env vars with testenv file (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongranick-okta authored Oct 18, 2019
1 parent 2aed898 commit a6f4e9f
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
node_modules
npm-debug.log
.DS_STORE
.vscode
.envrc
.yarnrc
*.code-workspace
Expand All @@ -10,6 +9,7 @@ yarn-debug.log*
yarn-error.log*
package-lock.json
dist
testenv

# Ignore TCK-related files in all folders
okta-oidc-tck*
Expand Down
15 changes: 15 additions & 0 deletions .oidc.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
* @param {Object} overrides - (optional) Overrides specific values for the configuration object
*/

// Support storing environment variables in a file named "testenv"
const path = require('path');
const dotenv = require('dotenv');
const fs = require('fs');

// Read environment variables from "testenv". Override environment vars if they are already set.
const TESTENV = path.resolve(__dirname, 'testenv');
if (fs.existsSync(TESTENV)) {
const envConfig = dotenv.parse(fs.readFileSync(TESTENV));
Object.keys(envConfig).forEach((k) => {
process.env[k] = envConfig[k];
});
}
process.env.CLIENT_ID = process.env.CLIENT_ID || process.env.SPA_CLIENT_ID;

module.exports = (overrides = {}) => {
const PORT = overrides.port || process.env.PORT || 3000;
const BASE_URI = process.env.BASE_URI || `http://localhost:${PORT}`;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"private": true,
"devDependencies": {
"dotenv": "^8.1.0",
"globby": "^6.1.0",
"lerna": "^2.11.0"
},
Expand Down
25 changes: 24 additions & 1 deletion packages/okta-react/test/e2e/harness/config-overrides.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
/* global __dirname, module */
/* global __dirname, module, process */

const webpack = require('webpack');
const path = require('path');
const MAIN_ENTRY = path.resolve(__dirname, '../../../dist/index.js');

require('../../../../../.oidc.config.js'); // will load environment vars from testenv

const env = {};
// List of environment variables made available to the app
[
'ISSUER',
'CLIENT_ID',
].forEach((key) => {
if (!process.env[key]) {
throw new Error(`Environment variable ${key} must be set. See README.md`);
}
env[key] = JSON.stringify(process.env[key]);
});


module.exports = {
/* eslint-disable no-param-reassign */
webpack: (config) => {
// Remove the 'ModuleScopePlugin' which keeps us from requiring outside the src/ dir
config.resolve.plugins = [];

// Define global vars from env vars (process.env has already been defined)
config.plugins = [
new webpack.DefinePlugin({
'process.env': env,
}),
].concat(config.plugins);

if (!config.resolve.alias) {
config.resolve.alias = {};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/okta-react/test/e2e/harness/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"webdriver-manager": "^12.1.4"
},
"scripts": {
"start": "REACT_APP_ISSUER=$ISSUER REACT_APP_CLIENT_ID=$SPA_CLIENT_ID PORT=8080 OKTA_TESTING_DISABLEHTTPSCHECK=$OKTA_TESTING_DISABLEHTTPSCHECK react-app-rewired start",
"build": "REACT_APP_ISSUER=$ISSUER REACT_APP_CLIENT_ID=$SPA_CLIENT_ID PORT=8080 OKTA_TESTING_DISABLEHTTPSCHECK=$OKTA_TESTING_DISABLEHTTPSCHECK react-app-rewired build",
"start": "PORT=8080 react-app-rewired start",
"build": "react-app-rewired build",
"build:test": "rimraf e2e/dist && babel e2e/ -d e2e/dist",
"kill:port": "kill -s TERM $(lsof -t -i:8080 -sTCP:LISTEN) || true",
"pretest": "../../../../../scripts/updateSeDrivers.sh 0 && yarn build:test",
Expand Down
6 changes: 3 additions & 3 deletions packages/okta-react/test/e2e/harness/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ if (!Auth) {
class App extends Component {
render() {
/* global process */
const { REACT_APP_ISSUER, REACT_APP_CLIENT_ID } = process.env;
const { ISSUER, CLIENT_ID } = process.env;
const { pkce, redirectUri } = this.props;
return (
<React.StrictMode>
<Router>
<Security issuer={REACT_APP_ISSUER}
clientId={REACT_APP_CLIENT_ID}
<Security issuer={ISSUER}
clientId={CLIENT_ID}
disableHttpsCheck={true}
redirectUri={redirectUri}
onAuthRequired={({history}) => history.push('/login')}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,11 @@ dot-prop@^3.0.0:
dependencies:
is-obj "^1.0.0"

dotenv@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.1.0.tgz#d811e178652bfb8a1e593c6dd704ec7e90d85ea2"
integrity sha512-GUE3gqcDCaMltj2++g6bRQ5rBJWtkWTmqmD0fo1RnnMuUqHNCt2oTPeDnS9n6fKYvlhn7AeBkb38lymBtWBQdA==

duplexer3@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/duplexer3/-/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2"
Expand Down

0 comments on commit a6f4e9f

Please sign in to comment.