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

Set user agent for Angular, React, Vue libs - plus Jest #151

Merged
merged 2 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ package-lock.json
.envrc
*.code-workspace
lerna-debug.log

# Ignore TCK-related files in all folders
okta-oidc-tck*
target
packages/okta-vue/test/e2e/harness/config/dev.env.js

# package info files are generated during build and included in final artifacts
packageInfo.js
packageInfo.ts
4 changes: 3 additions & 1 deletion packages/okta-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
"dist"
],
"scripts": {
"build:package-info": "node ../../util/write-package-info.js . src/okta/packageInfo.ts",
"pretest": "npm run build:dependencies",
"prestart": "npm run build:dependencies",
"prepublish": "npm run ngc",
"prengc": "npm run build:package-info",
"ngc": "./node_modules/.bin/ngc -p tsconfig.json",
"test": "npm run --prefix test/e2e/harness/ e2e",
"start": "npm run --prefix test/e2e/harness/ start",
Expand All @@ -30,7 +32,7 @@
],
"license": "Apache-2.0",
"dependencies": {
"@okta/okta-auth-js": "^1.8.0"
"@okta/okta-auth-js": "^1.14.0"
},
"devDependencies": {
"@angular/common": "^4.4.3",
Expand Down
6 changes: 5 additions & 1 deletion packages/okta-angular/src/okta/okta.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { Router } from '@angular/router';

import { OKTA_CONFIG } from './okta.config';

import packageInfo from './packageInfo';

/**
* Import the okta-auth-js library
*/
Expand Down Expand Up @@ -49,6 +51,8 @@ export class OktaAuthService {
redirectUri: auth.redirectUri
});

this.oktaAuth.userAgent = `${packageInfo.name}/${packageInfo.version} ${this.oktaAuth.userAgent}`;

