Skip to content

Commit

Permalink
fix ping test
Browse files Browse the repository at this point in the history
  • Loading branch information
lane711 committed Mar 1, 2024
1 parent c9275e9 commit 0cae9ad
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 110 deletions.
206 changes: 120 additions & 86 deletions src/cms/admin/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,101 +1,135 @@
it('auth.test.ts dummy', () => {});
// 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;
//
// env.useAuth = true;
//
// describe('admin should be restricted', () => {
// it('ping should return 200', async () => {
// const res = await app.request('http://localhost/admin/ping');
// expect(res.status).toBe(200);
// });
//
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;

describe('admin should be restricted', () => {
it('ping should return 200', async () => {
const res = await app.fetch(
new Request('http://localhost/v1/ping'),
env
);
expect(res.status).toBe(200);
});

// it('categories record', async () => {
// await createCategoriesTestTable1();

// await insertD1Data(env.D1DATA, env.KVDATA, 'categories', {
// title: 'My Title',
// body: 'Body goes here',
// });

// let req = new Request('http://localhost/v1/categories', {
// method: 'GET',
// headers: { 'Content-Type': 'application/json' }
// });
// let res = await app.fetch(req, env);
// expect(res.status).toBe(200);
// let body = await res.json();
// expect(body.data.length).toBe(1);
// });

// it('user record', async () => {
// createTestTables();
//
// 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 createTestTables() {
// createTestTable1();
// createTestTable2();
// createTestTable3();
// }
//
// function createTestTable1() {
// const db = drizzle(env.D1DATA);
// console.log('creating test table start');
// db.run(sql`
// CREATE TABLE users (
// id text PRIMARY KEY NOT NULL,
// firstName text,
// lastName text,
// email text,
// password text,
// role text,
// createdOn integer,
// updatedOn integer
// );
// `);
// console.log('creating test table end');
//
// return db;
// }
//
// function createTestTable2() {
// const db = drizzle(env.D1DATA);
// console.log('creating test table start');
// db.run(sql`
// CREATE TABLE user_keys (
// id text PRIMARY KEY NOT NULL,
// user_id text NOT NULL,
// hashed_password text,
// createdOn integer,
// updatedOn integer,
// FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE no action ON DELETE no action
// );
// `);
// console.log('creating test table end');
//
// return db;
// }
//
// function createTestTable3() {
// const db = drizzle(env.D1DATA);
// console.log('creating test table start');
// db.run(sql`
// CREATE TABLE user_sessions (
// id text PRIMARY KEY NOT NULL,
// user_id text NOT NULL,
// active_expires integer NOT NULL,
// idle_expires integer NOT NULL,
// createdOn integer,
// updatedOn integer,
// FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE no action ON DELETE no action
// );
// `);
// console.log('creating test table end');
//
// return db;
// }
//
});

function createUserTestTables() {
createUserTestTable1();
createUserTestTable2();
createUserTestTable3();
}

function createCategoriesTestTable1() {
const db = drizzle(env.D1DATA);
console.log('creating categories table start');
db.run(sql`
CREATE TABLE categories (
id text PRIMARY KEY NOT NULL,
title text,
body text,
createdOn integer,
updatedOn integer
);
`);
console.log('creating categories table end');

return db;
}

function createUserTestTable1() {
const db = drizzle(env.D1DATA);
console.log('creating test table start');
db.run(sql`
CREATE TABLE users (
id text PRIMARY KEY NOT NULL,
firstName text,
lastName text,
email text,
password text,
role text,
createdOn integer,
updatedOn integer
);
`);
console.log('creating test table end');

return db;
}

function createUserTestTable2() {
const db = drizzle(env.D1DATA);
console.log('creating test table start');
db.run(sql`
CREATE TABLE user_keys (
id text PRIMARY KEY NOT NULL,
user_id text NOT NULL,
hashed_password text,
createdOn integer,
updatedOn integer,
FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE no action ON DELETE no action
);
`);
console.log('creating test table end');

return db;
}

function createUserTestTable3() {
const db = drizzle(env.D1DATA);
console.log('creating test table start');
db.run(sql`
CREATE TABLE user_sessions (
id text PRIMARY KEY NOT NULL,
user_id text NOT NULL,
active_expires integer NOT NULL,
idle_expires integer NOT NULL,
createdOn integer,
updatedOn integer,
FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE no action ON DELETE no action
);
`);
console.log('creating test table end');

return db;
}
52 changes: 29 additions & 23 deletions src/cms/api/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
it('api.test.ts dummy', () => {});
// import app from '../../server';
// import { drizzle } from 'drizzle-orm/d1';
// import { sql } from 'drizzle-orm';
// import { insertD1Data } from '../data/d1-data';
// import { getRecords, insertRecord } from '../data/data';
//
// const env = getMiniflareBindings();
// const { __D1_BETA__D1DATA, KVDATA } = getMiniflareBindings();
//
// describe('Test the application', () => {
// it('ping should return 200', async () => {
// const res = await app.request('http://localhost/v1/ping');
// expect(res.status).toBe(200);
// });
//
// it('kvtest should return 200', async () => {
// const res = await app.fetch(
// new Request('http://localhost/v1/cache/kv'),
// env
// );
// expect(res.status).toBe(200);
// });
// it('api.test.ts dummy', () => {});
import app from '../../server';
import { drizzle } from 'drizzle-orm/d1';
import { sql } from 'drizzle-orm';
import { insertD1Data } from '../data/d1-data';
import { getRecords, insertRecord } from '../data/data';

const env = getMiniflareBindings();
const { __D1_BETA__D1DATA, KVDATA } = getMiniflareBindings();

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);
});

// it('kvtest should return 200', async () => {
// const res = await app.fetch(
// new Request('http://localhost/v1/cache/kv'),
// env
// );
// expect(res.status).toBe(200);
// });

//
// it('forms should return 200', async () => {
// const res = await app.request('http://localhost/v1/forms');
Expand Down Expand Up @@ -189,3 +194,4 @@ it('api.test.ts dummy', () => {});
// return db;
// }
//
});
1 change: 0 additions & 1 deletion src/cms/types/bindings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export type Bindings = {
KVDATA: KVNamespace;
D1DATA: D1Database;
__D1_BETA__D1DATA: D1Database;
useAuth?: string | boolean;
AUTH_ITERATIONS?: string;
AUTH_HASH?: 'SHA512' | 'SHA384' | 'SHA256';
AUTH_KDF?: 'pbkdf2' | 'scrypt';
Expand Down

0 comments on commit 0cae9ad

Please sign in to comment.