@@ -3,7 +3,7 @@ import app from '../../server';
33import { sql } from 'drizzle-orm' ;
44import { getD1DataByTable , insertD1Data } from '../data/d1-data' ;
55import { postData } from '../util/fetch' ;
6- import { createCategoriesTestTable1 , createUserTestTables } from '../util/testing' ;
6+ import { createCategoriesTestTable1 , createUserAndGetToken , createUserTestTables } from '../util/testing' ;
77import { createUser } from '../auth/lucia' ;
88const { __D1_BETA__D1DATA , KVDATA } = getMiniflareBindings ( ) ;
99
@@ -41,52 +41,23 @@ describe('admin should be restricted', () => {
4141 expect ( body . data [ 0 ] . id ) . toBe ( '1' ) ;
4242 } ) ;
4343
44- it ( 'user record' , async ( ) => {
45- await createUserTestTables ( ctx ) ;
46-
47- //TODO: create user properly using the lucia api so that the user keys data in populated
48- let user = { data : {
49- 50- password : "password123" ,
51- role :'admin' ,
52- table :'users'
53- } } ;
54- const result = await createUser ( { content : user , ctx } ) ;
55-
56-
57- // const userRecord = await insertD1Data(ctx.env.D1DATA, ctx.env.KVDATA, 'users', {
58- // firstName: 'John',
59- // id: 'aaa',
60- 61- // password: 'password123',
62- // role: 'admin'
63- // });
64-
65- const users = await getD1DataByTable ( ctx . env . D1DATA , 'users' , undefined ) ;
66- expect ( users . length ) . toBe ( 1 ) ;
44+ it ( 'create and login user' , async ( ) => {
45+ const token = await createUserAndGetToken ( app , ctx ) ;
46+ } ) ;
6747
68- //now log that users in
69- let payload = { data : {
70- 71- password : "password123"
72- } } ;
48+ it ( 'admin can list users' , async ( ) => {
49+ const token = await createUserAndGetToken ( app , ctx ) ;
7350
74- let req = new Request ( "http://localhost/v1/auth/login" , {
75- method : "POST" ,
76- headers : { "Content-Type" : "application/json" } ,
77- body : JSON . stringify ( payload )
51+ // use the token to get thee user info
52+ // TODO should be able to get users
53+ let req = new Request ( 'http://localhost/v1/categories' , {
54+ method : 'GET' ,
55+ headers : { 'Content-Type' : 'application/json' ,
56+ 'Authorization' : `Bearer ${ token } `
57+ }
7858 } ) ;
7959 let res = await app . fetch ( req , ctx . env ) ;
8060 expect ( res . status ) . toBe ( 200 ) ;
81-
82- //use the token to get thee user info
83-
84- // let req = new Request('http://localhost/v1/users/aaa', {
85- // method: 'GET',
86- // headers: { 'Content-Type': 'application/json' }
87- // });
88- // let res = await app.fetch(req, ctx.env);
89- // expect(res.status).toBe(200);
9061 } ) ;
9162
9263} ) ;
0 commit comments