Skip to content
Draft
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
124 changes: 124 additions & 0 deletions backend/src/api-tests/project/create.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { afterAll, beforeAll, beforeEach, describe, expect, it } from '@jest/globals'
import { ProjectDetailsType } from '../../../../frontend/src/shared/types'
import { pool } from '../../utils/db'
import { login, logout, noPermError, resetDatabase, resetDatabaseTimeout, send } from '../utils'
import {
newProjectBasis,
newProjectBasisWithInvalidMembers,
newProjectBasisWithoutCoordinator,
newProjectBasisWithoutMemberArray,
noCoordinatorError,
noMemberArrayError,
} from './data'

let createdProject: ProjectDetailsType | null = null

describe('Creating new project works', () => {
beforeAll(async () => {
await resetDatabase()
}, resetDatabaseTimeout)
beforeEach(async () => {
await login()
})
afterAll(async () => {
await pool.end()
})

it('Request succeeds and returns valid number id', async () => {
const { body: resultBody, status: getReqStatus } = await send<{ pid: number }>('projects/', 'PUT', {
project: newProjectBasis,
})
const { pid: createdId } = resultBody

expect(typeof createdId).toEqual('number')
expect(getReqStatus).toEqual(200)

const { body, status: getReqStat } = await send<ProjectDetailsType>(`project/${createdId}`, 'GET')
expect(getReqStat).toEqual(200)
createdProject = body
})

it('Contains correct data', () => {
const { pid, proj_name, proj_code, proj_status, proj_records } = createdProject!
expect(pid).toBeDefined()
expect(proj_name).toEqual(newProjectBasis.proj_name)
expect(proj_code).toEqual(newProjectBasis.proj_code)
expect(proj_status).toEqual(newProjectBasis.proj_status)
expect(proj_records).toEqual(newProjectBasis.proj_records)
})

it('Creation succeeds with empty member list', async () => {
const { status: getReqStatus } = await send<{ pid: number }>('projects/', 'PUT', {
project: { ...newProjectBasis, now_proj_people: [] },
})

expect(getReqStatus).toEqual(200)
})

it('Creation fails with no member list', async () => {
const { body: resultBody, status: getReqStatus } = await send('projects/', 'PUT', {
project: { ...newProjectBasisWithoutMemberArray },
})

//expect(typeof resultBody).toBe('array')
expect(resultBody).toContainEqual(noMemberArrayError)
expect(getReqStatus).toEqual(403)
})

it('Creation fails with member list containing members with no user ID information', async () => {
const { status: getReqStatus } = await send('projects/', 'PUT', {
project: { ...newProjectBasisWithInvalidMembers },
})

expect(getReqStatus).toEqual(500)
})

it('Creation succeeds with empty member list', async () => {
const { status: getReqStatus } = await send<{ pid: number }>('projects/', 'PUT', {
project: { ...newProjectBasis, now_proj_people: [] },
})

expect(getReqStatus).toEqual(200)
})

it('Creation fails with no coordinator', async () => {
const { body: resultBody, status: getReqStatus } = await send<{ pid: number }>('projects/', 'PUT', {
project: { ...newProjectBasisWithoutCoordinator },
})

//expect(typeof resultBody).toBe('array')
expect(resultBody).toContainEqual(noCoordinatorError)
expect(getReqStatus).toEqual(403)
})

it('Creation fails with coordinator that does not have a matching user', async () => {
const { status: getReqStatus } = await send<{ pid: number }>('projects/', 'PUT', {
project: { ...newProjectBasis, contact: 'NOTEXIST' },
})

expect(getReqStatus).toEqual(500)
})

it('Creation fails for non-admin users', async () => {
logout()
const { body: resultBodyAnon, status: resultStatusAnon } = await send<{ pid: number }>('projects/', 'PUT', {
project: newProjectBasis,
})
expect(resultBodyAnon).toEqual(noPermError)
expect(resultStatusAnon).toEqual(403)

await login('testEr')
const { body: resultBodyEr, status: resultStatusEr } = await send<{ pid: number }>('projects/', 'PUT', {
project: newProjectBasis,
})
expect(resultBodyEr).toEqual(noPermError)
expect(resultStatusEr).toEqual(403)

await login('testEu')
const { body: resultBodyEu, status: resultStatusEu } = await send<{ pid: number }>('projects/', 'PUT', {
project: newProjectBasis,
})
expect(resultBodyEu).toEqual(noPermError)
expect(resultStatusEu).toEqual(403)
})
})
70 changes: 70 additions & 0 deletions backend/src/api-tests/project/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { PersonDetailsType } from '../../../../frontend/src/shared/types'

