Skip to content

Commit

Permalink
feat: Added internal APIs for setting custom User Agent state (#12249)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblanc authored Oct 10, 2023
1 parent bd7c838 commit db28490
Showing 7 changed files with 301 additions and 41 deletions.
52 changes: 26 additions & 26 deletions packages/aws-amplify/package.json
Original file line number Diff line number Diff line change
@@ -252,31 +252,31 @@
"name": "[Analytics] record (Pinpoint)",
"path": "./lib-esm/analytics/index.js",
"import": "{ record }",
"limit": "21.62 kB"
"limit": "21.69 kB"
},
{
"name": "[Analytics] record (Kinesis)",
"path": "./lib-esm/analytics/kinesis/index.js",
"import": "{ record }",
"limit": "46.89 kB"
"limit": "46.96 kB"
},
{
"name": "[Analytics] record (Kinesis Firehose)",
"path": "./lib-esm/analytics/kinesis-firehose/index.js",
"import": "{ record }",
"limit": "43.23 kB"
"limit": "43.31 kB"
},
{
"name": "[Analytics] record (Personalize)",
"path": "./lib-esm/analytics/personalize/index.js",
"import": "{ record }",
"limit": "47.50 kB"
"limit": "47.59 kB"
},
{
"name": "[Analytics] identifyUser (Pinpoint)",
"path": "./lib-esm/analytics/index.js",
"import": "{ identifyUser }",
"limit": "19.72 kB"
"limit": "19.79 kB"
},
{
"name": "[Analytics] enable",
@@ -312,13 +312,13 @@
"name": "[Auth] resetPassword (Cognito)",
"path": "./lib-esm/auth/index.js",
"import": "{ resetPassword }",
"limit": "11.7 kB"
"limit": "11.77 kB"
},
{
"name": "[Auth] confirmResetPassword (Cognito)",
"path": "./lib-esm/auth/index.js",
"import": "{ confirmResetPassword }",
"limit": "11.64 kB"
"limit": "11.70 kB"
},
{
"name": "[Auth] signIn (Cognito)",
@@ -330,7 +330,7 @@
"name": "[Auth] resendSignUpCode (Cognito)",
"path": "./lib-esm/auth/index.js",
"import": "{ resendSignUpCode }",
"limit": "11.66 kB"
"limit": "11.73 kB"
},
{
"name": "[Auth] confirmSignUp (Cognito)",
@@ -342,31 +342,31 @@
"name": "[Auth] confirmSignIn (Cognito)",
"path": "./lib-esm/auth/index.js",
"import": "{ confirmSignIn }",
"limit": "29.6 kB"
"limit": "29.65 kB"
},
{
"name": "[Auth] updateMFAPreference (Cognito)",
"path": "./lib-esm/auth/index.js",
"import": "{ updateMFAPreference }",
"limit": "10.75 kB"
"limit": "10.82 kB"
},
{
"name": "[Auth] fetchMFAPreference (Cognito)",
"path": "./lib-esm/auth/index.js",
"import": "{ fetchMFAPreference }",
"limit": "10.79 kB"
"limit": "10.85 kB"
},
{
"name": "[Auth] verifyTOTPSetup (Cognito)",
"path": "./lib-esm/auth/index.js",
"import": "{ verifyTOTPSetup }",
"limit": "11.68 kB"
"limit": "11.74 kB"
},
{
"name": "[Auth] updatePassword (Cognito)",
"path": "./lib-esm/auth/index.js",
"import": "{ updatePassword }",
"limit": "11.66 kB"
"limit": "11.73 kB"
},
{
"name": "[Auth] setUpTOTP (Cognito)",
@@ -378,7 +378,7 @@
"name": "[Auth] updateUserAttributes (Cognito)",
"path": "./lib-esm/auth/index.js",
"import": "{ updateUserAttributes }",
"limit": "10.93 kB"
"limit": "11.0 kB"
},
{
"name": "[Auth] getCurrentUser (Cognito)",
@@ -390,73 +390,73 @@
"name": "[Auth] confirmUserAttribute (Cognito)",
"path": "./lib-esm/auth/index.js",
"import": "{ confirmUserAttribute }",
"limit": "11.69 kB"
"limit": "11.76 kB"
},
{
"name": "[Auth] signInWithRedirect (Cognito)",
"path": "./lib-esm/auth/index.js",
"import": "{ signInWithRedirect }",
"limit": "22.87 kB"
"limit": "22.94 kB"
},
{
"name": "[Auth] fetchUserAttributes (Cognito)",
"path": "./lib-esm/auth/index.js",
"import": "{ fetchUserAttributes }",
"limit": "10.78 kB"
"limit": "10.85 kB"
},
{
"name": "[Auth] Basic Auth Flow (Cognito)",
"path": "./lib-esm/auth/index.js",
"import": "{ signIn, signOut, fetchAuthSession, confirmSignIn }",
"limit": "31.88 kB"
"limit": "31.92 kB"
},
{
"name": "[Auth] OAuth Auth Flow (Cognito)",
"path": "./lib-esm/auth/index.js",
"import": "{ signInWithRedirect, signOut, fetchAuthSession }",
"limit": "23.36 kB"
"limit": "23.42 kB"
},
{
"name": "[Storage] copy (S3)",
"path": "./lib-esm/storage/index.js",
"import": "{ copy }",
"limit": "17.88 kB"
"limit": "17.95 kB"
},
{
"name": "[Storage] downloadData (S3)",
"path": "./lib-esm/storage/index.js",
"import": "{ downloadData }",
"limit": "18.24 kB"
"limit": "18.30 kB"
},
{
"name": "[Storage] getProperties (S3)",
"path": "./lib-esm/storage/index.js",
"import": "{ getProperties }",
"limit": "17.52 kB"
"limit": "17.6 kB"
},
{
"name": "[Storage] getUrl (S3)",
"path": "./lib-esm/storage/index.js",
"import": "{ getUrl }",
"limit": "18.96 kB"
"limit": "19.03 kB"
},
{
"name": "[Storage] list (S3)",
"path": "./lib-esm/storage/index.js",
"import": "{ list }",
"limit": "18.05 kB"
"limit": "18.12 kB"
},
{
"name": "[Storage] remove (S3)",
"path": "./lib-esm/storage/index.js",
"import": "{ remove }",
"limit": "17.36 kB"
"limit": "17.43 kB"
},
{
"name": "[Storage] uploadData (S3)",
"path": "./lib-esm/storage/index.js",
"import": "{ uploadData }",
"limit": "24.16 kB"
"limit": "24.22 kB"
}
],
"jest": {
79 changes: 79 additions & 0 deletions packages/core/__tests__/Platform/customUserAgent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
Category,
AuthAction,
StorageAction,
SetCustomUserAgentInput,
} from '../../src/Platform/types';

