1+ 'use strict' ;
2+ const { expect } = require ( '../../test/setup' ) ;
3+ const sinon = require ( 'sinon' ) ;
4+ const ParticleApi = require ( './api' ) ;
5+
6+ describe ( 'ParticleApi' , ( ) => {
7+ let particleApi ;
8+ let sandbox ;
9+
10+ beforeEach ( ( ) => {
11+ sandbox = sinon . createSandbox ( ) ;
12+ particleApi = new ParticleApi ( 'test-base-url' , { accessToken : 'test-token' } ) ;
13+ } ) ;
14+
15+ afterEach ( ( ) => {
16+ sandbox . restore ( ) ;
17+ } ) ;
18+
19+ describe ( 'getRollout' , ( ) => {
20+ it ( 'should call the correct API endpoint for product rollout without org' , async ( ) => {
21+ const productId = 'testProductId' ;
22+ const expectedUri = `/v1/products/${ productId } /env-vars/rollout` ;
23+ const expectedResponse = { body : { some : 'data' } } ;
24+
25+ const requestStub = sandbox . stub ( particleApi . api , 'request' ) . resolves ( expectedResponse ) ;
26+
27+ const result = await particleApi . getRollout ( { productId } ) ;
28+
29+ expect ( requestStub ) . to . have . been . calledWithMatch ( {
30+ uri : expectedUri ,
31+ method : 'get' ,
32+ auth : 'test-token'
33+ } ) ;
34+ expect ( result ) . to . deep . equal ( expectedResponse . body ) ;
35+ } ) ;
36+
37+ it ( 'should call the correct API endpoint for product rollout with org' , async ( ) => {
38+ const org = 'testOrg' ;
39+ const productId = 'testProductId' ;
40+ const expectedUri = `/v1/orgs/${ org } /products/${ productId } /env-vars/rollout` ;
41+ const expectedResponse = { body : { some : 'other-data' } } ;
42+
43+ const requestStub = sandbox . stub ( particleApi . api , 'request' ) . resolves ( expectedResponse ) ;
44+
45+ const result = await particleApi . getRollout ( { org, productId } ) ;
46+
47+ expect ( requestStub ) . to . have . been . calledWithMatch ( {
48+ uri : expectedUri ,
49+ method : 'get' ,
50+ auth : 'test-token'
51+ } ) ;
52+ expect ( result ) . to . deep . equal ( expectedResponse . body ) ;
53+ } ) ;
54+
55+ it ( 'should call the correct API endpoint for sandbox rollout when no org or product is provided' , async ( ) => {
56+ const expectedUri = `/v1/env-vars/rollout` ;
57+ const expectedResponse = { body : { some : 'sandbox-data' } } ;
58+
59+ const requestStub = sandbox . stub ( particleApi . api , 'request' ) . resolves ( expectedResponse ) ;
60+
61+ const result = await particleApi . getRollout ( { } ) ;
62+
63+ expect ( requestStub ) . to . have . been . calledWithMatch ( {
64+ uri : expectedUri ,
65+ method : 'get' ,
66+ auth : 'test-token'
67+ } ) ;
68+ expect ( result ) . to . deep . equal ( expectedResponse . body ) ;
69+ } ) ;
70+
71+ it ( 'should call the correct API endpoint for product rollout with deviceId' , async ( ) => {
72+ const productId = 'testProductId' ;
73+ const deviceId = 'testDeviceId' ;
74+ const expectedUri = `/v1/products/${ productId } /env-vars/${ deviceId } /rollout` ;
75+ const expectedResponse = { body : { some : 'device-data' } } ;
76+
77+ const requestStub = sandbox . stub ( particleApi . api , 'request' ) . resolves ( expectedResponse ) ;
78+
79+ const result = await particleApi . getRollout ( { productId, deviceId } ) ;
80+
81+ expect ( requestStub ) . to . have . been . calledWithMatch ( {
82+ uri : expectedUri ,
83+ method : 'get' ,
84+ auth : 'test-token'
85+ } ) ;
86+ expect ( result ) . to . deep . equal ( expectedResponse . body ) ;
87+ } ) ;
88+
89+ it ( 'should call the correct API endpoint for product rollout with org and deviceId' , async ( ) => {
90+ const org = 'testOrg' ;
91+ const productId = 'testProductId' ;
92+ const deviceId = 'testDeviceId' ;
93+ const expectedUri = `/v1/orgs/${ org } /products/${ productId } /env-vars/${ deviceId } /rollout` ;
94+ const expectedResponse = { body : { some : 'org-device-data' } } ;
95+
96+ const requestStub = sandbox . stub ( particleApi . api , 'request' ) . resolves ( expectedResponse ) ;
97+
98+ const result = await particleApi . getRollout ( { org, productId, deviceId } ) ;
99+
100+ expect ( requestStub ) . to . have . been . calledWithMatch ( {
101+ uri : expectedUri ,
102+ method : 'get' ,
103+ auth : 'test-token'
104+ } ) ;
105+ expect ( result ) . to . deep . equal ( expectedResponse . body ) ;
106+ } ) ;
107+
108+ it ( 'should call the correct API endpoint for sandbox rollout with deviceId' , async ( ) => {
109+ const deviceId = 'testDeviceId' ;
110+ const expectedUri = `/v1/env-vars/${ deviceId } /rollout` ;
111+ const expectedResponse = { body : { some : 'sandbox-device-data' } } ;
112+
113+ const requestStub = sandbox . stub ( particleApi . api , 'request' ) . resolves ( expectedResponse ) ;
114+
115+ const result = await particleApi . getRollout ( { deviceId } ) ;
116+
117+ expect ( requestStub ) . to . have . been . calledWithMatch ( {
118+ uri : expectedUri ,
119+ method : 'get' ,
120+ auth : 'test-token'
121+ } ) ;
122+ expect ( result ) . to . deep . equal ( expectedResponse . body ) ;
123+ } ) ;
124+
125+ it ( 'should handle API errors' , async ( ) => {
126+ const productId = 'testProductId' ;
127+ const expectedError = new Error ( 'API Error' ) ;
128+ expectedError . statusCode = 401 ;
129+ expectedError . body = { error_description : 'Unauthorized' } ;
130+
131+ sandbox . stub ( particleApi . api , 'request' ) . rejects ( expectedError ) ;
132+
133+ try {
134+ await particleApi . getRollout ( { productId } ) ;
135+ expect . fail ( 'should have thrown an error' ) ;
136+ } catch ( error ) {
137+ expect ( error . message ) . to . equal ( 'Unauthorized' ) ;
138+ expect ( error . name ) . to . equal ( 'UnauthorizedError' ) ;
139+ }
140+ } ) ;
141+ } ) ;
142+ } ) ;
0 commit comments