Skip to content

Commit b6750f3

Browse files
committed
tests: #WB-3279, better tests on positions
1 parent ee3579b commit b6750f3

File tree

2 files changed

+76
-41
lines changed

2 files changed

+76
-41
lines changed

tests/src/test/js/it/scenarios/position/attribute-position.js

+63-30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { check } from "k6";
22
import chai, { describe } from "https://jslib.k6.io/k6chaijs/4.3.4.2/index.js";
33
import {
44
authenticateWeb,
5-
assertOk,
65
makeAdml,
76
createPositionOrFail,
87
getUsersOfSchool,
@@ -14,8 +13,9 @@ import {
1413
getUserProfileOrFail,
1514
attachStructureAsChild,
1615
getAdmlsOrMakThem,
17-
checkReturnCode
18-
} from "https://raw.githubusercontent.com/edificeio/edifice-k6-commons/develop/dist/index.js";
16+
checkReturnCode,
17+
switchSession
18+
} from "../../../commons/index.js"; //"https://raw.githubusercontent.com/edificeio/edifice-k6-commons/develop/dist/index.js";
1919

2020

2121
chai.config.logFailures = true;
@@ -96,7 +96,8 @@ export function testAttributePositions({structures, admls, positions, headAdml }
9696
const [structure1, structure2] = structures
9797
const [position1, position2] = positions
9898
describe("[Position-Attribute] Attribute positions to users", () => {
99-
let session = authenticateWeb(__ENV.ADMC_LOGIN, __ENV.ADMC_PASSWORD)
99+
const admcSession = authenticateWeb(__ENV.ADMC_LOGIN, __ENV.ADMC_PASSWORD)
100+
let session;
100101
const users1 = getUsersOfSchool(structure1, session)
101102
const teacher1 = getRandomUserWithProfile(users1, 'Teacher', [adml1, headAdml]);
102103
const relative1 = getRandomUserWithProfile(users1, 'Relative', [adml1, headAdml]);
@@ -123,17 +124,17 @@ export function testAttributePositions({structures, admls, positions, headAdml }
123124
const [user, label] = profile
124125
let returnCode;
125126
if(user) {
126-
session = authenticateWeb(teacher1.login)
127-
returnCode = 401;
127+
session = authenticateWeb(user.login)
128+
returnCode = 200;
128129
} else {
129130
session = null
130131
logout();
131132
returnCode = 302
132133
}
133134
describe(`${label} attributes position to a teacher`, () => {
134-
checkReturnCode(attributePositions(teacher1, [position1], session), `${label} should not be able to attribute a position to a teacher`, returnCode);
135-
checkReturnCode(attributePositions(relative1, [position1], session), `${label} should not be able to attribute a position to a relative`, returnCode);
136-
checkReturnCode(attributePositions(student1, [position1], session), `${label} should not be able to attribute a position to a student`, returnCode);
135+
tryToAssignNewPositionAndCheckUserPositionsRemainUnchanged(teacher1, [position1], label, 'teacher', returnCode, session, admcSession);
136+
tryToAssignNewPositionAndCheckUserPositionsRemainUnchanged(relative1, [position1], label, 'relative', returnCode, session, admcSession);
137+
tryToAssignNewPositionAndCheckUserPositionsRemainUnchanged(student1, [position1], label, 'student', returnCode, session, admcSession);
137138
})
138139
}
139140

@@ -142,35 +143,28 @@ export function testAttributePositions({structures, admls, positions, headAdml }
142143
// Try to attribute position
143144
// from another structure
144145
describe("ADML attributes position to a user in another structure", () => {
145-
checkReturnCode(attributePositions(teacher1, [position2], session), `ADML should not be able to attribute a position from another structure to a teacher`, 401);
146-
checkReturnCode(attributePositions(relative1, [position2], session), `ADML should not be able to attribute a position from another structure to a relative`, 401);
147-
checkReturnCode(attributePositions(student1, [position2], session), `ADML should not be able to attribute a position from another structure to a student`, 401);
146+
tryToAssignNewPositionAndCheckUserPositionsRemainUnchanged(teacher1, [position2], 'ADML', 'teacher', 200, session, admcSession);
147+
tryToAssignNewPositionAndCheckUserPositionsRemainUnchanged(relative1, [position2], 'ADML', 'relative', 200, session, admcSession);
148+
tryToAssignNewPositionAndCheckUserPositionsRemainUnchanged(student1, [position2], 'ADML', 'student', 200, session, admcSession);
148149
})
149-
//////////////////////////////
150-
// Try to attribute position
151-
// from their structure
152150
describe("ADML attributes position to a user in the administered structure", () => {
153-
assertOk(attributePositions(teacher1, [position1], session), `ADML should be able to attribute a position to a teacher`);
154-
checkReturnCode(attributePositions(relative1, [position1], session), `ADML should not be able to attribute a position to a relative`, 401);
155-
checkReturnCode(attributePositions(student1, [position1], session), `ADML should not be able to attribute a position to a student`, 401);
151+
assignNewPositionAndCheckThatItSucceeded(teacher1, [position1], 'ADML', 'teacher', session, admcSession);
152+
tryToAssignNewPositionAndCheckUserPositionsRemainUnchanged(relative1, [position1], 'ADML', 'relative', 200, session, admcSession);
153+
tryToAssignNewPositionAndCheckUserPositionsRemainUnchanged(student1, [position1], 'ADML', 'student', 200, session, admcSession);
156154
})
157155

158156
session = authenticateWeb(__ENV.ADMC_LOGIN, __ENV.ADMC_PASSWORD)
159157

160158
describe("ADMC attributes position to a user in another structure", () => {
161-
//////////////////////////////
162-
// Try to attribute position
163-
// from another structure
164-
checkReturnCode(attributePositions(teacher1, [position2], session), `ADMC should not be able to attribute a position from another structure to a teacher`, 401);
165-
checkReturnCode(attributePositions(relative1, [position2], session), `ADMC should not be able to attribute a position from another structure to a relative`, 401);
166-
checkReturnCode(attributePositions(student1, [position2], session), `ADMC should not be able to attribute a position from another structure to a student`, 401);
159+
tryToAssignNewPositionAndCheckUserPositionsRemainUnchanged(teacher1, [position2], 'ADMC', 'teacher', 200, session, admcSession);
160+
tryToAssignNewPositionAndCheckUserPositionsRemainUnchanged(relative1, [position2], 'ADMC', 'relative', 200, session, admcSession);
161+
tryToAssignNewPositionAndCheckUserPositionsRemainUnchanged(student1, [position2], 'ADMC', 'student', 200, session, admcSession);
162+
})
167163

168-
//////////////////////////////
169-
// Try to attribute position
170-
// from their structure
171-
assertOk(attributePositions(teacher2, [position2], session), `ADMC should be able to attribute a position to a teacher`);
172-
checkReturnCode(attributePositions(relative2, [position2], session), `ADMC should not be able to attribute a position to a relative`, 401);
173-
checkReturnCode(attributePositions(student2, [position2], session), `ADMC should not be able to attribute a position to a student`, 401);
164+
describe("ADMC attributes position to a user in their structure", () => {
165+
assignNewPositionAndCheckThatItSucceeded(teacher2, [position2], 'ADMC', 'teacher', session, admcSession);
166+
tryToAssignNewPositionAndCheckUserPositionsRemainUnchanged(relative2, [position2], 'ADMC', 'relative', 200, session, admcSession);
167+
tryToAssignNewPositionAndCheckUserPositionsRemainUnchanged(student2, [position2], 'ADMC', 'student', 200, session, admcSession);
174168
})
175169

176170
describe("Ability to retrieve a user position", () => {
@@ -198,3 +192,42 @@ export function testAttributePositions({structures, admls, positions, headAdml }
198192
})
199193
})
200194
};
195+
196+
function assignNewPositionAndCheckThatItSucceeded(user, positions, requesterType, userType, session, admcSession) {
197+
checkReturnCode(attributePositions(user, positions, session), `${requesterType} call to attribute a position to a ${userType} should end with 200`, 200);
198+
const newUserPositions = (getUserProfileOrFail(user.id, admcSession).userPositions || []);
199+
const attributedPositions = positions.map(p => p.id)
200+
const checks = {}
201+
checks[`${requesterType} should be able to attribute new positions to a ${userType}`] = () => {
202+
const missingPositions = attributedPositions.filter(attPos => newUserPositions.indexOf(attPos) < 0);
203+
const ok = missingPositions.length === 0;
204+
if(!ok) {
205+
console.error(`${requesterType} should have been able to attribute all positions to a ${userType}, ${missingPositions.length}/${positions.length} where not added : ${missingPositions}`)
206+
}
207+
};
208+
return check(newUserPositions, checks);
209+
}
210+
211+
function tryToAssignNewPositionAndCheckUserPositionsRemainUnchanged(user, positions, requesterType, userType, returnCode, session, admcSession) {
212+
switchSession(admcSession);
213+
const oldUserPositions = (getUserProfileOrFail(user.id, admcSession).userPositions || []);
214+
switchSession(session);
215+
checkReturnCode(attributePositions(user, positions, session), `${requesterType} call to attribute a position to a ${userType} should end with ${returnCode}`, returnCode);
216+
switchSession(admcSession);
217+
const newUserPositions = (getUserProfileOrFail(user.id, admcSession).userPositions || []);
218+
switchSession(session);
219+
const checks = {}
220+
checks[`${requesterType} should not be able to attribute a position to a ${userType}`] = () => {
221+
let ok;
222+
if(oldUserPositions.length === newUserPositions.length) {
223+
ok = newUserPositions.filter(newPos => oldUserPositions.indexOf(newPos) < 0).length === 0;
224+
} else {
225+
ok = false;
226+
}
227+
if(!ok) {
228+
console.error(`${requesterType} should not be able to attribute a position to a ${userType}, expecting `, oldUserPositions, ` but got `, newUserPositions,` when adding `, positions)
229+
}
230+
return ok;
231+
};
232+
return check(newUserPositions, checks);
233+
}

tests/src/test/js/it/scenarios/position/crud.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
createEmptyStructure,
1313
initStructure,
1414
logout,
15-
activateUsers,
1615
deletePosition,
1716
getRandomUserWithProfile,
1817
attachStructureAsChild,
@@ -141,13 +140,12 @@ export function testCreatePosition({structure, adml, structureTree}) {
141140
res = createPosition(`${positionName} - bis`, structure, session);
142141
assertCondition(() => res.status === 201, "An ADML user should be able to create a position");
143142
positions.push(JSON.parse(res.body));
144-
145-
assertSearchCriteriaOnlyContainThesePositions(positions, "ADML with a structure with these positions", session);
146-
143+
//assertSearchCriteriaContainSpecifiedPositionsAndNotOther(positions, p => p.structureId !== structure.id, "ADML with a structure with these positions", session);
147144
session = authenticateWeb(adml1.login)
145+
148146
res = createPosition(`${positionName}-ADML2`, structure, session);
149147
assertCondition(() => res.status === 401, "An ADML of another structure should not be able to create a position");
150-
assertSearchCriteriaOnlyContainThesePositions([], "ADML in a structure without these positions", session);
148+
assertSearchCriteriaContainSpecifiedPositionsAndNotOther([], p => p.structureId !== structure.id, "ADML in a structure without these positions", session);
151149

152150
// An ADMC should be able to create a position
153151
session = authenticateWeb(__ENV.ADMC_LOGIN, __ENV.ADMC_PASSWORD);
@@ -166,7 +164,7 @@ export function testCreatePosition({structure, adml, structureTree}) {
166164
session = authenticateWeb(adml2.login);
167165
positions.push(createPositionOrFail(`${positionName}-ADML2-0`, structure2, session));
168166
session = authenticateWeb(headAdml.login);
169-
assertSearchCriteriaOnlyContainThesePositions(positions, "ADML of multiple structures", session);
167+
assertSearchCriteriaContainSpecifiedPositionsAndNotOther(positions, p => p.structureId === structure1.id || p.structureId === structure2.id, "ADML of multiple structures", session);
170168
})
171169
};
172170
/**
@@ -347,14 +345,18 @@ function noDuplicates(positions) {
347345
return positions.length === actualIds.size;
348346
}
349347

350-
function assertSearchCriteriaOnlyContainThesePositions(expected, userType) {
348+
function assertSearchCriteriaContainSpecifiedPositionsAndNotOther(structureId, expected, unwantedPredicate, userType) {
351349
describe(userType, () => {
352350
const criteria = getSearchCriteria();
353351
const criteriaPositions = criteria.positions || [];
354352
const actualIds = criteriaPositions.map(e => e.id)
355-
const checks = {}
356-
checks[`should have the same number of positions as expected`] = actual => actual.length === expected.length
357-
checks[`should contain all expected positions`] = () => expected.filter(exp => actualIds.indexOf(exp.id)) >= 0
358-
check(criteriaPositions, checks);
353+
const ok = check(criteriaPositions, {
354+
'should contain all expected positions' : () => expected.filter(exp => actualIds.indexOf(exp.id) < 0).length === 0,
355+
'should contain none of the unwanted positions' : () => actualIds.filter(act => unwantedPredicate({id: act.id})).length >= 0
356+
});
357+
if(!ok) {
358+
console.warn("actualIds", actualIds)
359+
console.warn('Expecting positions\n', expected, '\ngot\n', criteriaPositions)
360+
}
359361
})
360362
}

0 commit comments

Comments
 (0)