Skip to content

Commit 606b32b

Browse files
committed
refac user token for testing
1 parent 57437d9 commit 606b32b

File tree

2 files changed

+52
-42
lines changed

2 files changed

+52
-42
lines changed

src/cms/admin/auth.test.ts

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import app from '../../server';
33
import { sql } from 'drizzle-orm';
44
import { getD1DataByTable, insertD1Data } from '../data/d1-data';
55
import { postData } from '../util/fetch';
6-
import { createCategoriesTestTable1, createUserTestTables } from '../util/testing';
6+
import { createCategoriesTestTable1, createUserAndGetToken, createUserTestTables } from '../util/testing';
77
import { createUser } from '../auth/lucia';
88
const { __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-
// email: '[email protected]',
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
});

src/cms/util/testing.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
import { sql } from "drizzle-orm";
22
import { drizzle } from "drizzle-orm/d1";
3+
import { createUser } from "../auth/lucia";
4+
import { getD1DataByTable } from "../data/d1-data";
5+
6+
7+
export async function createUserAndGetToken(app, ctx){
8+
await createUserTestTables(ctx);
9+
10+
//TODO: create user properly using the lucia api so that the user keys data in populated
11+
let login = {
12+
13+
password: "password123"
14+
}
15+
16+
let user = {data: {
17+
email: login.email,
18+
password: login.password,
19+
role:'admin',
20+
table:'users'
21+
}};
22+
const result = await createUser({ content: user, ctx });
23+
24+
const users = await getD1DataByTable(ctx.env.D1DATA, 'users', undefined);
25+
expect(users.length).toBe(1);
26+
27+
const usersKeys = await getD1DataByTable(ctx.env.D1DATA, 'user_keys', undefined);
28+
expect(usersKeys.length).toBe(1);
29+
30+
//now log the users in
31+
let req = new Request("http://localhost/v1/auth/login", {
32+
method: "POST",
33+
headers: { "Content-Type": "application/json" },
34+
body: JSON.stringify(login)
35+
});
36+
let res = await app.fetch(req, ctx.env);
37+
expect(res.status).toBe(200);
38+
let body = await res.json();
39+
expect(body.bearer.length).toBeGreaterThan(10);
40+
return body.bearer;
41+
}
342

443
export async function createUserTestTables(ctx) {
544
await createUserTestTable1(ctx);

0 commit comments

Comments
 (0)