From 63c71b807e7a3acd1c1dd101628fe2f5f7898ca4 Mon Sep 17 00:00:00 2001 From: Veikkosuhonen Date: Tue, 16 Jan 2024 14:36:36 +0200 Subject: [PATCH] test: Add acual test --- docker-compose.ci.yml | 1 + docker-compose.yml | 1 + package.json | 5 +++++ src/index.js | 5 +++++ src/util/testRouter.js | 14 ++++++++++++++ tests/basic.spec.js | 21 +++++++++++++++++++-- tests/config.js | 1 + tests/data.js | 6 ++++++ tests/seed.js | 9 +++++++++ 9 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/util/testRouter.js create mode 100644 tests/config.js create mode 100644 tests/data.js create mode 100644 tests/seed.js diff --git a/docker-compose.ci.yml b/docker-compose.ci.yml index 6ed2146..0b8b685 100644 --- a/docker-compose.ci.yml +++ b/docker-compose.ci.yml @@ -9,6 +9,7 @@ services: environment: - DATABASE_URL=postgres://postgres:postgres@db:5432/postgres - PORT=3003 + - NODE_ENV=test volumes: - ./:/usr/src/app ports: diff --git a/docker-compose.yml b/docker-compose.yml index 09657c6..bb350f4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,7 @@ services: environment: - DATABASE_URL=postgres://postgres:postgres@db:5432/postgres - PORT=3000 + - NODE_ENV=development volumes: - ./:/usr/src/app ports: diff --git a/package.json b/package.json index 02954af..467feee 100644 --- a/package.json +++ b/package.json @@ -40,5 +40,10 @@ "sequelize": "^6.28.0", "umzug": "^3.2.1", "winston": "^3.8.2" + }, + "nodemonConfig": { + "ignore": [ + "tests" + ] } } diff --git a/src/index.js b/src/index.js index 33f1b3c..ff50b9d 100644 --- a/src/index.js +++ b/src/index.js @@ -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() @@ -117,6 +118,10 @@ app.get('/:id', async (req, res) => { }) }) +if (!inProduction) { + app.use(testRouter) +} + app.use(Sentry.Handlers.errorHandler()) app.use(errorHandler) diff --git a/src/util/testRouter.js b/src/util/testRouter.js new file mode 100644 index 0000000..6c26634 --- /dev/null +++ b/src/util/testRouter.js @@ -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 diff --git a/tests/basic.spec.js b/tests/basic.spec.js index cc71c73..cf86ff3 100644 --- a/tests/basic.spec.js +++ b/tests/basic.spec.js @@ -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: {}, + }) +}) diff --git a/tests/config.js b/tests/config.js new file mode 100644 index 0000000..6e6b68d --- /dev/null +++ b/tests/config.js @@ -0,0 +1 @@ +export const baseUrl = 'http://localhost:3000' diff --git a/tests/data.js b/tests/data.js new file mode 100644 index 0000000..a4cb9e1 --- /dev/null +++ b/tests/data.js @@ -0,0 +1,6 @@ +export const NO_RIGHTS_USER = { + id: 'user-1', + iamGroups: [], +} + +export const ALL_USERS = [NO_RIGHTS_USER] diff --git a/tests/seed.js b/tests/seed.js new file mode 100644 index 0000000..9ca1fc9 --- /dev/null +++ b/tests/seed.js @@ -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', + }) +}