Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for HTTP APIs and their routes #30

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions src/server/api/admins.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import anyTest, { TestFn } from 'ava'
import sinon from 'sinon'
import { spawnTestServer } from '../fixtures/spawnServer.js'
import { FastifyTypebox } from './index.js'
import ActivityPubSystem from '../apsystem.js'

interface TestContext {
server: FastifyTypebox
hasAdminPermissionForRequestStub: sinon.SinonStub
}

const test = anyTest as TestFn<TestContext>

test.beforeEach(async t => {
t.context.server = await spawnTestServer()
t.context.hasAdminPermissionForRequestStub = sinon.stub(ActivityPubSystem.prototype, 'hasAdminPermissionForRequest')
})

test.afterEach.always(async t => {
await t.context.server?.close()
t.context.hasAdminPermissionForRequestStub.restore()
})

test.serial('GET /admins - success', async t => {
t.context.hasAdminPermissionForRequestStub.resolves(true)

const response = await t.context.server.inject({
method: 'GET',
url: '/v1/admins'
})

t.is(response.statusCode, 200, 'returns a status code of 200')
})

test.serial('GET /admins - not allowed', async t => {
t.context.hasAdminPermissionForRequestStub.resolves(false)

const response = await t.context.server.inject({
method: 'GET',
url: '/v1/admins'
})

t.is(response.statusCode, 403, 'returns a status code of 403')
})

test.serial('POST /admins - add admins', async t => {
t.context.hasAdminPermissionForRequestStub.resolves(true)

const response = await t.context.server.inject({
method: 'POST',
url: '/v1/admins',
payload: '[email protected]',
headers: {
'Content-Type': 'text/plain'
}
})

t.is(response.statusCode, 200, 'returns a status code of 200')
})

test.serial('POST /admins - not allowed', async t => {
t.context.hasAdminPermissionForRequestStub.resolves(false)

const response = await t.context.server.inject({
method: 'POST',
url: '/v1/admins',
payload: '[email protected]',
headers: {
'Content-Type': 'text/plain'
}
})

t.is(response.statusCode, 403, 'returns a status code of 403')
})

test.serial('DELETE /admins - remove admins', async t => {
RangerMauve marked this conversation as resolved.
Show resolved Hide resolved
t.context.hasAdminPermissionForRequestStub.resolves(true)

const response = await t.context.server.inject({
method: 'DELETE',
url: '/v1/admins',
payload: '[email protected]',
headers: {
'Content-Type': 'text/plain'
}
})

t.is(response.statusCode, 200, 'returns a status code of 200')
})

test.serial('DELETE /admins - not allowed', async t => {
t.context.hasAdminPermissionForRequestStub.resolves(false)

const response = await t.context.server.inject({
method: 'DELETE',
url: '/v1/admins',
payload: '[email protected]'
})

t.is(response.statusCode, 403, 'returns a status code of 403')
})
Loading
Loading