export const existingPerson: Omit<PersonDetailsType, 'user' | 'project_relations' | 'coordinator_relations'> = {
initials: 'AD',
first_name: 'adf',
surname: 'ads',
full_name: 'adf ads',
format: null,
email: 'email',
user_id: 156,
organization: 'organization',
country: 'Finland',
password_set: new Date('2024-05-22T00:00:00.000Z'),
used_morph: null,
used_now: null,
used_gene: null,
now_user_group: 'eu',
}

export const newProjectBasis = {
proj_name: 'LATER Database',
proj_code: 'LATER',
contact: 'AD',
proj_status: 'current',
proj_records: true,
now_proj_people: [{ pid: 3, initials: 'AD', com_people: existingPerson }],
references: [],
comment: '',
}

export const editedProject = {
...newProjectBasis,
pid: 3,
proj_name: 'edited project name',
proj_code: 'LATER2',
}

export const newProjectBasisWithoutMemberArray = {
proj_name: 'LATER Database',
proj_code: 'LATER',
contact: 'AD',
proj_status: 'current',
proj_records: true,
references: [],
comment: '',
}

export const newProjectBasisWithInvalidMembers = {
proj_name: 'LATER Database',
proj_code: 'LATER',
contact: 'AD',
proj_status: 'current',
proj_records: true,
now_proj_people: [{ pid: 3, initials: 'AD' }],
references: [],
comment: '',
}

export const newProjectBasisWithoutCoordinator = {
proj_name: 'LATER Database',
proj_code: 'LATER',
proj_status: 'current',
proj_records: true,
now_proj_people: [{ pid: 3, initials: 'AD' }],
references: [],
comment: '',
}

export const noMemberArrayError = { name: 'Members', error: 'This field is required' }
export const noCoordinatorError = { name: 'Coordinator', error: 'This field is required' }
69 changes: 40 additions & 29 deletions backend/src/api-tests/project/delete.test.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,76 @@
import { afterAll, beforeAll, describe, expect, it } from '@jest/globals'
import { afterAll, beforeAll, beforeEach, describe, expect, it } from '@jest/globals'
import { login, logout, noPermError, resetDatabase, resetDatabaseTimeout, send } from '../utils'
import { pool } from '../../utils/db'
import { ProjectDetailsType } from '../../../../frontend/src/shared/types'

