diff options
Diffstat (limited to 'spec/frontend/jira_connect/subscriptions/components/sign_in_oauth_button_spec.js')
-rw-r--r-- | spec/frontend/jira_connect/subscriptions/components/sign_in_oauth_button_spec.js | 87 |
1 files changed, 59 insertions, 28 deletions
diff --git a/spec/frontend/jira_connect/subscriptions/components/sign_in_oauth_button_spec.js b/spec/frontend/jira_connect/subscriptions/components/sign_in_oauth_button_spec.js index ed0abaaf576..01317eb5dba 100644 --- a/spec/frontend/jira_connect/subscriptions/components/sign_in_oauth_button_spec.js +++ b/spec/frontend/jira_connect/subscriptions/components/sign_in_oauth_button_spec.js @@ -1,39 +1,41 @@ import { GlButton } from '@gitlab/ui'; import { shallowMount } from '@vue/test-utils'; -import MockAdapter from 'axios-mock-adapter'; import { nextTick } from 'vue'; + import SignInOauthButton from '~/jira_connect/subscriptions/components/sign_in_oauth_button.vue'; import { I18N_DEFAULT_SIGN_IN_BUTTON_TEXT, OAUTH_WINDOW_OPTIONS, } from '~/jira_connect/subscriptions/constants'; -import axios from '~/lib/utils/axios_utils'; import waitForPromises from 'helpers/wait_for_promises'; -import httpStatus from '~/lib/utils/http_status'; import AccessorUtilities from '~/lib/utils/accessor'; -import { getCurrentUser } from '~/rest_api'; +import { + getCurrentUser, + fetchOAuthApplicationId, + fetchOAuthToken, +} from '~/jira_connect/subscriptions/api'; import createStore from '~/jira_connect/subscriptions/store'; import { SET_ACCESS_TOKEN } from '~/jira_connect/subscriptions/store/mutation_types'; jest.mock('~/lib/utils/accessor'); jest.mock('~/jira_connect/subscriptions/utils'); jest.mock('~/jira_connect/subscriptions/api'); -jest.mock('~/rest_api'); jest.mock('~/jira_connect/subscriptions/pkce', () => ({ createCodeVerifier: jest.fn().mockReturnValue('mock-verifier'), createCodeChallenge: jest.fn().mockResolvedValue('mock-challenge'), })); -const mockOauthMetadata = { - oauth_authorize_url: 'https://gitlab.com/mockOauth', - oauth_token_url: 'https://gitlab.com/mockOauthToken', - state: 'good-state', -}; - describe('SignInOauthButton', () => { let wrapper; - let mockAxios; let store; + const mockOauthMetadata = { + oauth_authorize_url: 'https://gitlab.com/mockOauth', + oauth_token_path: 'https://gitlab.com/mockOauthToken', + oauth_token_payload: { + client_id: '543678901', + }, + state: 'good-state', + }; const createComponent = ({ slots, props } = {}) => { store = createStore(); @@ -50,13 +52,8 @@ describe('SignInOauthButton', () => { }); }; - beforeEach(() => { - mockAxios = new MockAdapter(axios); - }); - afterEach(() => { wrapper.destroy(); - mockAxios.restore(); }); const findButton = () => wrapper.findComponent(GlButton); @@ -69,6 +66,46 @@ describe('SignInOauthButton', () => { expect(findButton().props('category')).toBe('primary'); }); + describe('when `gitlabBasePath` is passed', () => { + const mockBasePath = 'https://gitlab.mycompany.com'; + + it('uses custom text for button', () => { + createComponent({ + props: { + gitlabBasePath: mockBasePath, + }, + }); + + expect(findButton().text()).toBe(`Sign in to ${mockBasePath}`); + }); + + describe('on click', () => { + const mockClientId = '798412381'; + + beforeEach(async () => { + fetchOAuthApplicationId.mockReturnValue({ data: { application_id: mockClientId } }); + jest.spyOn(window, 'open').mockReturnValue(); + createComponent({ + props: { + gitlabBasePath: mockBasePath, + }, + }); + + findButton().vm.$emit('click'); + + await nextTick(); + }); + + it('calls `window.open` with correct arguments', () => { + expect(window.open).toHaveBeenCalledWith( + `${mockBasePath}/mockOauth?code_challenge=mock-challenge&code_challenge_method=S256&client_id=${mockClientId}`, + I18N_DEFAULT_SIGN_IN_BUTTON_TEXT, + OAUTH_WINDOW_OPTIONS, + ); + }); + }); + }); + it.each` scenario | cryptoAvailable ${'when crypto API is available'} | ${true} @@ -96,7 +133,7 @@ describe('SignInOauthButton', () => { it('calls `window.open` with correct arguments', () => { expect(window.open).toHaveBeenCalledWith( - `${mockOauthMetadata.oauth_authorize_url}?code_challenge=mock-challenge&code_challenge_method=S256`, + `${mockOauthMetadata.oauth_authorize_url}?code_challenge=mock-challenge&code_challenge_method=S256&client_id=${mockOauthMetadata.oauth_token_payload.client_id}`, I18N_DEFAULT_SIGN_IN_BUTTON_TEXT, OAUTH_WINDOW_OPTIONS, ); @@ -151,11 +188,7 @@ describe('SignInOauthButton', () => { describe('when API requests succeed', () => { beforeEach(async () => { - jest.spyOn(axios, 'post'); - jest.spyOn(axios, 'get'); - mockAxios - .onPost(mockOauthMetadata.oauth_token_url) - .replyOnce(httpStatus.OK, { access_token: mockAccessToken }); + fetchOAuthToken.mockResolvedValue({ data: { access_token: mockAccessToken } }); getCurrentUser.mockResolvedValue({ data: mockUser }); window.dispatchEvent(new MessageEvent('message', mockEvent)); @@ -164,9 +197,10 @@ describe('SignInOauthButton', () => { }); it('executes POST request to Oauth token endpoint', () => { - expect(axios.post).toHaveBeenCalledWith(mockOauthMetadata.oauth_token_url, { + expect(fetchOAuthToken).toHaveBeenCalledWith(mockOauthMetadata.oauth_token_path, { code: '1234', code_verifier: 'mock-verifier', + client_id: mockOauthMetadata.oauth_token_payload.client_id, }); }); @@ -185,10 +219,7 @@ describe('SignInOauthButton', () => { describe('when API requests fail', () => { beforeEach(async () => { - jest.spyOn(axios, 'post'); - mockAxios - .onPost(mockOauthMetadata.oauth_token_url) - .replyOnce(httpStatus.INTERNAL_SERVER_ERROR); + fetchOAuthToken.mockRejectedValue(); window.dispatchEvent(new MessageEvent('message', mockEvent)); |