const MOCK_AUTH_UA_STATE: SetCustomUserAgentInput = {
category: Category.Auth,
apis: [AuthAction.ConfirmSignIn, AuthAction.SignIn],
additionalDetails: [['uastate', 'auth']],
};

const MOCK_STORAGE_UA_STATE: SetCustomUserAgentInput = {
category: Category.Storage,
apis: [StorageAction.Copy],
additionalDetails: [['uastate', 'storage']],
};

describe('Custom user agent utilities', () => {
let getCustomUserAgent;
let setCustomUserAgent;

beforeEach(() => {
jest.resetModules();
getCustomUserAgent =
require('../../src/Platform/customUserAgent').getCustomUserAgent;
setCustomUserAgent =
require('../../src/Platform/customUserAgent').setCustomUserAgent;
});

it('sets custom user agent state for multiple categories and APIs', () => {
setCustomUserAgent(MOCK_AUTH_UA_STATE);
setCustomUserAgent(MOCK_STORAGE_UA_STATE);

const confirmSignInState = getCustomUserAgent(
Category.Auth,
AuthAction.ConfirmSignIn
);
const signInState = getCustomUserAgent(Category.Auth, AuthAction.SignIn);
const copyState = getCustomUserAgent(Category.Storage, StorageAction.Copy);

expect(copyState).toEqual([['uastate', 'storage']]);
expect(confirmSignInState).toStrictEqual([['uastate', 'auth']]);
expect(signInState).toEqual(confirmSignInState);
});

it('returns a callback that will clear user agent state', () => {
const cleanUp = setCustomUserAgent(MOCK_AUTH_UA_STATE);
const cleanUp2 = setCustomUserAgent(MOCK_AUTH_UA_STATE);
const cleanUp3 = setCustomUserAgent(MOCK_STORAGE_UA_STATE);

// Setting state for the same category & API twice should prevent deletion until all references are cleaned up
cleanUp();
let confirmSignInState = getCustomUserAgent(
Category.Auth,
AuthAction.ConfirmSignIn
);
expect(confirmSignInState).toStrictEqual([['uastate', 'auth']]);

cleanUp2();
confirmSignInState = getCustomUserAgent(
Category.Auth,
AuthAction.ConfirmSignIn
);
expect(confirmSignInState).toStrictEqual(undefined);

// Calling the same cleanup callback twice shouldn't result in errors
cleanUp();

// Cleaning up shouldn't impact state set in a different call
let copyState = getCustomUserAgent(Category.Storage, StorageAction.Copy);
expect(copyState).toEqual([['uastate', 'storage']]);

cleanUp3();
copyState = getCustomUserAgent(Category.Storage, StorageAction.Copy);
expect(copyState).toStrictEqual(undefined);
});
});
Original file line number Diff line number Diff line change
@@ -2,13 +2,26 @@ import {
getAmplifyUserAgentObject,
getAmplifyUserAgent,
Platform,
} from '../src/Platform';
import { version } from '../src/Platform/version';
import { ApiAction, Category, Framework } from '../src/Platform/types';
import { detectFramework, clearCache } from '../src/Platform/detectFramework';
import * as detection from '../src/Platform/detection';
} from '../../src/Platform';
import { version } from '../../src/Platform/version';
import {
ApiAction,
AuthAction,
Category,
Framework,
} from '../../src/Platform/types';
import {
detectFramework,
clearCache,
} from '../../src/Platform/detectFramework';
import * as detection from '../../src/Platform/detection';
import { getCustomUserAgent } from '../../src/Platform/customUserAgent';