describe('DELETE /project/:id', () => {
describe('Deleting project works', () => {
beforeAll(async () => {
await resetDatabase()
}, resetDatabaseTimeout)

beforeEach(async () => {
await login()
})

afterAll(async () => {
await pool.end()
})

it('allows admins to delete a project and related data', async () => {
await login('testSu', 'test')

const createdProject = await send<{ pid: number }>('projects', 'POST', {
projectCode: 'DEL-001',
projectName: 'Project To Delete',
coordinatorUserId: 163,
memberUserIds: [167],
})

const deleted = await send(`project/${createdProject.body.pid}`, 'DELETE')
const { body: existingProject, status: getReqStatus } = await send<ProjectDetailsType>('project/14', 'GET')
expect(getReqStatus).toEqual(200)
expect(existingProject.pid).toEqual(14)

const deleted = await send(`project/${existingProject.pid}`, 'DELETE')
expect(deleted.status).toEqual(200)

const fetchAfterDelete = await send(`project/${createdProject.body.pid}`, 'GET')
const fetchAfterDelete = await send(`project/${existingProject.pid}`, 'GET')
expect(fetchAfterDelete.status).toEqual(404)
})

it('rejects deletion attempts from non-admin users', async () => {
it('project members are not deleted along with the project', async () => {
const { body: existingProject, status: getReqStatus } = await send<ProjectDetailsType>('project/3', 'GET')
expect(getReqStatus).toEqual(200)
expect(existingProject.pid).toEqual(3)
expect(existingProject.now_proj_people[0]).toHaveProperty('com_people')

await login('testSu', 'test')
const createdProject = await send<{ pid: number }>('projects', 'POST', {
projectCode: 'DEL-002',
projectName: 'Unauthorized Delete',
coordinatorUserId: 163,
})
const deleted = await send(`project/${existingProject.pid}`, 'DELETE')
expect(deleted.status).toEqual(200)

await login('testPl', 'test')
const plDeleteAttempt = await send(`project/${createdProject.body.pid}`, 'DELETE')
expect(plDeleteAttempt.status).toEqual(403)
expect(plDeleteAttempt.body).toEqual(noPermError)
const memberFetchAfterDelete = await send(
`person/${existingProject.now_proj_people[0].com_people!.initials}`,
'GET'
)
expect(memberFetchAfterDelete.status).toEqual(200)
})

it('rejects deletion attempts from non-admin users', async () => {
const { body: existingProject, status: getReqStatus } = await send<ProjectDetailsType>('project/23', 'GET')
expect(getReqStatus).toEqual(200)
expect(existingProject.pid).toEqual(23)

await login('testEu', 'test')
const euDeleteAttempt = await send(`project/${createdProject.body.pid}`, 'DELETE')
const euDeleteAttempt = await send(`project/${existingProject.pid}`, 'DELETE')
expect(euDeleteAttempt.status).toEqual(403)
expect(euDeleteAttempt.body).toEqual(noPermError)

await login('testEr', 'test')
const erDeleteAttempt = await send(`project/${createdProject.body.pid}`, 'DELETE')
const erDeleteAttempt = await send(`project/${existingProject.pid}`, 'DELETE')
expect(erDeleteAttempt.status).toEqual(403)
expect(erDeleteAttempt.body).toEqual(noPermError)

logout()
const anonDeleteAttempt = await send(`project/${createdProject.body.pid}`, 'DELETE')
const anonDeleteAttempt = await send(`project/${existingProject.pid}`, 'DELETE')
expect(anonDeleteAttempt.status).toEqual(403)
expect(anonDeleteAttempt.body).toEqual(noPermError)

await login('testSu', 'test')
const fetchAfterAttempts = await send(`project/${createdProject.body.pid}`, 'GET')
expect(fetchAfterAttempts.status).toEqual(200)
expect(fetchAfterAttempts.body).toHaveProperty('pid', createdProject.body.pid)
const { body: afterAttemptsBody, status: afterAttemptsStatus } = await send<ProjectDetailsType>(
`project/${existingProject.pid}`,
'GET'
)
expect(afterAttemptsStatus).toEqual(200)
expect(afterAttemptsBody.pid).toEqual(existingProject.pid)
})
})
75 changes: 75 additions & 0 deletions backend/src/api-tests/project/update.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { afterAll, beforeAll, describe, expect, it } from '@jest/globals'
import { login, logout, noPermError, resetDatabase, send } from '../utils'
import { pool } from '../../utils/db'
import { editedProject } from './data'
import { ProjectDetailsType } from '../../../../frontend/src/shared/types'

let existingProject: ProjectDetailsType | null = null

describe('Updating project works', () => {
beforeAll(async () => {
await resetDatabase()
await login()
const { body, status: getReqStat } = await send<ProjectDetailsType>('project/3', 'GET')
expect(getReqStat).toEqual(200)
existingProject = body
})

afterAll(async () => {
await pool.end()
})

it('Request succeeds and returns valid number id', async () => {
const { body: resultBody, status: updateStatus } = await send<{ pid: number }>('projects/', 'PUT', {
project: editedProject,
})
const { pid: updatedId } = resultBody

expect(typeof updatedId).toEqual('number')
expect(updateStatus).toEqual(200)

const { body, status: getReqStat } = await send<ProjectDetailsType>(`project/${updatedId}`, 'GET')
expect(getReqStat).toEqual(200)
existingProject = body
})

it('Contains correct data', () => {
const { proj_code, proj_name } = existingProject!
expect(proj_code).toEqual(editedProject.proj_code)
expect(proj_name).toEqual(editedProject.proj_name)
})

it('Updating fails with empty project code', async () => {
const { body: resultBody, status: getReqStatus } = await send('projects/', 'PUT', {
project: { ...editedProject, proj_code: '' },
})
expect(getReqStatus).toEqual(403)
expect(resultBody.length).toEqual(1) //There should be 1 validation error
})

it('Updating succeeds fails for non-admin users', async () => {
await login('testEr')
const { body: resultBodyEr, status: resultStatusEr } = await send<{ pid: number }>('projects/', 'PUT', {
project: { ...editedProject, proj_code: 'CODE2' },
})
expect(resultBodyEr).toEqual(noPermError)
expect(resultStatusEr).toEqual(403)

await login('testEu')
const { body: resultBodyEu, status: resultStatusEu } = await send<{ pid: number }>('projects/', 'PUT', {
project: { ...editedProject, proj_code: 'CODE3' },
})
expect(resultBodyEu).toEqual(noPermError)
expect(resultStatusEu).toEqual(403)

logout()
const { body: resultBodyAnon, status: resultStatusAnon } = await send('projects/', 'PUT', {
project: { ...editedProject, proj_code: 'CODE4' },
})
expect(resultBodyAnon).toEqual(noPermError)
expect(resultStatusAnon).toEqual(403)
})

it.todo('Adding members works')
it.todo('Removing members works')
})
Loading
Loading