Skip to content

Commit 843b8f1

Browse files
mock all API request using gock to fix
1 parent dad4a5c commit 843b8f1

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

internal/services/services_test.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package services
22

33
import (
44
"context"
5+
"strings"
56
"testing"
67

78
"github.com/h2non/gock"
@@ -36,10 +37,43 @@ func TestRun(t *testing.T) {
3637
require.NoError(t, utils.InitConfig(utils.InitParams{
3738
ProjectId: projectRef,
3839
}, fsys))
39-
40-
// Set project reference in flags
4140
flags.ProjectRef = projectRef
4241

42+
// Mock all API requests
43+
defer gock.OffAll()
44+
45+
// Mock API keys
46+
gock.New(utils.DefaultApiHost).
47+
Get("/v1/projects/" + projectRef + "/api-keys").
48+
Reply(200).
49+
JSON([]map[string]string{{"name": "anon", "api_key": "test-key"}})
50+
51+
// Mock database version
52+
gock.New(utils.DefaultApiHost).
53+
Get("/v1/projects").
54+
Reply(200).
55+
JSON([]map[string]interface{}{
56+
{
57+
"id": projectRef,
58+
"database": map[string]string{"version": "1.0.0"},
59+
},
60+
})
61+
62+
// Mock auth version
63+
gock.New("https://" + utils.GetSupabaseHost(projectRef)).
64+
Get("/auth/v1/health").
65+
Reply(200).
66+
JSON(map[string]string{"version": "2.0.0"})
67+
68+
// Mock postgrest version
69+
gock.New("https://" + utils.GetSupabaseHost(projectRef)).
70+
Get("/rest/v1/").
71+
Reply(200).
72+
JSON(map[string]interface{}{
73+
"swagger": "2.0",
74+
"info": map[string]string{"version": "3.0.0"},
75+
})
76+
4377
// Execute: Call the Run function
4478
err := Run(context.Background(), fsys)
4579

@@ -130,7 +164,7 @@ func TestCheckVersions(t *testing.T) {
130164
flags.ProjectRef = projectRef
131165

132166
// Mock remote versions
133-
token := "sbp_0102030405060708091011121314151617181920"
167+
token := "sbp_" + strings.Repeat("0", 36)
134168
require.NoError(t, utils.SaveAccessToken(token, fsys))
135169

136170
defer gock.OffAll()
@@ -168,7 +202,7 @@ func TestListRemoteImages(t *testing.T) {
168202
fsys := afero.NewMemMapFs()
169203

170204
// Setup: Create access token file with valid format
171-
token := "sbp_0102030405060708091011121314151617181920"
205+
token := "sbp_" + strings.Repeat("0", 36)
172206
require.NoError(t, utils.SaveAccessToken(token, fsys))
173207

174208
// Setup: Mock API responses
@@ -233,7 +267,7 @@ func TestListRemoteImages(t *testing.T) {
233267
fsys := afero.NewMemMapFs()
234268

235269
// Setup: Create access token file with valid format
236-
token := "sbp_0102030405060708091011121314151617181920"
270+
token := "sbp_" + strings.Repeat("0", 36)
237271
require.NoError(t, utils.SaveAccessToken(token, fsys))
238272

239273
// Setup: Mock API error response

0 commit comments

Comments
 (0)