1
+ import * as assert from 'assert' ;
2
+ import * as sinon from 'sinon' ;
3
+ import auth from '../../../../Auth' ;
4
+ import { Cli } from '../../../../cli/Cli' ;
5
+ import { CommandInfo } from '../../../../cli/CommandInfo' ;
6
+ import { Logger } from '../../../../cli/Logger' ;
7
+ import Command , { CommandError } from '../../../../Command' ;
8
+ import request from '../../../../request' ;
9
+ import { telemetry } from '../../../../telemetry' ;
10
+ import { pid } from '../../../../utils/pid' ;
11
+ import { sinonUtil } from '../../../../utils/sinonUtil' ;
12
+ import commands from '../../commands' ;
13
+ const command : Command = require ( './externalconnection-schema-add' ) ;
14
+
15
+ describe ( commands . EXTERNALCONNECTION_SCHEMA_ADD , ( ) => {
16
+ const externalConnectionId = 'TestConnectionForCLI' ;
17
+ const schema = '{"baseType": "microsoft.graph.externalItem","properties": [{"name": "ticketTitle","type": "String"}]}' ;
18
+
19
+ let log : string [ ] ;
20
+ let logger : Logger ;
21
+ let commandInfo : CommandInfo ;
22
+
23
+ before ( ( ) => {
24
+ sinon . stub ( auth , 'restoreAuth' ) . callsFake ( ( ) => Promise . resolve ( ) ) ;
25
+ sinon . stub ( telemetry , 'trackEvent' ) . callsFake ( ( ) => { } ) ;
26
+ sinon . stub ( pid , 'getProcessName' ) . callsFake ( ( ) => '' ) ;
27
+ auth . service . connected = true ;
28
+ commandInfo = Cli . getCommandInfo ( command ) ;
29
+ } ) ;
30
+
31
+ beforeEach ( ( ) => {
32
+ log = [ ] ;
33
+ logger = {
34
+ log : ( msg : string ) => {
35
+ log . push ( msg ) ;
36
+ } ,
37
+ logRaw : ( msg : string ) => {
38
+ log . push ( msg ) ;
39
+ } ,
40
+ logToStderr : ( msg : string ) => {
41
+ log . push ( msg ) ;
42
+ }
43
+ } ;
44
+ } ) ;
45
+
46
+ afterEach ( ( ) => {
47
+ sinonUtil . restore ( [
48
+ request . post
49
+ ] ) ;
50
+ } ) ;
51
+
52
+ after ( ( ) => {
53
+ sinonUtil . restore ( [
54
+ auth . restoreAuth ,
55
+ telemetry . trackEvent ,
56
+ pid . getProcessName
57
+ ] ) ;
58
+ auth . service . connected = false ;
59
+ } ) ;
60
+
61
+ it ( 'has correct name' , ( ) => {
62
+ assert . strictEqual ( command . name , commands . EXTERNALCONNECTION_SCHEMA_ADD ) ;
63
+ } ) ;
64
+
65
+ it ( 'has a description' , ( ) => {
66
+ assert . notStrictEqual ( command . description , null ) ;
67
+ } ) ;
68
+
69
+ it ( 'adds an external connection schema' , async ( ) => {
70
+ sinon . stub ( request , 'post' ) . callsFake ( async ( opts : any ) => {
71
+ if ( opts . url === `https://graph.microsoft.com/v1.0/external/connections/${ externalConnectionId } /schema` ) {
72
+ return ;
73
+ }
74
+ throw 'Invalid request' ;
75
+ } ) ;
76
+ await command . action ( logger , { options : { schema : schema , externalConnectionId : externalConnectionId , verbose : true } } as any ) ;
77
+ } ) ;
78
+
79
+ it ( 'correctly handles error when request is malformed or schema already exists' , async ( ) => {
80
+ const errorMessage = 'Error: The request is malformed or incorrect.' ;
81
+ sinon . stub ( request , 'post' ) . callsFake ( async ( opts : any ) => {
82
+ if ( opts . url === `https://graph.microsoft.com/v1.0/external/connections/${ externalConnectionId } /schema` ) {
83
+ throw errorMessage ;
84
+ }
85
+ throw 'Invalid request' ;
86
+ } ) ;
87
+ await assert . rejects ( command . action ( logger , { options : { schema : schema , externalConnectionId : externalConnectionId } } as any ) ,
88
+ new CommandError ( errorMessage ) ) ;
89
+ } ) ;
90
+
91
+ it ( 'fails validation if id is less than 3 characters' , async ( ) => {
92
+ const actual = await command . validate ( {
93
+ options : {
94
+ externalConnectionId : 'T' ,
95
+ schema : schema
96
+ }
97
+ } , commandInfo ) ;
98
+ assert . notStrictEqual ( actual , false ) ;
99
+ } ) ;
100
+
101
+ it ( 'fails validation if id is more than 32 characters' , async ( ) => {
102
+ const actual = await command . validate ( {
103
+ options : {
104
+ externalConnectionId : externalConnectionId + 'zzzzzzzzzzzzzzzzzz' ,
105
+ schema : schema
106
+ }
107
+ } , commandInfo ) ;
108
+ assert . notStrictEqual ( actual , false ) ;
109
+ } ) ;
110
+
111
+ it ( 'fails validation if id is not alphanumeric' , async ( ) => {
112
+ const actual = await command . validate ( {
113
+ options : {
114
+ externalConnectionId : externalConnectionId + '!' ,
115
+ schema : schema
116
+ }
117
+ } , commandInfo ) ;
118
+ assert . notStrictEqual ( actual , false ) ;
119
+ } ) ;
120
+
121
+ it ( 'fails validation if id starts with Microsoft' , async ( ) => {
122
+ const actual = await command . validate ( {
123
+ options : {
124
+ externalConnectionId : 'Microsoft' + externalConnectionId ,
125
+ schema : schema
126
+ }
127
+ } , commandInfo ) ;
128
+ assert . notStrictEqual ( actual , false ) ;
129
+ } ) ;
130
+
131
+ it ( 'fails validation if schema does not contain baseType' , async ( ) => {
132
+ const actual = await command . validate ( {
133
+ options : {
134
+ externalConnectionId : externalConnectionId ,
135
+ schema : '{"properties": [{"name": "ticketTitle","type": "String"}]}'
136
+ }
137
+ } , commandInfo ) ;
138
+ assert . notStrictEqual ( actual , false ) ;
139
+ } ) ;
140
+
141
+ it ( 'fails validation if schema does not contain properties' , async ( ) => {
142
+ const actual = await command . validate ( {
143
+ options : {
144
+ externalConnectionId : externalConnectionId ,
145
+ schema : '{"baseType": "microsoft.graph.externalItem"}'
146
+ }
147
+ } , commandInfo ) ;
148
+ assert . notStrictEqual ( actual , false ) ;
149
+ } ) ;
150
+
151
+ it ( 'fails validation if schema does contain more than 128 properties' , async ( ) => {
152
+ const schemaObject = JSON . parse ( schema ) ;
153
+ for ( let i = 0 ; i < 128 ; i ++ ) {
154
+ schemaObject . properties . push ( {
155
+ name : `Test${ i } ` ,
156
+ type : 'String'
157
+ } ) ;
158
+ }
159
+ const actual = await command . validate ( {
160
+ options : {
161
+ externalConnectionId : externalConnectionId ,
162
+ schema : JSON . stringify ( schemaObject )
163
+ }
164
+ } , commandInfo ) ;
165
+ assert . notStrictEqual ( actual , false ) ;
166
+ } ) ;
167
+
168
+ it ( 'passes validation with a correct schema and external connection id' , async ( ) => {
169
+ const actual = await command . validate ( {
170
+ options : {
171
+ externalConnectionId : externalConnectionId ,
172
+ schema : schema
173
+ }
174
+ } , commandInfo ) ;
175
+ assert . strictEqual ( actual , true ) ;
176
+ } ) ;
177
+ } ) ;
0 commit comments