From a6932255b0af2eba4662ca2b24ea054260e7836e Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Mon, 23 Dec 2024 12:33:41 +0100 Subject: [PATCH] Some tests progress [skip ci] --- packages/app/components/auth/actions.test.ts | 10 +- packages/app/components/auth/actions.ts | 5 +- .../auth/deviceCode/DeviceCodeBody.tsx | 2 +- .../containers/AuthFlowRouteContents.test.tsx | 62 ----------- .../app/services/authFlow/AuthFlow.test.ts | 8 +- .../services/authFlow/CompleteState.test.ts | 105 +++++++++++------- .../InitOAuthAuthCodeFlowState.test.ts | 48 ++++---- 7 files changed, 106 insertions(+), 134 deletions(-) delete mode 100644 packages/app/containers/AuthFlowRouteContents.test.tsx diff --git a/packages/app/components/auth/actions.test.ts b/packages/app/components/auth/actions.test.ts index 77d2a22..02537f4 100644 --- a/packages/app/components/auth/actions.test.ts +++ b/packages/app/components/auth/actions.test.ts @@ -88,9 +88,13 @@ describe('components/auth/actions', () => { [ setOAuthRequest({ params: { - userCode: '', + clientId: '', + redirectUrl: '', + responseType: '', + state: '', + scope: '', }, - prompt: 'none', + prompt: ['none'], loginHint: undefined, }), ], @@ -117,7 +121,7 @@ describe('components/auth/actions', () => { return callThunk(oAuthComplete).then(() => { expect(request.post, 'to have a call satisfying', [ - '/api/oauth2/v1/complete?client_id=&redirect_uri=&response_type=&description=&scope=&prompt=none&login_hint=&state=', + '/api/oauth2/v1/complete?client_id=&redirect_uri=&response_type=&scope=&state=', {}, ]); }); diff --git a/packages/app/components/auth/actions.ts b/packages/app/components/auth/actions.ts index c84ce29..eb49d47 100644 --- a/packages/app/components/auth/actions.ts +++ b/packages/app/components/auth/actions.ts @@ -481,10 +481,11 @@ interface SetOauthAction extends ReduxAction { payload: OAuthState | null; } +type SetOauthRequestPayload = Pick; + // Input data is coming right from the query string, so the names // are the same, as used for initializing OAuth2 request -// TODO: filter out allowed properties -export function setOAuthRequest(payload: OAuthState | null): SetOauthAction { +export function setOAuthRequest(payload: SetOauthRequestPayload | null): SetOauthAction { return { type: 'set_oauth', payload, diff --git a/packages/app/components/auth/deviceCode/DeviceCodeBody.tsx b/packages/app/components/auth/deviceCode/DeviceCodeBody.tsx index e0e97d4..912f982 100644 --- a/packages/app/components/auth/deviceCode/DeviceCodeBody.tsx +++ b/packages/app/components/auth/deviceCode/DeviceCodeBody.tsx @@ -15,7 +15,7 @@ export default class DeviceCodeBody extends BaseAuthBody { <> {this.renderErrors()} - + {(nodes) => ( { - beforeEach(() => { - sinon.stub(authFlow, 'handleRequest'); - }); - - afterEach(() => { - (authFlow.handleRequest as any).restore(); - }); - - let componentProps: { [key: string]: any }; - - function Component(props: { [key: string]: any }) { - componentProps = props; - - return
; - } - - it('should render component if route allowed', () => { - const authRequest = { - path: '/path', - params: { foo: 1 }, - query: new URLSearchParams(), - }; - - const routerProps: any = { - location: { - pathname: authRequest.path, - search: '', - query: new URLSearchParams(), - }, - match: { - params: authRequest.params, - }, - }; - - (authFlow.handleRequest as any).callsArg(2); - - render(); - - const component = screen.getByTestId('test-component'); - - uxpect(authFlow.handleRequest, 'to have a call satisfying', [ - { - ...authRequest, - query: uxpect.it('to be a', URLSearchParams), - }, - uxpect.it('to be a function'), - uxpect.it('to be a function'), - ]); - - expect(component).toBeInTheDocument(); - uxpect(componentProps, 'to equal', routerProps); - }); -}); diff --git a/packages/app/services/authFlow/AuthFlow.test.ts b/packages/app/services/authFlow/AuthFlow.test.ts index 5b92da3..7a90627 100644 --- a/packages/app/services/authFlow/AuthFlow.test.ts +++ b/packages/app/services/authFlow/AuthFlow.test.ts @@ -14,6 +14,7 @@ import ResendActivationState from 'app/services/authFlow/ResendActivationState'; import LoginState from 'app/services/authFlow/LoginState'; import CompleteState from 'app/services/authFlow/CompleteState'; import ChooseAccountState from 'app/services/authFlow/ChooseAccountState'; +import FinishState from 'app/services/authFlow/FinishState'; import { Store } from 'redux'; describe('AuthFlow', () => { @@ -211,11 +212,6 @@ describe('AuthFlow', () => { expect(actual, 'to be', expected); }); - - it('should throw if no state', () => { - // @ts-ignore - expect(() => flow.setState(), 'to throw', 'State is required'); - }); }); describe('#run', () => { @@ -313,7 +309,7 @@ describe('AuthFlow', () => { '/accept-rules': LoginState, '/oauth/permissions': LoginState, '/oauth/choose-account': LoginState, - '/oauth/finish': LoginState, + '/oauth/finish': FinishState, '/oauth2/v1/foo': InitOAuthAuthCodeFlowState, '/oauth2/v1': InitOAuthAuthCodeFlowState, '/oauth2': InitOAuthAuthCodeFlowState, diff --git a/packages/app/services/authFlow/CompleteState.test.ts b/packages/app/services/authFlow/CompleteState.test.ts index 85d3576..f0d625e 100644 --- a/packages/app/services/authFlow/CompleteState.test.ts +++ b/packages/app/services/authFlow/CompleteState.test.ts @@ -137,7 +137,9 @@ describe('CompleteState', () => { auth: { credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, code: 'XXX', }, }, @@ -157,7 +159,9 @@ describe('CompleteState', () => { auth: { credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, acceptRequired: true, }, }, @@ -176,7 +180,9 @@ describe('CompleteState', () => { auth: { credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, prompt: ['consent'], }, }, @@ -202,7 +208,9 @@ describe('CompleteState', () => { credentials: {}, isSwitcherEnabled: true, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, prompt: [], }, }, @@ -227,7 +235,9 @@ describe('CompleteState', () => { isSwitcherEnabled: true, credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, prompt: [], }, }, @@ -251,13 +261,15 @@ describe('CompleteState', () => { isSwitcherEnabled: false, credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, prompt: [], }, }, }); - expectRun(mock, 'oAuthComplete', {}).returns({ then() {} }); + expectRun(mock, 'oAuthComplete', {}).returns(Promise.resolve()); state.enter(context); }); @@ -275,7 +287,9 @@ describe('CompleteState', () => { isSwitcherEnabled: true, credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, prompt: ['select_account'], }, }, @@ -299,13 +313,15 @@ describe('CompleteState', () => { isSwitcherEnabled: false, credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, prompt: ['select_account'], }, }, }); - expectRun(mock, 'oAuthComplete', {}).returns({ then() {} }); + expectRun(mock, 'oAuthComplete', {}).returns(Promise.resolve()); state.enter(context); }); @@ -322,15 +338,15 @@ describe('CompleteState', () => { auth: { credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, prompt: [], }, }, }); - expectRun(mock, 'oAuthComplete', sinon.match.object).returns({ - then() {}, - }); + expectRun(mock, 'oAuthComplete', sinon.match.object).returns(Promise.resolve()); state.enter(context); }); @@ -343,7 +359,9 @@ describe('CompleteState', () => { auth: { credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, prompt: [], }, }, @@ -370,7 +388,9 @@ describe('CompleteState', () => { auth: { credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, prompt: [], }, }, @@ -399,7 +419,9 @@ describe('CompleteState', () => { auth: { credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, prompt: [], }, }, @@ -447,7 +469,9 @@ describe('CompleteState', () => { auth: { credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, loginHint: account[field], prompt: [], }, @@ -485,7 +509,9 @@ describe('CompleteState', () => { auth: { credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, loginHint: account.id, prompt: [], }, @@ -518,16 +544,16 @@ describe('CompleteState', () => { auth: { credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, loginHint: account.id, prompt: [], }, }, }); - expectRun(mock, 'oAuthComplete', {}).returns({ - then: () => Promise.resolve(), - }); + expectRun(mock, 'oAuthComplete', {}).returns(Promise.resolve()); return expect(state.enter(context), 'to be fulfilled'); }); @@ -560,16 +586,15 @@ describe('CompleteState', () => { auth: { credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, prompt: [], }, }, }); - mock.expects('run') - .once() - .withExactArgs('oAuthComplete', sinon.match(expected)) - .returns({ then() {} }); + mock.expects('run').once().withExactArgs('oAuthComplete', sinon.match(expected)).returns(Promise.resolve()); state.enter(context); }); @@ -585,15 +610,15 @@ describe('CompleteState', () => { auth: { credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, prompt: [], }, }, }); - expectRun(mock, 'oAuthComplete', sinon.match(expected)).returns({ - then() {}, - }); + expectRun(mock, 'oAuthComplete', sinon.match(expected)).returns(Promise.resolve()); state.enter(context); }); @@ -611,16 +636,16 @@ describe('CompleteState', () => { auth: { credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, prompt: [], acceptRequired: true, }, }, }); - expectRun(mock, 'oAuthComplete', sinon.match(expected)).returns({ - then() {}, - }); + expectRun(mock, 'oAuthComplete', sinon.match(expected)).returns(Promise.resolve()); state.enter(context); }); @@ -638,16 +663,16 @@ describe('CompleteState', () => { auth: { credentials: {}, oauth: { - clientId: 'ely.by', + params: { + clientId: 'ely.by', + }, prompt: [], acceptRequired: true, }, }, }); - expectRun(mock, 'oAuthComplete', sinon.match(expected)).returns({ - then() {}, - }); + expectRun(mock, 'oAuthComplete', sinon.match(expected)).returns(Promise.resolve()); state.enter(context); }); diff --git a/packages/app/services/authFlow/InitOAuthAuthCodeFlowState.test.ts b/packages/app/services/authFlow/InitOAuthAuthCodeFlowState.test.ts index bf1ba78..f80f86a 100644 --- a/packages/app/services/authFlow/InitOAuthAuthCodeFlowState.test.ts +++ b/packages/app/services/authFlow/InitOAuthAuthCodeFlowState.test.ts @@ -44,14 +44,16 @@ describe('OAuthState', () => { mock, 'oAuthValidate', sinon.match({ - clientId: query.client_id, - redirectUrl: query.redirect_uri, - responseType: query.response_type, + params: { + clientId: query.client_id, + redirectUrl: query.redirect_uri, + responseType: query.response_type, + scope: query.scope, + state: query.state, + }, description: query.description, - scope: query.scope, prompt: query.prompt, loginHint: query.login_hint, - state: query.state, }), ).returns({ then() {} }); @@ -76,11 +78,13 @@ describe('OAuthState', () => { mock, 'oAuthValidate', sinon.match({ - clientId, - redirectUrl: query.redirect_uri, - responseType: query.response_type, - scope: query.scope, - state: query.state, + params: { + clientId, + redirectUrl: query.redirect_uri, + responseType: query.response_type, + scope: query.scope, + state: query.state, + }, }), ).returns({ then() {} }); @@ -106,11 +110,13 @@ describe('OAuthState', () => { mock, 'oAuthValidate', sinon.match({ - clientId: query.client_id, - redirectUrl: query.redirect_uri, - responseType: query.response_type, - scope: query.scope, - state: query.state, + params: { + clientId: query.client_id, + redirectUrl: query.redirect_uri, + responseType: query.response_type, + scope: query.scope, + state: query.state, + }, }), ).returns({ then() {} }); @@ -134,11 +140,13 @@ describe('OAuthState', () => { mock, 'oAuthValidate', sinon.match({ - clientId: query.client_id, - redirectUrl: query.redirect_uri, - responseType: query.response_type, - scope: 'scope1 scope2 scope3', - state: query.state, + params: { + clientId: query.client_id, + redirectUrl: query.redirect_uri, + responseType: query.response_type, + scope: 'scope1 scope2 scope3', + state: query.state, + }, }), ).returns({ then() {} });