From 3c323824478ecfef0fd980e4451a062a5282caf3 Mon Sep 17 00:00:00 2001 From: juhanikat Date: Wed, 27 May 2026 14:42:55 +0300 Subject: [PATCH 1/3] Remove species test from root directory, and update the corresponding test in api-tests --- backend/src/api-tests/species/create.test.ts | 81 ++++++++--------- src/api-tests/species/create.test.ts | 94 -------------------- 2 files changed, 36 insertions(+), 139 deletions(-) delete mode 100644 src/api-tests/species/create.test.ts diff --git a/backend/src/api-tests/species/create.test.ts b/backend/src/api-tests/species/create.test.ts index fb35378ac..2843ee3b2 100644 --- a/backend/src/api-tests/species/create.test.ts +++ b/backend/src/api-tests/species/create.test.ts @@ -12,56 +12,48 @@ describe('Creating new species works', () => { await resetDatabase() }, resetDatabaseTimeout) beforeEach(async () => { - await resetDatabase() await login() - createdSpecies = null - }, resetDatabaseTimeout) + }) afterAll(async () => { await pool.end() }) - describe('after creating a species successfully', () => { - beforeEach(async () => { - const { body: resultBody } = await send<{ species_id: number }>('species', 'PUT', { - species: { ...newSpeciesBasis, comment: 'species test' }, - }) - const { species_id: createdId } = resultBody - - expect(typeof createdId).toEqual('number') // `Invalid result returned on write: ${createdId}` - - const { body } = await send(`species/${createdId}`, 'GET') - createdSpecies = body + it('Request succeeds and returns valid number id', async () => { + const { body: resultBody } = await send<{ species_id: number }>('species', 'PUT', { + species: { ...newSpeciesBasis, comment: 'species test' }, }) + const { species_id: createdId } = resultBody - it('Request succeeds and returns valid number id', () => { - expect(typeof createdSpecies?.species_id).toEqual('number') - }) + expect(typeof createdId).toEqual('number') // `Invalid result returned on write: ${createdId}` - it('Contains correct data', () => { - const { species_name, now_ls } = createdSpecies! - expect(species_name).toEqual(newSpeciesBasis.species_name) // 'Name is different' - const locality = now_ls.find(ls => ls.now_loc.lid === 24750) - expect(!!locality).toEqual(true) // 'Locality in locality-species not found' - }) + const { body } = await send(`species/${createdId}`, 'GET') + createdSpecies = body + }) - it('Locality-species change was updated also to locality', async () => { - const localityFound = createdSpecies!.now_ls.find(ls => ls.now_loc.loc_name.startsWith('Romany')) - if (!localityFound) throw new Error('Locality was not found in now_ls') - const speciesResult = await send(`locality/24750`, 'GET') - const update = speciesResult.body.now_lau.find(lau => lau.lid === 24750 && lau.lau_comment === 'species test') - if (!update) throw new Error('Update not found') - const logRows = update.updates - const expectedLogRows: Partial[] = [ - { - oldValue: null, - value: '24750', - type: 'add', - column: 'lid', - table: 'now_ls', - }, - ] - testLogRows(logRows, expectedLogRows, 2) - }) + it('Contains correct data', () => { + const { species_name, now_ls } = createdSpecies! + expect(species_name).toEqual(newSpeciesBasis.species_name) // 'Name is different' + const locality = now_ls.find(ls => ls.now_loc.lid === 24750) + expect(!!locality).toEqual(true) // 'Locality in locality-species not found' + }) + + it('Locality-species change was updated also to locality', async () => { + const localityFound = createdSpecies!.now_ls.find(ls => ls.now_loc.loc_name.startsWith('Romany')) + if (!localityFound) throw new Error('Locality was not found in now_ls') + const speciesResult = await send(`locality/24750`, 'GET') + const update = speciesResult.body.now_lau.find(lau => lau.lid === 24750 && lau.lau_comment === 'species test') + if (!update) throw new Error('Update not found') + const logRows = update.updates + const expectedLogRows: Partial[] = [ + { + oldValue: null, + value: '24750', + type: 'add', + column: 'lid', + table: 'now_ls', + }, + ] + testLogRows(logRows, expectedLogRows, 2) }) it('Species without required fields fails', async () => { @@ -72,10 +64,11 @@ describe('Creating new species works', () => { }) it('Creation fails without reference', async () => { + await resetDatabase() const resultNoRef = await send('species', 'PUT', { species: { ...newSpeciesBasis, references: [] }, }) - expect(resultNoRef.status).toEqual(403) // can't create one without a reference + expect(resultNoRef.status).toEqual(403) // can't create species without a reference const resultWithRef = await send('species', 'PUT', { species: { ...newSpeciesBasis }, @@ -83,16 +76,14 @@ describe('Creating new species works', () => { expect(resultWithRef.status).toEqual(200) }) - it('Creation fails without permissions for non-authenticated users', async () => { + it('Creation fails without permissions for non-authenticated and non-privileged users', async () => { logout() const result1 = await send('species', 'PUT', { species: { ...newSpeciesBasis, comment: 'species test' }, }) expect(result1.body).toEqual(noPermError) expect(result1.status).toEqual(403) - }) - it('Creation succeeds for EditRestricted users', async () => { logout() await login('testEr', 'test') const result2 = await send('species', 'PUT', { diff --git a/src/api-tests/species/create.test.ts b/src/api-tests/species/create.test.ts deleted file mode 100644 index d571d5e0a..000000000 --- a/src/api-tests/species/create.test.ts +++ /dev/null @@ -1,94 +0,0 @@ -import { beforeEach, beforeAll, afterAll, describe, it, expect } from '@jest/globals' -import { LocalityDetailsType, SpeciesDetailsType } from '../../../../frontend/src/shared/types' -import { LogRow } from '../../services/write/writeOperations/types' -import { newSpeciesBasis, newSpeciesWithoutRequiredFields } from './data' -import { login, logout, resetDatabase, send, testLogRows, resetDatabaseTimeout, noPermError } from '../utils' -import { pool } from '../../utils/db' - -let createdSpecies: SpeciesDetailsType | null = null - -describe('Creating new species 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 } = await send<{ species_id: number }>('species', 'PUT', { - species: { ...newSpeciesBasis, comment: 'species test' }, - }) - const { species_id: createdId } = resultBody - - expect(typeof createdId).toEqual('number') // `Invalid result returned on write: ${createdId}` - - const { body } = await send(`species/${createdId}`, 'GET') - createdSpecies = body - }) - - it('Contains correct data', () => { - const { species_name, now_ls } = createdSpecies! - expect(species_name).toEqual(newSpeciesBasis.species_name) // 'Name is different' - const locality = now_ls.find(ls => ls.now_loc.lid === 24750) - expect(!!locality).toEqual(true) // 'Locality in locality-species not found' - }) - - it('Locality-species change was updated also to locality', async () => { - const localityFound = createdSpecies!.now_ls.find(ls => ls.now_loc.loc_name.startsWith('Romany')) - if (!localityFound) throw new Error('Locality was not found in now_ls') - const speciesResult = await send(`locality/24750`, 'GET') - const update = speciesResult.body.now_lau.find(lau => lau.lid === 24750 && lau.lau_comment === 'species test') - if (!update) throw new Error('Update not found') - const logRows = update.updates - const expectedLogRows: Partial[] = [ - { - oldValue: null, - value: '24750', - type: 'add', - column: 'lid', - table: 'now_ls', - }, - ] - testLogRows(logRows, expectedLogRows, 2) - }) - - it('Species without required fields fails', async () => { - const res = await send('species', 'PUT', { - species: { ...newSpeciesWithoutRequiredFields, comment: 'species test' }, - }) - expect(res.status).toEqual(403) - }) - - it('Creation fails without reference', async () => { - const resultNoRef = await send('species', 'PUT', { - species: { ...newSpeciesBasis, references: [] }, - }) - expect(resultNoRef.status).toEqual(403) // can't create one without a reference - - const resultWithRef = await send('species', 'PUT', { - species: { ...newSpeciesBasis }, - }) - expect(resultWithRef.status).toEqual(200) - }) - - it('Creation fails without permissions for non-authenticated and non-privileged users', async () => { - logout() - const result1 = await send('species', 'PUT', { - species: { ...newSpeciesBasis, comment: 'species test' }, - }) - expect(result1.body).toEqual(noPermError) - expect(result1.status).toEqual(403) - - logout() - await login('testEr', 'test') - const result2 = await send('species', 'PUT', { - species: { ...newSpeciesBasis, comment: 'species test' }, - }) - expect(result2.body).toEqual(noPermError) - expect(result2.status).toEqual(403) - }) -}) \ No newline at end of file From c889b834faf4402b47530618e6e5e352cf49d877 Mon Sep 17 00:00:00 2001 From: juhanikat Date: Tue, 9 Jun 2026 13:34:20 +0300 Subject: [PATCH 2/3] fix permission test --- backend/src/api-tests/species/create.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/src/api-tests/species/create.test.ts b/backend/src/api-tests/species/create.test.ts index 2843ee3b2..8983d57a9 100644 --- a/backend/src/api-tests/species/create.test.ts +++ b/backend/src/api-tests/species/create.test.ts @@ -78,8 +78,9 @@ describe('Creating new species works', () => { it('Creation fails without permissions for non-authenticated and non-privileged users', async () => { logout() + const newSpeciesBasis2 = { ...newSpeciesBasis, species_name: 'aspecies' } const result1 = await send('species', 'PUT', { - species: { ...newSpeciesBasis, comment: 'species test' }, + species: { ...newSpeciesBasis2, comment: 'species test' }, }) expect(result1.body).toEqual(noPermError) expect(result1.status).toEqual(403) @@ -87,7 +88,7 @@ describe('Creating new species works', () => { logout() await login('testEr', 'test') const result2 = await send('species', 'PUT', { - species: { ...newSpeciesBasis, comment: 'species test' }, + species: { ...newSpeciesBasis2, comment: 'species edit test' }, }) expect(result2.status).toEqual(200) }) From e50a540db2d4b55c123a695a50e3850ba7136f35 Mon Sep 17 00:00:00 2001 From: juhanikat Date: Mon, 15 Jun 2026 12:29:30 +0300 Subject: [PATCH 3/3] update species tests --- backend/src/api-tests/species/create.test.ts | 25 ++++++++++---------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/backend/src/api-tests/species/create.test.ts b/backend/src/api-tests/species/create.test.ts index 8983d57a9..22a43f755 100644 --- a/backend/src/api-tests/species/create.test.ts +++ b/backend/src/api-tests/species/create.test.ts @@ -64,32 +64,33 @@ describe('Creating new species works', () => { }) it('Creation fails without reference', async () => { - await resetDatabase() + const speciesWithoutRef = { ...newSpeciesBasis, species_name: 'anotherspecies', references: [] } const resultNoRef = await send('species', 'PUT', { - species: { ...newSpeciesBasis, references: [] }, + species: { ...speciesWithoutRef }, }) expect(resultNoRef.status).toEqual(403) // can't create species without a reference + const speciesWithRef = { ...newSpeciesBasis, species_name: 'anotherspecies' } const resultWithRef = await send('species', 'PUT', { - species: { ...newSpeciesBasis }, + species: { ...speciesWithRef }, }) expect(resultWithRef.status).toEqual(200) }) - it('Creation fails without permissions for non-authenticated and non-privileged users', async () => { + it('Creation fails for unauthenticated users and succeeds for EditRestricted users', async () => { logout() - const newSpeciesBasis2 = { ...newSpeciesBasis, species_name: 'aspecies' } - const result1 = await send('species', 'PUT', { - species: { ...newSpeciesBasis2, comment: 'species test' }, + const newSpeciesBasis3 = { ...newSpeciesBasis, species_name: 'yetanotherspecies' } + const resultNoAuth = await send('species', 'PUT', { + species: { ...newSpeciesBasis3, comment: 'species test' }, }) - expect(result1.body).toEqual(noPermError) - expect(result1.status).toEqual(403) + expect(resultNoAuth.body).toEqual(noPermError) + expect(resultNoAuth.status).toEqual(403) logout() await login('testEr', 'test') - const result2 = await send('species', 'PUT', { - species: { ...newSpeciesBasis2, comment: 'species edit test' }, + const resultEr = await send('species', 'PUT', { + species: { ...newSpeciesBasis3, comment: 'species edit test' }, }) - expect(result2.status).toEqual(200) + expect(resultEr.status).toEqual(200) }) })