Skip to content

Commit

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

Revert "feat: [DSTRBTN-524] [DSTRBTN-538] api additions"
  • Loading branch information
panvourtsis committed Oct 16, 2023
2 parents 37be294 + 1946024 commit f5d5b16
Show file tree
Hide file tree
Showing 16 changed files with 223 additions and 504 deletions.
7 changes: 2 additions & 5 deletions __mocks__/@auth0/auth0-spa-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ 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 @@ -16,8 +13,8 @@ export const fakeTokenData = {
iss: 'https://sso.orfium-staging.com/',
sub: 'auth0|62da8eaa586d8cd67d1746b6',
aud: ['orfium', 'https://orfium-staging.us.auth0.com/userinfo'],
iat: fakeIAT,
exp: fakeEXP,
iat: new Date().getTime(),
exp: new Date().getTime() + 30000,
azp: '1eWaFhQJpHS3xMDQRwrZJai3kIrF04eI',
scope: 'openid profile email offline_access',
org_id: 'org_WYZLEMyTm2xEbnbn',
Expand Down
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
"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: 30 additions & 32 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/organizations';
import useOrganization, { Organization } from '../store/useOrganization';
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,16 +43,10 @@ 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,
organizations,
selectedOrganization,
} = useAuthentication();
const { setOrganizations, setSelectedOrganization } = useOrganization();
const { isLoading, isAuthenticated, getAccessTokenSilently, logout, loginWithRedirect } =
useAuthentication();
const { organizations, setOrganizations, setSelectedOrganization, selectedOrganization } =
useOrganization();
const [systemLoading, setSystemLoading] = useState<boolean | undefined>(undefined);

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

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

if (organizations.length === 0) {
return (
<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>
</ThemeProvider>
);
}

if (!selectedOrganization) {
return (
<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>
</ThemeProvider>
);
}

Expand Down
63 changes: 19 additions & 44 deletions src/authentication/authentication.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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 @@ -15,41 +16,23 @@ const TestComp = () => {
};

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

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

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

afterEach(() => {});

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

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

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

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

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

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

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

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

0 comments on commit f5d5b16

Please sign in to comment.