Skip to content

Commit

Permalink
server & db tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Dec 20, 2023
1 parent c7d6f7a commit 965669b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 103 deletions.
65 changes: 32 additions & 33 deletions packages/ozone/tests/db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import { sql } from 'kysely'
import { wait } from '@atproto/common'
import { TestNetwork } from '@atproto/dev-env'
import { Database } from '../src'
import { PrimaryDatabase } from '../src/db'
import { Leader } from '../src/db/leader'
import assert from 'assert'

describe('db', () => {
let network: TestNetwork
let db: PrimaryDatabase
let db: Database

beforeAll(async () => {
network = await TestNetwork.create({
dbPostgresSchema: 'bsky_db',
ozone: { enabled: true },
})
db = network.bsky.ctx.db.getPrimary()
assert(network.ozone)
db = network.ozone.ctx.db
})

afterAll(async () => {
Expand Down Expand Up @@ -49,46 +51,44 @@ describe('db', () => {
it('commits changes', async () => {
const result = await db.transaction(async (dbTxn) => {
return await dbTxn.db
.insertInto('actor')
.insertInto('repo_push_event')
.values({
did: 'x',
handle: 'x',
indexedAt: 'bad-date',
eventType: 'takedown',
subjectDid: 'x',
})
.returning('did')
.returning('subjectDid')
.executeTakeFirst()
})

if (!result) {
return expect(result).toBeTruthy()
}

expect(result.did).toEqual('x')
expect(result.subjectDid).toEqual('x')

const row = await db.db
.selectFrom('actor')
.selectFrom('repo_push_event')
.selectAll()
.where('did', '=', 'x')
.where('subjectDid', '=', 'x')
.executeTakeFirst()

expect(row).toEqual({
did: 'x',
handle: 'x',
indexedAt: 'bad-date',
eventType: 'takedown',
subjectDid: 'x',
takedownId: null,
confirmedAt: null,
})
})

it('rolls-back changes on failure', async () => {
const promise = db.transaction(async (dbTxn) => {
await dbTxn.db
.insertInto('actor')
.insertInto('repo_push_event')
.values({
did: 'y',
handle: 'y',
indexedAt: 'bad-date',
eventType: 'takedown',
subjectDid: 'y',
})
.returning('did')
.returning('subjectDid')
.executeTakeFirst()

throw new Error('Oops!')
Expand All @@ -97,9 +97,9 @@ describe('db', () => {
await expect(promise).rejects.toThrow('Oops!')

const row = await db.db
.selectFrom('actor')
.selectFrom('repo_push_event')
.selectAll()
.where('did', '=', 'y')
.where('subjectDid', '=', 'y')
.executeTakeFirst()

expect(row).toBeUndefined()
Expand Down Expand Up @@ -130,23 +130,23 @@ describe('db', () => {
const tx = db.transaction(async (dbTxn) => {
leakedTx = dbTxn
await dbTxn.db
.insertInto('actor')
.values({ handle: 'a', did: 'a', indexedAt: 'bad-date' })
.insertInto('repo_push_event')
.values({ eventType: 'takedown', subjectDid: 'a' })
.execute()
throw new Error('test tx failed')
})
await expect(tx).rejects.toThrow('test tx failed')

const attempt = leakedTx?.db
.insertInto('actor')
.values({ handle: 'b', did: 'b', indexedAt: 'bad-date' })
.insertInto('repo_push_event')
.values({ eventType: 'takedown', subjectDid: 'b' })
.execute()
await expect(attempt).rejects.toThrow('tx already failed')

const res = await db.db
.selectFrom('actor')
.selectFrom('repo_push_event')
.selectAll()
.where('did', 'in', ['a', 'b'])
.where('subjectDid', 'in', ['a', 'b'])
.execute()

expect(res.length).toBe(0)
Expand All @@ -161,11 +161,10 @@ describe('db', () => {
for (let i = 0; i < 20; i++) {
const name = `user${i}`
const query = dbTxn.db
.insertInto('actor')
.insertInto('repo_push_event')
.values({
handle: name,
did: name,
indexedAt: 'bad-date',
eventType: 'takedown',
subjectDid: name,
})
.execute()
names.push(name)
Expand All @@ -182,9 +181,9 @@ describe('db', () => {
}

const res = await db.db
.selectFrom('actor')
.selectFrom('repo_push_event')
.selectAll()
.where('did', 'in', names)
.where('subjectDid', 'in', names)
.execute()
expect(res.length).toBe(0)
})
Expand Down
5 changes: 2 additions & 3 deletions packages/ozone/tests/moderation-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
REASONMISLEADING,
REASONSPAM,
} from '../src/lexicon/types/com/atproto/moderation/defs'
import assert from 'assert'

describe('moderation-events', () => {
let network: TestNetwork
Expand Down Expand Up @@ -74,9 +75,7 @@ describe('moderation-events', () => {
dbPostgresSchema: 'bsky_moderation_events',
ozone: { enabled: true },
})
if (!network.ozone) {
throw new Error('Ozone not setup')
}
assert(network.ozone)
agent = network.ozone?.getClient()
pdsAgent = network.pds.getClient()
sc = network.getSeedClient()
Expand Down
5 changes: 2 additions & 3 deletions packages/ozone/tests/moderation-statuses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
REASONMISLEADING,
REASONSPAM,
} from '../src/lexicon/types/com/atproto/moderation/defs'
import assert from 'assert'

describe('moderation-statuses', () => {
let network: TestNetwork
Expand Down Expand Up @@ -77,9 +78,7 @@ describe('moderation-statuses', () => {
dbPostgresSchema: 'bsky_moderation_statuses',
ozone: { enabled: true },
})
if (!network.ozone) {
throw new Error('Ozone not setup')
}
assert(network.ozone)
agent = network.ozone.getClient()
pdsAgent = network.pds.getClient()
sc = network.getSeedClient()
Expand Down
6 changes: 5 additions & 1 deletion packages/ozone/tests/moderation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
REVIEWESCALATED,
} from '../src/lexicon/types/com/atproto/admin/defs'
import { PeriodicModerationEventReversal } from '../src'
import assert from 'assert'

type BaseCreateReportParams =
| { account: string }
Expand Down Expand Up @@ -132,8 +133,10 @@ describe('moderation', () => {
beforeAll(async () => {
network = await TestNetwork.create({
dbPostgresSchema: 'bsky_moderation',
ozone: { enabled: true },
})
agent = network.bsky.getClient()
assert(network.ozone)
agent = network.ozone.getClient()
pdsAgent = network.pds.getClient()
sc = network.getSeedClient()
await basicSeed(sc)
Expand Down Expand Up @@ -739,6 +742,7 @@ describe('moderation', () => {
takendown: true,
})

assert(network.ozone)
// In the actual app, this will be instantiated and run on server startup
const periodicReversal = new PeriodicModerationEventReversal(
network.ozone.ctx,
Expand Down
73 changes: 10 additions & 63 deletions packages/ozone/tests/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,28 @@ import express from 'express'
import axios, { AxiosError } from 'axios'
import { TestNetwork } from '@atproto/dev-env'
import { handler as errorHandler } from '../src/error'
import { Database } from '../src'
import basicSeed from './seeds/basic'
import { TestOzone } from '@atproto/dev-env/src/ozone'
import assert from 'assert'

describe('server', () => {
let network: TestNetwork
let db: Database
let alice: string
let ozone: TestOzone

beforeAll(async () => {
network = await TestNetwork.create({
dbPostgresSchema: 'bsky_server',
ozone: { enabled: true },
})
const sc = network.getSeedClient()
await basicSeed(sc)
await network.processAll()
alice = sc.dids.alice
db = network.bsky.ctx.db.getPrimary()
assert(network.ozone)
ozone = network.ozone
})

afterAll(async () => {
await network.close()
})

it('preserves 404s.', async () => {
const promise = axios.get(`${network.bsky.url}/unknown`)
const promise = axios.get(`${ozone.url}/unknown`)
await expect(promise).rejects.toThrow('failed with status code 404')
})

Expand Down Expand Up @@ -55,66 +52,16 @@ describe('server', () => {
})

it('healthcheck succeeds when database is available.', async () => {
const { data, status } = await axios.get(`${network.bsky.url}/xrpc/_health`)
const { data, status } = await axios.get(`${ozone.url}/xrpc/_health`)
expect(status).toEqual(200)
expect(data).toEqual({ version: '0.0.0' })
})

// TODO(bsky) check on a different endpoint that accepts json, currently none.
it.skip('limits size of json input.', async () => {
let error: AxiosError
try {
await axios.post(
`${network.bsky.url}/xrpc/com.atproto.repo.createRecord`,
{
data: 'x'.repeat(100 * 1024), // 100kb
},
// { headers: sc.getHeaders(alice) },
)
throw new Error('Request should have failed')
} catch (err) {
if (axios.isAxiosError(err)) {
error = err
} else {
throw err
}
}
expect(error.response?.status).toEqual(413)
expect(error.response?.data).toEqual({
error: 'PayloadTooLargeError',
message: 'request entity too large',
})
})

it('compresses large json responses', async () => {
const res = await axios.get(
`${network.bsky.url}/xrpc/app.bsky.feed.getTimeline`,
{
decompress: false,
headers: {
...(await network.serviceHeaders(alice)),
'accept-encoding': 'gzip',
},
},
)
expect(res.headers['content-encoding']).toEqual('gzip')
})

it('does not compress small payloads', async () => {
const res = await axios.get(`${network.bsky.url}/xrpc/_health`, {
decompress: false,
headers: { 'accept-encoding': 'gzip' },
})
expect(res.headers['content-encoding']).toBeUndefined()
})

it('healthcheck fails when database is unavailable.', async () => {
await network.bsky.ingester.sub.destroy()
await network.bsky.indexer.sub.destroy()
await db.close()
await ozone.ctx.db.close()
let error: AxiosError
try {
await axios.get(`${network.bsky.url}/xrpc/_health`)
await axios.get(`${ozone.url}/xrpc/_health`)
throw new Error('Healthcheck should have failed')
} catch (err) {
if (axios.isAxiosError(err)) {
Expand Down

0 comments on commit 965669b

Please sign in to comment.