From 366951a7b82c653977d7c4af6c657d95eebebcaa Mon Sep 17 00:00:00 2001 From: Justin Dalrymple Date: Sat, 7 Oct 2023 00:08:14 -0400 Subject: [PATCH] Additional tests --- packages/core/src/resources/Users.ts | 55 ++--- packages/core/test/__mocks__/RequestHelper.ts | 1 + packages/core/test/unit/resources/GeoSites.ts | 95 ++++++++ .../core/test/unit/resources/GitlabPages.ts | 24 ++ .../test/unit/resources/GroupEpicBoards.ts | 60 +++++ .../resources/InstanceLevelCICDVariables.ts | 63 +++++ packages/core/test/unit/resources/Users.ts | 217 ++++++++++++++++-- .../unit/templates/ResourceIssueBoards.ts | 2 +- 8 files changed, 474 insertions(+), 43 deletions(-) create mode 100644 packages/core/test/unit/resources/GeoSites.ts create mode 100644 packages/core/test/unit/resources/GitlabPages.ts create mode 100644 packages/core/test/unit/resources/GroupEpicBoards.ts create mode 100644 packages/core/test/unit/resources/InstanceLevelCICDVariables.ts diff --git a/packages/core/src/resources/Users.ts b/packages/core/src/resources/Users.ts index efd28c9e0..2aea01891 100644 --- a/packages/core/src/resources/Users.ts +++ b/packages/core/src/resources/Users.ts @@ -407,27 +407,6 @@ export class Users extends BaseResource { return RequestHelper.post()(this, endpoint`users/${userId}/block`, options); } - deactivate( - userId: number, - options?: Sudo & ShowExpanded, - ): Promise> { - return RequestHelper.post()(this, endpoint`users/${userId}/deactivate`, options); - } - - disableTwoFactor( - userId: number, - options?: Sudo & ShowExpanded, - ): Promise> { - return RequestHelper.patch()(this, endpoint`users/${userId}/disable_two_factor`, options); - } - - follow( - userId: number, - options?: Sudo & ShowExpanded, - ): Promise> { - return RequestHelper.post()(this, endpoint`users/${userId}/follow`, options); - } - create( options?: CreateUserOptions & Sudo & ShowExpanded, ): Promise> { @@ -461,6 +440,20 @@ export class Users extends BaseResource { }); } + deactivate( + userId: number, + options?: Sudo & ShowExpanded, + ): Promise> { + return RequestHelper.post()(this, endpoint`users/${userId}/deactivate`, options); + } + + disableTwoFactor( + userId: number, + options?: Sudo & ShowExpanded, + ): Promise> { + return RequestHelper.patch()(this, endpoint`users/${userId}/disable_two_factor`, options); + } + edit( userId: number, options?: EditUserOptions & Sudo & ShowExpanded, @@ -491,13 +484,20 @@ export class Users extends BaseResource { showWhitespaceInDiffs: boolean, options?: Sudo & ShowExpanded, ): Promise> { - return RequestHelper.get()(this, 'user/preferences', { + return RequestHelper.put()(this, 'user/preferences', { viewDiffsFileByFile, showWhitespaceInDiffs, ...options, }); } + follow( + userId: number, + options?: Sudo & ShowExpanded, + ): Promise> { + return RequestHelper.post()(this, endpoint`users/${userId}/follow`, options); + } + reject( userId: number, options?: Sudo & ShowExpanded, @@ -549,12 +549,15 @@ export class Users extends BaseResource { return RequestHelper.get()(this, 'user/preferences', options); } - showStatus( - options?: { iDOrUsername?: string | number } & Sudo & ShowExpanded, - ): Promise> { + showStatus({ + iDOrUsername, + ...options + }: { iDOrUsername?: string | number } & Sudo & ShowExpanded = {}): Promise< + GitlabAPIResponse + > { let url: string; - if (options?.iDOrUsername) url = `users/${options?.iDOrUsername}/status`; + if (iDOrUsername) url = `users/${iDOrUsername}/status`; else url = 'user/status'; return RequestHelper.get()(this, url, options); diff --git a/packages/core/test/__mocks__/RequestHelper.ts b/packages/core/test/__mocks__/RequestHelper.ts index 848473c11..484b4214b 100644 --- a/packages/core/test/__mocks__/RequestHelper.ts +++ b/packages/core/test/__mocks__/RequestHelper.ts @@ -6,5 +6,6 @@ export default { get: () => mockHelperFn, put: () => mockHelperFn, del: () => mockHelperFn, + patch: () => mockHelperFn, }, }; diff --git a/packages/core/test/unit/resources/GeoSites.ts b/packages/core/test/unit/resources/GeoSites.ts new file mode 100644 index 000000000..105d4f01c --- /dev/null +++ b/packages/core/test/unit/resources/GeoSites.ts @@ -0,0 +1,95 @@ +import { RequestHelper } from '../../../src/infrastructure'; +import { GeoSites } from '../../../src'; + +jest.mock( + '../../../src/infrastructure/RequestHelper', + () => require('../../__mocks__/RequestHelper').default, +); + +let service: GeoSites; + +beforeEach(() => { + service = new GeoSites({ + requesterFn: jest.fn(), + token: 'abcdefg', + }); +}); + +describe('GeoSites.all', () => { + it('should request GET /geo_sites', async () => { + await service.all(); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'geo_sites', undefined); + }); +}); + +describe('GeoSites.create', () => { + it('should request POST /geo_sites', async () => { + await service.create('name', 'url'); + + expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'geo_sites', { + name: 'name', + url: 'url', + }); + }); +}); + +describe('GeoSites.edit', () => { + it('should request PUT /geo_sites/:id', async () => { + await service.edit(1); + + expect(RequestHelper.put()).toHaveBeenCalledWith(service, 'geo_sites/1', undefined); + }); + + it('should request PUT /geo_sites/:id with options', async () => { + await service.edit(1, { internalUrl: 'url' }); + + expect(RequestHelper.put()).toHaveBeenCalledWith(service, 'geo_sites/1', { + internalUrl: 'url', + }); + }); +}); + +describe('GeoSites.allFailures', () => { + it('should request POST /geo_sites/current/failures', async () => { + await service.allFailures(); + + expect(RequestHelper.post()).toHaveBeenCalledWith( + service, + 'geo_sites/current/failures', + undefined, + ); + }); +}); + +describe('GeoSites.repair', () => { + it('should request POST /geo_sites/:id/repair', async () => { + await service.repair(1); + + expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'geo_sites/1/repair', undefined); + }); +}); + +describe('GeoSites.show', () => { + it('should request GET /geo_sites/:id', async () => { + await service.show(1); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'geo_sites/1', undefined); + }); +}); + +describe('GeoSites.showStatus', () => { + it('should request GET /geo_sites/:id/status', async () => { + await service.showStatus(1); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'geo_sites/1/status', undefined); + }); +}); + +describe('GeoSites.allStatuses', () => { + it('should request GET /geo_sites/statuses', async () => { + await service.allStatuses(); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'geo_sites/statuses', undefined); + }); +}); diff --git a/packages/core/test/unit/resources/GitlabPages.ts b/packages/core/test/unit/resources/GitlabPages.ts new file mode 100644 index 000000000..a3af068c4 --- /dev/null +++ b/packages/core/test/unit/resources/GitlabPages.ts @@ -0,0 +1,24 @@ +import { RequestHelper } from '../../../src/infrastructure'; +import { GitlabPages } from '../../../src'; + +jest.mock( + '../../../src/infrastructure/RequestHelper', + () => require('../../__mocks__/RequestHelper').default, +); + +let service: GitlabPages; + +beforeEach(() => { + service = new GitlabPages({ + requesterFn: jest.fn(), + token: 'abcdefg', + }); +}); + +describe('GitlabPages.remove', () => { + it('should request DEL /projects/1/pages', async () => { + await service.remove(1); + + expect(RequestHelper.del()).toHaveBeenCalledWith(service, 'projects/1/pages', undefined); + }); +}); diff --git a/packages/core/test/unit/resources/GroupEpicBoards.ts b/packages/core/test/unit/resources/GroupEpicBoards.ts new file mode 100644 index 000000000..af6ddb0be --- /dev/null +++ b/packages/core/test/unit/resources/GroupEpicBoards.ts @@ -0,0 +1,60 @@ +import { GroupEpicBoards } from '../../../src/resources'; +import { RequestHelper } from '../../../src/infrastructure'; + +jest.mock( + '../../../src/infrastructure/RequestHelper', + () => require('../../__mocks__/RequestHelper').default, +); + +let service: GroupEpicBoards; + +beforeEach(() => { + service = new GroupEpicBoards({ + requesterFn: jest.fn(), + token: 'abcdefg', + }); +}); + +afterEach(() => { + jest.clearAllMocks(); +}); + +describe('GroupEpicBoards.all', () => { + it('should call the correct url with a group id', async () => { + await service.all(1); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'groups/1/epic_boards', undefined); + }); +}); + +describe('GroupEpicBoards.allLists', () => { + it('should call the correct url with a group id and board id', async () => { + await service.allLists('5', 6); + + expect(RequestHelper.get()).toHaveBeenCalledWith( + service, + 'groups/5/epic_boards/6/lists', + undefined, + ); + }); +}); + +describe('GroupEpicBoards.show', () => { + it('should call the correct url with a group id and board id', async () => { + await service.show('5', 6); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'groups/5/epic_boards/6', undefined); + }); +}); + +describe('GroupEpicBoards.showList', () => { + it('should call the correct url with a group id, board id and list id', async () => { + await service.showList('5', 6, 7); + + expect(RequestHelper.get()).toHaveBeenCalledWith( + service, + 'groups/5/epic_boards/6/lists/7', + undefined, + ); + }); +}); diff --git a/packages/core/test/unit/resources/InstanceLevelCICDVariables.ts b/packages/core/test/unit/resources/InstanceLevelCICDVariables.ts new file mode 100644 index 000000000..2486d145f --- /dev/null +++ b/packages/core/test/unit/resources/InstanceLevelCICDVariables.ts @@ -0,0 +1,63 @@ +import { RequestHelper } from '../../../src/infrastructure'; +import { InstanceLevelCICDVariables } from '../../../src'; + +jest.mock( + '../../../src/infrastructure/RequestHelper', + () => require('../../__mocks__/RequestHelper').default, +); + +let service: InstanceLevelCICDVariables; + +beforeEach(() => { + service = new InstanceLevelCICDVariables({ + requesterFn: jest.fn(), + token: 'abcdefg', + }); +}); + +describe('InstanceLevelCICDVariables.all', () => { + it('should request GET admin/ci/variables', async () => { + await service.all(); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'admin/ci/variables', undefined); + }); +}); + +describe('InstanceLevelCICDVariables.create', () => { + it('should request POST admin/ci/variables', async () => { + await service.create('key', 'value', { raw: false }); + + expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'admin/ci/variables', { + key: 'key', + value: 'value', + raw: false, + }); + }); +}); + +describe('InstanceLevelCICDVariables.edit', () => { + it('should request PUT admin/ci/variables/:key', async () => { + await service.edit('key', 'value', { raw: false }); + + expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'admin/ci/variables/key', { + value: 'value', + raw: false, + }); + }); +}); + +describe('InstanceLevelCICDVariables.remove', () => { + it('should request DEL admin/ci/variables/:key', async () => { + await service.remove('key'); + + expect(RequestHelper.del()).toHaveBeenCalledWith(service, 'admin/ci/variables/key', undefined); + }); +}); + +describe('InstanceLevelCICDVariables.show', () => { + it('should request GET admin/ci/variables/:name', async () => { + await service.show('key'); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'admin/ci/variables/key', undefined); + }); +}); diff --git a/packages/core/test/unit/resources/Users.ts b/packages/core/test/unit/resources/Users.ts index ac4b91a71..e17910ca6 100644 --- a/packages/core/test/unit/resources/Users.ts +++ b/packages/core/test/unit/resources/Users.ts @@ -15,6 +15,22 @@ beforeEach(() => { }); }); +describe('Users.activate', () => { + it('should request POST users/:id/activate', async () => { + await service.activate(1); + + expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'users/1/activate', undefined); + }); +}); + +describe('Users.approve', () => { + it('should request POST users/:id/approve', async () => { + await service.approve(1); + + expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'users/1/approve', undefined); + }); +}); + describe('Users.all', () => { it('should request GET users', async () => { await service.all(); @@ -31,14 +47,6 @@ describe('Users.allActivities', () => { }); }); -describe('Users.showCurrentUserPreferences', () => { - it('should request GET users', async () => { - await service.showCurrentUserPreferences(); - - expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'user/preferences', undefined); - }); -}); - describe('Users.allMemberships', () => { it('should request GET users/:user_id/memberships', async () => { await service.allMemberships(1); @@ -71,6 +79,46 @@ describe('Users.allProjects', () => { }); }); +describe('Users.allEvents', () => { + it('should request GET users/:id/projects', async () => { + await service.allEvents(1); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'users/1/events', undefined); + }); +}); + +describe('Users.allContributedProjects', () => { + it('should request GET users/:id/contributed_projects', async () => { + await service.allContributedProjects(1); + + expect(RequestHelper.get()).toHaveBeenCalledWith( + service, + 'users/1/contributed_projects', + undefined, + ); + }); +}); + +describe('Users.allStarredProjects', () => { + it('should request GET users/:id/starred_projects', async () => { + await service.allStarredProjects(1); + + expect(RequestHelper.get()).toHaveBeenCalledWith( + service, + 'users/1/starred_projects', + undefined, + ); + }); +}); + +describe('Users.ban', () => { + it('should request POST users/:id/ban', async () => { + await service.ban(1); + + expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'users/1/ban', undefined); + }); +}); + describe('Users.block', () => { it('should request POST users/:id/block', async () => { await service.block(1); @@ -87,11 +135,45 @@ describe('Users.create', () => { }); }); -describe('Users.showCurrentUser', () => { - it('should request GET user', async () => { - await service.showCurrentUser(); +describe('Users.createPersonalAccessToken', () => { + it('should request POST users/:id/personal_access_tokens', async () => { + await service.createPersonalAccessToken(1, 'token', ['test'], { expiresAt: '10' }); - expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'user', undefined); + expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'users/1/personal_access_tokens', { + name: 'token', + scopes: ['test'], + expiresAt: '10', + }); + }); +}); + +describe('Users.createCIRunner', () => { + it('should request POST users/:id/runners', async () => { + await service.createCIRunner('instance_type'); + + expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'user/runners', { + runnerType: 'instance_type', + }); + }); +}); + +describe('Users.deactivate', () => { + it('should request POST users/:id/deactivate', async () => { + await service.deactivate(1); + + expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'users/1/deactivate', undefined); + }); +}); + +describe('Users.disableTwoFactor', () => { + it('should request PATCH users/:id/disable_two_factor', async () => { + await service.disableTwoFactor(1); + + expect(RequestHelper.patch()).toHaveBeenCalledWith( + service, + 'users/1/disable_two_factor', + undefined, + ); }); }); @@ -103,11 +185,40 @@ describe('Users.edit', () => { }); }); -describe('Users.allEvents', () => { - it('should request GET users/:id/projects', async () => { - await service.allEvents(1); +describe('Users.editStatus', () => { + it('should request PUT users/:id', async () => { + await service.editStatus({ message: 'my message' }); - expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'users/1/events', undefined); + expect(RequestHelper.put()).toHaveBeenCalledWith(service, 'user/status', { + message: 'my message', + }); + }); +}); + +describe('Users.editCurrentUserPreferences', () => { + it('should request PUT user/preferences', async () => { + await service.editCurrentUserPreferences(true, false); + + expect(RequestHelper.put()).toHaveBeenCalledWith(service, 'user/preferences', { + viewDiffsFileByFile: true, + showWhitespaceInDiffs: false, + }); + }); +}); + +describe('Users.follow', () => { + it('should request POST users/:id/follow', async () => { + await service.follow(1); + + expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'users/1/follow', undefined); + }); +}); + +describe('Users.reject', () => { + it('should request POST users/:id/reject', async () => { + await service.reject(1); + + expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'users/1/reject', undefined); }); }); @@ -119,6 +230,56 @@ describe('Users.show', () => { }); }); +describe('Users.showCount', () => { + it('should request GET user_counts', async () => { + await service.showCount(); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'user_counts', undefined); + }); +}); + +describe('Users.showAssociationsCount', () => { + it('should request GET users/:id/associations_count', async () => { + await service.showAssociationsCount(1); + + expect(RequestHelper.get()).toHaveBeenCalledWith( + service, + 'users/1/associations_count', + undefined, + ); + }); +}); + +describe('Users.showCurrentUser', () => { + it('should request GET user', async () => { + await service.showCurrentUser(); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'user', undefined); + }); +}); + +describe('Users.showCurrentUserPreferences', () => { + it('should request GET users', async () => { + await service.showCurrentUserPreferences(); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'user/preferences', undefined); + }); +}); + +describe('Users.showStatus', () => { + it('should request GET user/status', async () => { + await service.showStatus(); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'user/status', {}); + }); + + it('should request GET users/:id/status', async () => { + await service.showStatus({ iDOrUsername: 1 }); + + expect(RequestHelper.get()).toHaveBeenCalledWith(service, 'users/1/status', {}); + }); +}); + describe('Users.remove', () => { it('should request DELETE users/:id', async () => { await service.remove(1); @@ -127,6 +288,22 @@ describe('Users.remove', () => { }); }); +describe('Users.removeAuthenticationIdentity', () => { + it('should request DELETE users/:id/identities/:provider', async () => { + await service.removeAuthenticationIdentity(1, 'prov'); + + expect(RequestHelper.del()).toHaveBeenCalledWith(service, 'users/1/identities/prov', undefined); + }); +}); + +describe('Users.unban', () => { + it('should request POST users/:id/unban', async () => { + await service.unban(1); + + expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'users/1/unban', undefined); + }); +}); + describe('Users.unblock', () => { it('should request POST users/:id/unblock', async () => { await service.unblock(1); @@ -134,3 +311,11 @@ describe('Users.unblock', () => { expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'users/1/unblock', undefined); }); }); + +describe('Users.unfollow', () => { + it('should request POST users/:id/unfollow', async () => { + await service.unfollow(1); + + expect(RequestHelper.post()).toHaveBeenCalledWith(service, 'users/1/unfollow', undefined); + }); +}); diff --git a/packages/core/test/unit/templates/ResourceIssueBoards.ts b/packages/core/test/unit/templates/ResourceIssueBoards.ts index aa1c492b4..6e8497eda 100644 --- a/packages/core/test/unit/templates/ResourceIssueBoards.ts +++ b/packages/core/test/unit/templates/ResourceIssueBoards.ts @@ -95,7 +95,7 @@ describe('ResourceIssueBoards.editList', () => { }); }); -describe('ResourceIssueBoards.lists', () => { +describe('ResourceIssueBoards.allLists', () => { it('should call the correct url with a resource id and board id', async () => { await service.allLists('5', 6);