Skip to content

Commit

Permalink
fix cat list test
Browse files Browse the repository at this point in the history
  • Loading branch information
lane711 committed Mar 6, 2024
1 parent ff081a6 commit 49c3e56
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 46 deletions.
22 changes: 21 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
85 changes: 43 additions & 42 deletions src/cms/admin/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
Expand All @@ -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: '[email protected]',
// 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: '[email protected]',
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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/cms/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
5 changes: 2 additions & 3 deletions src/cms/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 49c3e56

Please sign in to comment.