Skip to content

Commit

Permalink
add tests for opcode.ts
Browse files Browse the repository at this point in the history
Because joinWithTokenOrUrl in dynamicConfig.ts calls functions from opcode.ts and there are tests for that, most of opcode.ts is already covered. However, I am adding a few more dedicated tests to cover more scenarios.
  • Loading branch information
JGreenlee committed Oct 17, 2024
1 parent 3752e2c commit ee85fb0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
81 changes: 81 additions & 0 deletions www/__tests__/opcode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// * @example getTokenFromUrl('https://open-access-openpath.nrel.gov/join/') => nrelop_open-access_default_randomLongStringWith32Characters
// * @example getTokenFromUrl('emission://login_token?token=nrelop_study_subgroup_random') => nrelop_study_subgroup_random
// * @example getTokenFromUrl('nrelopenpath://login_token?token=nrelop_study_subgroup_random') => nrelop_study_subgroup_random

import { getStudyNameFromToken, getSubgroupFromToken, getTokenFromUrl } from '../js/config/opcode';
import AppConfig from '../js/types/appConfigTypes';
describe('opcode', () => {
describe('getStudyNameFromToken', () => {
const token = 'nrelop_great-study_default_randomLongStringWith32Characters';
it('returns the study name from a token', () => {
expect(getStudyNameFromToken(token)).toBe('great-study');
});
});

describe('getSubgroupFromToken', () => {
const amazingSubgroupToken = 'nrelop_great-study_amazing-subgroup_000';
it('returns the subgroup from a token with valid subgroup', () => {
const fakeconfig = {
opcode: {
subgroups: ['amazing-subgroup', 'other-subgroup'],
},
} as any as AppConfig;
expect(getSubgroupFromToken(amazingSubgroupToken, fakeconfig)).toBe('amazing-subgroup');
});

it("throws error if token's subgroup is not in config", () => {
const fakeconfig = {
opcode: {
subgroups: ['sad-subgroup', 'other-subgroup'],
},
} as any as AppConfig;
expect(() => getSubgroupFromToken(amazingSubgroupToken, fakeconfig)).toThrow();
});

it("returns 'default' if token has 'default' and config is not configured with subgroups", () => {
const defaultSubgroupToken = 'nrelop_great-study_default_000';
const fakeconfig = {
opcode: {},
} as any as AppConfig;
expect(getSubgroupFromToken(defaultSubgroupToken, fakeconfig)).toBe('default');
});

it("throws error if token's subgroup is not 'default' and config is not configured with subgroups", () => {
const invalidSubgroupToken = 'nrelop_great-study_imaginary-subgroup_000';
const fakeconfig = {
opcode: {},
} as any as AppConfig;
expect(() => getSubgroupFromToken(invalidSubgroupToken, fakeconfig)).toThrow();
});
});

describe('getTokenFromUrl', () => {
it('generates a token for an nrel.gov join page URL', () => {
const url = 'https://open-access-openpath.nrel.gov/join/';
expect(getTokenFromUrl(url)).toMatch(/^nrelop_open-access_default_[a-zA-Z0-9]{32}$/);
});

it('generates a token for an nrel.gov join page URL with a sub_group parameter', () => {
const url = 'https://open-access-openpath.nrel.gov/join/?sub_group=foo';
expect(getTokenFromUrl(url)).toMatch(/^nrelop_open-access_foo_[a-zA-Z0-9]{32}$/);
});

it('generates a token for an emission://join URL', () => {
const url = 'emission://join?study_config=great-study';
expect(getTokenFromUrl(url)).toMatch(/^nrelop_great-study_default_[a-zA-Z0-9]{32}$/);
});

it('extracts the token from a nrelopenpath://login_token URL', () => {
const url = 'nrelopenpath://login_token?token=nrelop_study_subgroup_random';
expect(getTokenFromUrl(url)).toBe('nrelop_study_subgroup_random');
});

it('throws error for any URL with a path other than "join" or "login_token"', () => {
expect(() => getTokenFromUrl('https://open-access-openpath.nrel.gov/invalid/')).toThrow();
expect(() => getTokenFromUrl('nrelopenpath://jion?study_config=open-access')).toThrow();
expect(() =>
getTokenFromUrl('emission://togin_loken?token=nrelop_open-access_000'),
).toThrow();
});
});
});
2 changes: 1 addition & 1 deletion www/js/config/opcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function generateOpcodeFromUrl(url: URL) {
* @example getTokenFromUrl('emission://login_token?token=nrelop_study_subgroup_random') => nrelop_study_subgroup_random
* @example getTokenFromUrl('nrelopenpath://login_token?token=nrelop_study_subgroup_random') => nrelop_study_subgroup_random
*/
function getTokenFromUrl(url: string) {
export function getTokenFromUrl(url: string) {
const parsedUrl = new URL(url);
const path = parsedUrl.pathname.replace(/\//g, '') || parsedUrl.hostname;
if (path == 'join') {
Expand Down

0 comments on commit ee85fb0

Please sign in to comment.