-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
68 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: '[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, | ||
|
@@ -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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters