Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Add isActive session method
Browse files Browse the repository at this point in the history
  • Loading branch information
carmelal committed Mar 30, 2021
1 parent fa9ef02 commit a9ad33b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/auth/session/session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {OnlineAccessInfo} from '../oauth/types';
import {Context} from '../../context';

/**
* Stores App information from logged in merchants so they can make authenticated requests to the Admin API.
Expand Down Expand Up @@ -27,6 +28,14 @@ class Session {
public onlineAccessInfo?: OnlineAccessInfo;

constructor(readonly id: string) {}

public isActive(): boolean {
const scopesChanged = !Context.SCOPES.equals(this.scope);
if (!scopesChanged && this.accessToken && (!this.expires || this.expires >= new Date())) {
return true;
}
return false;
}
}

export {Session};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import '../../../test/test_helper';
import '../../../../test/test_helper';

import {Session} from '../session';
import {CustomSessionStorage} from '../storage/custom';
import {SessionStorageError} from '../../../error';
import {Session} from '../../session';
import {CustomSessionStorage} from '../../storage/custom';
import {SessionStorageError} from '../../../../error';

describe('custom session storage', () => {
test('can perform actions', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '../../../test/test_helper';
import '../../../../test/test_helper';

import {Session} from '../session';
import {MemorySessionStorage} from '../storage/memory';
import {Session} from '../../session';
import {MemorySessionStorage} from '../memory';

test('can store and delete sessions in memory', async () => {
const sessionId = 'test_session';
Expand Down
50 changes: 50 additions & 0 deletions src/auth/session/test/session.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { ApiVersion, ContextParams } from '../../../base_types';
import { Context } from '../../../context';
import '../../../test/test_helper';

import {Session} from '../../session';

const validContextParams: ContextParams = {
API_KEY: 'C_major',
API_SECRET_KEY: 'A_minor',
SCOPES: ['ice_scream_scope'],
HOST_NAME: 'chopin',
API_VERSION: ApiVersion.Unstable,
IS_EMBEDDED_APP: true,
IS_PRIVATE_APP: false,
LOG_FILE: 'going-chopin-with-chopin.txt',
}

describe('session', () => {
it('can clone a session', () => {
const session = new Session('original');
const sessionClone = Session.cloneSession(session, 'new');

expect(session.id).not.toEqual(sessionClone.id);
expect(session.shop).toStrictEqual(sessionClone.shop);
expect(session.state).toStrictEqual(sessionClone.state);
expect(session.scope).toStrictEqual(sessionClone.scope);
expect(session.expires).toStrictEqual(sessionClone.expires);
expect(session.isOnline).toStrictEqual(sessionClone.isOnline);
expect(session.accessToken).toStrictEqual(sessionClone.accessToken);
expect(session.onlineAccessInfo).toStrictEqual(sessionClone.onlineAccessInfo);
});
});

describe('isActive', () => {
it('returns true if session is active', () => { // actual code broken
Context.initialize(validContextParams);
const session = new Session('active');
session.scope = 'ice_cream_scope';
session.accessToken = 'indeed';
expect(session.isActive()).toBe(true);
});

it('returns false if session is not active', () => {
Context.initialize(validContextParams);
const session = new Session('not_active');
session.scope = 'not_same'
session.expires = new Date(Date.now() - 1);
expect(session.isActive()).toBe(false);
});
});

0 comments on commit a9ad33b

Please # to comment.