Skip to content

Commit

Permalink
fixup! (HP-2023) Get download data action
Browse files Browse the repository at this point in the history
  • Loading branch information
NikoHelle committed Oct 6, 2023
1 parent d6982ea commit 35283c2
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 72 deletions.
131 changes: 117 additions & 14 deletions src/gdprApi/actions/__tests__/getDownloadData.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,133 @@
import fetchMock from 'jest-fetch-mock';
import to from 'await-to-js';
import { waitFor } from '@testing-library/react';

import { getGdprQueryScopesAction } from '../getGdprScopes';
import {
getDownloadDataAction,
getDownloadDataResult,
} from '../getDownloadData';
import { createActionQueueRunner } from '../../../common/actionQueue/actionQueueRunner';
import { Action, getOption } from '../../../common/actionQueue/actionQueue';
import {
Action,
QueueController,
} from '../../../common/actionQueue/actionQueue';
tunnistamoAuthCodeParserAction,
keycloakAuthCodeParserAction,
} from '../authCodeParser';
import { getMockCalls } from '../../../common/test/jestMockHelper';

describe('getDownloadData.ts', () => {
const queryTracker = jest.fn();
const successfulResponse = { variable1: 'variable1' };
const keycloakAuthCode = 'keycloak-auth-code';
const tunnistamoAuthCode = 'tunnistamo-auth-code';
const initTests = ({
noKeycloadAuthCode,
returnNoData,
returnError,
}: {
noKeycloadAuthCode?: boolean;
noTunnistamoAuthCode?: boolean;
returnNoData?: boolean;
returnError?: boolean;
} = {}) => {
fetchMock.mockIf(/.*\/graphql\/.*$/, async (req: Request) => {
const payload = await req.json();
queryTracker(payload);

describe('getGdprScopes.ts', () => {
const initTests = (returnError = true) => {
const queue = [getGdprQueryScopesAction];
const response = {
data: {
downloadMyProfile: returnNoData !== true ? successfulResponse : null,
},
};
if (returnError === true) {
return Promise.reject({
body: JSON.stringify({
message: 'Error',
}),
});
}
return Promise.resolve({ body: JSON.stringify(response) });
});
const queue = [
tunnistamoAuthCodeParserAction,
keycloakAuthCodeParserAction,
getDownloadDataAction,
];
const runner = createActionQueueRunner(queue);
runner.updateActionAndQueue(tunnistamoAuthCodeParserAction.type, {
result: tunnistamoAuthCode,
complete: true,
});

if (!noKeycloadAuthCode) {
runner.updateActionAndQueue(keycloakAuthCodeParserAction.type, {
result: keycloakAuthCode,
complete: true,
});
}
return {
runner,
action: runner.getByType(getGdprQueryScopesAction.type) as Action,
getAction: () => runner.getByType(getDownloadDataAction.type) as Action,
getPayloadVariables: () => {
const calls = getMockCalls(queryTracker);
const lastCallArgs = calls[calls.length - 1];
const source = lastCallArgs[0];
if (!source) {
return {};
}
return source.variables;
},
};
};

afterEach(() => {
fetchMock.resetMocks();
jest.restoreAllMocks();
jest.resetAllMocks();
});
it('Fetches serviceConnections', async () => {
const { runner, action } = initTests(false);
const [error, result] = await to(
action.executor({} as Action, {} as QueueController)
);
expect(result).toHaveLength(2);
describe('The executor', () => {
it('Fetches download data', async () => {
const { runner, getAction } = initTests();
const [, result] = await to(getAction().executor(getAction(), runner));
expect(result).toMatchObject(successfulResponse);
});
it('Sends both auth codes in query variables when both are set', async () => {
const { runner, getAction, getPayloadVariables } = initTests();
await to(getAction().executor(getAction(), runner));
expect(getPayloadVariables()).toMatchObject({
authorizationCode: tunnistamoAuthCode,
authorizationCodeKeycloak: keycloakAuthCode,
});
});
it('Sends only tunnistamo auth code, when keycload code is not set', async () => {
const { runner, getAction, getPayloadVariables } = initTests({
noKeycloadAuthCode: true,
});
await to(getAction().executor(getAction(), runner));
expect(getPayloadVariables()).toMatchObject({
authorizationCode: tunnistamoAuthCode,
});
});
it('Empty data rejects the promise', async () => {
const { runner, getAction } = initTests({ returnNoData: true });
const [error] = await to(getAction().executor(getAction(), runner));
expect(error).toBeDefined();
});
it('Errors are handled', async () => {
const { runner, getAction } = initTests({ returnError: true });
const [error] = await to(getAction().executor(getAction(), runner));
expect(error).toBeDefined();
});
it('Result should not be stored to sessionStorage', async () => {
const { getAction } = initTests();
expect(getOption(getAction(), 'noStorage')).toBeTruthy();
});
it('getDownloadDataResult() helper returns the result', async () => {
const { runner } = initTests();
runner.resume(getDownloadDataAction.type);
await waitFor(() => {
expect(runner.isFinished()).toBeTruthy();
});
expect(getDownloadDataResult(runner)).toMatchObject(successfulResponse);
});
});
});
59 changes: 1 addition & 58 deletions src/gdprApi/actions/getDownloadData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const getDownloadDataExecutor: ActionExecutor = async (
}
if (!result || !result.data) {
return Promise.reject(
new Error("'No results in GDPR_SERVICE_CONNECTIONS query")
new Error("'No results in DOWNLOAD_MY_PROFILE query")
);
}
return Promise.resolve(result.data.downloadMyProfile);
Expand All @@ -73,60 +73,3 @@ export const getDownloadDataAction: ActionProps = {
idleWhenActive: true,
},
};

/*
export const downloadProfileDataExecutor: ActionExecutor = async (
action,
queueController
) =>
new Promise((resolve, reject) => {
(async () => {
const authorizationCode = getTunnistamoAuthorizationCode(queueController);
const authorizationCodeKeycloak = getkeycloakAuthorizationCode(
queueController
);
const variables: Mutable<DownloadMyProfileQueryVariables> = {
authorizationCode,
};
if (typeof authorizationCodeKeycloak === 'string') {
variables.authorizationCodeKeycloak = authorizationCodeKeycloak;
}
console.log('variables', variables);
//authorizationCodeKeycloak
const [error, result] = await to(
graphqlClient.query<DownloadMyProfileQuery>({
query: DOWNLOAD_MY_PROFILE,
fetchPolicy: 'network-only',
variables: {
...variables,
},
})
);
if (error) {
return reject(error);
}
if (!result || !result.data) {
return reject(
new Error("'No results in GDPR_SERVICE_CONNECTIONS query")
);
}
return resolve(result.data.downloadMyProfile);
})();
});
const downloadProfileDataActionType = 'downloadProfileData';
export const downloadProfileDataAction: ActionProps = {
type: downloadProfileDataActionType,
executor: downloadProfileDataExecutor,
options: {
noStorage: true,
},
};
*/

0 comments on commit 35283c2

Please sign in to comment.