Skip to content

Commit 6d8f575

Browse files
committed
formatting
1 parent 606b32b commit 6d8f575

File tree

4 files changed

+73
-81
lines changed

4 files changed

+73
-81
lines changed

src/cms/admin/auth.test.ts

+30-21
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@ 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, createUserAndGetToken, createUserTestTables } from '../util/testing';
6+
import {
7+
createCategoriesTestTable1,
8+
createUserAndGetToken,
9+
createUserTestTables
10+
} from '../util/testing';
711
import { createUser } from '../auth/lucia';
812
const { __D1_BETA__D1DATA, KVDATA } = getMiniflareBindings();
913

10-
const toJson = function(json){
11-
return json
14+
const toJson = function (json) {
15+
return json;
1216
};
1317

14-
const ctx = { env: { KVDATA: KVDATA, D1DATA: __D1_BETA__D1DATA },
15-
json: toJson };
18+
const ctx = {
19+
env: { KVDATA: KVDATA, D1DATA: __D1_BETA__D1DATA },
20+
json: toJson
21+
};
1622

1723
describe('admin should be restricted', () => {
1824
it('ping should return 200', async () => {
19-
const res = await app.fetch(new Request('http://localhost/v1/ping'), ctx.env);
25+
const res = await app.fetch(
26+
new Request('http://localhost/v1/ping'),
27+
ctx.env
28+
);
2029
expect(res.status).toBe(200);
2130
let body = await res.json();
2231
expect(body).toBe('/v1/ping is all good');
@@ -41,23 +50,23 @@ describe('admin should be restricted', () => {
4150
expect(body.data[0].id).toBe('1');
4251
});
4352

44-
it('create and login user', async () => {
45-
const token = await createUserAndGetToken(app, ctx);
46-
});
53+
it('create and login user', async () => {
54+
const token = await createUserAndGetToken(app, ctx);
55+
});
4756

48-
it('admin can list users', async () => {
49-
const token = await createUserAndGetToken(app, ctx);
57+
it('admin can list users', async () => {
58+
const token = await createUserAndGetToken(app, ctx);
5059

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}`
60+
// use the token to get thee user info
61+
// TODO should be able to get users
62+
let req = new Request('http://localhost/v1/categories', {
63+
method: 'GET',
64+
headers: {
65+
'Content-Type': 'application/json',
66+
Authorization: `Bearer ${token}`
5767
}
58-
});
59-
let res = await app.fetch(req, ctx.env);
60-
expect(res.status).toBe(200);
6168
});
62-
69+
let res = await app.fetch(req, ctx.env);
70+
expect(res.status).toBe(200);
71+
});
6372
});

src/cms/api/api.test.ts

+17-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ const ctx = { env: { KVDATA: KVDATA, D1DATA: __D1_BETA__D1DATA } };
1111

1212
describe('Test the APIs', () => {
1313
it('ping should return 200', async () => {
14-
const res = await app.fetch(new Request('http://localhost/v1/ping'), ctx.env);
14+
const res = await app.fetch(
15+
new Request('http://localhost/v1/ping'),
16+
ctx.env
17+
);
1518
expect(res.status).toBe(200);
1619
let body = await res.json();
1720
expect(body).toBe('/v1/ping is all good');
@@ -90,20 +93,20 @@ describe('Test the APIs', () => {
9093
// expect(body2.source).toBe('cache');
9194
// });
9295
//
93-
it('post (insert) should return 204', async () => {
94-
createCategoriesTestTable1(ctx);
95-
let payload = JSON.stringify({ data: { title: 'My Category' } });
96-
let req = new Request('http://localhost/v1/categories', {
97-
method: 'POST',
98-
body: payload,
99-
headers: { 'Content-Type': 'application/json' }
100-
});
101-
let res = await app.fetch(req, ctx.env);
102-
expect(res.status).toBe(201);
103-
let body = await res.json();
104-
expect(body.title).toBe('My Category');
105-
expect(body.id.length).toBeGreaterThan(1);
96+
it('post (insert) should return 204', async () => {
97+
createCategoriesTestTable1(ctx);
98+
let payload = JSON.stringify({ data: { title: 'My Category' } });
99+
let req = new Request('http://localhost/v1/categories', {
100+
method: 'POST',
101+
body: payload,
102+
headers: { 'Content-Type': 'application/json' }
106103
});
104+
let res = await app.fetch(req, ctx.env);
105+
expect(res.status).toBe(201);
106+
let body = await res.json();
107+
expect(body.title).toBe('My Category');
108+
expect(body.id.length).toBeGreaterThan(1);
109+
});
107110
//
108111
// it('put should return 200 and return id', async () => {
109112
// //create test record to update

src/cms/util/fetch.ts

-25
This file was deleted.

src/cms/util/testing.ts

+26-21
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
1-
import { sql } from "drizzle-orm";
2-
import { drizzle } from "drizzle-orm/d1";
3-
import { createUser } from "../auth/lucia";
4-
import { getD1DataByTable } from "../data/d1-data";
1+
import { sql } from 'drizzle-orm';
2+
import { drizzle } from 'drizzle-orm/d1';
3+
import { createUser } from '../auth/lucia';
4+
import { getD1DataByTable } from '../data/d1-data';
55

6-
7-
export async function createUserAndGetToken(app, ctx){
6+
export async function createUserAndGetToken(app, ctx) {
87
await createUserTestTables(ctx);
98

109
//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-
}};
10+
let login = {
11+
12+
password: 'password123'
13+
};
14+
15+
let user = {
16+
data: {
17+
email: login.email,
18+
password: login.password,
19+
role: 'admin',
20+
table: 'users'
21+
}
22+
};
2223
const result = await createUser({ content: user, ctx });
2324

2425
const users = await getD1DataByTable(ctx.env.D1DATA, 'users', undefined);
2526
expect(users.length).toBe(1);
2627

27-
const usersKeys = await getD1DataByTable(ctx.env.D1DATA, 'user_keys', undefined);
28+
const usersKeys = await getD1DataByTable(
29+
ctx.env.D1DATA,
30+
'user_keys',
31+
undefined
32+
);
2833
expect(usersKeys.length).toBe(1);
2934

3035
//now log the users in
31-
let req = new Request("http://localhost/v1/auth/login", {
32-
method: "POST",
33-
headers: { "Content-Type": "application/json" },
36+
let req = new Request('http://localhost/v1/auth/login', {
37+
method: 'POST',
38+
headers: { 'Content-Type': 'application/json' },
3439
body: JSON.stringify(login)
3540
});
3641
let res = await app.fetch(req, ctx.env);

0 commit comments

Comments
 (0)