@@ -3,13 +3,19 @@ const sinon = require('sinon')
33const chai = require ( 'chai' )
44const expect = chai . expect
55const { faker } = require ( '@faker-js/faker' )
6+ const mongoose = require ( 'mongoose' )
7+ const argon2 = require ( 'argon2' )
68
79const { USER_CREATE_SINGLE } = require ( '../../../src/controller/org.controller/org.controller' )
8-
9- const UserRepository = require ( '../../../src/repositories/userRepository' )
10- const OrgRepository = require ( '../../../src/repositories/orgRepository' )
10+ const BaseOrgRepository = require ( '../../../src/repositories/baseOrgRepository.js' )
11+ const RegistryUserModel = require ( '../../../src/model/registryuser.js' )
12+ const BaseOrg = require ( '../../../src/model/baseorg.js' )
13+ const BaseUserRepository = require ( '../../../src/repositories/baseUserRepository.js' )
14+ const BaseUser = require ( '../../../src/model/baseuser.js' )
15+ const UserRepository = require ( '../../../src/repositories/userRepository.js' )
1116
1217const stubOrgUUID = faker . datatype . uuid ( )
18+ const stubUserUUID = faker . datatype . uuid ( )
1319
1420const stubOrg = {
1521 short_name : 'stubOrg' ,
@@ -19,16 +25,36 @@ const stubOrg = {
1925 active_roles : [ 'ADMIN' , 'CNA' ]
2026 }
2127}
22-
28+ const fakeBaseUserSavedObject = {
29+ username : 'test_user' ,
30+ secret : 'test_secret' ,
31+ role : 'Admin' ,
32+ UUID : stubUserUUID
33+ }
34+ const fakeLegacySavedObject = {
35+ short_name : 'mitre' ,
36+ name : 'The MITRE Corporation' ,
37+ authority : {
38+ active_roles : [
39+ 'CNA' ,
40+ 'SECRETARIAT'
41+ ]
42+ } ,
43+ policies : {
44+ id_quota : 1000
45+ }
46+ }
2347const stubUser = {
2448 username : 'stubUser' ,
25- org_UUID : stubOrgUUID ,
26- UUID : faker . datatype . uuid ( )
49+ org_UUID : stubOrgUUID
2750}
2851
52+ const fakeUserMongooseDocument = new BaseUser ( fakeBaseUserSavedObject )
53+ const fakeOrgMongooseDocument = new BaseOrg ( fakeLegacySavedObject )
54+
2955// eslint-disable-next-line mocha/no-skipped-tests
30- describe . skip ( 'Testing the POST /org/:shortname/user endpoint in Org Controller' , ( ) => {
31- let status , json , res , next , orgRepo , getOrgRepository , getUserRepository , userRepo , req , getOrg
56+ describe ( 'Testing the POST /org/:shortname/user endpoint in Org Controller' , ( ) => {
57+ let status , json , res , next , mockSession , baseOrgRepo , getBaseOrgRepository , baseUserRepo , getBaseUserRepository , req , userRepo , getUserRepository
3258
3359 beforeEach ( ( ) => {
3460 status = sinon . stub ( )
@@ -37,51 +63,64 @@ describe.skip('Testing the POST /org/:shortname/user endpoint in Org Controller'
3763 next = sinon . spy ( )
3864 status . returns ( res )
3965
40- userRepo = new UserRepository ( )
41- getUserRepository = sinon . stub ( )
42- getUserRepository . returns ( userRepo )
66+ // Stub Mongoose session methods
67+ mockSession = {
68+ startTransaction : sinon . stub ( ) ,
69+ commitTransaction : sinon . stub ( ) . resolves ( ) ,
70+ abortTransaction : sinon . stub ( ) . resolves ( ) ,
71+ endSession : sinon . stub ( ) . resolves ( )
72+ }
73+ sinon . stub ( mongoose , 'startSession' ) . resolves ( mockSession )
4374
44- orgRepo = new OrgRepository ( )
45- getOrgRepository = sinon . stub ( )
46- getOrgRepository . returns ( orgRepo )
47- // May have to replace this based on tests
48- getOrg = sinon . stub ( orgRepo , 'getOrgUUID' ) . returns ( stubOrgUUID )
49- sinon . stub ( userRepo , 'findUsersByOrgUUID' ) . returns ( 1 )
50- sinon . stub ( orgRepo , 'isSecretariatUUID' ) . returns ( false )
51- sinon . stub ( userRepo , 'isAdminUUID' ) . returns ( true )
52- sinon . stub ( userRepo , 'findOneByUserNameAndOrgUUID' ) . returns ( null )
53- sinon . stub ( userRepo , 'updateByUserNameAndOrgUUID' ) . returns ( true )
54- sinon . stub ( userRepo , 'aggregate' ) . returns ( [ stubUser ] )
55- sinon . stub ( userRepo , 'getUserUUID' ) . returns ( stubUser . UUID )
75+ baseOrgRepo = new BaseOrgRepository ( )
76+ getBaseOrgRepository = sinon . stub ( ) . returns ( baseOrgRepo )
77+ baseUserRepo = new BaseUserRepository ( )
78+ getBaseUserRepository = sinon . stub ( ) . returns ( baseUserRepo )
79+ userRepo = new UserRepository ( )
80+ getUserRepository = sinon . stub ( ) . returns ( userRepo )
5681
5782 req = {
5883 ctx : {
5984 org : stubOrg . short_name ,
6085 uuid : stubOrg . UUID ,
6186 params : {
62-
87+ shortname : stubOrg . short_name
6388 } ,
6489 repositories : {
65- getOrgRepository,
90+ getBaseOrgRepository,
91+ getBaseUserRepository,
6692 getUserRepository
6793 } ,
6894 body : {
69- stubUser
95+ ... stubUser
7096 }
7197 }
7298 }
7399 } )
100+ afterEach ( ( ) => {
101+ sinon . restore ( )
102+ } )
74103 context ( 'Positive Tests' , ( ) => {
75104 it ( 'User is created' , async ( ) => {
105+ sinon . stub ( baseUserRepo , 'orgHasUser' ) . resolves ( false )
106+ sinon . stub ( baseUserRepo , 'isAdminOrSecretariat' ) . resolves ( true )
107+ sinon . stub ( argon2 , 'hash' ) . resolves ( 'hashedPassword' )
108+ sinon . stub ( BaseOrgRepository . prototype , 'findOneByShortName' ) . resolves ( fakeOrgMongooseDocument )
109+ sinon . stub ( baseUserRepo , 'findUsersByOrgShortname' ) . resolves ( [ fakeUserMongooseDocument ] )
110+ sinon . stub ( RegistryUserModel . prototype , 'save' ) . resolves ( fakeBaseUserSavedObject )
111+ // stub the prototype since createUser in baseUserRepository creates a new internal instance of the legacy UserRepository
112+ sinon . stub ( UserRepository . prototype , 'updateByUserNameAndOrgUUID' ) . resolves ( fakeUserMongooseDocument )
113+
76114 await USER_CREATE_SINGLE ( req , res , next )
77115 expect ( status . args [ 0 ] [ 0 ] ) . to . equal ( 200 )
78116 expect ( res . json . args [ 0 ] [ 0 ] . message ) . contains ( 'was successfully created' )
79117 } )
80118 } )
81119 context ( 'Negitive tests' , ( ) => {
82120 it ( 'User Fails to be created because not in the same org' , async ( ) => {
121+ sinon . stub ( baseUserRepo , 'orgHasUser' ) . resolves ( false )
122+ sinon . stub ( baseUserRepo , 'isAdminOrSecretariat' ) . resolves ( false )
83123 req . ctx . org = 'FakeShortName'
84- getOrg . withArgs ( 'FakeShortName' ) . returns ( 'FAKEUUID' )
85124 await USER_CREATE_SINGLE ( req , res , next )
86125 expect ( status . args [ 0 ] [ 0 ] ) . to . equal ( 403 )
87126 expect ( res . json . args [ 0 ] [ 0 ] . error ) . contains ( 'NOT_ORG_ADMIN_OR_SECRETARIAT' )
0 commit comments