|
| 1 | +import { canReadGroup } from '../canReadGroup' |
| 2 | + |
| 3 | +import * as hasPermission from '../../../utils/hasPermission' |
| 4 | +import { forbiddenError } from '../../../utils/forbiddenError' |
| 5 | + |
| 6 | +vi.mock('graphql-parse-resolve-info') |
| 7 | +vi.mock('../../../utils/parseRequestedFields') |
| 8 | + |
| 9 | +describe('canReadGroup', () => { |
| 10 | + describe('when the group is a CMR group', () => { |
| 11 | + test('returns true if the user has permission', async () => { |
| 12 | + const hasPermissionMock = vi.spyOn(hasPermission, 'hasPermission').mockResolvedValue(true) |
| 13 | + |
| 14 | + const result = await canReadGroup.resolve( |
| 15 | + null, |
| 16 | + { |
| 17 | + params: { |
| 18 | + id: '1234-abcd-5678' |
| 19 | + } |
| 20 | + }, |
| 21 | + { |
| 22 | + dataSources: { |
| 23 | + groupSourceFetch: vi.fn().mockResolvedValue({ tag: 'CMR' }) |
| 24 | + }, |
| 25 | + edlUsername: 'test-user' |
| 26 | + }, |
| 27 | + {} |
| 28 | + ) |
| 29 | + |
| 30 | + expect(result).toEqual(true) |
| 31 | + |
| 32 | + expect(hasPermissionMock).toHaveBeenCalledTimes(1) |
| 33 | + expect(hasPermissionMock).toHaveBeenCalledWith( |
| 34 | + expect.any(Object), |
| 35 | + { |
| 36 | + permissions: 'read', |
| 37 | + permissionOptions: { |
| 38 | + user_id: 'test-user', |
| 39 | + system_object: 'GROUP' |
| 40 | + } |
| 41 | + } |
| 42 | + ) |
| 43 | + }) |
| 44 | + |
| 45 | + test('throws a ForbiddenError if the user does not have permission', async () => { |
| 46 | + const hasPermissionMock = vi.spyOn(hasPermission, 'hasPermission').mockResolvedValue(false) |
| 47 | + |
| 48 | + const result = await canReadGroup.resolve( |
| 49 | + null, |
| 50 | + { |
| 51 | + params: { |
| 52 | + id: '1234-abcd-5678' |
| 53 | + } |
| 54 | + }, |
| 55 | + { |
| 56 | + dataSources: { |
| 57 | + groupSourceFetch: vi.fn().mockResolvedValue({ tag: 'CMR' }) |
| 58 | + }, |
| 59 | + edlUsername: 'test-user' |
| 60 | + } |
| 61 | + ) |
| 62 | + |
| 63 | + expect(result).toEqual(forbiddenError('Not authorized to perform [read] on system object [GROUP]')) |
| 64 | + |
| 65 | + expect(hasPermissionMock).toHaveBeenCalledTimes(1) |
| 66 | + expect(hasPermissionMock).toHaveBeenCalledWith( |
| 67 | + expect.any(Object), |
| 68 | + { |
| 69 | + permissions: 'read', |
| 70 | + permissionOptions: { |
| 71 | + user_id: 'test-user', |
| 72 | + system_object: 'GROUP' |
| 73 | + } |
| 74 | + } |
| 75 | + ) |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + describe('when the group is a provider group', () => { |
| 80 | + test('returns true if the user has permission', async () => { |
| 81 | + const hasPermissionMock = vi.spyOn(hasPermission, 'hasPermission').mockResolvedValue(true) |
| 82 | + |
| 83 | + const result = await canReadGroup.resolve( |
| 84 | + null, |
| 85 | + { |
| 86 | + params: { |
| 87 | + id: '1234-abcd-5678' |
| 88 | + } |
| 89 | + }, |
| 90 | + { |
| 91 | + dataSources: { |
| 92 | + groupSourceFetch: vi.fn().mockResolvedValue({ tag: 'GQL' }) |
| 93 | + }, |
| 94 | + edlUsername: 'test-user' |
| 95 | + }, |
| 96 | + {} |
| 97 | + ) |
| 98 | + |
| 99 | + expect(result).toEqual(true) |
| 100 | + |
| 101 | + expect(hasPermissionMock).toHaveBeenCalledTimes(1) |
| 102 | + expect(hasPermissionMock).toHaveBeenCalledWith( |
| 103 | + expect.any(Object), |
| 104 | + { |
| 105 | + permissions: 'read', |
| 106 | + permissionOptions: { |
| 107 | + provider: 'GQL', |
| 108 | + target: 'GROUP', |
| 109 | + user_id: 'test-user' |
| 110 | + } |
| 111 | + } |
| 112 | + ) |
| 113 | + }) |
| 114 | + |
| 115 | + test('throws a ForbiddenError if the user does not have permission', async () => { |
| 116 | + const hasPermissionMock = vi.spyOn(hasPermission, 'hasPermission').mockResolvedValue(false) |
| 117 | + |
| 118 | + const result = await canReadGroup.resolve( |
| 119 | + null, |
| 120 | + { |
| 121 | + params: { |
| 122 | + id: '1234-abcd-5678' |
| 123 | + } |
| 124 | + }, |
| 125 | + { |
| 126 | + dataSources: { |
| 127 | + groupSourceFetch: vi.fn().mockResolvedValue({ tag: 'GQL' }) |
| 128 | + }, |
| 129 | + edlUsername: 'test-user' |
| 130 | + } |
| 131 | + ) |
| 132 | + |
| 133 | + expect(result).toEqual(forbiddenError('Not authorized to perform [read] on provider object [GROUP]')) |
| 134 | + |
| 135 | + expect(hasPermissionMock).toHaveBeenCalledTimes(1) |
| 136 | + expect(hasPermissionMock).toHaveBeenCalledWith( |
| 137 | + expect.any(Object), |
| 138 | + { |
| 139 | + permissions: 'read', |
| 140 | + permissionOptions: { |
| 141 | + provider: 'GQL', |
| 142 | + target: 'GROUP', |
| 143 | + user_id: 'test-user' |
| 144 | + } |
| 145 | + } |
| 146 | + ) |
| 147 | + }) |
| 148 | + }) |
| 149 | + |
| 150 | + test('throws a ForbiddenError if the user does not exist', async () => { |
| 151 | + vi.spyOn(hasPermission, 'hasPermission').mockResolvedValue(false) |
| 152 | + |
| 153 | + const result = await canReadGroup.resolve( |
| 154 | + null, |
| 155 | + { |
| 156 | + params: { |
| 157 | + id: '1234-abcd-5678' |
| 158 | + } |
| 159 | + }, |
| 160 | + { |
| 161 | + edlUsername: null |
| 162 | + } |
| 163 | + ) |
| 164 | + |
| 165 | + expect(result).toEqual(forbiddenError('Not authorized to perform [read] on system object [GROUP]')) |
| 166 | + }) |
| 167 | +}) |
0 commit comments