From 49c3e56aa2315f02b3dff65e1b18e172f7945613 Mon Sep 17 00:00:00 2001 From: Lane Date: Wed, 6 Mar 2024 09:10:26 -0800 Subject: [PATCH] fix cat list test --- .vscode/settings.json | 22 +++++++++- src/cms/admin/auth.test.ts | 85 +++++++++++++++++++------------------- src/cms/api/api.test.ts | 2 + src/cms/api/api.ts | 5 +-- 4 files changed, 68 insertions(+), 46 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 708a43800..90cf0c376 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,5 +2,25 @@ "editor.defaultFormatter": "esbenp.prettier-vscode", "[markdown]": { "editor.defaultFormatter": "DavidAnson.vscode-markdownlint" - } + }, + "workbench.colorCustomizations": { + "activityBar.activeBackground": "#65c89b", + "activityBar.background": "#65c89b", + "activityBar.foreground": "#15202b", + "activityBar.inactiveForeground": "#15202b99", + "activityBarBadge.background": "#945bc4", + "activityBarBadge.foreground": "#e7e7e7", + "commandCenter.border": "#15202b99", + "sash.hoverBorder": "#65c89b", + "statusBar.background": "#42b883", + "statusBar.foreground": "#15202b", + "statusBarItem.hoverBackground": "#359268", + "statusBarItem.remoteBackground": "#42b883", + "statusBarItem.remoteForeground": "#15202b", + "titleBar.activeBackground": "#42b883", + "titleBar.activeForeground": "#15202b", + "titleBar.inactiveBackground": "#42b88399", + "titleBar.inactiveForeground": "#15202b99" + }, + "peacock.color": "#42b883" } diff --git a/src/cms/admin/auth.test.ts b/src/cms/admin/auth.test.ts index 5397c7ee5..a46c054ae 100644 --- a/src/cms/admin/auth.test.ts +++ b/src/cms/admin/auth.test.ts @@ -2,21 +2,22 @@ import { drizzle } from 'drizzle-orm/d1'; import app from '../../server'; import { sql } from 'drizzle-orm'; import { insertD1Data } from '../data/d1-data'; - -const env = getMiniflareBindings(); -env.KVDATA = env.KVDATA; -env.D1DATA = env.__D1_BETA__D1DATA; +const { __D1_BETA__D1DATA, KVDATA } = getMiniflareBindings(); +const ctx = { env: { KVDATA: KVDATA, D1DATA: __D1_BETA__D1DATA } }; describe('admin should be restricted', () => { it('ping should return 200', async () => { - const res = await app.fetch(new Request('http://localhost/v1/ping'), env); + const res = await app.fetch(new Request('http://localhost/v1/ping'), ctx.env); expect(res.status).toBe(200); + let body = await res.json(); + expect(body).toBe('/v1/ping is all good'); }); it('categories record', async () => { await createCategoriesTestTable1(); - await insertD1Data(env.D1DATA, env.KVDATA, 'categories', { + await insertD1Data(ctx.env.D1DATA, ctx.env.KVDATA, 'categories', { + id: '1', title: 'My Title', body: 'Body goes here' }); @@ -25,42 +26,42 @@ describe('admin should be restricted', () => { method: 'GET', headers: { 'Content-Type': 'application/json' } }); - let res = await app.fetch(req, env); + let res = await app.fetch(req, ctx.env); expect(res.status).toBe(200); let body = await res.json(); - // expect(body.data.length).toBe(1); + expect(body.data[0].id).toBe('1'); }); - // it('user record', async () => { - // await createUserTestTables(); - - // await insertD1Data(env.D1DATA, env.KVDATA, 'users', { - // firstName: 'John', - // id: 'aaa', - // email: 'a@a.com', - // password: 'password', - // role: 'admin' - // }); - - // let req = new Request('http://localhost/v1/users/aaa', { - // method: 'GET', - // headers: { 'Content-Type': 'application/json' } - // }); - // let res = await app.fetch(req, env); - // expect(res.status).toBe(200); - // }); + it('user record', async () => { + await createUserTestTables(); + + await insertD1Data(env.D1DATA, env.KVDATA, 'users', { + firstName: 'John', + id: 'aaa', + email: 'a@a.com', + password: 'password', + role: 'admin' + }); + + let req = new Request('http://localhost/v1/users/aaa', { + method: 'GET', + headers: { 'Content-Type': 'application/json' } + }); + let res = await app.fetch(req, env); + expect(res.status).toBe(200); + }); }); -function createUserTestTables() { - createUserTestTable1(); - createUserTestTable2(); - createUserTestTable3(); +async function createUserTestTables() { + await createUserTestTable1(); + await createUserTestTable2(); + await createUserTestTable3(); } -function createCategoriesTestTable1() { - const db = drizzle(env.D1DATA); +async function createCategoriesTestTable1() { + const db = drizzle(ctx.env.D1DATA); console.log('creating categories table start'); - db.run(sql` + await db.run(sql` CREATE TABLE categories ( id text PRIMARY KEY NOT NULL, title text, @@ -74,10 +75,10 @@ function createCategoriesTestTable1() { return db; } -function createUserTestTable1() { - const db = drizzle(env.D1DATA); +async function createUserTestTable1() { + const db = drizzle(ctx.env.D1DATA); console.log('creating test table start'); - db.run(sql` + await db.run(sql` CREATE TABLE users ( id text PRIMARY KEY NOT NULL, firstName text, @@ -94,10 +95,10 @@ function createUserTestTable1() { return db; } -function createUserTestTable2() { - const db = drizzle(env.D1DATA); +async function createUserTestTable2() { + const db = drizzle(ctx.env.D1DATA); console.log('creating test table start'); - db.run(sql` + await db.run(sql` CREATE TABLE user_keys ( id text PRIMARY KEY NOT NULL, user_id text NOT NULL, @@ -112,10 +113,10 @@ function createUserTestTable2() { return db; } -function createUserTestTable3() { - const db = drizzle(env.D1DATA); +async function createUserTestTable3() { + const db = drizzle(ctx.env.D1DATA); console.log('creating test table start'); - db.run(sql` + await db.run(sql` CREATE TABLE user_sessions ( id text PRIMARY KEY NOT NULL, user_id text NOT NULL, diff --git a/src/cms/api/api.test.ts b/src/cms/api/api.test.ts index 2583ec175..666ed88bb 100644 --- a/src/cms/api/api.test.ts +++ b/src/cms/api/api.test.ts @@ -12,6 +12,8 @@ describe('Test the APIs', () => { it('ping should return 200', async () => { const res = await app.fetch(new Request('http://localhost/v1/ping'), env); expect(res.status).toBe(200); + let body = await res.json(); + expect(body).toBe('/v1/ping is all good'); }); // it('kvtest should return 200', async () => { diff --git a/src/cms/api/api.ts b/src/cms/api/api.ts index 9a8745896..41d6ae6ba 100644 --- a/src/cms/api/api.ts +++ b/src/cms/api/api.ts @@ -362,9 +362,8 @@ tables.forEach((entry) => { }); }); -api.get('/ping', (c) => { - console.log('testing ping', Date()); - return c.text(Date()); +api.get('/ping', (ctx) => { + return ctx.json(`${ctx.req.path} is all good`); }); api.get('/kv-test', async (ctx) => {