Files
api/packages/decent-offchain/test/api/comments.test.ts
jigglyjamsandGitHub d8501cf1b3 ENG-833 migrate cookie based auth to bearer (#9)
* feat(auth): migrate auth and tests to Authorization Bearer <sessionId> method

* fix(test/socket): update sockettest to use Bearer auth

* fix(typo): beaerAuth -> bearerAuth

* docs(openapi): update docs with Bearer information

* fix(auth): no user check required
2025-05-13 21:59:15 -04:00

97 lines
2.9 KiB
TypeScript

import { describe, it, expect } from 'bun:test';
import { ApiResponse, Comment } from 'decent-sdk';
import app from '@/api/index';
import { authHeader, clientStore } from 'test/client';
import { daoChainId, daoAddress, newComment } from 'test/constants';
describe('Comments API', () => {
it('POST comment without a cookie', async () => {
const res = await app.request(
`/d/${daoChainId}/${daoAddress}/proposals/${clientStore.proposalSlug}/comments`,
{
method: 'POST',
body: JSON.stringify(newComment),
},
);
expect(res.status).toBe(401);
});
it('POST comment with wallet 2', async () => {
const res = await app.request(
`/d/${daoChainId}/${daoAddress}/proposals/${clientStore.proposalSlug}/comments`,
{
method: 'POST',
headers: authHeader(2),
body: JSON.stringify(newComment),
},
);
expect(res.status).toBe(200);
const json = (await res.json()) as ApiResponse<Comment>;
if (!json.data?.id) throw new Error('issue creating comment');
clientStore.commentId = json.data.id;
});
it('PUT wallet 2 comment with wallet 3', async () => {
const res = await app.request(
`/d/${daoChainId}/${daoAddress}/proposals/${clientStore.proposalSlug}/comments/${clientStore.commentId}`,
{
method: 'PUT',
headers: authHeader(1),
body: JSON.stringify({ content: 'updated comment' }),
},
);
expect(res.status).toBe(403);
});
it('PUT wallet 2 comment with wallet 2', async () => {
const res = await app.request(
`/d/${daoChainId}/${daoAddress}/proposals/${clientStore.proposalSlug}/comments/${clientStore.commentId}`,
{
method: 'PUT',
headers: authHeader(2),
body: JSON.stringify({ content: 'updated comment' }),
},
);
expect(res.status).toBe(200);
const json = (await res.json()) as ApiResponse<Comment>;
if (!json.data?.id) throw new Error('issue updating comment');
expect(json.data.content).toBe('updated comment');
expect(json.data.updatedAt).toBeDefined();
});
it('POST comment with wallet 1', async () => {
const res = await app.request(
`/d/${daoChainId}/${daoAddress}/proposals/${clientStore.proposalSlug}/comments`,
{
method: 'POST',
headers: authHeader(1),
body: JSON.stringify(newComment),
},
);
expect(res.status).toBe(200);
const json = (await res.json()) as ApiResponse<Comment>;
if (!json.data?.id) throw new Error('issue creating comment');
clientStore.commentId = json.data.id;
});
it('DELETE wallet 1 comment with wallet 1', async () => {
const res = await app.request(
`/d/${daoChainId}/${daoAddress}/proposals/${clientStore.proposalSlug}/comments/${clientStore.commentId}`,
{
method: 'DELETE',
headers: authHeader(1),
},
);
expect(res.status).toBe(200);
});
});