Skip to content

Commit

Permalink
chore: use oktaAuth._oktaUserAgent for tracking SDK versions
Browse files Browse the repository at this point in the history
OKTA-419567
<<<Jenkins Check-In of Tested SHA: 30d3500 for [email protected]>>>
Artifact: okta-react
Files changed count: 4
PR Link: "#159"
  • Loading branch information
oleksandrpravosudko-okta authored and eng-prod-CI-bot-okta committed Aug 23, 2021
1 parent 16f1b06 commit 9bd6305
Show file tree
Hide file tree
Showing 4 changed files with 1,976 additions and 1,733 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@babel/runtime": "^7.11.2"
},
"peerDependencies": {
"@okta/okta-auth-js": "^5.2.3",
"@okta/okta-auth-js": "^5.3.1",
"@types/react-router-dom": "^5.1.6",
"react": ">=16.8.0",
"react-dom": ">=16.8.0",
Expand Down Expand Up @@ -105,4 +105,4 @@
"./",
"test/e2e/harness"
]
}
}
15 changes: 12 additions & 3 deletions src/Security.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import * as React from 'react';
import { AuthSdkError, OktaAuth, AuthState } from '@okta/okta-auth-js';
import { AuthSdkError, AuthState, OktaAuth } from '@okta/okta-auth-js';
import OktaContext, { OnAuthRequiredFunction, RestoreOriginalUriFunction } from './OktaContext';
import OktaError from './OktaError';

Expand All @@ -33,7 +33,12 @@ const Security: React.FC<{
return oktaAuth.authStateManager.getAuthState();
});
const [oktaAuthMajorVersion] = React.useState(() => {
const majorVersion = oktaAuth?.userAgent?.split('/')[1]?.split('.')[0];
if (!oktaAuth || !oktaAuth._oktaUserAgent) {
return null;
}

const oktaAuthVersion = oktaAuth._oktaUserAgent.getVersion();
const majorVersion = oktaAuthVersion?.split('.')[0];
return majorVersion;
});

Expand All @@ -51,7 +56,11 @@ const Security: React.FC<{
};

// Add okta-react userAgent
oktaAuth.userAgent = `${process.env.PACKAGE_NAME}/${process.env.PACKAGE_VERSION} ${oktaAuth.userAgent}`;
if (oktaAuth._oktaUserAgent) {
oktaAuth._oktaUserAgent.addEnvironment(`${process.env.PACKAGE_NAME}/${process.env.PACKAGE_VERSION}`);
} else {
console.warn('_oktaUserAgent is not available on auth SDK instance. Please use okta-auth-js@^5.3.1 .');
}

// Update Security provider with latest authState
const handler = (authState: AuthState) => {
Expand Down
49 changes: 45 additions & 4 deletions test/jest/security.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { act } from 'react-dom/test-utils';
import { MemoryRouter } from 'react-router-dom';
import Security from '../../src/Security';
import { useOktaAuth } from '../../src/OktaContext';
import * as pkg from '../../package.json';

console.warn = jest.fn();

describe('<Security />', () => {
let oktaAuth;
Expand All @@ -25,11 +26,17 @@ describe('<Security />', () => {
location.href = url;
};
beforeEach(() => {
jest.clearAllMocks();

initialAuthState = {
isInitialState: true
};
oktaAuth = {
userAgent: 'okta/okta-auth-js',
_oktaUserAgent: {
addEnvironment: jest.fn(),
getHttpHeader: jest.fn(),
getVersion: jest.fn()
},
options: {},
authStateManager: {
getAuthState: jest.fn().mockImplementation(() => initialAuthState),
Expand All @@ -42,13 +49,28 @@ describe('<Security />', () => {
};
});

it('should set userAgent for oktaAuth', () => {
it('adds an environmemnt to oktaAuth\'s _oktaUserAgent', () => {
const addEnvironmentSpy = jest.spyOn(oktaAuth._oktaUserAgent, 'addEnvironment');

const mockProps = {
oktaAuth,
restoreOriginalUri
};
mount(<Security {...mockProps} />);
expect(oktaAuth.userAgent).toEqual(`${pkg.name}/${pkg.version} okta/okta-auth-js`);
expect(addEnvironmentSpy).toBeCalledWith(`${process.env.PACKAGE_NAME}/${process.env.PACKAGE_VERSION}`);
});

it('logs a warning in case _oktaUserAgent is not available on auth SDK instance', () => {
const oktaAuthWithoutUserAgent = {
...oktaAuth
};
delete oktaAuthWithoutUserAgent['_oktaUserAgent'];
const mockProps = {
oktaAuth: oktaAuthWithoutUserAgent,
restoreOriginalUri
};
mount(<Security {...mockProps} />);
expect(console.warn).toBeCalled();
});

describe('throws version not match error', () => {
Expand All @@ -70,6 +92,25 @@ describe('<Security />', () => {
const wrapper = mount(<Security {...mockProps} />);
expect(wrapper.find(Security).html()).toBe(`<p>AuthSdkError: Passed in oktaAuth is not compatible with the SDK, okta-auth-js version 5.x is the current supported version.</p>`);
});

it('can get okta-auth version from _oktaUserAgent property', () => {
const oktaAuthWithMismatchingSDKVersion = {
...oktaAuth,
_oktaUserAgent: {
addEnvironment: jest.fn(),
getVersion: jest.fn().mockReturnValue('okta-auth-js/9999.0.0') // intentional large mock version
}
};

const mockProps = {
oktaAuth: oktaAuthWithMismatchingSDKVersion,
restoreOriginalUri
};
// mock auth-js version from dependencies
process.env.AUTH_JS_MAJOR_VERSION = '5';
const wrapper = mount(<Security {...mockProps} />);
expect(wrapper.find(Security).html()).toBe(`<p>AuthSdkError: Passed in oktaAuth is not compatible with the SDK, okta-auth-js version 5.x is the current supported version.</p>`);
});
});

it('should set default restoreOriginalUri callback in oktaAuth.options', () => {
Expand Down
Loading

0 comments on commit 9bd6305

Please sign in to comment.