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

Commit

Permalink
Set user agent for Angular, React, Vue libs - plus Jest (#151)
Browse files Browse the repository at this point in the history
Uses the new property in AuthJS to append the framework lib to the user agent

Also adds unit testing via Jest for React, Vue

Angular not yet tested, will add a "unit" test to app.component.spec.ts after #143 lands in master

Add lniter to test in vue
  • Loading branch information
robertjd authored and jmelberg-okta committed Mar 20, 2018
1 parent 95095af commit a627542
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 9 deletions.
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 lint && npm run test:unit && npm run test:e2e",
"test:unit": "npm run --prefix test/e2e/harness/ test",
Expand All @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions 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, NavigationExtras } 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
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" }}]
]
}
}
}
1 change: 1 addition & 0 deletions packages/okta-vue/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/config/
/dist/
/*.js
src/packageInfo.js
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 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$": "<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');
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);

0 comments on commit a627542

Please sign in to comment.