diff --git a/.gitignore b/.gitignore index 6f4e44e86..9f3f95543 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/packages/okta-angular/package.json b/packages/okta-angular/package.json index d206d5508..a739e69d6 100644 --- a/packages/okta-angular/package.json +++ b/packages/okta-angular/package.json @@ -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 lint && npm run test:unit && npm run test:e2e", "test:unit": "npm run --prefix test/e2e/harness/ test", @@ -32,7 +34,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", diff --git a/packages/okta-angular/src/okta/okta.service.ts b/packages/okta-angular/src/okta/okta.service.ts index 055e9fee5..b2289a37f 100644 --- a/packages/okta-angular/src/okta/okta.service.ts +++ b/packages/okta-angular/src/okta/okta.service.ts @@ -15,6 +15,8 @@ import { Router, NavigationExtras } from '@angular/router'; import { OKTA_CONFIG } from './okta.config'; +import packageInfo from './packageInfo'; + /** * Import the okta-auth-js library */ @@ -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 */ diff --git a/packages/okta-react/package.json b/packages/okta-react/package.json index a449ef101..c82697d09 100644 --- a/packages/okta-react/package.json +++ b/packages/okta-react/package.json @@ -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", @@ -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" }, @@ -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", diff --git a/packages/okta-react/src/Auth.js b/packages/okta-react/src/Auth.js index b82db7564..90ee10392 100644 --- a/packages/okta-react/src/Auth.js +++ b/packages/okta-react/src/Auth.js @@ -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 { @@ -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; diff --git a/packages/okta-react/test/jest/auth.test.js b/packages/okta-react/test/jest/auth.test.js new file mode 100644 index 000000000..a9affde02 --- /dev/null +++ b/packages/okta-react/test/jest/auth.test.js @@ -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); +}); diff --git a/packages/okta-vue/.babelrc b/packages/okta-vue/.babelrc index 4f153f829..ea9970575 100644 --- a/packages/okta-vue/.babelrc +++ b/packages/okta-vue/.babelrc @@ -6,7 +6,9 @@ "plugins": ["transform-runtime"], "env": { "test": { - "presets": ["env", "stage-2", "es2015"] + "presets": [ + ["env", { "targets": { "node": "current" }}] + ] } } } diff --git a/packages/okta-vue/.eslintignore b/packages/okta-vue/.eslintignore index e1fcc9c45..6727f2b26 100644 --- a/packages/okta-vue/.eslintignore +++ b/packages/okta-vue/.eslintignore @@ -2,3 +2,4 @@ /config/ /dist/ /*.js +src/packageInfo.js diff --git a/packages/okta-vue/.eslintrc.js b/packages/okta-vue/.eslintrc.js index 2e2e21183..f7f84fee6 100644 --- a/packages/okta-vue/.eslintrc.js +++ b/packages/okta-vue/.eslintrc.js @@ -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: { diff --git a/packages/okta-vue/package.json b/packages/okta-vue/package.json index 487f9b6ab..1ae202222 100644 --- a/packages/okta-vue/package.json +++ b/packages/okta-vue/package.json @@ -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 lint && npm run jest && npm run --prefix test/e2e/harness/ test", "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", "lint": "eslint --ext .js,.vue src" }, + "jest": { + "moduleFileExtensions": [ + "js", + "json", + "vue" + ], + "transform": { + "^.+\\.js$": "/node_modules/babel-jest", + ".*\\.(vue)$": "/node_modules/vue-jest" + }, + "moduleNameMapper": { + "^@/(.*)$": "/src/$1" + }, + "mapCoverage": true + }, "repository": { "type": "git", "url": "git+https://github.com/okta/okta-oidc-js.git" @@ -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", @@ -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", diff --git a/packages/okta-vue/src/Auth.js b/packages/okta-vue/src/Auth.js index 4830cff1a..d14302be5 100644 --- a/packages/okta-vue/src/Auth.js +++ b/packages/okta-vue/src/Auth.js @@ -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) { @@ -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) { diff --git a/packages/okta-vue/src/Auth.spec.js b/packages/okta-vue/src/Auth.spec.js new file mode 100644 index 000000000..f2eca09f6 --- /dev/null +++ b/packages/okta-vue/src/Auth.spec.js @@ -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) + }) +}) diff --git a/util/write-package-info.js b/util/write-package-info.js new file mode 100644 index 000000000..c978805fb --- /dev/null +++ b/util/write-package-info.js @@ -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'); +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) + ';'; + +console.log('Writing config to', configDest); + +fs.writeFileSync(configDest, output);