|
| 1 | +import { describe, test, expect, beforeEach } from 'vitest'; |
| 2 | +import { mockClient } from 'aws-sdk-client-mock'; |
| 3 | +import { DynamoDBDocumentClient, GetCommand, QueryCommand } from '@aws-sdk/lib-dynamodb'; |
| 4 | +import { handler as getTrainingStatisticsHandler } from '../src/gql-lambda-functions/Query.getTrainingStatistics'; |
| 5 | +import { handler as getTrainingOverviewStatisticsHandler } from '../src/gql-lambda-functions/Query.getTrainingOverviewStatistics'; |
| 6 | +import { handler as getTrainingDayStatisticsHandler } from '../src/gql-lambda-functions/Query.getTrainingDayStatistics'; |
| 7 | + |
| 8 | +const ddbMock = mockClient(DynamoDBDocumentClient); |
| 9 | + |
| 10 | +const USERS_TABLE = 'train-with-joe-users-sandbox'; |
| 11 | +const TRAININGS_TABLE = 'train-with-joe-trainings-sandbox'; |
| 12 | + |
| 13 | +const ADMIN_EMAIL = 'johannes.koch@gmail.com'; |
| 14 | +const NON_ADMIN_EMAIL = 'regular-user@example.com'; |
| 15 | + |
| 16 | +const makeUser = (id: string, email: string) => ({ |
| 17 | + id, |
| 18 | + email, |
| 19 | + createdAt: '2024-01-01T00:00:00.000Z', |
| 20 | + updatedAt: '2024-01-01T00:00:00.000Z', |
| 21 | +}); |
| 22 | + |
| 23 | +describe('Admin Authorization', () => { |
| 24 | + beforeEach(() => { |
| 25 | + ddbMock.reset(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('Query.getTrainingStatistics', () => { |
| 29 | + test('should return authentication error when no identity.sub', async () => { |
| 30 | + const result = await getTrainingStatisticsHandler({ |
| 31 | + arguments: { trainingId: 'training-1' }, |
| 32 | + identity: { sub: '' }, |
| 33 | + } as any); |
| 34 | + |
| 35 | + expect(result.success).toBe(false); |
| 36 | + expect(result.error).toBe('Authentication required'); |
| 37 | + }); |
| 38 | + |
| 39 | + test('should deny non-admin user from viewing other user statistics', async () => { |
| 40 | + const callerId = 'caller-123'; |
| 41 | + |
| 42 | + ddbMock.on(GetCommand).callsFake((input) => { |
| 43 | + if (input.TableName === USERS_TABLE) { |
| 44 | + return { Item: makeUser(callerId, NON_ADMIN_EMAIL) }; |
| 45 | + } |
| 46 | + return {}; |
| 47 | + }); |
| 48 | + |
| 49 | + const result = await getTrainingStatisticsHandler({ |
| 50 | + arguments: { trainingId: 'training-1', userId: 'other-user-456' }, |
| 51 | + identity: { sub: callerId }, |
| 52 | + } as any); |
| 53 | + |
| 54 | + expect(result.success).toBe(false); |
| 55 | + expect(result.error).toBe("Not authorized to view other users' statistics"); |
| 56 | + }); |
| 57 | + |
| 58 | + test('should deny when caller user not found in DynamoDB', async () => { |
| 59 | + const callerId = 'non-existent-user'; |
| 60 | + |
| 61 | + ddbMock.on(GetCommand).callsFake((input) => { |
| 62 | + if (input.TableName === USERS_TABLE) { |
| 63 | + return {}; |
| 64 | + } |
| 65 | + return {}; |
| 66 | + }); |
| 67 | + |
| 68 | + const result = await getTrainingStatisticsHandler({ |
| 69 | + arguments: { trainingId: 'training-1', userId: 'other-user-456' }, |
| 70 | + identity: { sub: callerId }, |
| 71 | + } as any); |
| 72 | + |
| 73 | + expect(result.success).toBe(false); |
| 74 | + expect(result.error).toBe("Not authorized to view other users' statistics"); |
| 75 | + }); |
| 76 | + |
| 77 | + test('should allow admin user to view other user statistics', async () => { |
| 78 | + const callerId = 'admin-123'; |
| 79 | + const targetUserId = 'target-456'; |
| 80 | + |
| 81 | + ddbMock.on(GetCommand).callsFake((input) => { |
| 82 | + if (input.TableName === USERS_TABLE) { |
| 83 | + return { Item: makeUser(callerId, ADMIN_EMAIL) }; |
| 84 | + } |
| 85 | + if (input.TableName === TRAININGS_TABLE) { |
| 86 | + return {}; |
| 87 | + } |
| 88 | + return {}; |
| 89 | + }); |
| 90 | + |
| 91 | + ddbMock.on(QueryCommand).resolves({ Items: [] }); |
| 92 | + |
| 93 | + const result = await getTrainingStatisticsHandler({ |
| 94 | + arguments: { trainingId: 'training-1', userId: targetUserId }, |
| 95 | + identity: { sub: callerId }, |
| 96 | + } as any); |
| 97 | + |
| 98 | + // Should not return the authorization error |
| 99 | + expect(result.error).not.toBe("Not authorized to view other users' statistics"); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('Query.getTrainingOverviewStatistics', () => { |
| 104 | + test('should return authentication error when no identity.sub', async () => { |
| 105 | + const result = await getTrainingOverviewStatisticsHandler({ |
| 106 | + arguments: { fromDate: '2024-01-01', toDate: '2024-01-31' }, |
| 107 | + identity: { sub: '' }, |
| 108 | + } as any); |
| 109 | + |
| 110 | + expect(result.success).toBe(false); |
| 111 | + expect(result.error).toBe('Authentication required'); |
| 112 | + }); |
| 113 | + |
| 114 | + test('should deny non-admin user from viewing other user statistics', async () => { |
| 115 | + const callerId = 'caller-123'; |
| 116 | + |
| 117 | + ddbMock.on(GetCommand).callsFake((input) => { |
| 118 | + if (input.TableName === USERS_TABLE) { |
| 119 | + return { Item: makeUser(callerId, NON_ADMIN_EMAIL) }; |
| 120 | + } |
| 121 | + return {}; |
| 122 | + }); |
| 123 | + |
| 124 | + const result = await getTrainingOverviewStatisticsHandler({ |
| 125 | + arguments: { fromDate: '2024-01-01', toDate: '2024-01-31', userId: 'other-user-456' }, |
| 126 | + identity: { sub: callerId }, |
| 127 | + } as any); |
| 128 | + |
| 129 | + expect(result.success).toBe(false); |
| 130 | + expect(result.error).toBe("Not authorized to view other users' statistics"); |
| 131 | + }); |
| 132 | + |
| 133 | + test('should deny when caller user not found in DynamoDB', async () => { |
| 134 | + const callerId = 'non-existent-user'; |
| 135 | + |
| 136 | + ddbMock.on(GetCommand).callsFake((input) => { |
| 137 | + if (input.TableName === USERS_TABLE) { |
| 138 | + return {}; |
| 139 | + } |
| 140 | + return {}; |
| 141 | + }); |
| 142 | + |
| 143 | + const result = await getTrainingOverviewStatisticsHandler({ |
| 144 | + arguments: { fromDate: '2024-01-01', toDate: '2024-01-31', userId: 'other-user-456' }, |
| 145 | + identity: { sub: callerId }, |
| 146 | + } as any); |
| 147 | + |
| 148 | + expect(result.success).toBe(false); |
| 149 | + expect(result.error).toBe("Not authorized to view other users' statistics"); |
| 150 | + }); |
| 151 | + |
| 152 | + test('should allow admin user to view other user statistics', async () => { |
| 153 | + const callerId = 'admin-123'; |
| 154 | + const targetUserId = 'target-456'; |
| 155 | + |
| 156 | + ddbMock.on(GetCommand).callsFake((input) => { |
| 157 | + if (input.TableName === USERS_TABLE) { |
| 158 | + return { Item: makeUser(callerId, ADMIN_EMAIL) }; |
| 159 | + } |
| 160 | + return {}; |
| 161 | + }); |
| 162 | + |
| 163 | + ddbMock.on(QueryCommand).resolves({ Items: [] }); |
| 164 | + |
| 165 | + const result = await getTrainingOverviewStatisticsHandler({ |
| 166 | + arguments: { fromDate: '2024-01-01', toDate: '2024-01-31', userId: targetUserId }, |
| 167 | + identity: { sub: callerId }, |
| 168 | + } as any); |
| 169 | + |
| 170 | + expect(result.error).not.toBe("Not authorized to view other users' statistics"); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe('Query.getTrainingDayStatistics', () => { |
| 175 | + test('should return authentication error when no identity.sub', async () => { |
| 176 | + const result = await getTrainingDayStatisticsHandler({ |
| 177 | + arguments: { date: '2024-01-15' }, |
| 178 | + identity: { sub: '' }, |
| 179 | + } as any); |
| 180 | + |
| 181 | + expect(result.success).toBe(false); |
| 182 | + expect(result.error).toBe('Authentication required'); |
| 183 | + }); |
| 184 | + |
| 185 | + test('should deny non-admin user from viewing other user statistics', async () => { |
| 186 | + const callerId = 'caller-123'; |
| 187 | + |
| 188 | + ddbMock.on(GetCommand).callsFake((input) => { |
| 189 | + if (input.TableName === USERS_TABLE) { |
| 190 | + return { Item: makeUser(callerId, NON_ADMIN_EMAIL) }; |
| 191 | + } |
| 192 | + return {}; |
| 193 | + }); |
| 194 | + |
| 195 | + const result = await getTrainingDayStatisticsHandler({ |
| 196 | + arguments: { date: '2024-01-15', userId: 'other-user-456' }, |
| 197 | + identity: { sub: callerId }, |
| 198 | + } as any); |
| 199 | + |
| 200 | + expect(result.success).toBe(false); |
| 201 | + expect(result.error).toBe("Not authorized to view other users' statistics"); |
| 202 | + }); |
| 203 | + |
| 204 | + test('should deny when caller user not found in DynamoDB', async () => { |
| 205 | + const callerId = 'non-existent-user'; |
| 206 | + |
| 207 | + ddbMock.on(GetCommand).callsFake((input) => { |
| 208 | + if (input.TableName === USERS_TABLE) { |
| 209 | + return {}; |
| 210 | + } |
| 211 | + return {}; |
| 212 | + }); |
| 213 | + |
| 214 | + const result = await getTrainingDayStatisticsHandler({ |
| 215 | + arguments: { date: '2024-01-15', userId: 'other-user-456' }, |
| 216 | + identity: { sub: callerId }, |
| 217 | + } as any); |
| 218 | + |
| 219 | + expect(result.success).toBe(false); |
| 220 | + expect(result.error).toBe("Not authorized to view other users' statistics"); |
| 221 | + }); |
| 222 | + |
| 223 | + test('should allow admin user to view other user statistics', async () => { |
| 224 | + const callerId = 'admin-123'; |
| 225 | + const targetUserId = 'target-456'; |
| 226 | + |
| 227 | + ddbMock.on(GetCommand).callsFake((input) => { |
| 228 | + if (input.TableName === USERS_TABLE) { |
| 229 | + return { Item: makeUser(callerId, ADMIN_EMAIL) }; |
| 230 | + } |
| 231 | + return {}; |
| 232 | + }); |
| 233 | + |
| 234 | + ddbMock.on(QueryCommand).resolves({ Items: [] }); |
| 235 | + |
| 236 | + const result = await getTrainingDayStatisticsHandler({ |
| 237 | + arguments: { date: '2024-01-15', userId: targetUserId }, |
| 238 | + identity: { sub: callerId }, |
| 239 | + } as any); |
| 240 | + |
| 241 | + expect(result.error).not.toBe("Not authorized to view other users' statistics"); |
| 242 | + }); |
| 243 | + }); |
| 244 | +}); |
0 commit comments