diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-14 09:12:58 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-14 09:12:58 +0000 |
commit | 3cb798d80b6b5235b5f5febaaacef410e75c2963 (patch) | |
tree | 9568ccb5e3919feb3f61f1731d6d75ff581b56d1 /spec/frontend/jira_connect | |
parent | d898994a1465412b024091e22ff59fd863e5ac2f (diff) | |
download | gitlab-ce-3cb798d80b6b5235b5f5febaaacef410e75c2963.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/frontend/jira_connect')
3 files changed, 101 insertions, 35 deletions
diff --git a/spec/frontend/jira_connect/subscriptions/api_spec.js b/spec/frontend/jira_connect/subscriptions/api_spec.js index 76a0b8fa82c..cf496d5836a 100644 --- a/spec/frontend/jira_connect/subscriptions/api_spec.js +++ b/spec/frontend/jira_connect/subscriptions/api_spec.js @@ -6,6 +6,7 @@ import { fetchGroups, getCurrentUser, addJiraConnectSubscription, + updateInstallation, } from '~/jira_connect/subscriptions/api'; import { getJwt } from '~/jira_connect/subscriptions/utils'; import httpStatus from '~/lib/utils/http_status'; @@ -153,4 +154,40 @@ describe('JiraConnect API', () => { expect(response.data).toEqual(mockResponse); }); }); + + describe('updateInstallation', () => { + const expectedUrl = '/-/jira_connect/installations'; + + it.each` + instanceUrl | expectedInstanceUrl + ${'https://gitlab.com'} | ${null} + ${'https://gitlab.mycompany.com'} | ${'https://gitlab.mycompany.com'} + `( + 'when instanceUrl is $instanceUrl, it passes `instance_url` as $expectedInstanceUrl', + async ({ instanceUrl, expectedInstanceUrl }) => { + const makeRequest = () => updateInstallation(instanceUrl); + + jest.spyOn(axiosInstance, 'put'); + axiosMock + .onPut(expectedUrl, { + jwt: mockJwt, + installation: { + instance_url: expectedInstanceUrl, + }, + }) + .replyOnce(httpStatus.OK, mockResponse); + + response = await makeRequest(); + + expect(getJwt).toHaveBeenCalled(); + expect(axiosInstance.put).toHaveBeenCalledWith(expectedUrl, { + jwt: mockJwt, + installation: { + instance_url: expectedInstanceUrl, + }, + }); + expect(response.data).toEqual(mockResponse); + }, + ); + }); }); 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 951adb006f8..653143a6243 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,6 +1,5 @@ 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'; @@ -8,11 +7,13 @@ 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 '~/jira_connect/subscriptions/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'; @@ -24,16 +25,17 @@ jest.mock('~/jira_connect/subscriptions/pkce', () => ({ 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_url: '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); @@ -70,7 +67,7 @@ describe('SignInOauthButton', () => { }); describe('when `gitlabBasePath` is passed', () => { - const mockBasePath = 'gitlab.mycompany.com'; + const mockBasePath = 'https://gitlab.mycompany.com'; it('uses custom text for button', () => { createComponent({ @@ -81,6 +78,32 @@ describe('SignInOauthButton', () => { 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` @@ -110,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, ); @@ -165,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)); @@ -178,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_url, { code: '1234', code_verifier: 'mock-verifier', + client_id: mockOauthMetadata.oauth_token_payload.client_id, }); }); @@ -199,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)); diff --git a/spec/frontend/jira_connect/subscriptions/pages/sign_in/sign_in_gitlab_multiversion/index_spec.js b/spec/frontend/jira_connect/subscriptions/pages/sign_in/sign_in_gitlab_multiversion/index_spec.js index 48f57194363..6b85ca78914 100644 --- a/spec/frontend/jira_connect/subscriptions/pages/sign_in/sign_in_gitlab_multiversion/index_spec.js +++ b/spec/frontend/jira_connect/subscriptions/pages/sign_in/sign_in_gitlab_multiversion/index_spec.js @@ -5,6 +5,17 @@ import SignInGitlabMultiversion from '~/jira_connect/subscriptions/pages/sign_in import VersionSelectForm from '~/jira_connect/subscriptions/pages/sign_in/sign_in_gitlab_multiversion/version_select_form.vue'; import SignInOauthButton from '~/jira_connect/subscriptions/components/sign_in_oauth_button.vue'; +import { updateInstallation } from '~/jira_connect/subscriptions/api'; +import { reloadPage, persistBaseUrl, retrieveBaseUrl } from '~/jira_connect/subscriptions/utils'; + +jest.mock('~/jira_connect/subscriptions/api', () => { + return { + updateInstallation: jest.fn(), + setApiBaseURL: jest.fn(), + }; +}); +jest.mock('~/jira_connect/subscriptions/utils'); + describe('SignInGitlabMultiversion', () => { let wrapper; @@ -31,25 +42,26 @@ describe('SignInGitlabMultiversion', () => { }); describe('when form emits "submit" event', () => { - it('hides the version select form and shows the sign in button', async () => { + it('updates the backend, then saves the baseUrl and reloads', async () => { + updateInstallation.mockResolvedValue({}); + createComponent(); findVersionSelectForm().vm.$emit('submit', mockBasePath); await nextTick(); - expect(findVersionSelectForm().exists()).toBe(false); - expect(findSignInOauthButton().exists()).toBe(true); + expect(updateInstallation).toHaveBeenCalled(); + expect(persistBaseUrl).toHaveBeenCalledWith(mockBasePath); + expect(reloadPage).toHaveBeenCalled(); }); }); }); }); describe('when version is selected', () => { - beforeEach(async () => { + beforeEach(() => { + retrieveBaseUrl.mockReturnValue(mockBasePath); createComponent(); - - findVersionSelectForm().vm.$emit('submit', mockBasePath); - await nextTick(); }); describe('sign in button', () => { |