1
+
2
+ import assert from 'assert' ;
3
+ import sinon from 'sinon' ;
4
+ import { spe } from './spe.js' ;
5
+ import { sinonUtil } from './sinonUtil.js' ;
6
+ import request from '../request.js' ;
7
+ import auth from '../Auth.js' ;
8
+ import config from '../config.js' ;
9
+ import { cli } from '../cli/cli.js' ;
10
+
11
+ describe ( 'utils/spe' , ( ) => {
12
+ const siteUrl = 'https://contoso.sharepoint.com' ;
13
+ const adminUrl = siteUrl . replace ( '.sharepoint.com' , '-admin.sharepoint.com' ) ;
14
+
15
+ const containerTypeResponse = [
16
+ {
17
+ _ObjectType_ : 'Microsoft.Online.SharePoint.TenantAdministration.SPContainerTypeProperties' ,
18
+ ApplicationRedirectUrl : null ,
19
+ AzureSubscriptionId : '/Guid(00000000-0000-0000-0000-000000000000)/' ,
20
+ ContainerTypeId : '/Guid(073269af-f1d2-042d-2ef5-5bdd6ac83115)/' ,
21
+ CreationDate : null ,
22
+ DisplayName : 'test1' ,
23
+ ExpiryDate : null ,
24
+ IsBillingProfileRequired : true ,
25
+ OwningAppId : '/Guid(df4085cc-9a38-4255-badc-5c5225610475)/' ,
26
+ OwningTenantId : '/Guid(00000000-0000-0000-0000-000000000000)/' ,
27
+ Region : null ,
28
+ ResourceGroup : null ,
29
+ SPContainerTypeBillingClassification : 0
30
+ } ,
31
+ {
32
+ _ObjectType_ : 'Microsoft.Online.SharePoint.TenantAdministration.SPContainerTypeProperties' ,
33
+ ApplicationRedirectUrl : null ,
34
+ AzureSubscriptionId : '/Guid(00000000-0000-0000-0000-000000000000)/' ,
35
+ ContainerTypeId : '/Guid(880ab3bd-5b68-01d4-3744-01a7656cf2ba)/' ,
36
+ CreationDate : null ,
37
+ DisplayName : 'test2' ,
38
+ ExpiryDate : null ,
39
+ IsBillingProfileRequired : true ,
40
+ OwningAppId : '/Guid(50785fde-3082-47ac-a36d-06282ac5c7da)/' ,
41
+ OwningTenantId : '/Guid(00000000-0000-0000-0000-000000000000)/' ,
42
+ Region : null ,
43
+ ResourceGroup : null ,
44
+ SPContainerTypeBillingClassification : 0
45
+ }
46
+ ] ;
47
+
48
+ before ( ( ) => {
49
+ auth . connection . active = true ;
50
+ auth . connection . spoUrl = siteUrl ;
51
+ } ) ;
52
+
53
+ afterEach ( ( ) => {
54
+ sinonUtil . restore ( [
55
+ request . post
56
+ ] ) ;
57
+ } ) ;
58
+
59
+ after ( ( ) => {
60
+ auth . connection . active = false ;
61
+ auth . connection . spoUrl = undefined ;
62
+ sinon . restore ( ) ;
63
+ } ) ;
64
+
65
+ it ( 'correctly retrieves a list of container types' , async ( ) => {
66
+ const postStub = sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
67
+ if ( opts . url === `${ adminUrl } /_vti_bin/client.svc/ProcessQuery` ) {
68
+ return [
69
+ {
70
+ "SchemaVersion" : "15.0.0.0" , "LibraryVersion" : "16.0.24817.12005" , "ErrorInfo" : null , "TraceCorrelationId" : "2d63d39f-3016-0000-a532-30514e76ae73"
71
+ } , 46 , {
72
+ "IsNull" : false
73
+ } , 47 , containerTypeResponse
74
+ ] ;
75
+ }
76
+
77
+ throw 'Invalid request' ;
78
+ } ) ;
79
+
80
+ await spe . getAllContainerTypes ( adminUrl ) ;
81
+ assert . deepStrictEqual ( postStub . lastCall . args [ 0 ] . data , `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="${ config . applicationName } " xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><ObjectPath Id="46" ObjectPathId="45" /><Method Name="GetSPOContainerTypes" Id="47" ObjectPathId="45"><Parameters><Parameter Type="Enum">1</Parameter></Parameters></Method></Actions><ObjectPaths><Constructor Id="45" TypeId="{268004ae-ef6b-4e9b-8425-127220d84719}" /></ObjectPaths></Request>` ) ;
82
+ } ) ;
83
+
84
+ it ( 'correctly outputs a list of container types' , async ( ) => {
85
+ sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
86
+ if ( opts . url === `${ adminUrl } /_vti_bin/client.svc/ProcessQuery` ) {
87
+ return [
88
+ {
89
+ "SchemaVersion" : "15.0.0.0" , "LibraryVersion" : "16.0.24817.12005" , "ErrorInfo" : null , "TraceCorrelationId" : "2d63d39f-3016-0000-a532-30514e76ae73"
90
+ } , 46 , {
91
+ "IsNull" : false
92
+ } , 47 , containerTypeResponse
93
+ ] ;
94
+ }
95
+
96
+ throw 'Invalid request' ;
97
+ } ) ;
98
+
99
+ const actual = await spe . getAllContainerTypes ( adminUrl ) ;
100
+ assert . deepStrictEqual ( actual , [
101
+ {
102
+ ApplicationRedirectUrl : null ,
103
+ AzureSubscriptionId : '00000000-0000-0000-0000-000000000000' ,
104
+ ContainerTypeId : '073269af-f1d2-042d-2ef5-5bdd6ac83115' ,
105
+ CreationDate : null ,
106
+ DisplayName : 'test1' ,
107
+ ExpiryDate : null ,
108
+ IsBillingProfileRequired : true ,
109
+ OwningAppId : 'df4085cc-9a38-4255-badc-5c5225610475' ,
110
+ OwningTenantId : '00000000-0000-0000-0000-000000000000' ,
111
+ Region : null ,
112
+ ResourceGroup : null ,
113
+ SPContainerTypeBillingClassification : 0
114
+ } ,
115
+ {
116
+ ApplicationRedirectUrl : null ,
117
+ AzureSubscriptionId : '00000000-0000-0000-0000-000000000000' ,
118
+ ContainerTypeId : '880ab3bd-5b68-01d4-3744-01a7656cf2ba' ,
119
+ CreationDate : null ,
120
+ DisplayName : 'test2' ,
121
+ ExpiryDate : null ,
122
+ IsBillingProfileRequired : true ,
123
+ OwningAppId : '50785fde-3082-47ac-a36d-06282ac5c7da' ,
124
+ OwningTenantId : '00000000-0000-0000-0000-000000000000' ,
125
+ Region : null ,
126
+ ResourceGroup : null ,
127
+ SPContainerTypeBillingClassification : 0
128
+ }
129
+ ] ) ;
130
+ } ) ;
131
+
132
+ it ( 'correctly throws error when retrieving container types' , async ( ) => {
133
+ sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
134
+ if ( opts . url === `${ adminUrl } /_vti_bin/client.svc/ProcessQuery` ) {
135
+ return [
136
+ {
137
+ "SchemaVersion" : "15.0.0.0" , "LibraryVersion" : "16.0.7324.1200" , "ErrorInfo" : {
138
+ "ErrorMessage" : "An error has occurred" , "ErrorValue" : null , "TraceCorrelationId" : "e13c489e-2026-5000-8242-7ec96d02ba1d" , "ErrorCode" : - 1 , "ErrorTypeName" : "SPException"
139
+ } , "TraceCorrelationId" : "e13c489e-2026-5000-8242-7ec96d02ba1d"
140
+ }
141
+ ] ;
142
+ }
143
+
144
+ throw 'Invalid request' ;
145
+ } ) ;
146
+
147
+ await assert . rejects ( spe . getAllContainerTypes ( adminUrl ) , new Error ( 'An error has occurred' ) ) ;
148
+ } ) ;
149
+
150
+ it ( 'correctly retrieves the container type ID by name when using getContainerTypeIdByName' , async ( ) => {
151
+ sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
152
+ if ( opts . url === `${ adminUrl } /_vti_bin/client.svc/ProcessQuery` ) {
153
+ return [
154
+ {
155
+ "SchemaVersion" : "15.0.0.0" , "LibraryVersion" : "16.0.24817.12005" , "ErrorInfo" : null , "TraceCorrelationId" : "2d63d39f-3016-0000-a532-30514e76ae73"
156
+ } , 46 , {
157
+ "IsNull" : false
158
+ } , 47 , containerTypeResponse
159
+ ] ;
160
+ }
161
+
162
+ throw 'Invalid request' ;
163
+ } ) ;
164
+
165
+ const actual = await spe . getContainerTypeIdByName ( adminUrl , 'test2' ) ;
166
+ assert . strictEqual ( actual , '880ab3bd-5b68-01d4-3744-01a7656cf2ba' ) ;
167
+ } ) ;
168
+
169
+ it ( 'correctly throws error when name not found when using getContainerTypeIdByName' , async ( ) => {
170
+ sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
171
+ if ( opts . url === `${ adminUrl } /_vti_bin/client.svc/ProcessQuery` ) {
172
+ return [
173
+ {
174
+ "SchemaVersion" : "15.0.0.0" , "LibraryVersion" : "16.0.24817.12005" , "ErrorInfo" : null , "TraceCorrelationId" : "2d63d39f-3016-0000-a532-30514e76ae73"
175
+ } , 46 , {
176
+ "IsNull" : false
177
+ } , 47 , containerTypeResponse
178
+ ] ;
179
+ }
180
+
181
+ throw 'Invalid request' ;
182
+ } ) ;
183
+
184
+ await assert . rejects ( spe . getContainerTypeIdByName ( adminUrl , 'nonexistent' ) ,
185
+ new Error ( `The specified container type 'nonexistent' does not exist.` ) ) ;
186
+ } ) ;
187
+
188
+ it ( 'correctly handles multiple results when using getContainerTypeIdByName' , async ( ) => {
189
+ const containerTypes = [
190
+ ...containerTypeResponse ,
191
+ {
192
+ _ObjectType_ : 'Microsoft.Online.SharePoint.TenantAdministration.SPContainerTypeProperties' ,
193
+ ApplicationRedirectUrl : null ,
194
+ AzureSubscriptionId : '/Guid(00000000-0000-0000-0000-000000000000)/' ,
195
+ ContainerTypeId : '/Guid(4c8bc473-2d5a-474d-b2f3-fc60b7d39726)/' ,
196
+ CreationDate : null ,
197
+ DisplayName : 'test1' ,
198
+ ExpiryDate : null ,
199
+ IsBillingProfileRequired : true ,
200
+ OwningAppId : '/Guid(48cc3066-7f0d-4cb9-80fb-f7891069c0f9)/' ,
201
+ OwningTenantId : '/Guid(00000000-0000-0000-0000-000000000000)/' ,
202
+ Region : null ,
203
+ ResourceGroup : null ,
204
+ SPContainerTypeBillingClassification : 0
205
+ }
206
+ ] ;
207
+
208
+ sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
209
+ if ( opts . url === `${ adminUrl } /_vti_bin/client.svc/ProcessQuery` ) {
210
+ return [
211
+ {
212
+ "SchemaVersion" : "15.0.0.0" , "LibraryVersion" : "16.0.24817.12005" , "ErrorInfo" : null , "TraceCorrelationId" : "2d63d39f-3016-0000-a532-30514e76ae73"
213
+ } , 46 , {
214
+ "IsNull" : false
215
+ } , 47 , containerTypes
216
+ ] ;
217
+ }
218
+
219
+ throw 'Invalid request' ;
220
+ } ) ;
221
+
222
+ const stubMultiResults = sinon . stub ( cli , 'handleMultipleResultsFound' ) . resolves ( containerTypes . find ( c => c . ContainerTypeId === '/Guid(4c8bc473-2d5a-474d-b2f3-fc60b7d39726)/' ) ! ) ;
223
+ const actual = await spe . getContainerTypeIdByName ( adminUrl , 'test1' ) ;
224
+ assert ( stubMultiResults . calledOnce ) ;
225
+ assert . strictEqual ( actual , '4c8bc473-2d5a-474d-b2f3-fc60b7d39726' ) ;
226
+ } ) ;
227
+ } ) ;
0 commit comments