1+ import { IModel } from "@itwin/imodels-client-management" ;
2+ import { ITwin } from "@itwin/itwins-client" ;
3+ import { runCommand } from "@oclif/test" ;
4+ import { expect } from "chai" ;
5+
6+ import { User } from "../src/services/user-client/models/user.js" ;
7+
8+ describe ( "API Integration Tests" , ( ) => {
9+ let iTwin : ITwin ;
10+ let iModel : IModel ;
11+
12+ before ( async ( ) => {
13+ const date = new Date ( ) ;
14+ const iTwinRespone = await runCommand < ITwin > ( `itwin create --class Thing --sub-class Asset --name itwin-cli-test-${ date . toISOString ( ) } ` ) ;
15+ expect ( iTwinRespone . error ) . to . be . undefined ;
16+ iTwin = iTwinRespone . result ! ;
17+ const iModelResponse = await runCommand < IModel > ( `imodel create --name imodel-cli-test-${ date . toISOString ( ) } --itwin-id ${ iTwin . id } ` ) ;
18+ expect ( iModelResponse . error ) . to . be . undefined ;
19+ iModel = iModelResponse . result ! ;
20+ } ) ;
21+
22+ after ( async ( ) => {
23+ await runCommand ( `itwin delete -i ${ iTwin . id } ` ) ;
24+ } ) ;
25+
26+ it ( "should send a GET request and get user me info" , async ( ) => {
27+ const apiResponse = await runCommand ( "api --method GET --path users/me" ) ;
28+ expect ( apiResponse . error ) . to . be . undefined ;
29+ const userApiInfo = JSON . parse ( apiResponse . stdout ) ;
30+
31+ const userCommandInfo = await runCommand < User > ( "user me" ) ;
32+
33+ expect ( userApiInfo ) . to . have . property ( "user" ) . that . is . an ( "object" ) ;
34+ expect ( userApiInfo . user ) . to . deep . equal ( userCommandInfo . result ) ;
35+ } ) ;
36+
37+ it ( "should send a GET request with query parameters" , async ( ) => {
38+ const apiResponse = await runCommand ( `api --method GET --path itwins --query displayName:${ iTwin . displayName } ` ) ;
39+ expect ( apiResponse . error ) . to . be . undefined ;
40+ const apiResponseJSON = JSON . parse ( apiResponse . stdout ) ;
41+
42+ expect ( apiResponseJSON ) . to . have . property ( "iTwins" ) . that . is . an ( "array" ) . with . lengthOf ( 1 ) ;
43+ expect ( apiResponseJSON . iTwins [ 0 ] ) . to . have . property ( "displayName" , iTwin . displayName ) ;
44+ expect ( apiResponseJSON . iTwins [ 0 ] ) . to . have . property ( "id" , iTwin . id ) ;
45+ } ) ;
46+
47+ it ( "should send a POST request with body parameters" , async ( ) => {
48+ const user = await runCommand < User > ( "user me" ) ;
49+ const apiResponse = await runCommand ( `api --method POST --path users/getbyidlist --body ["${ user . result ?. id } "]` ) ;
50+ expect ( apiResponse . error ) . to . be . undefined ;
51+ const apiResponseJSON = JSON . parse ( apiResponse . stdout ) ;
52+
53+ expect ( apiResponseJSON ) . to . have . property ( "users" ) . that . is . an ( "array" ) . with . lengthOf ( 1 ) ;
54+ expect ( apiResponseJSON . users [ 0 ] ) . to . have . property ( "id" , user . result ?. id ) ;
55+ expect ( apiResponseJSON . users [ 0 ] ) . to . have . property ( "email" , user . result ?. email ) ;
56+ } ) ;
57+
58+ it ( "should update an iTwin with a PATCH request" , async ( ) => {
59+ const updatedName = `itwin-cli-test-updated-${ new Date ( ) . toISOString ( ) } ` ;
60+ // eslint-disable-next-line no-useless-escape
61+ const apiResponse = await runCommand ( `api --method PATCH --path itwins/${ iTwin . id } --body "{\"displayName\": \"${ updatedName } \"}"` ) ;
62+ expect ( apiResponse . error ) . to . be . undefined ;
63+ const apiResponseJSON = JSON . parse ( apiResponse . stdout ) ;
64+
65+ expect ( apiResponseJSON ) . to . have . property ( "iTwin" ) . that . is . an ( "object" ) . and . not . undefined ;
66+ expect ( apiResponseJSON . iTwin ) . to . have . property ( "id" , iTwin . id ) ;
67+ expect ( apiResponseJSON . iTwin ) . to . have . property ( "displayName" , updatedName ) ;
68+ } ) ;
69+
70+ it ( "should send a DELETE request to delete the iTwin" , async ( ) => {
71+ const apiResponse = await runCommand ( `api --method DELETE --path itwins/${ iTwin . id } --empty-response` ) ;
72+ expect ( apiResponse . error ) . to . be . undefined ;
73+ const deleteResponse = JSON . parse ( apiResponse . stdout ) ;
74+ expect ( deleteResponse ) . to . have . property ( "result" ) . that . is . equal ( "success" ) ;
75+ } ) ;
76+
77+ it ( 'should send the header to the API (iModel minimal)' , async ( ) => {
78+ const minimalApiResponse = await runCommand ( `api --method GET --path imodels/?iTwinId=${ iTwin . id } --header "Prefer: return=minimal"` ) ;
79+ expect ( minimalApiResponse . error ) . to . be . undefined ;
80+ const minimalApiResponseJSON = JSON . parse ( minimalApiResponse . stdout ) ;
81+
82+ expect ( minimalApiResponseJSON ) . to . have . property ( "iModels" ) . that . is . an ( "array" ) . with . lengthOf ( 1 ) ;
83+ expect ( minimalApiResponseJSON . iModels [ 0 ] ) . to . have . property ( "id" , iModel . id ) ;
84+ expect ( minimalApiResponseJSON . iModels [ 0 ] ) . to . have . property ( "displayName" , iModel . displayName ) ;
85+ expect ( minimalApiResponseJSON . iModels [ 0 ] ) . to . not . have . property ( "createdDateTime" ) ;
86+ expect ( minimalApiResponseJSON . iModels [ 0 ] ) . to . not . have . property ( "iTwinId" ) ;
87+ } ) ;
88+
89+ it ( 'should send the header to the API (iModel representation)' , async ( ) => {
90+ const minimalApiResponse = await runCommand ( `api --method GET --path imodels/?iTwinId=${ iTwin . id } --header "Prefer: return=representation"` ) ;
91+ expect ( minimalApiResponse . error ) . to . be . undefined ;
92+ const minimalApiResponseJSON = JSON . parse ( minimalApiResponse . stdout ) ;
93+
94+ expect ( minimalApiResponseJSON ) . to . have . property ( "iModels" ) . that . is . an ( "array" ) . with . lengthOf ( 1 ) ;
95+ expect ( minimalApiResponseJSON . iModels [ 0 ] ) . to . have . property ( "id" , iModel . id ) ;
96+ expect ( minimalApiResponseJSON . iModels [ 0 ] ) . to . have . property ( "displayName" , iModel . displayName ) ;
97+ expect ( minimalApiResponseJSON . iModels [ 0 ] ) . to . have . property ( "createdDateTime" ) ;
98+ expect ( minimalApiResponseJSON . iModels [ 0 ] ) . to . have . property ( "iTwinId" , iModel . iTwinId ) ;
99+ } ) ;
100+
101+ it ( 'should handle version header correctly (Echo V1)' , async ( ) => {
102+ const apiResponse = await runCommand ( `api --method GET --path echo/context --version-header application/vnd.bentley.itwin-platform.v1+json` ) ;
103+ expect ( apiResponse . error ) . to . be . undefined ;
104+ const apiResponseJSON = JSON . parse ( apiResponse . stdout ) ;
105+
106+ expect ( apiResponseJSON ) . to . have . property ( "api" ) . that . is . an ( "object" ) . and . not . undefined ;
107+ expect ( apiResponseJSON . api ) . to . have . property ( "version" ) . that . is . an ( "string" ) . and . not . undefined ;
108+ expect ( apiResponseJSON . api . version ) . to . equal ( "v1" ) ;
109+ } ) ;
110+ } ) ;
0 commit comments