debian-mirror-gitlab/spec/frontend/api/groups_api_spec.js

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

98 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-08-27 11:52:29 +05:30
import MockAdapter from 'axios-mock-adapter';
2023-01-13 00:05:48 +05:30
import getGroupTransferLocationsResponse from 'test_fixtures/api/groups/transfer_locations.json';
2023-03-17 16:20:25 +05:30
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
2022-08-27 11:52:29 +05:30
import axios from '~/lib/utils/axios_utils';
2023-01-13 00:05:48 +05:30
import { DEFAULT_PER_PAGE } from '~/api';
2023-04-23 21:23:45 +05:30
import { updateGroup, getGroupTransferLocations, getGroupMembers } from '~/api/groups_api';
2022-08-27 11:52:29 +05:30
const mockApiVersion = 'v4';
const mockUrlRoot = '/gitlab';
2023-01-13 00:05:48 +05:30
const mockGroupId = '99';
2022-08-27 11:52:29 +05:30
describe('GroupsApi', () => {
let originalGon;
let mock;
const dummyGon = {
api_version: mockApiVersion,
relative_url_root: mockUrlRoot,
};
beforeEach(() => {
mock = new MockAdapter(axios);
originalGon = window.gon;
window.gon = { ...dummyGon };
});
afterEach(() => {
mock.restore();
window.gon = originalGon;
});
describe('updateGroup', () => {
const mockData = { attr: 'value' };
const expectedUrl = `${mockUrlRoot}/api/${mockApiVersion}/groups/${mockGroupId}`;
beforeEach(() => {
mock.onPut(expectedUrl).reply(({ data }) => {
2023-03-17 16:20:25 +05:30
return [HTTP_STATUS_OK, { id: mockGroupId, ...JSON.parse(data) }];
2022-08-27 11:52:29 +05:30
});
});
it('updates group', async () => {
const res = await updateGroup(mockGroupId, mockData);
expect(res.data).toMatchObject({ id: mockGroupId, ...mockData });
});
});
2023-01-13 00:05:48 +05:30
describe('getGroupTransferLocations', () => {
beforeEach(() => {
jest.spyOn(axios, 'get');
});
it('retrieves transfer locations from the correct URL and returns them in the response data', async () => {
const params = { page: 1 };
const expectedUrl = `${mockUrlRoot}/api/${mockApiVersion}/groups/${mockGroupId}/transfer_locations`;
2023-04-23 21:23:45 +05:30
mock
.onGet(expectedUrl)
.replyOnce(HTTP_STATUS_OK, { data: getGroupTransferLocationsResponse });
2023-01-13 00:05:48 +05:30
await expect(getGroupTransferLocations(mockGroupId, params)).resolves.toMatchObject({
data: { data: getGroupTransferLocationsResponse },
});
expect(axios.get).toHaveBeenCalledWith(expectedUrl, {
params: { ...params, per_page: DEFAULT_PER_PAGE },
});
});
});
2023-04-23 21:23:45 +05:30
describe('getGroupMembers', () => {
it('requests members of a group', async () => {
const expectedUrl = `${mockUrlRoot}/api/${mockApiVersion}/groups/${mockGroupId}/members`;
const response = [{ id: 0, username: 'root' }];
mock.onGet(expectedUrl).replyOnce(HTTP_STATUS_OK, response);
await expect(getGroupMembers(mockGroupId)).resolves.toMatchObject({
data: response,
});
});
it('requests inherited members of a group when requested', async () => {
const expectedUrl = `${mockUrlRoot}/api/${mockApiVersion}/groups/${mockGroupId}/members/all`;
const response = [{ id: 0, username: 'root' }];
mock.onGet(expectedUrl).replyOnce(HTTP_STATUS_OK, response);
await expect(getGroupMembers(mockGroupId, true)).resolves.toMatchObject({
data: response,
});
});
});
2022-08-27 11:52:29 +05:30
});