Skip to content

Commit 4bd6a25

Browse files
committed
[backend] tests (#13901)
1 parent 8432e44 commit 4bd6a25

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

opencti-platform/opencti-graphql/src/domain/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ const loadUserToUpdateWithAccessCheck = async (context, user, userId) => {
534534
// Check in an organization admin edits a user that's not in its administrated organizations
535535
if (isOnlyOrgaAdmin(user)) {
536536
const myAdministratedOrganizationsIds = user.administrated_organizations.map((orga) => orga.id);
537-
if (!userToUpdate[RELATION_PARTICIPATE_TO].find((orga) => myAdministratedOrganizationsIds.includes(orga))) {
537+
if (!userToUpdate[RELATION_PARTICIPATE_TO]?.find((orga) => myAdministratedOrganizationsIds.includes(orga))) {
538538
throw ForbiddenAccess();
539539
}
540540
} else {

opencti-platform/opencti-graphql/tests/03-integration/02-resolvers/user-test.ts

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import {
1919
TESTING_USERS,
2020
USER_CONNECTOR,
2121
USER_DISINFORMATION_ANALYST,
22-
USER_EDITOR
22+
USER_EDITOR,
23+
USER_SECURITY,
2324
} from '../../utils/testQuery';
2425
import { ENTITY_TYPE_IDENTITY_ORGANIZATION } from '../../../src/modules/organization/organization-types';
2526
import { VIRTUAL_ORGANIZATION_ADMIN } from '../../../src/utils/access';
@@ -31,7 +32,7 @@ import {
3132
queryAsAdminWithSuccess,
3233
queryAsUserIsExpectedError,
3334
queryAsUserIsExpectedForbidden,
34-
queryAsUserWithSuccess
35+
queryAsUserWithSuccess,
3536
} from '../../utils/testQueryHelper';
3637
import { OPENCTI_ADMIN_UUID } from '../../../src/schema/general';
3738
import type { Capability, Member, UserAddInput } from '../../../src/generated/graphql';
@@ -307,7 +308,7 @@ describe('User resolver standard behavior', () => {
307308
user_confidence_level: {
308309
max_confidence: 50,
309310
overrides: [{ entity_type: 'Report', max_confidence: 80 }],
310-
}
311+
},
311312
},
312313
};
313314
const user2 = await adminQuery({
@@ -432,7 +433,7 @@ describe('User resolver standard behavior', () => {
432433
query: UPDATE_QUERY,
433434
variables: {
434435
id: userInternalId,
435-
input: { key: 'user_confidence_level', value: { max_confidence: 33, overrides: [] } }
436+
input: { key: 'user_confidence_level', value: { max_confidence: 33, overrides: [] } },
436437
},
437438
});
438439
expect(queryResult.data.userEdit.fieldPatch.user_confidence_level.max_confidence).toEqual(33);
@@ -480,7 +481,7 @@ describe('User resolver standard behavior', () => {
480481
group_confidence_level: {
481482
max_confidence: 60,
482483
overrides: [],
483-
}
484+
},
484485
},
485486
};
486487
const group = await adminQuery({
@@ -543,7 +544,7 @@ describe('User resolver standard behavior', () => {
543544
query: UPDATE_QUERY,
544545
variables: {
545546
id: userInternalId,
546-
input: { key: 'user_confidence_level', value: [null] }
547+
input: { key: 'user_confidence_level', value: [null] },
547548
},
548549
});
549550
const { userEdit } = queryResult.data;
@@ -748,6 +749,7 @@ describe('User list members query behavior', () => {
748749
it('Should user lists all members', async () => {
749750
const queryResult = await editorQuery({ query: LIST_MEMBERS_QUERY });
750751
const usersEdges = queryResult.data.members.edges as { node: Member }[];
752+
expect(usersEdges.map((n) => n.node)).toEqual('test');
751753
expect(usersEdges.length).toEqual(25);
752754
expect(usersEdges.filter(({ node: { entity_type } }) => entity_type === ENTITY_TYPE_USER).length).toEqual(TESTING_USERS.length + 1); // +1 = Plus admin user
753755
expect(usersEdges.filter(({ node: { entity_type } }) => entity_type === ENTITY_TYPE_GROUP).length).toEqual(entitiesCounter.Group);
@@ -796,7 +798,7 @@ describe('User has no capability query behavior', () => {
796798
query: GROUP_UPDATE_QUERY,
797799
variables: {
798800
id: 'group--a7991a4f-6192-59a4-87d3-d006d2c41cc8',
799-
input: { key: 'default_assignation', value: [false] }
801+
input: { key: 'default_assignation', value: [false] },
800802
},
801803
});
802804
// Create the user
@@ -829,7 +831,7 @@ describe('User has no capability query behavior', () => {
829831
query: GROUP_UPDATE_QUERY,
830832
variables: {
831833
id: 'group--a7991a4f-6192-59a4-87d3-d006d2c41cc8',
832-
input: { key: 'default_assignation', value: [true] }
834+
input: { key: 'default_assignation', value: [true] },
833835
},
834836
});
835837
});
@@ -979,6 +981,18 @@ describe('User has no settings capability and is organization admin query behavi
979981
});
980982
expect(queryResult.data.userEdit.fieldPatch.account_status).toEqual('Inactive');
981983
});
984+
it('should not update user with no organization', async () => {
985+
await queryAsUserIsExpectedForbidden(USER_EDITOR.client, {
986+
query: UPDATE_QUERY,
987+
variables: { id: ADMIN_USER.id, input: { key: 'account_status', value: ['Inactive'] } },
988+
});
989+
});
990+
it('should not update user from an other organization', async () => {
991+
await queryAsUserIsExpectedForbidden(USER_EDITOR.client, {
992+
query: UPDATE_QUERY,
993+
variables: { id: USER_SECURITY.id, input: { key: 'account_status', value: ['Inactive'] } },
994+
});
995+
});
982996
it('should not add organization to user if not admin', async () => {
983997
platformOrganizationId = await getOrganizationIdByName(PLATFORM_ORGANIZATION.name);
984998
await queryAsUserIsExpectedForbidden(USER_EDITOR.client, {
@@ -989,6 +1003,15 @@ describe('User has no settings capability and is organization admin query behavi
9891003
},
9901004
});
9911005
});
1006+
it('should not add organization to user if user is not in its own organization', async () => {
1007+
await queryAsUserIsExpectedForbidden(USER_EDITOR.client, {
1008+
query: ORGANIZATION_ADD_QUERY,
1009+
variables: {
1010+
id: ADMIN_USER.id,
1011+
organizationId: testOrganizationId,
1012+
},
1013+
});
1014+
});
9921015
it('should administrate more than 1 organization', async () => {
9931016
// Need to add granted_groups to PLATFORM_ORGANIZATION because of line 533 in domain/user.js
9941017
const grantableGroupQueryResult = await adminQuery({
@@ -1087,7 +1110,7 @@ describe('meUser specific resolvers', async () => {
10871110
password: USER_EDITOR.password,
10881111
input: [
10891112
{ key: 'language', value: 'fr-fr' },
1090-
]
1113+
],
10911114
};
10921115
const queryResult = await queryAsUserWithSuccess(USER_EDITOR.client, {
10931116
query: ME_EDIT,
@@ -1100,7 +1123,7 @@ describe('meUser specific resolvers', async () => {
11001123
password: USER_EDITOR.password,
11011124
input: [
11021125
{ key: 'api_token', value: 'd434ce02-e58e-4cac-8b4c-42bf16748e84' },
1103-
]
1126+
],
11041127
};
11051128
await queryAsUserIsExpectedForbidden(USER_EDITOR.client, {
11061129
query: ME_EDIT,
@@ -1113,7 +1136,7 @@ describe('meUser specific resolvers', async () => {
11131136
input: [
11141137
{ key: 'language', value: 'en-us' },
11151138
{ key: 'theme', value: 'dark' },
1116-
]
1139+
],
11171140
};
11181141
const queryResult = await queryAsUserWithSuccess(USER_EDITOR.client, {
11191142
query: ME_EDIT,
@@ -1127,7 +1150,7 @@ describe('meUser specific resolvers', async () => {
11271150
input: [
11281151
{ key: 'language', value: 'fr-fr' },
11291152
{ key: 'api_token', value: 'd434ce02-e58e-4cac-8b4c-42bf16748e84' },
1130-
]
1153+
],
11311154
};
11321155
await queryAsUserIsExpectedForbidden(USER_EDITOR.client, {
11331156
query: ME_EDIT,
@@ -1139,7 +1162,7 @@ describe('meUser specific resolvers', async () => {
11391162
password: 'incorrect_current_password',
11401163
input: [
11411164
{ key: 'password', value: 'new_password' },
1142-
]
1165+
],
11431166
};
11441167
await queryAsUserIsExpectedError(USER_EDITOR.client, {
11451168
query: ME_EDIT,
@@ -1241,7 +1264,7 @@ describe('Service account User coverage', async () => {
12411264
query: UPDATE_QUERY,
12421265
variables: {
12431266
id: userInternalId,
1244-
input: { key: 'user_service_account', value: [false] }
1267+
input: { key: 'user_service_account', value: [false] },
12451268
},
12461269
});
12471270
const { userEdit } = queryResult.data;
@@ -1255,7 +1278,7 @@ describe('Service account User coverage', async () => {
12551278
query: UPDATE_QUERY,
12561279
variables: {
12571280
id: userInternalId,
1258-
input: [{ key: 'user_service_account', value: [true] }, { key: 'password', value: ['toto'] }]
1281+
input: [{ key: 'user_service_account', value: [true] }, { key: 'password', value: ['toto'] }],
12591282
},
12601283
});
12611284
const { userEdit } = queryResult.data;
@@ -1269,7 +1292,7 @@ describe('Service account User coverage', async () => {
12691292
query: UPDATE_QUERY,
12701293
variables: {
12711294
id: userInternalId,
1272-
input: [{ key: 'password', value: ['toto'] }]
1295+
input: [{ key: 'password', value: ['toto'] }],
12731296
},
12741297
}, 'Cannot update password for Service account');
12751298
});

0 commit comments

Comments
 (0)