Skip to content

Commit

Permalink
test: Add acual test
Browse files Browse the repository at this point in the history
  • Loading branch information
Veikkosuhonen committed Jan 16, 2024
1 parent b5cc589 commit 63c71b8
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions docker-compose.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
environment:
- DATABASE_URL=postgres://postgres:postgres@db:5432/postgres
- PORT=3003
- NODE_ENV=test
volumes:
- ./:/usr/src/app
ports:
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
environment:
- DATABASE_URL=postgres://postgres:postgres@db:5432/postgres
- PORT=3000
- NODE_ENV=development
volumes:
- ./:/usr/src/app
ports:
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@
"sequelize": "^6.28.0",
"umzug": "^3.2.1",
"winston": "^3.8.2"
},
"nodemonConfig": {
"ignore": [
"tests"
]
}
}
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { data } = require('./auth/data')

const { connectToDatabase } = require('./db/connection')
const { User } = require('./db/models')
const testRouter = require('./util/testRouter')

initializeSentry()

Expand Down Expand Up @@ -117,6 +118,10 @@ app.get('/:id', async (req, res) => {
})
})

if (!inProduction) {
app.use(testRouter)
}

app.use(Sentry.Handlers.errorHandler())
app.use(errorHandler)

Expand Down
14 changes: 14 additions & 0 deletions src/util/testRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { Router } = require('express')
const User = require('../db/models/user')

const testRouter = Router()

testRouter.post('/users', (req, res) => {
const users = req.body

User.bulkCreate(users)

res.status(201).json(users)
})

module.exports = testRouter
21 changes: 19 additions & 2 deletions tests/basic.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import { expect, test } from 'vitest'
import { expect, test, beforeAll } from 'vitest'
import { baseUrl } from './config'
import { seed } from './seed'

const baseUrl = 'http://localhost:3000'
beforeAll(async () => {
await seed()
})

test('Ping', async () => {
const res = await fetch(`${baseUrl}/ping`)
expect(res.status).toBe(200)
expect(await res.text()).toBe('pong')
})

test('User with no iam groups gets no access', async () => {
const res = await fetch(baseUrl, {
body: JSON.stringify({ userId: 'user-1', iamGroups: [] }),
method: 'POST',
})

expect(res.status).toBe(200)
expect(await res.json()).toEqual({
// access: {}, <-- this is the expected response. Fix api.
specialGroup: {},
})
})
1 change: 1 addition & 0 deletions tests/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const baseUrl = 'http://localhost:3000'
6 changes: 6 additions & 0 deletions tests/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const NO_RIGHTS_USER = {
id: 'user-1',
iamGroups: [],
}

export const ALL_USERS = [NO_RIGHTS_USER]
9 changes: 9 additions & 0 deletions tests/seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { baseUrl } from './config'
import { ALL_USERS } from './data'

export const seed = async () => {
await fetch(`${baseUrl}/test/users`, {
body: ALL_USERS,
method: 'POST',
})
}

0 comments on commit 63c71b8

Please sign in to comment.