@@ -19,7 +19,7 @@ func TestFetchSnapshotVersion(t *testing.T) {
1919 fakeApiServer := givenApiResponse (http .StatusOK , responsePayload )
2020 defer fakeApiServer .Close ()
2121
22- apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL )
22+ apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL , "" )
2323 version , _ := apiService .FetchSnapshotVersion ("domainId" , "default" )
2424
2525 assert .Contains (t , version , "version" , "Missing version in response" )
@@ -30,7 +30,7 @@ func TestFetchSnapshotVersion(t *testing.T) {
3030 fakeApiServer := givenApiResponse (http .StatusUnauthorized , `{ "error": "Invalid API token" }` )
3131 defer fakeApiServer .Close ()
3232
33- apiService := NewApiService ("INVALID_KEY" , fakeApiServer .URL )
33+ apiService := NewApiService ("INVALID_KEY" , fakeApiServer .URL , "" )
3434 version , _ := apiService .FetchSnapshotVersion ("domainId" , "default" )
3535
3636 assert .Contains (t , version , "Invalid API token" )
@@ -41,14 +41,14 @@ func TestFetchSnapshotVersion(t *testing.T) {
4141 fakeApiServer := givenApiResponse (http .StatusUnauthorized , responsePayload )
4242 defer fakeApiServer .Close ()
4343
44- apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL )
44+ apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL , "" )
4545 version , _ := apiService .FetchSnapshotVersion ("INVALID_DOMAIN" , "default" )
4646
4747 assert .Contains (t , version , "errors" )
4848 })
4949
5050 t .Run ("Should return error - invalid API URL" , func (t * testing.T ) {
51- apiService := NewApiService (config .GetEnv (SWITCHER_API_JWT_SECRET ), "http://localhost:8080" )
51+ apiService := NewApiService (config .GetEnv (SWITCHER_API_JWT_SECRET ), "http://localhost:8080" , "" )
5252 _ , err := apiService .FetchSnapshotVersion ("domainId" , "default" )
5353
5454 assert .NotNil (t , err )
@@ -61,7 +61,7 @@ func TestFetchSnapshot(t *testing.T) {
6161 fakeApiServer := givenApiResponse (http .StatusOK , responsePayload )
6262 defer fakeApiServer .Close ()
6363
64- apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL )
64+ apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL , "" )
6565 snapshot , _ := apiService .FetchSnapshot ("domainId" , "default" )
6666
6767 assert .Contains (t , snapshot , "domain" , "Missing domain in snapshot" )
@@ -75,7 +75,7 @@ func TestFetchSnapshot(t *testing.T) {
7575 fakeApiServer := givenApiResponse (http .StatusOK , responsePayload )
7676 defer fakeApiServer .Close ()
7777
78- apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL )
78+ apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL , "" )
7979 snapshot , _ := apiService .FetchSnapshot ("domainId" , "default" )
8080 data := apiService .NewDataFromJson ([]byte (snapshot ))
8181
@@ -88,7 +88,7 @@ func TestFetchSnapshot(t *testing.T) {
8888 fakeApiServer := givenApiResponse (http .StatusUnauthorized , `{ "error": "Invalid API token" }` )
8989 defer fakeApiServer .Close ()
9090
91- apiService := NewApiService ("INVALID_KEY" , fakeApiServer .URL )
91+ apiService := NewApiService ("INVALID_KEY" , fakeApiServer .URL , "" )
9292 snapshot , _ := apiService .FetchSnapshot ("domainId" , "default" )
9393
9494 assert .Contains (t , snapshot , "Invalid API token" )
@@ -99,14 +99,14 @@ func TestFetchSnapshot(t *testing.T) {
9999 fakeApiServer := givenApiResponse (http .StatusUnauthorized , responsePayload )
100100 defer fakeApiServer .Close ()
101101
102- apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL )
102+ apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL , "" )
103103 snapshot , _ := apiService .FetchSnapshot ("INVALID_DOMAIN" , "default" )
104104
105105 assert .Contains (t , snapshot , "errors" )
106106 })
107107
108108 t .Run ("Should return error - invalid API URL" , func (t * testing.T ) {
109- apiService := NewApiService (config .GetEnv (SWITCHER_API_JWT_SECRET ), "http://localhost:8080" )
109+ apiService := NewApiService (config .GetEnv (SWITCHER_API_JWT_SECRET ), "http://localhost:8080" , "" )
110110 _ , err := apiService .FetchSnapshot ("domainId" , "default" )
111111
112112 assert .NotNil (t , err )
@@ -123,7 +123,7 @@ func TestPushChangesToAPI(t *testing.T) {
123123 }` )
124124 defer fakeApiServer .Close ()
125125
126- apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL )
126+ apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL , "" )
127127
128128 // Test
129129 response , _ := apiService .PushChanges ("domainId" , diff )
@@ -140,7 +140,7 @@ func TestPushChangesToAPI(t *testing.T) {
140140 fakeApiServer := givenApiResponse (http .StatusBadRequest , `{ "error": "Config already exists" }` )
141141 defer fakeApiServer .Close ()
142142
143- apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL )
143+ apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL , "" )
144144
145145 // Test
146146 _ , err := apiService .PushChanges ("domainId" , diff )
@@ -157,7 +157,7 @@ func TestPushChangesToAPI(t *testing.T) {
157157 fakeApiServer := givenApiResponse (http .StatusUnauthorized , `{ "error": "Invalid API token" }` )
158158 defer fakeApiServer .Close ()
159159
160- apiService := NewApiService ("[INVALID_KEY]" , fakeApiServer .URL )
160+ apiService := NewApiService ("[INVALID_KEY]" , fakeApiServer .URL , "" )
161161
162162 // Test
163163 _ , err := apiService .PushChanges ("domainId" , diff )
@@ -170,7 +170,7 @@ func TestPushChangesToAPI(t *testing.T) {
170170 t .Run ("Should return error - API not accessible" , func (t * testing.T ) {
171171 // Given
172172 diff := givenDiffResult ("default" )
173- apiService := NewApiService ("[SWITCHER_API_JWT_SECRET]" , "http://localhost:8080" )
173+ apiService := NewApiService ("[SWITCHER_API_JWT_SECRET]" , "http://localhost:8080" , "" )
174174
175175 // Test
176176 _ , err := apiService .PushChanges ("domainId" , diff )
@@ -180,6 +180,33 @@ func TestPushChangesToAPI(t *testing.T) {
180180 })
181181}
182182
183+ func TestFetchSnapshotWithCaCert (t * testing.T ) {
184+ t .Run ("Should return snapshot" , func (t * testing.T ) {
185+ responsePayload := utils .ReadJsonFromFile ("../../resources/fixtures/api/default_snapshot.json" )
186+ fakeApiServer := givenApiResponse (http .StatusOK , responsePayload )
187+ defer fakeApiServer .Close ()
188+
189+ apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL , "../../resources/fixtures/api/dummy.pem" )
190+ snapshot , _ := apiService .FetchSnapshot ("domainId" , "default" )
191+
192+ assert .Contains (t , snapshot , "domain" , "Missing domain in snapshot" )
193+ assert .Contains (t , snapshot , "version" , "Missing version in snapshot" )
194+ assert .Contains (t , snapshot , "group" , "Missing groups in snapshot" )
195+ assert .Contains (t , snapshot , "config" , "Missing config in snapshot" )
196+ })
197+
198+ t .Run ("Should return error - certificate not found" , func (t * testing.T ) {
199+ responsePayload := utils .ReadJsonFromFile ("../../resources/fixtures/api/default_snapshot.json" )
200+ fakeApiServer := givenApiResponse (http .StatusOK , responsePayload )
201+ defer fakeApiServer .Close ()
202+
203+ apiService := NewApiService (SWITCHER_API_JWT_SECRET , fakeApiServer .URL , "invalid.pem" )
204+ _ , err := apiService .FetchSnapshot ("domainId" , "default" )
205+
206+ assert .NotNil (t , err )
207+ })
208+ }
209+
183210// Helpers
184211
185212func givenDiffResult (environment string ) model.DiffResult {
0 commit comments