Skip to content

Commit

Permalink
Dont make empty authScopes object if scopes is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
lizkenyon committed Apr 17, 2024
1 parent a86df65 commit 0704100
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
14 changes: 10 additions & 4 deletions packages/apps/shopify-api/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,18 @@ export function validateConfig<Params extends ConfigParams>(
...mandatoryParams
} = params;

let scopes;
if (params.scopes === undefined) {
scopes = undefined;
} else if (params.scopes instanceof AuthScopes) {
scopes = params.scopes;
} else {
scopes = new AuthScopes(params.scopes);
}

Object.assign(config, mandatoryParams, {
hostName: params.hostName.replace(/\/$/, ''),
scopes:
params.scopes instanceof AuthScopes
? params.scopes
: new AuthScopes(params.scopes),
scopes,
hostScheme: hostScheme ?? config.hostScheme,
isCustomStoreApp: isCustomStoreApp ?? config.isCustomStoreApp,
adminApiAccessToken: adminApiAccessToken ?? config.adminApiAccessToken,
Expand Down
20 changes: 18 additions & 2 deletions packages/apps/shopify-api/lib/session/__tests__/session.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Session} from '../session';
import {testConfig} from '../../__tests__/test-config';
import {shopifyApi} from '../..';
import {AuthScopes} from '../../auth';

describe('session', () => {
it('can create a session from another session', () => {
Expand Down Expand Up @@ -60,7 +61,7 @@ describe('isActive', () => {
});
});

it('returns true when scopes that passed in empty and scopes are not equal', () => {
it('returns true when scopes that passed in undefined and scopes are not equal', () => {
const session = new Session({
id: 'active',
shop: 'active-shop',
Expand All @@ -71,7 +72,22 @@ it('returns true when scopes that passed in empty and scopes are not equal', ()
expires: new Date(Date.now() + 86400),
});

expect(session.isActive('')).toBeTruthy();
expect(session.isActive(undefined)).toBeTruthy();
});

it('returns false when scopes that passed in empty and scopes are not equal', () => {
const session = new Session({
id: 'active',
shop: 'active-shop',
state: 'test_state',
isOnline: true,
scope: 'test_scope',
accessToken: 'indeed',
expires: new Date(Date.now() + 86400),
});

const scopes = new AuthScopes([]);
expect(session.isActive(scopes)).toBeFalsy();
});

it('returns false if session is not active', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/shopify-api/lib/session/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class Session {
): boolean {
const scopesObject =
scopes instanceof AuthScopes ? scopes : new AuthScopes(scopes);
if (typeof scopes === undefined || scopesObject.toArray().length === 0) {
if (typeof scopes === 'undefined') {
return false;
}

Expand Down

0 comments on commit 0704100

Please sign in to comment.