3
3
*/
4
4
import chai from 'chai' ;
5
5
import request from 'supertest' ;
6
+ import config from 'config' ;
7
+ import _ from 'lodash' ;
6
8
7
9
import models from '../../models' ;
8
10
import server from '../../app' ;
9
11
import testUtil from '../../tests/util' ;
10
12
11
13
const should = chai . should ( ) ;
12
14
15
+ const ES_ORGCONFIG_INDEX = config . get ( 'elasticsearchConfig.metadataIndexName' ) ;
16
+ const ES_ORGCONFIG_TYPE = config . get ( 'elasticsearchConfig.metadataDocType' ) ;
17
+
18
+ const validateOrgConfig = ( resJson , orgConfig ) => {
19
+ resJson . id . should . be . eql ( orgConfig . id ) ;
20
+ resJson . orgId . should . be . eql ( orgConfig . orgId ) ;
21
+ resJson . configName . should . be . eql ( orgConfig . configName ) ;
22
+ resJson . configValue . should . be . eql ( orgConfig . configValue ) ;
23
+ should . exist ( resJson . createdAt ) ;
24
+ resJson . updatedBy . should . be . eql ( orgConfig . updatedBy ) ;
25
+ should . exist ( resJson . updatedAt ) ;
26
+ should . not . exist ( resJson . deletedBy ) ;
27
+ should . not . exist ( resJson . deletedAt ) ;
28
+ } ;
29
+
13
30
describe ( 'LIST organization config' , ( ) => {
14
31
const orgConfigPath = '/v5/projects/metadata/orgConfig' ;
15
32
const configs = [
@@ -23,17 +40,50 @@ describe('LIST organization config', () => {
23
40
} ,
24
41
{
25
42
id : 2 ,
26
- orgId : 'ORG1 ' ,
43
+ orgId : 'ORG2 ' ,
27
44
configName : 'project_catalog_url' ,
28
45
configValue : '/projects/2' ,
29
46
createdBy : 1 ,
30
47
updatedBy : 1 ,
31
48
} ,
49
+ {
50
+ id : 3 ,
51
+ orgId : 'ORG3' ,
52
+ configName : 'project_catalog_url' ,
53
+ configValue : '/projects/3' ,
54
+ createdBy : 1 ,
55
+ updatedBy : 1 ,
56
+ } ,
57
+ {
58
+ id : 4 ,
59
+ orgId : 'ORG4' ,
60
+ configName : 'project_catalog_url' ,
61
+ configValue : '/projects/4' ,
62
+ createdBy : 1 ,
63
+ updatedBy : 1 ,
64
+ } ,
32
65
] ;
33
66
34
67
beforeEach ( ( done ) => {
35
68
testUtil . clearDb ( )
36
- . then ( ( ) => models . OrgConfig . bulkCreate ( configs ) . then ( ( ) => done ( ) ) ) ;
69
+ . then ( ( ) => models . OrgConfig . bulkCreate ( configs , { returning : true } ) ,
70
+ ) . then ( async ( createdConfigs ) => {
71
+ // Index to ES only orgConfigs with id: 3 and 4
72
+ const indexedConfigs = _ ( createdConfigs ) . filter ( createdConfig => createdConfig . toJSON ( ) . id > 2 )
73
+ . map ( ( filteredConfig ) => {
74
+ const orgConfigJson = _ . omit ( filteredConfig . toJSON ( ) , 'deletedAt' , 'deletedBy' ) ;
75
+ return orgConfigJson ;
76
+ } ) . value ( ) ;
77
+
78
+ await server . services . es . index ( {
79
+ index : ES_ORGCONFIG_INDEX ,
80
+ type : ES_ORGCONFIG_TYPE ,
81
+ body : {
82
+ orgConfigs : indexedConfigs ,
83
+ } ,
84
+ } ) ;
85
+ done ( ) ;
86
+ } ) ;
37
87
} ) ;
38
88
after ( ( done ) => {
39
89
testUtil . clearDb ( done ) ;
@@ -48,19 +98,79 @@ describe('LIST organization config', () => {
48
98
} )
49
99
. expect ( 200 )
50
100
. end ( ( err , res ) => {
51
- const config = configs [ 0 ] ;
101
+ const config1 = configs [ 0 ] ;
102
+
103
+ const resJson = res . body ;
104
+ resJson . should . have . length ( 1 ) ;
105
+ validateOrgConfig ( resJson [ 0 ] , config1 ) ;
106
+
107
+ done ( ) ;
108
+ } ) ;
109
+ } ) ;
110
+
111
+ it ( 'should return 200 for admin with filter (ES)' , ( done ) => {
112
+ request ( server )
113
+ . get ( `${ orgConfigPath } ?orgId=${ configs [ 2 ] . orgId } &configName=${ configs [ 2 ] . configName } ` )
114
+ . set ( {
115
+ Authorization : `Bearer ${ testUtil . jwts . admin } ` ,
116
+ } )
117
+ . expect ( 200 )
118
+ . end ( ( err , res ) => {
119
+ const config3 = configs [ 2 ] ;
52
120
53
121
const resJson = res . body ;
54
122
resJson . should . have . length ( 1 ) ;
55
- resJson [ 0 ] . id . should . be . eql ( config . id ) ;
56
- resJson [ 0 ] . orgId . should . be . eql ( config . orgId ) ;
57
- resJson [ 0 ] . configName . should . be . eql ( config . configName ) ;
58
- resJson [ 0 ] . configValue . should . be . eql ( config . configValue ) ;
59
- should . exist ( resJson [ 0 ] . createdAt ) ;
60
- resJson [ 0 ] . updatedBy . should . be . eql ( config . updatedBy ) ;
61
- should . exist ( resJson [ 0 ] . updatedAt ) ;
62
- should . not . exist ( resJson [ 0 ] . deletedBy ) ;
63
- should . not . exist ( resJson [ 0 ] . deletedAt ) ;
123
+ validateOrgConfig ( resJson [ 0 ] , config3 ) ;
124
+
125
+ done ( ) ;
126
+ } ) ;
127
+ } ) ;
128
+
129
+ it ( 'should return 200 for admin and filter by multiple orgId (DB)' , ( done ) => {
130
+ request ( server )
131
+ . get ( `${ orgConfigPath } ?orgId=${ configs [ 0 ] . orgId } ,${ configs [ 1 ] . orgId } ` )
132
+ . set ( {
133
+ Authorization : `Bearer ${ testUtil . jwts . admin } ` ,
134
+ } )
135
+ . expect ( 200 )
136
+ . end ( ( err , res ) => {
137
+ const config1 = configs [ 0 ] ;
138
+ const config2 = configs [ 1 ] ;
139
+
140
+ const resJson = res . body ;
141
+ resJson . should . have . length ( 2 ) ;
142
+ resJson . forEach ( ( result ) => {
143
+ if ( result . id === 1 ) {
144
+ validateOrgConfig ( result , config1 ) ;
145
+ } else {
146
+ validateOrgConfig ( result , config2 ) ;
147
+ }
148
+ } ) ;
149
+
150
+ done ( ) ;
151
+ } ) ;
152
+ } ) ;
153
+
154
+ it ( 'should return 200 for admin and filter by multiple orgId (ES)' , ( done ) => {
155
+ request ( server )
156
+ . get ( `${ orgConfigPath } ?orgId=${ configs [ 2 ] . orgId } ,${ configs [ 3 ] . orgId } ` )
157
+ . set ( {
158
+ Authorization : `Bearer ${ testUtil . jwts . admin } ` ,
159
+ } )
160
+ . expect ( 200 )
161
+ . end ( ( err , res ) => {
162
+ const config3 = configs [ 2 ] ;
163
+ const config4 = configs [ 3 ] ;
164
+
165
+ const resJson = res . body ;
166
+ resJson . should . have . length ( 2 ) ;
167
+ resJson . forEach ( ( result ) => {
168
+ if ( result . id === 3 ) {
169
+ validateOrgConfig ( result , config3 ) ;
170
+ } else {
171
+ validateOrgConfig ( result , config4 ) ;
172
+ }
173
+ } ) ;
64
174
65
175
done ( ) ;
66
176
} ) ;
0 commit comments