Skip to content

Commit 64b668e

Browse files
committed
api test for entry edition
1 parent 38a420d commit 64b668e

File tree

2 files changed

+151
-5
lines changed

2 files changed

+151
-5
lines changed

src/server/routes/entry.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,10 @@ entryRouter.put('/:entryId', async (req: RequestWithUser, res: any) => {
116116
return res.status(500).send('Error when calculating risks')
117117
}
118118

119-
// Preserve history - store current version in updatedData array
120119
const currentData = entry.data
121120
const updatedDataArray = currentData.updatedData ?? []
122121

123122
// Store the current state with the timestamp showing when this version was last active
124-
// Use the current updatedAt (or createdAt if never updated) to show when this version was last modified
125123
const previousVersionTimestamp = entry.updatedAt?.toISOString() ?? entry.createdAt.toISOString()
126124

127125
updatedDataArray.push({
@@ -132,7 +130,6 @@ entryRouter.put('/:entryId', async (req: RequestWithUser, res: any) => {
132130
createdAt: previousVersionTimestamp,
133131
})
134132

135-
// Update entry with new data (updatedAt will be automatically set by Sequelize to current time)
136133
await entry.update({
137134
data: {
138135
...riskData,

tests/api.spec.ts

Lines changed: 151 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { test, expect } from '@playwright/test'
22
import { testUser, riskResponse, compareUnordered, compareOrdered } from './helpers'
33

4+
const baseUrl = 'http://localhost:8000'
5+
46
test.describe.configure({ mode: 'serial' })
57

68
test.describe('api', () => {
@@ -32,7 +34,7 @@ test.describe('api', () => {
3234
tuhatData: {},
3335
}
3436

35-
const response = await fetch('http://localhost:8000/api/entries/1/dryrun', {
37+
const response = await fetch(`${baseUrl}/api/entries/1/dryrun`, {
3638
method: 'POST',
3739
headers: { 'Content-Type': 'application/json' },
3840
body: JSON.stringify(payload),
@@ -85,7 +87,7 @@ test.describe('api', () => {
8587
tuhatData: {},
8688
}
8789

88-
const response = await fetch('http://localhost:8000/api/entries/1/dryrun', {
90+
const response = await fetch(`${baseUrl}/api/entries/1/dryrun`, {
8991
method: 'POST',
9092
headers: { 'Content-Type': 'application/json' },
9193
body: JSON.stringify(payload),
@@ -148,4 +150,151 @@ test.describe('api', () => {
148150
expect(data.country[0].code).toStrictEqual('AF')
149151
compareOrdered(data.multilateralCountries, expectedCountries)
150152
})
153+
154+
test('editing an entry', async () => {
155+
const initialPayload = {
156+
data: {
157+
'1': 'Testi Kayttaja',
158+
'2': testUser,
159+
'3': 'Initial Project',
160+
'4': 'bilateral',
161+
'6': 'university',
162+
'8': 'Afghanistan',
163+
'9': 'partner',
164+
'10': 'agreementNotDone',
165+
'11': ['education'],
166+
'12': 'mediumDuration',
167+
'13': 'noExternalFunding',
168+
'16': 'mediumBudget',
169+
'17': 'noTransferPersonalData',
170+
'20': 'Kardan University',
171+
'23': 'noTransferMilitaryKnowledge',
172+
'24': 'noSuccessfulCollaboration',
173+
'25': 'likelyNoEthicalIssues',
174+
faculty: 'H40',
175+
unit: 'H528',
176+
tuhatProjectExists: 'tuhatOptionNegative',
177+
},
178+
sessionToken: 'e1e841e1-1368-4deb-b9f9-227ab1261e64',
179+
tuhatData: {},
180+
}
181+
182+
const createResponse = await fetch(`${baseUrl}/api/entries/1`, {
183+
method: 'POST',
184+
headers: { 'Content-Type': 'application/json' },
185+
body: JSON.stringify(initialPayload),
186+
})
187+
188+
expect(createResponse.status).toBe(201)
189+
const createdEntry = await createResponse.json()
190+
191+
let updatedPayload = {
192+
data: {
193+
'1': 'Testi Kayttaja',
194+
'2': testUser,
195+
'3': 'Updated Project Name',
196+
'4': 'bilateral',
197+
'6': 'university',
198+
'8': 'Sweden',
199+
'9': 'coordinator',
200+
'10': 'agreementDone',
201+
'11': ['education', 'research'],
202+
'12': 'longDuration',
203+
'13': 'externalFunding',
204+
'16': 'largeBudget',
205+
'17': 'noTransferPersonalData',
206+
'20': 'Ahlobait University',
207+
'23': 'noTransferMilitaryKnowledge',
208+
'24': 'successfulCollaboration',
209+
'25': 'likelyNoEthicalIssues',
210+
faculty: 'H40',
211+
unit: 'H528',
212+
tuhatProjectExists: 'tuhatOptionNegative',
213+
},
214+
tuhatData: {},
215+
}
216+
217+
let updateResponse = await fetch(`${baseUrl}/api/entries/${createdEntry.id}`, {
218+
method: 'PUT',
219+
headers: { 'Content-Type': 'application/json' },
220+
body: JSON.stringify(updatedPayload),
221+
})
222+
223+
expect(updateResponse.status).toBe(200)
224+
225+
let updatedEntry = await updateResponse.json()
226+
227+
let expectedRisks = riskResponse([
228+
{ id: 'country', level: 1 },
229+
{ id: 'dualUseEU', level: 1 },
230+
{ id: 'economic', level: 3 },
231+
{ id: 'economicScope', level: 3 },
232+
{ id: 'ethical', level: 1 },
233+
{ id: 'total', level: 2 },
234+
{ id: 'university', level: 1 },
235+
])
236+
237+
compareUnordered(updatedEntry.data.risks, expectedRisks)
238+
239+
expect(updatedEntry.data.country[0].code).toBe('SE')
240+
241+
expect(updatedEntry.data.updatedData).toBeDefined()
242+
expect(updatedEntry.data.updatedData.length).toBe(1)
243+
expect(updatedEntry.data.updatedData[0].country[0].code).toBe('AF')
244+
245+
updatedPayload = {
246+
data: {
247+
'1': 'Testi Kayttaja',
248+
'2': testUser,
249+
'3': 'Updated Project Name',
250+
'4': 'bilateral',
251+
'6': 'university',
252+
'8': 'Denmark',
253+
'9': 'coordinator',
254+
'10': 'agreementDone',
255+
'11': ['education', 'research'],
256+
'12': 'shortDuration',
257+
'13': 'noExternalFunding',
258+
'16': 'smallBudget',
259+
'17': 'noTransferPersonalData',
260+
'20': 'Ahlobait University',
261+
'23': 'noTransferMilitaryKnowledge',
262+
'24': 'successfulCollaboration',
263+
'25': 'likelyNoEthicalIssues',
264+
faculty: 'H40',
265+
unit: 'H528',
266+
tuhatProjectExists: 'tuhatOptionNegative',
267+
},
268+
tuhatData: {},
269+
}
270+
271+
updateResponse = await fetch(`${baseUrl}/api/entries/${createdEntry.id}`, {
272+
method: 'PUT',
273+
headers: { 'Content-Type': 'application/json' },
274+
body: JSON.stringify(updatedPayload),
275+
})
276+
277+
expect(updateResponse.status).toBe(200)
278+
279+
updatedEntry = await updateResponse.json()
280+
281+
expectedRisks = riskResponse([
282+
{ id: 'country', level: 1 },
283+
{ id: 'dualUseEU', level: 1 },
284+
{ id: 'economic', level: 1 },
285+
{ id: 'economicScope', level: 1 },
286+
{ id: 'ethical', level: 1 },
287+
{ id: 'total', level: 1 },
288+
{ id: 'university', level: 1 },
289+
])
290+
291+
compareUnordered(updatedEntry.data.risks, expectedRisks)
292+
293+
expect(updatedEntry.data.country[0].code).toBe('DK')
294+
295+
expect(updatedEntry.data.updatedData).toBeDefined()
296+
expect(updatedEntry.data.updatedData.length).toBe(2)
297+
expect(updatedEntry.data.updatedData[0].country[0].code).toBe('AF')
298+
expect(updatedEntry.data.updatedData[1].country[0].code).toBe('SE')
299+
})
151300
})

0 commit comments

Comments
 (0)