|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import test from 'node:test'; |
| 3 | +import { GET, PUT, profileRouteDeps } from './route'; |
| 4 | +import { defaultStudentProfile, type StudentProfile } from '@/src/features/dashboard/student-profile-model'; |
| 5 | + |
| 6 | +const sampleProfile: StudentProfile = { |
| 7 | + ...defaultStudentProfile, |
| 8 | + name: 'Harpreet Singh', |
| 9 | + classDesignation: 'Undergraduate', |
| 10 | + userRole: 'College Student (Undergraduate)', |
| 11 | + ageRange: '19-22', |
| 12 | + primaryLearningGoals: ['Web Development'], |
| 13 | + learningPace: 'Focused', |
| 14 | + onboardingCompleted: true, |
| 15 | + onboardingCompletedAt: '2026-04-07T12:00:00.000Z', |
| 16 | +}; |
| 17 | + |
| 18 | +test('PUT /api/profile rejects invalid payloads', async (t) => { |
| 19 | + const originalDeps = { ...profileRouteDeps }; |
| 20 | + t.after(() => Object.assign(profileRouteDeps, originalDeps)); |
| 21 | + |
| 22 | + profileRouteDeps.normalizeStudentProfileInput = () => null; |
| 23 | + |
| 24 | + const response = await PUT( |
| 25 | + new Request('http://localhost/api/profile', { |
| 26 | + method: 'PUT', |
| 27 | + headers: { 'Content-Type': 'application/json' }, |
| 28 | + body: JSON.stringify({ nope: true }), |
| 29 | + }), |
| 30 | + ); |
| 31 | + const data = (await response.json()) as { error?: string }; |
| 32 | + |
| 33 | + assert.equal(response.status, 400); |
| 34 | + assert.match(data.error || '', /invalid student profile payload/i); |
| 35 | +}); |
| 36 | + |
| 37 | +test('PUT /api/profile returns 401 when there is no authenticated user', async (t) => { |
| 38 | + const originalDeps = { ...profileRouteDeps }; |
| 39 | + t.after(() => Object.assign(profileRouteDeps, originalDeps)); |
| 40 | + |
| 41 | + profileRouteDeps.normalizeStudentProfileInput = () => sampleProfile; |
| 42 | + profileRouteDeps.updateAuthenticatedProfile = async () => null; |
| 43 | + |
| 44 | + const response = await PUT( |
| 45 | + new Request('http://localhost/api/profile', { |
| 46 | + method: 'PUT', |
| 47 | + headers: { 'Content-Type': 'application/json' }, |
| 48 | + body: JSON.stringify(sampleProfile), |
| 49 | + }), |
| 50 | + ); |
| 51 | + const data = (await response.json()) as { error?: string }; |
| 52 | + |
| 53 | + assert.equal(response.status, 401); |
| 54 | + assert.match(data.error || '', /unauthorized/i); |
| 55 | +}); |
| 56 | + |
| 57 | +test('PUT /api/profile returns the saved profile and default profile', async (t) => { |
| 58 | + const originalDeps = { ...profileRouteDeps }; |
| 59 | + t.after(() => Object.assign(profileRouteDeps, originalDeps)); |
| 60 | + |
| 61 | + profileRouteDeps.normalizeStudentProfileInput = () => sampleProfile; |
| 62 | + profileRouteDeps.updateAuthenticatedProfile = async () => ({ |
| 63 | + user: { id: 'user-123', email: 'harpreet@example.com' } as never, |
| 64 | + profile: sampleProfile, |
| 65 | + defaultProfile: sampleProfile, |
| 66 | + supportsOnboardingSchema: true, |
| 67 | + supportsEnhancedOnboardingSchema: true, |
| 68 | + }); |
| 69 | + |
| 70 | + const response = await PUT( |
| 71 | + new Request('http://localhost/api/profile', { |
| 72 | + method: 'PUT', |
| 73 | + headers: { 'Content-Type': 'application/json' }, |
| 74 | + body: JSON.stringify(sampleProfile), |
| 75 | + }), |
| 76 | + ); |
| 77 | + const data = (await response.json()) as { |
| 78 | + profile?: StudentProfile; |
| 79 | + defaultProfile?: StudentProfile; |
| 80 | + }; |
| 81 | + |
| 82 | + assert.equal(response.status, 200); |
| 83 | + assert.equal(data.profile?.name, 'Harpreet Singh'); |
| 84 | + assert.deepEqual(data.defaultProfile, sampleProfile); |
| 85 | +}); |
| 86 | + |
| 87 | +test('GET /api/profile returns unauthorized when there is no authenticated user', async (t) => { |
| 88 | + const originalDeps = { ...profileRouteDeps }; |
| 89 | + t.after(() => Object.assign(profileRouteDeps, originalDeps)); |
| 90 | + |
| 91 | + profileRouteDeps.createClient = async () => |
| 92 | + ({ |
| 93 | + auth: { |
| 94 | + getUser: async () => ({ |
| 95 | + data: { user: null }, |
| 96 | + error: null, |
| 97 | + }), |
| 98 | + }, |
| 99 | + }) as never; |
| 100 | + |
| 101 | + const response = await GET(); |
| 102 | + const data = (await response.json()) as { error?: string }; |
| 103 | + |
| 104 | + assert.equal(response.status, 401); |
| 105 | + assert.match(data.error || '', /unauthorized/i); |
| 106 | +}); |
0 commit comments