11import { describe , expect , test } from "bun:test" ;
2- import { createHeaderAuthentication } from "@listee/auth" ;
2+ import { AuthenticationError } from "@listee/auth" ;
33import type {
4+ AuthenticationProvider ,
45 Category ,
56 CategoryQueries ,
67 ListCategoriesResult ,
8+ SupabaseToken ,
79 Task ,
810 TaskQueries ,
911} from "@listee/types" ;
1012import { createApp } from "./app" ;
1113
14+ const BASE_CLAIMS = {
15+ iss : "https://example.supabase.co/auth/v1" ,
16+ aud : "authenticated" as const ,
17+ exp : 1_700_000_000 ,
18+ iat : 1_700_000_000 ,
19+ } ;
20+
1221function createRequest ( path : string , init : RequestInit = { } ) : Request {
1322 return new Request ( `http://localhost${ path } ` , init ) ;
1423}
1524
25+ function createTestAuthentication ( ) : AuthenticationProvider {
26+ return {
27+ async authenticate ( { request } ) {
28+ const header = request . headers . get ( "authorization" ) ;
29+ if ( header === null ) {
30+ throw new AuthenticationError ( "Missing authorization header" ) ;
31+ }
32+
33+ const prefix = "Bearer " ;
34+ if ( ! header . startsWith ( prefix ) ) {
35+ throw new AuthenticationError ( "Invalid authorization scheme" ) ;
36+ }
37+
38+ const tokenValue = header . slice ( prefix . length ) . trim ( ) ;
39+ if ( tokenValue . length === 0 ) {
40+ throw new AuthenticationError ( "Missing token value" ) ;
41+ }
42+
43+ const token : SupabaseToken = {
44+ ...BASE_CLAIMS ,
45+ sub : tokenValue ,
46+ role : "authenticated" ,
47+ } ;
48+
49+ return {
50+ user : {
51+ id : tokenValue ,
52+ token,
53+ } ,
54+ } ;
55+ } ,
56+ } ;
57+ }
58+
1659describe ( "health routes" , ( ) => {
1760 test ( "returns ok status" , async ( ) => {
1861 const app = createApp ( ) ;
@@ -61,7 +104,7 @@ describe("health routes", () => {
61104describe ( "category routes" , ( ) => {
62105 test ( "lists categories for a user" , async ( ) => {
63106 const { categoryQueries, categories } = createCategoryQueries ( ) ;
64- const authentication = createHeaderAuthentication ( ) ;
107+ const authentication = createTestAuthentication ( ) ;
65108 const app = createApp ( { categoryQueries, authentication } ) ;
66109
67110 const response = await app . fetch (
@@ -80,7 +123,7 @@ describe("category routes", () => {
80123
81124 test ( "rejects invalid limit" , async ( ) => {
82125 const { categoryQueries } = createCategoryQueries ( ) ;
83- const authentication = createHeaderAuthentication ( ) ;
126+ const authentication = createTestAuthentication ( ) ;
84127 const app = createApp ( { categoryQueries, authentication } ) ;
85128
86129 const response = await app . fetch (
@@ -96,7 +139,7 @@ describe("category routes", () => {
96139
97140 test ( "finds category by id" , async ( ) => {
98141 const { categoryQueries, categories } = createCategoryQueries ( ) ;
99- const authentication = createHeaderAuthentication ( ) ;
142+ const authentication = createTestAuthentication ( ) ;
100143 const app = createApp ( { categoryQueries, authentication } ) ;
101144 const target = categories [ 0 ] ;
102145
@@ -113,7 +156,7 @@ describe("category routes", () => {
113156
114157 test ( "returns 404 when category is missing" , async ( ) => {
115158 const { categoryQueries } = createCategoryQueries ( ) ;
116- const authentication = createHeaderAuthentication ( ) ;
159+ const authentication = createTestAuthentication ( ) ;
117160 const app = createApp ( { categoryQueries, authentication } ) ;
118161
119162 const response = await app . fetch (
@@ -126,7 +169,7 @@ describe("category routes", () => {
126169
127170 test ( "creates category for a user" , async ( ) => {
128171 const { categoryQueries } = createCategoryQueries ( ) ;
129- const authentication = createHeaderAuthentication ( ) ;
172+ const authentication = createTestAuthentication ( ) ;
130173 const app = createApp ( { categoryQueries, authentication } ) ;
131174
132175 const response = await app . fetch (
@@ -148,7 +191,7 @@ describe("category routes", () => {
148191
149192 test ( "updates category for a user" , async ( ) => {
150193 const { categoryQueries, categories } = createCategoryQueries ( ) ;
151- const authentication = createHeaderAuthentication ( ) ;
194+ const authentication = createTestAuthentication ( ) ;
152195 const app = createApp ( { categoryQueries, authentication } ) ;
153196 const target = categories [ 0 ] ;
154197
@@ -170,7 +213,7 @@ describe("category routes", () => {
170213
171214 test ( "deletes category for a user" , async ( ) => {
172215 const { categoryQueries, categories } = createCategoryQueries ( ) ;
173- const authentication = createHeaderAuthentication ( ) ;
216+ const authentication = createTestAuthentication ( ) ;
174217 const app = createApp ( { categoryQueries, authentication } ) ;
175218 const target = categories [ 0 ] ;
176219
@@ -193,7 +236,7 @@ describe("category routes", () => {
193236describe ( "task routes" , ( ) => {
194237 test ( "lists tasks for a category" , async ( ) => {
195238 const { taskQueries, tasks } = createTaskQueries ( ) ;
196- const authentication = createHeaderAuthentication ( ) ;
239+ const authentication = createTestAuthentication ( ) ;
197240 const app = createApp ( { taskQueries, authentication } ) ;
198241 const categoryId = tasks [ 0 ] . categoryId ;
199242
@@ -211,7 +254,7 @@ describe("task routes", () => {
211254
212255 test ( "finds task by id" , async ( ) => {
213256 const { taskQueries, tasks } = createTaskQueries ( ) ;
214- const authentication = createHeaderAuthentication ( ) ;
257+ const authentication = createTestAuthentication ( ) ;
215258 const app = createApp ( { taskQueries, authentication } ) ;
216259 const target = tasks [ 0 ] ;
217260
@@ -228,7 +271,7 @@ describe("task routes", () => {
228271
229272 test ( "returns 404 when task is missing" , async ( ) => {
230273 const { taskQueries } = createTaskQueries ( ) ;
231- const authentication = createHeaderAuthentication ( ) ;
274+ const authentication = createTestAuthentication ( ) ;
232275 const app = createApp ( { taskQueries, authentication } ) ;
233276
234277 const response = await app . fetch (
@@ -242,7 +285,7 @@ describe("task routes", () => {
242285 test ( "creates task for a category" , async ( ) => {
243286 const { categoryQueries } = createCategoryQueries ( ) ;
244287 const { taskQueries } = createTaskQueries ( ) ;
245- const authentication = createHeaderAuthentication ( ) ;
288+ const authentication = createTestAuthentication ( ) ;
246289 const category = await categoryQueries . findById ( {
247290 categoryId : "category-1" ,
248291 userId : "user-1" ,
@@ -277,7 +320,7 @@ describe("task routes", () => {
277320
278321 test ( "updates task for a user" , async ( ) => {
279322 const { taskQueries, tasks } = createTaskQueries ( ) ;
280- const authentication = createHeaderAuthentication ( ) ;
323+ const authentication = createTestAuthentication ( ) ;
281324 const app = createApp ( { taskQueries, authentication } ) ;
282325 const target = tasks [ 0 ] ;
283326
@@ -299,7 +342,7 @@ describe("task routes", () => {
299342
300343 test ( "deletes task for a user" , async ( ) => {
301344 const { taskQueries, tasks } = createTaskQueries ( ) ;
302- const authentication = createHeaderAuthentication ( ) ;
345+ const authentication = createTestAuthentication ( ) ;
303346 const app = createApp ( { taskQueries, authentication } ) ;
304347 const target = tasks [ 0 ] ;
305348
0 commit comments