chore: sync remaining local changes to Gitea
This commit is contained in:
@@ -1,41 +1,82 @@
|
||||
import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test';
|
||||
import { SELF } from 'cloudflare:test';
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import worker from '../src';
|
||||
|
||||
describe('Hello World user worker', () => {
|
||||
describe('request for /message', () => {
|
||||
it('/ responds with "Hello, World!" (unit style)', async () => {
|
||||
const request = new Request<unknown, IncomingRequestCfProperties>('http://example.com/message');
|
||||
// Create an empty context to pass to `worker.fetch()`.
|
||||
const ctx = createExecutionContext();
|
||||
const response = await worker.fetch(request, env, ctx);
|
||||
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions
|
||||
await waitOnExecutionContext(ctx);
|
||||
expect(await response.text()).toMatchInlineSnapshot(`"Hello, World!"`);
|
||||
describe('tangfamily API', () => {
|
||||
describe('POST /api/login', () => {
|
||||
it('rejects wrong visitor password', async () => {
|
||||
const res = await SELF.fetch('http://example.com/api/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ password: 'wrongpass', isAdmin: false }),
|
||||
});
|
||||
expect(res.status).toBe(401);
|
||||
const data = await res.json() as { success: boolean };
|
||||
expect(data.success).toBe(false);
|
||||
});
|
||||
|
||||
it('responds with "Hello, World!" (integration style)', async () => {
|
||||
const request = new Request('http://example.com/message');
|
||||
const response = await SELF.fetch(request);
|
||||
expect(await response.text()).toMatchInlineSnapshot(`"Hello, World!"`);
|
||||
it('accepts correct visitor password', async () => {
|
||||
const res = await SELF.fetch('http://example.com/api/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ password: 'tangfamily', isAdmin: false }),
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
const data = await res.json() as { success: boolean; token: string };
|
||||
expect(data.success).toBe(true);
|
||||
expect(typeof data.token).toBe('string');
|
||||
});
|
||||
|
||||
it('rejects wrong admin password', async () => {
|
||||
const res = await SELF.fetch('http://example.com/api/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ password: 'wrongpass', isAdmin: true }),
|
||||
});
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it('accepts correct admin password', async () => {
|
||||
const res = await SELF.fetch('http://example.com/api/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ password: 'shumengya5201314', isAdmin: true }),
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
const data = await res.json() as { success: boolean; isAdmin: boolean };
|
||||
expect(data.success).toBe(true);
|
||||
expect(data.isAdmin).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('request for /random', () => {
|
||||
it('/ responds with a random UUID (unit style)', async () => {
|
||||
const request = new Request<unknown, IncomingRequestCfProperties>('http://example.com/random');
|
||||
// Create an empty context to pass to `worker.fetch()`.
|
||||
const ctx = createExecutionContext();
|
||||
const response = await worker.fetch(request, env, ctx);
|
||||
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions
|
||||
await waitOnExecutionContext(ctx);
|
||||
expect(await response.text()).toMatch(/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/);
|
||||
describe('GET /api/members', () => {
|
||||
it('returns 401 without token', async () => {
|
||||
const res = await SELF.fetch('http://example.com/api/members');
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it('responds with a random UUID (integration style)', async () => {
|
||||
const request = new Request('http://example.com/random');
|
||||
const response = await SELF.fetch(request);
|
||||
expect(await response.text()).toMatch(/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/);
|
||||
it('returns member list with valid token', async () => {
|
||||
// login first
|
||||
const loginRes = await SELF.fetch('http://example.com/api/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ password: 'tangfamily', isAdmin: false }),
|
||||
});
|
||||
const { token } = await loginRes.json() as { token: string };
|
||||
|
||||
const res = await SELF.fetch('http://example.com/api/members', {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
const data = await res.json();
|
||||
expect(Array.isArray(data)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('unknown routes', () => {
|
||||
it('returns 404 for unknown API route', async () => {
|
||||
const res = await SELF.fetch('http://example.com/api/unknown');
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user