Skip to content

Commit

Permalink
Merge pull request #73 from Orfium/feat/DSTRBTN-524_DSTRBTN-538_api_a…
Browse files Browse the repository at this point in the history
…dditions

feat: [DSTRBTN-524] [DSTRBTN-538] api additions
  • Loading branch information
mkarajohn authored Oct 9, 2023
2 parents 793008c + c72ca9e commit 37be294
Show file tree
Hide file tree
Showing 16 changed files with 504 additions and 223 deletions.
7 changes: 5 additions & 2 deletions __mocks__/@auth0/auth0-spa-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import sign from 'jwt-encode';
export const FAKE_TOKEN =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';

const fakeIAT = new Date().getTime();
const fakeEXP = fakeIAT + 30000;

/*
* Pre-made fake token data that sets new creation and expiration time for that token
* Creation time is `now` and expiration time is `now + 30seconds`
Expand All @@ -13,8 +16,8 @@ export const fakeTokenData = {
iss: 'https://sso.orfium-staging.com/',
sub: 'auth0|62da8eaa586d8cd67d1746b6',
aud: ['orfium', 'https://orfium-staging.us.auth0.com/userinfo'],
iat: new Date().getTime(),
exp: new Date().getTime() + 30000,
iat: fakeIAT,
exp: fakeEXP,
azp: '1eWaFhQJpHS3xMDQRwrZJai3kIrF04eI',
scope: 'openid profile email offline_access',
org_id: 'org_WYZLEMyTm2xEbnbn',
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
"exports": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js",
"default": "./dist/cjs/index.js"
},
"files": [
"dist"
],
Expand Down
62 changes: 32 additions & 30 deletions src/authentication/Authentication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, { useEffect, useState } from 'react';
import { ErrorBoundary } from 'react-error-boundary';

import { orfiumIdBaseInstance } from '../request';
import useOrganization, { Organization } from '../store/useOrganization';
import useOrganization, { Organization } from '../store/organizations';
import { Box, LoadingContent, Wrapper } from './Authentication.style';
import ErrorFallback from './components/ErrorFallback/ErrorFallback';
import { TopBar, TopBarProps } from './components/TopBar/TopBar';
Expand Down Expand Up @@ -43,10 +43,16 @@ Authentication.TopBar = TopBar;
* This is the main component that is wrapped on the authentication.
*/
const AuthenticationWrapper: React.FunctionComponent = ({ children }) => {
const { isLoading, isAuthenticated, getAccessTokenSilently, logout, loginWithRedirect } =
useAuthentication();
const { organizations, setOrganizations, setSelectedOrganization, selectedOrganization } =
useOrganization();
const {
isLoading,
isAuthenticated,
getAccessTokenSilently,
logout,
loginWithRedirect,
organizations,
selectedOrganization,
} = useAuthentication();
const { setOrganizations, setSelectedOrganization } = useOrganization();
const [systemLoading, setSystemLoading] = useState<boolean | undefined>(undefined);

/**
Expand All @@ -72,7 +78,7 @@ const AuthenticationWrapper: React.FunctionComponent = ({ children }) => {

setOrganizations(data);
if (!selectedOrganization?.org_id && data?.length > 0) {
setSelectedOrganization(data[0]);
setSelectedOrganization(data[0].org_id);
}
// if token doesn't have an organization and the user has available organizations
// set continue and set one
Expand Down Expand Up @@ -108,35 +114,31 @@ const AuthenticationWrapper: React.FunctionComponent = ({ children }) => {

if (organizations.length === 0) {
return (
<ThemeProvider>
<Wrapper data-testid={'orfium-no-organizations'}>
<h2>There are no organizations to pick.</h2>
<div>Go back or contact your administrator for more information.</div>
<Box>
<div>OR</div>
</Box>
<Button onClick={logout} type={'primary'}>
Logout
</Button>
</Wrapper>
</ThemeProvider>
<Wrapper data-testid={'orfium-no-organizations'}>
<h2>There are no organizations to pick.</h2>
<div>Go back or contact your administrator for more information.</div>
<Box>
<div>OR</div>
</Box>
<Button onClick={logout} type={'primary'}>
Logout
</Button>
</Wrapper>
);
}

if (!selectedOrganization) {
return (
<ThemeProvider>
<Wrapper data-testid={'orfium-no-org-id'}>
<h2>You dont have access to this Product.</h2>
<div>Go back or contact your administrator for more information.</div>
<Box>
<div>OR</div>
</Box>
<Button onClick={logout} type={'primary'}>
Logout
</Button>
</Wrapper>
</ThemeProvider>
<Wrapper data-testid={'orfium-no-org-id'}>
<h2>You dont have access to this Product.</h2>
<div>Go back or contact your administrator for more information.</div>
<Box>
<div>OR</div>
</Box>
<Button onClick={logout} type={'primary'}>
Logout
</Button>
</Wrapper>
);
}

Expand Down
63 changes: 44 additions & 19 deletions src/authentication/authentication.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { cleanup, render, waitFor } from '@testing-library/react';
import React from 'react';

import { QueryClient, QueryClientProvider } from 'react-query';
import {
getNewFakeToken,
getTokenSilently,
isAuthenticated,
loginWithRedirect,
// @ts-ignore
} from '../../__mocks__/@auth0/auth0-spa-js';
import { orfiumIdBaseInstance } from '../request';
import MockRequest from '../request/mock';
Expand All @@ -16,23 +15,41 @@ const TestComp = () => {
};

describe('Authentication: ', () => {
let mock: MockRequest;
const apiInstance = orfiumIdBaseInstance.instance;
const mock: MockRequest = new MockRequest(apiInstance);
let queryClient: QueryClient;

beforeEach(() => {
mock = new MockRequest(apiInstance);
queryClient = new QueryClient();
mock.onGet('/products/').reply(200, [
{
name: 'string',
organization_usage: 'string',
client_metadata: {
product_code: 'string',
},
logo_url: 'string',
login_url: 'string',
},
]);
});

afterEach(() => {
// clear all mocks and mocked values
jest.clearAllMocks();
cleanup();
mock.reset();
});

afterEach(() => {});

it('renders without crashing', () => {
render(
<AuthenticationProvider>
<TestComp />
</AuthenticationProvider>
<QueryClientProvider client={queryClient}>
<AuthenticationProvider>
<TestComp />
</AuthenticationProvider>
</QueryClientProvider>
);
});

Expand All @@ -42,9 +59,11 @@ describe('Authentication: ', () => {
mock.onGet('/memberships/').reply(200, [{ org_id: 'a' }]);

const { findByTestId } = render(
<AuthenticationProvider>
<TestComp />
</AuthenticationProvider>
<QueryClientProvider client={queryClient}>
<AuthenticationProvider>
<TestComp />
</AuthenticationProvider>
</QueryClientProvider>
);

expect(await findByTestId('orfium-auth-loading')).toBeTruthy();
Expand All @@ -58,9 +77,11 @@ describe('Authentication: ', () => {
mock.onGet('/memberships/').replyOnce(200, []);

render(
<AuthenticationProvider>
<TestComp />
</AuthenticationProvider>
<QueryClientProvider client={queryClient}>
<AuthenticationProvider>
<TestComp />
</AuthenticationProvider>
</QueryClientProvider>
);

await waitFor(() => expect(loginWithRedirect).toHaveBeenCalled());
Expand All @@ -70,9 +91,11 @@ describe('Authentication: ', () => {
getTokenSilently.mockResolvedValue(getNewFakeToken());
isAuthenticated.mockResolvedValue(true);
const { findByTestId } = render(
<AuthenticationProvider>
<TestComp />
</AuthenticationProvider>
<QueryClientProvider client={queryClient}>
<AuthenticationProvider>
<TestComp />
</AuthenticationProvider>
</QueryClientProvider>
);
expect(await findByTestId('orfium-auth-loading')).toBeTruthy();
});
Expand All @@ -83,9 +106,11 @@ describe('Authentication: ', () => {
mock.onGet('/memberships/').replyOnce(200, []);

const { findByTestId } = render(
<AuthenticationProvider>
<TestComp />
</AuthenticationProvider>
<QueryClientProvider client={queryClient}>
<AuthenticationProvider>
<TestComp />
</AuthenticationProvider>
</QueryClientProvider>
);

expect(await findByTestId('orfium-auth-loading')).toBeTruthy();
Expand Down
Loading

0 comments on commit 37be294

Please sign in to comment.