Jobley / website / test / api.test.mjs
| 1 | import { test } from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { ADMIN, CANDIDATE, JOB, MODERATOR, call, makeEnv } from './helpers.mjs'; |
| 4 | |
| 5 | const jobId = 'j_historical'; |
| 6 | const candidateId = 'c_historical'; |
| 7 | |
| 8 | async function seedHistorical(env, status = 'published') { |
| 9 | const at = new Date().toISOString(); |
| 10 | const later = new Date(Date.now() + 86400000).toISOString(); |
| 11 | await env.DB.prepare(`INSERT INTO jobs (id, status, title, company, location, workplace, employment_type, category, pay_min, pay_max, pay_period, description, contact_name, contact_email, manage_token_hash, published_at, expires_at, created_at, updated_at) |
| 12 | VALUES (?, ?, 'Historical listing', 'Project ATOM', 'Anywhere', 'remote', 'full_time', 'engineering', 200000, 250000, 'year', 'Historical job text', 'Private Person', 'private@example.org', 'test', ?, ?, ?, ?)`).bind(jobId, status, at, later, at, at).run(); |
| 13 | await env.DB.prepare(`INSERT INTO candidates (id, job_id, name, email, email_key, headline, resume_url, consent_at, created_at, updated_at) |
| 14 | VALUES (?, ?, 'Private Person', 'private@example.org', 'private@example.org', 'Private headline', 'https://example.org/private', ?, ?, ?)`).bind(candidateId, jobId, at, at, at).run(); |
| 15 | } |
| 16 | |
| 17 | test('public metadata describes volunteer-only mode and still supplies moderator challenge key', async () => { |
| 18 | const env = makeEnv({ TURNSTILE_SITE_KEY: 'site-key' }); |
| 19 | const r = await call(env, 'GET', '/api/meta'); |
| 20 | assert.equal(r.status, 200); |
| 21 | assert.equal(r.data.mode, 'volunteer_only'); |
| 22 | assert.equal(r.data.turnstile_site_key, 'site-key'); |
| 23 | assert.equal(r.data.volunteer_url, 'https://jobley.ai/volunteer/'); |
| 24 | assert.equal(r.data.public_job_submissions, false); |
| 25 | assert.equal(r.data.candidate_applications, false); |
| 26 | assert.equal(r.data.pay_floor, undefined); |
| 27 | }); |
| 28 | |
| 29 | test('legacy public feed is an empty, noncacheable compatibility response even with historical listings', async () => { |
| 30 | const env = makeEnv(); |
| 31 | await seedHistorical(env); |
| 32 | const r = await call(env, 'GET', '/api/jobs?q=Historical'); |
| 33 | assert.deepEqual(r.data, { ok: true, jobs: [], next_cursor: null }); |
| 34 | assert.equal(r.headers.get('cache-control'), 'no-store'); |
| 35 | const detail = await call(env, 'GET', '/api/jobs/' + jobId); |
| 36 | assert.equal(detail.status, 410); |
| 37 | assert.equal(detail.data.volunteer_url, 'https://jobley.ai/volunteer/'); |
| 38 | const admin = await call(env, 'GET', '/api/admin/jobs/' + jobId, { token: ADMIN }); |
| 39 | assert.equal(admin.data.job.title, 'Historical listing'); |
| 40 | }); |
| 41 | |
| 42 | test('public candidate intake rejects all submissions without parsing or storing personal information', async () => { |
| 43 | const env = makeEnv(); |
| 44 | await seedHistorical(env); |
| 45 | for (const body of [CANDIDATE, { ...CANDIDATE, job_id: jobId }, 'not json']) { |
| 46 | const r = await call(env, 'POST', '/api/candidates', { body }); |
| 47 | assert.equal(r.status, 410); |
| 48 | assert.equal(r.data.error, 'volunteer_only'); |
| 49 | } |
| 50 | assert.equal((await env.DB.prepare('SELECT COUNT(*) AS n FROM candidates').first()).n, 1); |
| 51 | assert.equal((await env.DB.prepare('SELECT COUNT(*) AS n FROM rate_limits').first()).n, 0); |
| 52 | assert.equal((await call(env, 'GET', '/api/admin/candidates/' + candidateId)).status, 401); |
| 53 | const admin = await call(env, 'GET', '/api/admin/candidates/' + candidateId, { token: ADMIN }); |
| 54 | assert.equal(admin.data.candidate.email, 'private@example.org'); |
| 55 | assert.equal((await call(env, 'GET', '/api/admin/candidates/' + candidateId, { token: ADMIN, client: 'mcp' })).status, 403); |
| 56 | assert.equal((await call(env, 'POST', '/api/admin/candidates/' + candidateId + '/introduce', { token: ADMIN, body: {} })).status, 410); |
| 57 | }); |
| 58 | |
| 59 | test('public and curator APIs cannot create or republish legacy jobs; records remain intact', async () => { |
| 60 | const env = makeEnv(); |
| 61 | await seedHistorical(env, 'approved'); |
| 62 | assert.equal((await call(env, 'POST', '/api/jobs', { body: JOB })).status, 410); |
| 63 | assert.equal((await call(env, 'POST', '/api/admin/jobs', { body: JOB, token: ADMIN })).status, 410); |
| 64 | for (const action of ['publish', 'approve']) { |
| 65 | const r = await call(env, 'POST', '/api/admin/jobs/' + jobId + '/' + action, { body: { publish: true }, token: ADMIN }); |
| 66 | assert.equal(r.status, 410); |
| 67 | } |
| 68 | assert.deepEqual((await env.DB.prepare('SELECT id, status FROM jobs').all()).results, [{ id: jobId, status: 'approved' }]); |
| 69 | assert.equal((await call(env, 'POST', '/api/admin/jobs/' + jobId + '/reject', { body: { reason: 'Historical listing' }, token: ADMIN })).status, 200); |
| 70 | }); |
| 71 | |
| 72 | test('moderator intake retains its honeypot and Turnstile checks', async () => { |
| 73 | const env = makeEnv({ ALLOW_NO_TURNSTILE: undefined }); |
| 74 | assert.equal((await call(env, 'POST', '/api/moderators', { body: MODERATOR })).status, 503); |
| 75 | assert.equal((await call(env, 'POST', '/api/moderators', { body: { ...MODERATOR, website: 'https://example.org' } })).status, 201); |
| 76 | assert.equal((await env.DB.prepare('SELECT COUNT(*) AS n FROM moderator_applications').first()).n, 0); |
| 77 | }); |
| 78 | |
| 79 | test('admin needs a passkey session; ADMIN_TOKEN no longer signs anyone in', async () => { |
| 80 | const env = makeEnv(); |
| 81 | assert.equal((await call(env, 'GET', '/api/admin/jobs')).status, 401); |
| 82 | assert.equal((await call(env, 'GET', '/api/admin/jobs', { token: 'wrong' })).status, 401); |
| 83 | assert.equal((await call(env, 'GET', '/api/admin/me', { token: ADMIN })).data.role, 'curator'); |
| 84 | const legacy = makeEnv({ ADMIN_TOKEN: 'an-old-curator-token-that-is-long-enough' }); |
| 85 | assert.equal((await call(legacy, 'GET', '/api/admin/me', { token: 'an-old-curator-token-that-is-long-enough' })).status, 401); |
| 86 | const unset = makeEnv({ ADMIN_TOKEN: undefined }); |
| 87 | assert.equal((await call(unset, 'GET', '/api/admin/me', { token: '' })).status, 401); |
| 88 | }); |