debian-mirror-gitlab/spec/frontend/jira_connect/subscriptions/api_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

193 lines
5.3 KiB
JavaScript
Raw Normal View History

2021-03-08 18:12:59 +05:30
import MockAdapter from 'axios-mock-adapter';
2022-10-11 01:57:18 +05:30
import {
axiosInstance,
addSubscription,
removeSubscription,
fetchGroups,
getCurrentUser,
addJiraConnectSubscription,
updateInstallation,
} from '~/jira_connect/subscriptions/api';
2021-10-27 15:23:28 +05:30
import { getJwt } from '~/jira_connect/subscriptions/utils';
2023-03-17 16:20:25 +05:30
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
2021-03-08 18:12:59 +05:30
2021-10-27 15:23:28 +05:30
jest.mock('~/jira_connect/subscriptions/utils', () => ({
2021-04-29 21:17:54 +05:30
getJwt: jest.fn().mockResolvedValue('jwt'),
}));
2021-03-08 18:12:59 +05:30
describe('JiraConnect API', () => {
2022-10-11 01:57:18 +05:30
let axiosMock;
2021-03-08 18:12:59 +05:30
let response;
const mockAddPath = 'addPath';
const mockRemovePath = 'removePath';
const mockNamespace = 'namespace';
const mockJwt = 'jwt';
2022-10-11 01:57:18 +05:30
const mockAccessToken = 'accessToken';
2021-03-08 18:12:59 +05:30
const mockResponse = { success: true };
beforeEach(() => {
2022-10-11 01:57:18 +05:30
axiosMock = new MockAdapter(axiosInstance);
window.gon = { api_version: 'v4' };
2021-03-08 18:12:59 +05:30
});
afterEach(() => {
2022-10-11 01:57:18 +05:30
axiosMock.restore();
2021-03-08 18:12:59 +05:30
response = null;
});
describe('addSubscription', () => {
const makeRequest = () => addSubscription(mockAddPath, mockNamespace);
it('returns success response', async () => {
2022-10-11 01:57:18 +05:30
jest.spyOn(axiosInstance, 'post');
axiosMock
2021-03-08 18:12:59 +05:30
.onPost(mockAddPath, {
jwt: mockJwt,
namespace_path: mockNamespace,
})
2023-03-17 16:20:25 +05:30
.replyOnce(HTTP_STATUS_OK, mockResponse);
2021-03-08 18:12:59 +05:30
response = await makeRequest();
2021-04-29 21:17:54 +05:30
expect(getJwt).toHaveBeenCalled();
2022-10-11 01:57:18 +05:30
expect(axiosInstance.post).toHaveBeenCalledWith(mockAddPath, {
2021-03-08 18:12:59 +05:30
jwt: mockJwt,
namespace_path: mockNamespace,
});
expect(response.data).toEqual(mockResponse);
});
});
describe('removeSubscription', () => {
const makeRequest = () => removeSubscription(mockRemovePath);
it('returns success response', async () => {
2022-10-11 01:57:18 +05:30
jest.spyOn(axiosInstance, 'delete');
2023-03-17 16:20:25 +05:30
axiosMock.onDelete(mockRemovePath).replyOnce(HTTP_STATUS_OK, mockResponse);
2021-03-08 18:12:59 +05:30
response = await makeRequest();
2021-04-29 21:17:54 +05:30
expect(getJwt).toHaveBeenCalled();
2022-10-11 01:57:18 +05:30
expect(axiosInstance.delete).toHaveBeenCalledWith(mockRemovePath, {
2021-03-08 18:12:59 +05:30
params: {
jwt: mockJwt,
},
});
expect(response.data).toEqual(mockResponse);
});
});
describe('fetchGroups', () => {
const mockGroupsPath = 'groupsPath';
const mockPage = 1;
const mockPerPage = 10;
const makeRequest = () =>
fetchGroups(mockGroupsPath, {
page: mockPage,
perPage: mockPerPage,
});
it('returns success response', async () => {
2022-10-11 01:57:18 +05:30
jest.spyOn(axiosInstance, 'get');
axiosMock
2021-03-08 18:12:59 +05:30
.onGet(mockGroupsPath, {
page: mockPage,
per_page: mockPerPage,
})
2023-03-17 16:20:25 +05:30
.replyOnce(HTTP_STATUS_OK, mockResponse);
2021-03-08 18:12:59 +05:30
response = await makeRequest();
2022-10-11 01:57:18 +05:30
expect(axiosInstance.get).toHaveBeenCalledWith(mockGroupsPath, {
2023-04-23 21:23:45 +05:30
headers: {},
2021-03-08 18:12:59 +05:30
params: {
page: mockPage,
per_page: mockPerPage,
2023-04-23 21:23:45 +05:30
search: undefined,
2021-03-08 18:12:59 +05:30
},
});
expect(response.data).toEqual(mockResponse);
});
});
2022-10-11 01:57:18 +05:30
describe('getCurrentUser', () => {
const makeRequest = () => getCurrentUser();
it('returns success response', async () => {
const expectedUrl = '/api/v4/user';
jest.spyOn(axiosInstance, 'get');
2023-03-17 16:20:25 +05:30
axiosMock.onGet(expectedUrl).replyOnce(HTTP_STATUS_OK, mockResponse);
2022-10-11 01:57:18 +05:30
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');
2023-03-17 16:20:25 +05:30
axiosMock.onPost(expectedUrl).replyOnce(HTTP_STATUS_OK, mockResponse);
2022-10-11 01:57:18 +05:30
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,
},
})
2023-03-17 16:20:25 +05:30
.replyOnce(HTTP_STATUS_OK, mockResponse);
2022-10-11 01:57:18 +05:30
response = await makeRequest();
expect(getJwt).toHaveBeenCalled();
expect(axiosInstance.put).toHaveBeenCalledWith(expectedUrl, {
jwt: mockJwt,
installation: {
instance_url: expectedInstanceUrl,
},
});
expect(response.data).toEqual(mockResponse);
},
);
});
2021-03-08 18:12:59 +05:30
});