jest.mock('../../src/Platform/customUserAgent');

describe('Platform test', () => {
const mockGetCustomUserAgent = getCustomUserAgent as jest.Mock;

beforeAll(() => {
jest.useFakeTimers();
});
@@ -18,6 +31,7 @@ describe('Platform test', () => {
});

beforeEach(() => {
mockGetCustomUserAgent.mockReset();
clearCache();
});

@@ -39,13 +53,32 @@ describe('Platform test', () => {
test('with customUserAgentDetails', () => {
expect(
getAmplifyUserAgentObject({
category: Category.API,
action: ApiAction.None,
category: Category.Auth,
action: AuthAction.ConfirmSignIn,
})
).toStrictEqual([
['aws-amplify', version],
[Category.Auth, AuthAction.ConfirmSignIn],
['framework', Framework.WebUnknown],
]);
});

it('injects global user agent details when available', () => {
const mockUAState = [['uiversion', '1.0.0'], ['flag']];

mockGetCustomUserAgent.mockReturnValue(mockUAState);

expect(
getAmplifyUserAgentObject({
category: Category.Auth,
action: AuthAction.ConfirmSignIn,
})
).toStrictEqual([
['aws-amplify', version],
[Category.API, ApiAction.None],
[Category.Auth, AuthAction.ConfirmSignIn],
['framework', Framework.WebUnknown],
['uiversion', '1.0.0'],
['flag'],
]);
});
});
@@ -60,11 +93,26 @@ describe('Platform test', () => {
test('with customUserAgentDetails', () => {
expect(
getAmplifyUserAgent({
category: Category.API,
action: ApiAction.None,
category: Category.Auth,
action: AuthAction.ConfirmSignIn,
})
).toBe(
`${Platform.userAgent} ${Category.Auth}/${AuthAction.ConfirmSignIn} framework/${Framework.WebUnknown}`
);
});

it('handles flag UA attributes', () => {
const mockUAState = [['uiversion', '1.0.0'], ['flag']];

mockGetCustomUserAgent.mockReturnValue(mockUAState);

expect(
getAmplifyUserAgent({
category: Category.Auth,
action: AuthAction.ConfirmSignIn,
})
).toBe(
`${Platform.userAgent} ${Category.API}/${ApiAction.None} framework/${Framework.WebUnknown}`
`${Platform.userAgent} ${Category.Auth}/${AuthAction.ConfirmSignIn} framework/${Framework.WebUnknown} uiversion/1.0.0 flag`
);
});
});
@@ -86,7 +134,7 @@ describe('detectFramework observers', () => {

beforeAll(() => {
jest.resetModules();
module = require('../src/Platform/detectFramework');
module = require('../../src/Platform/detectFramework');
jest.useFakeTimers();
});

Loading

0 comments on commit db28490

Please sign in to comment.