/**
* Scrub scopes to ensure 'openid' is included
*/
Expand Down Expand Up @@ -127,7 +131,7 @@ export class OktaAuthService {

/**
* Stores the intended path to redirect after successful login.
* @param uri
* @param uri
*/
setFromUri(uri) {
localStorage.setItem('referrerPath', uri);
Expand Down
9 changes: 6 additions & 3 deletions packages/okta-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
"scripts": {
"build": "rimraf dist/ && babel src -d dist",
"build:harness": "npm --prefix test/e2e/harness install",
"build:package-info": "node ../../util/write-package-info.js . src/packageInfo.js",
"jest": "jest test/jest/",
"lint": "eslint src/**; exit 0",
"lint:watch": "esw -w lib/**",
"prepublish": "npm run build",
"prepublish": "npm run build:package-info && npm run build",
"prestart": "npm run build && npm run build:harness",
"start": "npm run --prefix test/e2e/harness start",
"pretest": "npm run build && npm run build:harness",
"test": "npm run --prefix test/e2e/harness test"
"test": "npm run jest && npm run --prefix test/e2e/harness test"
},
"repository": {
"type": "git",
Expand All @@ -32,7 +34,7 @@
},
"homepage": "https://github.com/okta/okta-oidc-js#readme",
"dependencies": {
"@okta/okta-auth-js": "^1.8.0",
"@okta/okta-auth-js": "^1.14.0",
"babel-runtime": "^6.26.0",
"prop-types": "^15.5.10"
},
Expand All @@ -56,6 +58,7 @@
"eslint-watch": "^3.1.2",
"jasmine-core": "~2.6.2",
"jasmine-spec-reporter": "~4.1.0",
"jest": "^22.4.2",
"polished": "^1.7.0",
"protractor": "^5.1.2",
"react-dom": "^15.6.1",
Expand Down
3 changes: 3 additions & 0 deletions packages/okta-react/src/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import OktaAuth from '@okta/okta-auth-js';

import packageInfo from './packageInfo';

const containsAuthTokens = /id_token|access_token|code/;

export default class Auth {
Expand All @@ -22,6 +24,7 @@ export default class Auth {
issuer: config.issuer,
redirectUri: config.redirect_uri
});
this._oktaAuth.userAgent = `${packageInfo.name}/${packageInfo.version} ${this._oktaAuth.userAgent}`;
this._config = config;
this._history = config.history;

Expand Down
11 changes: 11 additions & 0 deletions packages/okta-react/test/jest/auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Auth from '../../src/Auth';

const pkg = require('../../package.json');

test('Auth component sets the right user agent on AuthJS', () => {
const auth = new Auth({
issuer: 'https://foo/oauth2/default'
});
const expectedUserAgent = `${pkg.name}/${pkg.version} okta-auth-js-`;
expect(auth._oktaAuth.userAgent).toMatch(expectedUserAgent);
});
4 changes: 3 additions & 1 deletion packages/okta-vue/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"plugins": ["transform-runtime"],
"env": {
"test": {
"presets": ["env", "stage-2", "es2015"]
"presets": [
["env", { "targets": { "node": "current" }}]
]
}
}
}
4 changes: 3 additions & 1 deletion packages/okta-vue/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ module.exports = {
},
env: {
browser: true,
"jest/globals": true
},
// https://github.com/standard/standard/blob/master/docs/RULES-en.md
extends: 'standard',
// required to lint *.vue files
plugins: [
'html'
'html',
'jest'
],
// add your custom rules here
rules: {
Expand Down
27 changes: 25 additions & 2 deletions packages/okta-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,33 @@
"src"
],
"scripts": {
"prebuild": "npm run build:package-info",
"prestart": "npm run build",
"pretest": "npm run build && npm run build:harness",
"prepublish": "npm run build",
"test": "npm run --prefix test/e2e/harness/ test",
"jest": "jest src/",
"test": "npm run jest && npm run --prefix test/e2e/harness/ test",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaning towards thinking we should prepend npm run lint to this command. That way we can start enforcing the eslint style.

"start": "npm run --prefix test/e2e/harness/ start",
"build": "rimraf dist/ && cross-env NODE_ENV=production webpack --config webpack.config.js --output-library-target=umd -p",
"build:harness": "npm --prefix test/e2e/harness install",
"build:package-info": "node ../../util/write-package-info.js . src/packageInfo.js",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should consider using lerna run to handle this for us.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This task needs to be integrated into the build pipeline of each package, and ran locally while working on that specific package, so I'm not sure how lerna would help here?

"lint": "eslint --ext .js,.vue src"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"vue"
],
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/babel-jest",
".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
},
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
},
"mapCoverage": true
},
"repository": {
"type": "git",
"url": "git+https://github.com/okta/okta-oidc-js.git"
Expand All @@ -31,15 +49,17 @@
},
"homepage": "https://github.com/okta/okta-oidc-js#readme",
"dependencies": {
"@okta/okta-auth-js": "^1.11.0",
"@okta/okta-auth-js": "^1.14.0",
"cross-env": "^5.1.1",
"vue": "^2.5.9",
"vue-router": "^3.0.1"
},
"devDependencies": {
"@vue/test-utils": "^1.0.0-beta.12",
"autoprefixer": "^7.1.2",
"babel-core": "^6.22.1",
"babel-eslint": "^7.1.1",
"babel-jest": "^23.0.0-alpha.0",
"babel-loader": "^7.1.1",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-preset-env": "^1.3.2",
Expand All @@ -52,13 +72,16 @@
"eslint-loader": "^1.7.1",
"eslint-plugin-html": "^3.0.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-jest": "^21.15.0",
"eslint-plugin-node": "^5.2.0",
"eslint-plugin-promise": "^3.4.0",
"eslint-plugin-standard": "^3.0.1",
"jest": "^22.4.2",
"nightwatch": "^0.9.12",
"rimraf": "^2.6.2",
"uglifyjs-webpack-plugin": "^1.1.1",
"url-loader": "^0.5.8",
"vue-jest": "^2.1.1",
"vue-loader": "13.0.2",
"vue-style-loader": "^3.0.1",
"vue-template-compiler": "^2.5.2",
Expand Down
4 changes: 3 additions & 1 deletion packages/okta-vue/src/Auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as AuthJS from '@okta/okta-auth-js'
import AuthJS from '@okta/okta-auth-js'
import packageInfo from './packageInfo'
import ImplicitCallback from './components/ImplicitCallback'

function install (Vue, options) {
Expand All @@ -9,6 +10,7 @@ function install (Vue, options) {
redirectUri: authConfig.redirect_uri,
url: authConfig.issuer.split('/oauth2/')[0]
})
oktaAuth.userAgent = `${packageInfo.name}/${packageInfo.version} ${oktaAuth.userAgent}`

Vue.prototype.$auth = {
loginRedirect (additionalParams) {
Expand Down
27 changes: 27 additions & 0 deletions packages/okta-vue/src/Auth.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import AuthJS from '@okta/okta-auth-js'
import { createLocalVue } from '@vue/test-utils'
import { default as Auth } from './Auth'

const mockAuthJsInstance = {userAgent: 'foo'}
const pkg = require('../package.json')

jest.mock('@okta/okta-auth-js')

AuthJS.mockImplementation(() => {
return mockAuthJsInstance
})

describe('Auth', () => {
test('is a Vue plugin', () => {
expect(Auth.install).toBeTruthy()
})
test('sets the right user agent on AuthJS', () => {
const expectedUserAgent = `${pkg.name}/${pkg.version} foo`
createLocalVue().use(Auth, {
issuer: '1',
client_id: '2',
redirect_uri: '3'
})
expect(mockAuthJsInstance.userAgent).toMatch(expectedUserAgent)
})
})
17 changes: 17 additions & 0 deletions util/write-package-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require('fs');
const path = require('path');

const workingDirectory = process.argv[2];
const destinationFile = process.argv[3];
const packageJsonPath = path.join(process.cwd(), workingDirectory, 'package.json')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon

const configDest = path.join(process.cwd(), workingDirectory, destinationFile);
const packageJson = require(packageJsonPath);
const packageInfo = {
name: packageJson.name,
version: packageJson.version
};
const output = 'export default ' + JSON.stringify(packageInfo, null, 2) + ';'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: There is no ; in our okta-vue/src/*.js files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This util script is at the root, so not part of lerna. I can put together a PR to add eslint to the root, but we'd have to do testing to make sure it doesn't break what we just fixed for windows.


console.log('Writing config to', configDest);

fs.writeFileSync(configDest, output);