|
| 1 | +const openopsCommonMock = { |
| 2 | + ...jest.requireActual('@openops/common'), |
| 3 | + makeOpenOpsTablesGet: jest.fn(), |
| 4 | +}; |
| 5 | +jest.mock('@openops/common', () => openopsCommonMock); |
| 6 | + |
| 7 | +import { DatabaseToken } from '@openops/common'; |
| 8 | +import { AxiosHeaders } from 'axios'; |
| 9 | +import { listDatabaseTokens } from '../../../src/app/openops-tables/list-database-tokens'; |
| 10 | + |
| 11 | +describe('listDatabases', () => { |
| 12 | + beforeEach(() => { |
| 13 | + jest.clearAllMocks(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should return only database tokens for the provided workspace', async () => { |
| 17 | + const workspaceId = 1; |
| 18 | + const token = 'test_token'; |
| 19 | + const mockApplications: DatabaseToken[] = [ |
| 20 | + { |
| 21 | + id: 1, |
| 22 | + name: 'Token 1', |
| 23 | + workspace: workspaceId, |
| 24 | + key: 'key', |
| 25 | + permissions: { create: true, read: true, update: true, delete: true }, |
| 26 | + }, |
| 27 | + { |
| 28 | + id: 2, |
| 29 | + name: 'Token 2', |
| 30 | + workspace: 2, |
| 31 | + key: 'key', |
| 32 | + permissions: { create: true, read: true, update: true, delete: true }, |
| 33 | + }, |
| 34 | + { |
| 35 | + id: 3, |
| 36 | + name: 'Token 3', |
| 37 | + workspace: workspaceId, |
| 38 | + key: 'key', |
| 39 | + permissions: { create: true, read: true, update: true, delete: true }, |
| 40 | + }, |
| 41 | + { |
| 42 | + id: 4, |
| 43 | + name: 'Token 4', |
| 44 | + workspace: 2, |
| 45 | + key: 'key', |
| 46 | + permissions: { create: true, read: true, update: true, delete: true }, |
| 47 | + }, |
| 48 | + ]; |
| 49 | + |
| 50 | + openopsCommonMock.makeOpenOpsTablesGet.mockResolvedValue([ |
| 51 | + mockApplications, |
| 52 | + ]); |
| 53 | + |
| 54 | + const result = await listDatabaseTokens(workspaceId, token); |
| 55 | + |
| 56 | + expect(result).toHaveLength(2); |
| 57 | + expect(result).toEqual([mockApplications[0], mockApplications[2]]); |
| 58 | + expect(openopsCommonMock.makeOpenOpsTablesGet).toHaveBeenCalledWith( |
| 59 | + 'api/database/tokens/', |
| 60 | + new AxiosHeaders({ |
| 61 | + 'Content-Type': 'application/json', |
| 62 | + Authorization: `JWT ${token}`, |
| 63 | + }), |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should return empty array when no database tokens exist for the workspace', async () => { |
| 68 | + const workspaceId = 1; |
| 69 | + const token = 'test_token'; |
| 70 | + const mockApplications: DatabaseToken[] = [ |
| 71 | + { |
| 72 | + id: 4, |
| 73 | + name: 'Token 4', |
| 74 | + workspace: 2, |
| 75 | + key: 'key', |
| 76 | + permissions: { create: true, read: true, update: true, delete: true }, |
| 77 | + }, |
| 78 | + ]; |
| 79 | + |
| 80 | + openopsCommonMock.makeOpenOpsTablesGet.mockResolvedValue(mockApplications); |
| 81 | + |
| 82 | + const result = await listDatabaseTokens(workspaceId, token); |
| 83 | + |
| 84 | + expect(result).toHaveLength(0); |
| 85 | + expect(result).toEqual([]); |
| 86 | + expect(openopsCommonMock.makeOpenOpsTablesGet).toHaveBeenCalledWith( |
| 87 | + 'api/database/tokens/', |
| 88 | + new AxiosHeaders({ |
| 89 | + 'Content-Type': 'application/json', |
| 90 | + Authorization: `JWT ${token}`, |
| 91 | + }), |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should return empty array when no database tokens exist', async () => { |
| 96 | + const workspaceId = 1; |
| 97 | + const token = 'test_token'; |
| 98 | + const mockApplications: DatabaseToken[] = []; |
| 99 | + |
| 100 | + openopsCommonMock.makeOpenOpsTablesGet.mockResolvedValue(mockApplications); |
| 101 | + |
| 102 | + const result = await listDatabaseTokens(workspaceId, token); |
| 103 | + |
| 104 | + expect(result).toHaveLength(0); |
| 105 | + expect(result).toEqual([]); |
| 106 | + expect(openopsCommonMock.makeOpenOpsTablesGet).toHaveBeenCalledWith( |
| 107 | + 'api/database/tokens/', |
| 108 | + new AxiosHeaders({ |
| 109 | + 'Content-Type': 'application/json', |
| 110 | + Authorization: `JWT ${token}`, |
| 111 | + }), |
| 112 | + ); |
| 113 | + }); |
| 114 | +}); |
0 commit comments