Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/frontend/jira_connect/subscriptions/api_spec.js')
-rw-r--r--spec/frontend/jira_connect/subscriptions/api_spec.js118
1 files changed, 104 insertions, 14 deletions
diff --git a/spec/frontend/jira_connect/subscriptions/api_spec.js b/spec/frontend/jira_connect/subscriptions/api_spec.js
index 57b11bdbc27..cf496d5836a 100644
--- a/spec/frontend/jira_connect/subscriptions/api_spec.js
+++ b/spec/frontend/jira_connect/subscriptions/api_spec.js
@@ -1,7 +1,14 @@
import MockAdapter from 'axios-mock-adapter';
-import { addSubscription, removeSubscription, fetchGroups } from '~/jira_connect/subscriptions/api';
+import {
+ axiosInstance,
+ addSubscription,
+ removeSubscription,
+ fetchGroups,
+ getCurrentUser,
+ addJiraConnectSubscription,
+ updateInstallation,
+} from '~/jira_connect/subscriptions/api';
import { getJwt } from '~/jira_connect/subscriptions/utils';
-import axios from '~/lib/utils/axios_utils';
import httpStatus from '~/lib/utils/http_status';
jest.mock('~/jira_connect/subscriptions/utils', () => ({
@@ -9,21 +16,26 @@ jest.mock('~/jira_connect/subscriptions/utils', () => ({
}));
describe('JiraConnect API', () => {
- let mock;
+ let axiosMock;
+ let originalGon;
let response;
const mockAddPath = 'addPath';
const mockRemovePath = 'removePath';
const mockNamespace = 'namespace';
const mockJwt = 'jwt';
+ const mockAccessToken = 'accessToken';
const mockResponse = { success: true };
beforeEach(() => {
- mock = new MockAdapter(axios);
+ axiosMock = new MockAdapter(axiosInstance);
+ originalGon = window.gon;
+ window.gon = { api_version: 'v4' };
});
afterEach(() => {
- mock.restore();
+ axiosMock.restore();
+ window.gon = originalGon;
response = null;
});
@@ -31,8 +43,8 @@ describe('JiraConnect API', () => {
const makeRequest = () => addSubscription(mockAddPath, mockNamespace);
it('returns success response', async () => {
- jest.spyOn(axios, 'post');
- mock
+ jest.spyOn(axiosInstance, 'post');
+ axiosMock
.onPost(mockAddPath, {
jwt: mockJwt,
namespace_path: mockNamespace,
@@ -42,7 +54,7 @@ describe('JiraConnect API', () => {
response = await makeRequest();
expect(getJwt).toHaveBeenCalled();
- expect(axios.post).toHaveBeenCalledWith(mockAddPath, {
+ expect(axiosInstance.post).toHaveBeenCalledWith(mockAddPath, {
jwt: mockJwt,
namespace_path: mockNamespace,
});
@@ -54,13 +66,13 @@ describe('JiraConnect API', () => {
const makeRequest = () => removeSubscription(mockRemovePath);
it('returns success response', async () => {
- jest.spyOn(axios, 'delete');
- mock.onDelete(mockRemovePath).replyOnce(httpStatus.OK, mockResponse);
+ jest.spyOn(axiosInstance, 'delete');
+ axiosMock.onDelete(mockRemovePath).replyOnce(httpStatus.OK, mockResponse);
response = await makeRequest();
expect(getJwt).toHaveBeenCalled();
- expect(axios.delete).toHaveBeenCalledWith(mockRemovePath, {
+ expect(axiosInstance.delete).toHaveBeenCalledWith(mockRemovePath, {
params: {
jwt: mockJwt,
},
@@ -81,8 +93,8 @@ describe('JiraConnect API', () => {
});
it('returns success response', async () => {
- jest.spyOn(axios, 'get');
- mock
+ jest.spyOn(axiosInstance, 'get');
+ axiosMock
.onGet(mockGroupsPath, {
page: mockPage,
per_page: mockPerPage,
@@ -91,7 +103,7 @@ describe('JiraConnect API', () => {
response = await makeRequest();
- expect(axios.get).toHaveBeenCalledWith(mockGroupsPath, {
+ expect(axiosInstance.get).toHaveBeenCalledWith(mockGroupsPath, {
params: {
page: mockPage,
per_page: mockPerPage,
@@ -100,4 +112,82 @@ describe('JiraConnect API', () => {
expect(response.data).toEqual(mockResponse);
});
});
+
+ describe('getCurrentUser', () => {
+ const makeRequest = () => getCurrentUser();
+
+ it('returns success response', async () => {
+ const expectedUrl = '/api/v4/user';
+
+ jest.spyOn(axiosInstance, 'get');
+
+ axiosMock.onGet(expectedUrl).replyOnce(httpStatus.OK, mockResponse);
+
+ response = await makeRequest();
+
+ expect(axiosInstance.get).toHaveBeenCalledWith(expectedUrl, {});
+ expect(response.data).toEqual(mockResponse);
+ });
+ });
+
+ describe('addJiraConnectSubscription', () => {
+ const makeRequest = () =>
+ addJiraConnectSubscription(mockNamespace, { jwt: mockJwt, accessToken: mockAccessToken });
+
+ it('returns success response', async () => {
+ const expectedUrl = '/api/v4/integrations/jira_connect/subscriptions';
+
+ jest.spyOn(axiosInstance, 'post');
+
+ axiosMock.onPost(expectedUrl).replyOnce(httpStatus.OK, mockResponse);
+
+ response = await makeRequest();
+
+ expect(axiosInstance.post).toHaveBeenCalledWith(
+ expectedUrl,
+ {
+ jwt: mockJwt,
+ namespace_path: mockNamespace,
+ },
+ { headers: { Authorization: `Bearer ${mockAccessToken}` } },
+ );
+ 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);
+ },
+ );
+ });
});