-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.ts
69 lines (62 loc) · 1.84 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {
GetObjectCommand,
PutObjectCommand,
S3Client,
} from '@aws-sdk/client-s3'
import 'dotenv/config'
import { promises as fs } from 'fs'
import request from 'supertest'
import app from './index'
function checkEnv(key: string): string {
if (!process.env[key]) {
console.log(`Missing environment variable: ${key}`)
process.exit(1)
}
return process.env[key] || ''
}
const s3 = new S3Client({
credentials: {
accessKeyId: checkEnv('S3_KEY'),
secretAccessKey: checkEnv('S3_SECRET'),
},
endpoint: process.env.S3_ENDPOINT,
forcePathStyle: true,
region: process.env.S3_REGION || 'unknown',
})
describe('Main', () => {
test('GET / returns ok', async () => {
const response = await request(app).get('/')
expect(response.statusCode).toBe(200)
expect(response.body).toEqual({ status: 'ok' })
})
test('POST /convert returns hash', async () => {
await s3.send(
new PutObjectCommand({
Bucket: checkEnv('S3_BUCKET'),
Key: 'SusFile/test',
Body: await fs.readFile('./test.sus'),
})
)
const postData = {
hash: 'test',
}
const response = await request(app).post('/convert').send(postData)
expect(response.statusCode).toBe(200)
await s3.send(
new GetObjectCommand({
Bucket: checkEnv('S3_BUCKET'),
Key: `LevelData/${response.body.hash}`,
})
) // check if the file exists
})
test('POST /convert with unknown file returns 404', async () => {
const postData = {
hash: 'unknown',
}
const response = await request(app).post('/convert').send(postData)
expect(response.statusCode).toBe(404)
})
})
afterAll(() => {
s3.destroy()
})