Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 4 additions & 15 deletions backend/jest-config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
module.exports = {
testPathIgnorePatterns: [
"<rootDir>/build",
"<rootDir>/node_modules",
],
collectCoverageFrom: [
"./src/**",
"!./src/api-tests/**"
],
roots: [
"./src"
],
coverageReporters: [
"text",
"lcov"
]
testPathIgnorePatterns: ["<rootDir>/build", "<rootDir>/node_modules", "<rootDir>/../src/api-tests"],
collectCoverageFrom: ["./src/**", "!./src/api-tests/**"],
roots: ["./src"],
coverageReporters: ["text", "lcov"],
}
4 changes: 2 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"scripts": {
"dev": "npx tsx watch --clear-screen=false src/index.ts",
"build": "tsc",
"test:api": "jest src/api-tests --runInBand --coverage --config jest-config.js --detectOpenHandles --forceExit --silent",
"test:unit": "jest src/unit-tests --coverage --config jest-config.js --detectOpenHandles --forceExit --silent",
"test:api": "jest ./src/api-tests --runInBand --coverage --config jest-config.js --detectOpenHandles --forceExit --silent",
"test:unit": "DOTENV_CONFIG_PATH=../.test.env jest src/unit-tests --coverage --config jest-config.js --detectOpenHandles --forceExit --silent --setupFiles dotenv/config",
"test:api:local": "DOTENV_CONFIG_PATH=../.test.env npm run test:api -- --setupFiles dotenv/config",
"start": "node build/index.js",
"lint": "eslint --report-unused-disable-directives --max-warnings 0",
Expand Down
4 changes: 3 additions & 1 deletion backend/src/api-tests/locality/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ describe('Locality update works', () => {
it('Edits name, synonyms and locality species correctly', async () => {
const writeResult = await send<{ id: number }>('locality', 'PUT', { locality: editedLocality })

expect(writeResult.status).toEqual(200)
expect(writeResult.body.id).toEqual(editedLocality.lid) // `Invalid result returned on write: ${writeResult.body.id}`

const { body } = await send<LocalityDetailsType>(`locality/${editedLocality.lid}`, 'GET')
const { body, status } = await send<LocalityDetailsType>(`locality/${editedLocality.lid}`, 'GET')
expect(status).toEqual(200)
resultLocality = body
})

Expand Down
4 changes: 4 additions & 0 deletions backend/src/api-tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export const setToken = (newToken: string) => (token = newToken)
export const login = async (username: string = 'testSu', password: string = 'test') => {
// Login and set token
const result = await send<{ token: string }>('user/login', 'POST', { username, password })
if (!result.body.token || result.status !== 200) {
throw new Error(`Login failed for ${username}: ${result.status}`)
}
token = result.body.token
}

Expand Down Expand Up @@ -95,6 +98,7 @@ export const testLogRows = (logRows: UpdateLog[], expectedLogRows: Partial<LogRo

export const resetDatabase = async () => {
await send('test/reset-test-database', 'GET')
await send('test/create-test-users', 'GET')
}

export const resetDatabaseTimeout: number = 1200000 // 120 seconds
Expand Down
25 changes: 19 additions & 6 deletions backend/src/controllers/projectsController.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { Request, Response } from 'express'
import { createProject, CreateProjectInput, updateProject, UpdateProjectInput } from '../services/projectsService'
import { NotFoundError, ValidationError } from '../validators/projectsValidator'

export const createProjectHandler = async (req: Request<object, object, CreateProjectInput>, res: Response) => {
const createdProject = await createProject(req.body)

return res.status(201).json(createdProject)
try {
const createdProject = await createProject(req.body)
return res.status(201).json(createdProject)
} catch (error) {
if (error instanceof ValidationError) {
return res.status(error.status).json({ message: error.message })
}
throw error
}
}

export const updateProjectHandler = async (req: Request<{ id: string }, object, UpdateProjectInput>, res: Response) => {
const updatedProject = await updateProject(Number(req.params.id), req.body)

return res.status(200).json(updatedProject)
try {
const updatedProject = await updateProject(Number(req.params.id), req.body)
return res.status(200).json(updatedProject)
} catch (error) {
if (error instanceof ValidationError || error instanceof NotFoundError) {
return res.status(error.status).json({ message: error.message })
}
throw error
}
}
7 changes: 7 additions & 0 deletions backend/src/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ export const createTestUsers = async () => {
userId = createdUser.user_id
} else {
userId = existingUser.user_id
await nowDb.com_users.update({
where: { user_id: userId },
data: {
newpassword: passwordHash,
now_user_group: testUser.now_user_group,
},
})
}
const initials = `TEST-${testUser.now_user_group.toUpperCase()}`
const existingPerson = await nowDb.com_people.findFirst({
Expand Down
4 changes: 3 additions & 1 deletion backend/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@ const requiredEnvs = {
}

const missingEnvs = Object.entries(requiredEnvs).filter(([, value]) => value === undefined)
if (missingEnvs.length > 0)
const isJestEnvironment = process.env.JEST_WORKER_ID !== undefined
if (missingEnvs.length > 0 && !isJestEnvironment) {
throw new Error(`Missing environment variables: ${missingEnvs.map(env => env[0]).join(', ')}`)
}
94 changes: 0 additions & 94 deletions src/api-tests/species/create.test.ts

This file was deleted.

11 changes: 5 additions & 6 deletions test_data/sqlfiles/now_test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,11 @@ INSERT INTO `com_users` VALUES
(160,'project',NULL,'0cc175b9c0f1b6a831c399e269772661','2024-05-27','pl',NULL,NULL),
(161,'read',NULL,'0cc175b9c0f1b6a831c399e269772661','2024-05-23','ro',NULL,NULL),
(162,'coord',NULL,'0cc175b9c0f1b6a831c399e269772661','2024-05-27','su',NULL,NULL),
(163,'testSu',NULL,'$2b$10$hPVP4Zl9WmKle5S42WIeBu3otM2eV0BWB21eXxn/YEcMm7TNJiQ02',NULL,'su',NULL,NULL),
(164,'testEu',NULL,'$2b$10$hPVP4Zl9WmKle5S42WIeBu3otM2eV0BWB21eXxn/YEcMm7TNJiQ02',NULL,'eu',NULL,NULL),
(165,'testEr',NULL,'$2b$10$hPVP4Zl9WmKle5S42WIeBu3otM2eV0BWB21eXxn/YEcMm7TNJiQ02',NULL,'er',NULL,NULL),
(166,'testNo',NULL,'$2b$10$hPVP4Zl9WmKle5S42WIeBu3otM2eV0BWB21eXxn/YEcMm7TNJiQ02',NULL,'no',NULL,NULL),
(167,'testPl',NULL,'$2b$10$hPVP4Zl9WmKle5S42WIeBu3otM2eV0BWB21eXxn/YEcMm7TNJiQ02',NULL,'pl',NULL,NULL);
(163,'testSu',NULL,'$2b$10$JGpnCPk9ONMuE3MNLjO8ce7kRElCyq1hdVgteYZlCCczq5kkKiVVG',NULL,'su',NULL,NULL),
(164,'testEu',NULL,'$2b$10$JGpnCPk9ONMuE3MNLjO8ce7kRElCyq1hdVgteYZlCCczq5kkKiVVG',NULL,'eu',NULL,NULL),
(165,'testEr',NULL,'$2b$10$JGpnCPk9ONMuE3MNLjO8ce7kRElCyq1hdVgteYZlCCczq5kkKiVVG',NULL,'er',NULL,NULL),
(166,'testNo',NULL,'$2b$10$JGpnCPk9ONMuE3MNLjO8ce7kRElCyq1hdVgteYZlCCczq5kkKiVVG',NULL,'no',NULL,NULL),
(167,'testPl',NULL,'$2b$10$JGpnCPk9ONMuE3MNLjO8ce7kRElCyq1hdVgteYZlCCczq5kkKiVVG',NULL,'pl',NULL,NULL);
/*!40000 ALTER TABLE `com_users` ENABLE KEYS */;
UNLOCK TABLES;

Expand Down Expand Up @@ -2241,4 +2241,3 @@ INSERT INTO `ref_ref_type` VALUES
(14,'Undefined');
/*!40000 ALTER TABLE `ref_ref_type` ENABLE KEYS */;
UNLOCK TABLES;

Loading