|
| 1 | +import { env } from 'cloudflare:test'; |
| 2 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 3 | +import app from '../src/index'; |
| 4 | + |
| 5 | +async function applyMigrations(db: D1Database) { |
| 6 | + const migrations = [ |
| 7 | + `CREATE TABLE IF NOT EXISTS itineraries ( |
| 8 | + id TEXT PRIMARY KEY, |
| 9 | + title TEXT NOT NULL, |
| 10 | + theme_id TEXT NOT NULL DEFAULT 'standard-autumn', |
| 11 | + memo TEXT, |
| 12 | + password TEXT, |
| 13 | + created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 14 | + updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP |
| 15 | + );`, |
| 16 | + `CREATE TABLE IF NOT EXISTS steps ( |
| 17 | + id TEXT PRIMARY KEY, |
| 18 | + itinerary_id TEXT NOT NULL, |
| 19 | + title TEXT NOT NULL, |
| 20 | + date TEXT NOT NULL, |
| 21 | + time TEXT NOT NULL, |
| 22 | + location TEXT, |
| 23 | + notes TEXT, |
| 24 | + created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 25 | + updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 26 | + FOREIGN KEY (itinerary_id) REFERENCES itineraries(id) ON DELETE CASCADE |
| 27 | + );`, |
| 28 | + `CREATE INDEX IF NOT EXISTS idx_steps_itinerary ON steps(itinerary_id);`, |
| 29 | + `CREATE TABLE IF NOT EXISTS itinerary_secrets ( |
| 30 | + itinerary_id TEXT PRIMARY KEY, |
| 31 | + enabled BOOLEAN DEFAULT FALSE, |
| 32 | + offset_minutes INTEGER DEFAULT 60, |
| 33 | + created_at TEXT NOT NULL, |
| 34 | + updated_at TEXT NOT NULL, |
| 35 | + FOREIGN KEY (itinerary_id) REFERENCES itineraries(id) ON DELETE CASCADE |
| 36 | + );`, |
| 37 | + `CREATE TABLE IF NOT EXISTS itinerary_walica_settings ( |
| 38 | + itinerary_id TEXT PRIMARY KEY, |
| 39 | + walica_id TEXT NOT NULL, |
| 40 | + created_at TEXT NOT NULL, |
| 41 | + updated_at TEXT NOT NULL, |
| 42 | + FOREIGN KEY (itinerary_id) REFERENCES itineraries(id) ON DELETE CASCADE |
| 43 | + );`, |
| 44 | + ]; |
| 45 | + |
| 46 | + for (const sql of migrations) { |
| 47 | + await db.prepare(sql).run(); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +describe('Password Authentication', () => { |
| 52 | + beforeEach(async () => { |
| 53 | + await applyMigrations(env.DB); |
| 54 | + await env.DB.prepare('DELETE FROM steps').run(); |
| 55 | + await env.DB.prepare('DELETE FROM itinerary_secrets').run(); |
| 56 | + await env.DB.prepare('DELETE FROM itinerary_walica_settings').run(); |
| 57 | + await env.DB.prepare('DELETE FROM itineraries').run(); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('Itinerary with password', () => { |
| 61 | + it('creates itinerary with hashed password', async () => { |
| 62 | + const request = new Request('http://localhost/api/v1/itineraries', { |
| 63 | + method: 'POST', |
| 64 | + headers: { 'Content-Type': 'application/json' }, |
| 65 | + body: JSON.stringify({ title: 'Protected Trip', password: 'secret123' }), |
| 66 | + }); |
| 67 | + |
| 68 | + const response = await app.fetch(request, env); |
| 69 | + expect(response.status).toBe(201); |
| 70 | + |
| 71 | + const { success, data } = await response.json() as { success: boolean; data: { id: string; is_password_protected: boolean } }; |
| 72 | + expect(success).toBe(true); |
| 73 | + expect(data.is_password_protected).toBe(true); |
| 74 | + |
| 75 | + const dbResult = await env.DB.prepare('SELECT password FROM itineraries WHERE id = ?').bind(data.id).first() as { password: string } | null; |
| 76 | + expect(dbResult).not.toBeNull(); |
| 77 | + expect(dbResult!.password).not.toBe('secret123'); |
| 78 | + expect(dbResult!.password).toMatch(/^\$2[aby]\$\d{2}\$.{53}$/); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('POST /api/v1/auth/password', () => { |
| 83 | + it('authenticates with correct password', async () => { |
| 84 | + const createRequest = new Request('http://localhost/api/v1/itineraries', { |
| 85 | + method: 'POST', |
| 86 | + headers: { 'Content-Type': 'application/json' }, |
| 87 | + body: JSON.stringify({ title: 'Protected Trip', password: 'secret123' }), |
| 88 | + }); |
| 89 | + const createResponse = await app.fetch(createRequest, env); |
| 90 | + const { data: created } = await createResponse.json() as { data: { id: string } }; |
| 91 | + |
| 92 | + const authRequest = new Request('http://localhost/api/v1/auth/password', { |
| 93 | + method: 'POST', |
| 94 | + headers: { 'Content-Type': 'application/json' }, |
| 95 | + body: JSON.stringify({ shioriId: created.id, password: 'secret123' }), |
| 96 | + }); |
| 97 | + const response = await app.fetch(authRequest, env); |
| 98 | + |
| 99 | + expect(response.status).toBe(200); |
| 100 | + const { success, data } = await response.json() as { success: boolean; data: { token: string } }; |
| 101 | + expect(success).toBe(true); |
| 102 | + expect(data.token).toBeDefined(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('rejects authentication with incorrect password', async () => { |
| 106 | + const createRequest = new Request('http://localhost/api/v1/itineraries', { |
| 107 | + method: 'POST', |
| 108 | + headers: { 'Content-Type': 'application/json' }, |
| 109 | + body: JSON.stringify({ title: 'Protected Trip', password: 'secret123' }), |
| 110 | + }); |
| 111 | + const createResponse = await app.fetch(createRequest, env); |
| 112 | + const { data: created } = await createResponse.json() as { data: { id: string } }; |
| 113 | + |
| 114 | + const authRequest = new Request('http://localhost/api/v1/auth/password', { |
| 115 | + method: 'POST', |
| 116 | + headers: { 'Content-Type': 'application/json' }, |
| 117 | + body: JSON.stringify({ shioriId: created.id, password: 'wrongpassword' }), |
| 118 | + }); |
| 119 | + const response = await app.fetch(authRequest, env); |
| 120 | + |
| 121 | + expect(response.status).toBe(401); |
| 122 | + const { success, error } = await response.json() as { success: boolean; error: { code: string } }; |
| 123 | + expect(success).toBe(false); |
| 124 | + expect(error.code).toBe('UNAUTHORIZED'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('allows access to itinerary without password', async () => { |
| 128 | + const createRequest = new Request('http://localhost/api/v1/itineraries', { |
| 129 | + method: 'POST', |
| 130 | + headers: { 'Content-Type': 'application/json' }, |
| 131 | + body: JSON.stringify({ title: 'Public Trip' }), |
| 132 | + }); |
| 133 | + const createResponse = await app.fetch(createRequest, env); |
| 134 | + const { data: created } = await createResponse.json() as { data: { id: string } }; |
| 135 | + |
| 136 | + const authRequest = new Request('http://localhost/api/v1/auth/password', { |
| 137 | + method: 'POST', |
| 138 | + headers: { 'Content-Type': 'application/json' }, |
| 139 | + body: JSON.stringify({ shioriId: created.id }), |
| 140 | + }); |
| 141 | + const response = await app.fetch(authRequest, env); |
| 142 | + |
| 143 | + expect(response.status).toBe(200); |
| 144 | + const { success, data } = await response.json() as { success: boolean; data: { token: string } }; |
| 145 | + expect(success).toBe(true); |
| 146 | + expect(data.token).toBeDefined(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('returns 404 for non-existent itinerary', async () => { |
| 150 | + const authRequest = new Request('http://localhost/api/v1/auth/password', { |
| 151 | + method: 'POST', |
| 152 | + headers: { 'Content-Type': 'application/json' }, |
| 153 | + body: JSON.stringify({ shioriId: 'nonexistent', password: 'secret' }), |
| 154 | + }); |
| 155 | + const response = await app.fetch(authRequest, env); |
| 156 | + |
| 157 | + expect(response.status).toBe(404); |
| 158 | + const { success, error } = await response.json() as { success: boolean; error: { code: string } }; |
| 159 | + expect(success).toBe(false); |
| 160 | + expect(error.code).toBe('NOT_FOUND'); |
| 161 | + }); |
| 162 | + }); |
| 163 | +}); |
0 commit comments