@@ -19,6 +19,30 @@ import (
1919 "github.com/timescale/tiger-cli/internal/tiger/util"
2020)
2121
22+ // mockStoredCredentials overrides the common.GetStoredCredentials seam for the
23+ // duration of the test, restoring the original automatically via t.Cleanup.
24+ func mockStoredCredentials (t * testing.T , creds * config.Credentials , err error ) {
25+ t .Helper ()
26+ original := common .GetStoredCredentials
27+ common .GetStoredCredentials = func () (* config.Credentials , error ) {
28+ return creds , err
29+ }
30+ t .Cleanup (func () { common .GetStoredCredentials = original })
31+ }
32+
33+ // mockTestPAT injects a fixed PAT credential.
34+ func mockTestPAT (t * testing.T ) {
35+ mockStoredCredentials (t , & config.Credentials {
36+ APIKey : "test-api-key" ,
37+ ProjectID : "test-project-123" ,
38+ }, nil )
39+ }
40+
41+ // mockNotLoggedIn simulates the absence of stored credentials.
42+ func mockNotLoggedIn (t * testing.T ) {
43+ mockStoredCredentials (t , nil , config .ErrNotLoggedIn )
44+ }
45+
2246func setupDBTest (t * testing.T ) string {
2347 t .Helper ()
2448
@@ -81,11 +105,7 @@ func TestDBConnectionString_NoServiceID(t *testing.T) {
81105 }
82106
83107 // Mock authentication
84- originalGetCredentials := common .GetCredentials
85- common .GetCredentials = func () (string , string , error ) {
86- return "test-api-key" , "test-project-123" , nil
87- }
88- defer func () { common .GetCredentials = originalGetCredentials }()
108+ mockTestPAT (t )
89109
90110 // Execute db connection-string command without service ID
91111 _ , err = executeDBCommand (t .Context (), "db" , "connection-string" )
@@ -111,11 +131,7 @@ func TestDBConnectionString_NoAuth(t *testing.T) {
111131 }
112132
113133 // Mock authentication failure
114- originalGetCredentials := common .GetCredentials
115- common .GetCredentials = func () (string , string , error ) {
116- return "" , "" , fmt .Errorf ("not logged in" )
117- }
118- defer func () { common .GetCredentials = originalGetCredentials }()
134+ mockNotLoggedIn (t )
119135
120136 // Execute db connection-string command
121137 _ , err = executeDBCommand (t .Context (), "db" , "connection-string" )
@@ -174,11 +190,7 @@ func TestDBConnect_NoServiceID(t *testing.T) {
174190 }
175191
176192 // Mock authentication
177- originalGetCredentials := common .GetCredentials
178- common .GetCredentials = func () (string , string , error ) {
179- return "test-api-key" , "test-project-123" , nil
180- }
181- defer func () { common .GetCredentials = originalGetCredentials }()
193+ mockTestPAT (t )
182194
183195 // Execute db connect command without service ID
184196 _ , err = executeDBCommand (t .Context (), "db" , "connect" )
@@ -204,11 +216,7 @@ func TestDBConnect_NoAuth(t *testing.T) {
204216 }
205217
206218 // Mock authentication failure
207- originalGetCredentials := common .GetCredentials
208- common .GetCredentials = func () (string , string , error ) {
209- return "" , "" , fmt .Errorf ("not logged in" )
210- }
211- defer func () { common .GetCredentials = originalGetCredentials }()
219+ mockNotLoggedIn (t )
212220
213221 // Execute db connect command
214222 _ , err = executeDBCommand (t .Context (), "db" , "connect" )
@@ -234,11 +242,7 @@ func TestDBConnect_PsqlNotFound(t *testing.T) {
234242 }
235243
236244 // Mock authentication
237- originalGetCredentials := common .GetCredentials
238- common .GetCredentials = func () (string , string , error ) {
239- return "test-api-key" , "test-project-123" , nil
240- }
241- defer func () { common .GetCredentials = originalGetCredentials }()
245+ mockTestPAT (t )
242246
243247 // Test that psql alias works the same as connect
244248 _ , err1 := executeDBCommand (t .Context (), "db" , "connect" )
@@ -540,11 +544,7 @@ func TestDBTestConnection_NoServiceID(t *testing.T) {
540544 }
541545
542546 // Mock authentication
543- originalGetCredentials := common .GetCredentials
544- common .GetCredentials = func () (string , string , error ) {
545- return "test-api-key" , "test-project-123" , nil
546- }
547- defer func () { common .GetCredentials = originalGetCredentials }()
547+ mockTestPAT (t )
548548
549549 // Execute db test-connection command without service ID
550550 _ , err = executeDBCommand (t .Context (), "db" , "test-connection" )
@@ -570,11 +570,7 @@ func TestDBTestConnection_NoAuth(t *testing.T) {
570570 }
571571
572572 // Mock authentication failure
573- originalGetCredentials := common .GetCredentials
574- common .GetCredentials = func () (string , string , error ) {
575- return "" , "" , fmt .Errorf ("not logged in" )
576- }
577- defer func () { common .GetCredentials = originalGetCredentials }()
573+ mockNotLoggedIn (t )
578574
579575 // Execute db test-connection command
580576 _ , err = executeDBCommand (t .Context (), "db" , "test-connection" )
@@ -830,11 +826,7 @@ func TestDBTestConnection_TimeoutParsing(t *testing.T) {
830826 }
831827
832828 // Mock authentication
833- originalGetCredentials := common .GetCredentials
834- common .GetCredentials = func () (string , string , error ) {
835- return "test-api-key" , "test-project-123" , nil
836- }
837- defer func () { common .GetCredentials = originalGetCredentials }()
829+ mockTestPAT (t )
838830
839831 // Execute db test-connection command with timeout flag
840832 _ , err = executeDBCommand (t .Context (), "db" , "test-connection" , "--timeout" , tc .timeoutFlag )
@@ -982,11 +974,7 @@ func TestDBSavePassword_ExplicitPassword(t *testing.T) {
982974 }
983975
984976 originalGetServiceDetails := getServiceDetailsFunc
985- originalGetCredentials := common .GetCredentials
986- common .GetCredentials = func () (string , string , error ) {
987- return "test-api-key" , "test-project-123" , nil
988- }
989- defer func () { common .GetCredentials = originalGetCredentials }()
977+ mockTestPAT (t )
990978 getServiceDetailsFunc = func (cmd * cobra.Command , cfg * common.Config , args []string ) (api.Service , error ) {
991979 return mockService , nil
992980 }
@@ -1056,11 +1044,7 @@ func TestDBSavePassword_EnvironmentVariable(t *testing.T) {
10561044 }
10571045
10581046 originalGetServiceDetails := getServiceDetailsFunc
1059- originalGetCredentials := common .GetCredentials
1060- common .GetCredentials = func () (string , string , error ) {
1061- return "test-api-key" , "test-project-123" , nil
1062- }
1063- defer func () { common .GetCredentials = originalGetCredentials }()
1047+ mockTestPAT (t )
10641048 getServiceDetailsFunc = func (cmd * cobra.Command , cfg * common.Config , args []string ) (api.Service , error ) {
10651049 return mockService , nil
10661050 }
@@ -1130,11 +1114,7 @@ func TestDBSavePassword_InteractivePrompt(t *testing.T) {
11301114 }
11311115
11321116 originalGetServiceDetails := getServiceDetailsFunc
1133- originalGetCredentials := common .GetCredentials
1134- common .GetCredentials = func () (string , string , error ) {
1135- return "test-api-key" , "test-project-123" , nil
1136- }
1137- defer func () { common .GetCredentials = originalGetCredentials }()
1117+ mockTestPAT (t )
11381118 getServiceDetailsFunc = func (cmd * cobra.Command , cfg * common.Config , args []string ) (api.Service , error ) {
11391119 return mockService , nil
11401120 }
@@ -1211,11 +1191,7 @@ func TestDBSavePassword_InteractivePromptEmpty(t *testing.T) {
12111191 }
12121192
12131193 originalGetServiceDetails := getServiceDetailsFunc
1214- originalGetCredentials := common .GetCredentials
1215- common .GetCredentials = func () (string , string , error ) {
1216- return "test-api-key" , "test-project-123" , nil
1217- }
1218- defer func () { common .GetCredentials = originalGetCredentials }()
1194+ mockTestPAT (t )
12191195 getServiceDetailsFunc = func (cmd * cobra.Command , cfg * common.Config , args []string ) (api.Service , error ) {
12201196 return mockService , nil
12211197 }
@@ -1285,11 +1261,7 @@ func TestDBSavePassword_CustomRole(t *testing.T) {
12851261 }
12861262
12871263 originalGetServiceDetails := getServiceDetailsFunc
1288- originalGetCredentials := common .GetCredentials
1289- common .GetCredentials = func () (string , string , error ) {
1290- return "test-api-key" , "test-project-123" , nil
1291- }
1292- defer func () { common .GetCredentials = originalGetCredentials }()
1264+ mockTestPAT (t )
12931265 getServiceDetailsFunc = func (cmd * cobra.Command , cfg * common.Config , args []string ) (api.Service , error ) {
12941266 return mockService , nil
12951267 }
@@ -1342,11 +1314,7 @@ func TestDBSavePassword_NoServiceID(t *testing.T) {
13421314 if err != nil {
13431315 t .Fatalf ("Failed to save test config: %v" , err )
13441316 }
1345- originalGetCredentials := common .GetCredentials
1346- common .GetCredentials = func () (string , string , error ) {
1347- return "test-api-key" , "test-project-123" , nil
1348- }
1349- defer func () { common .GetCredentials = originalGetCredentials }()
1317+ mockTestPAT (t )
13501318
13511319 // No need to mock service details since it should fail before reaching getServiceDetailsFunc
13521320
@@ -1375,11 +1343,7 @@ func TestDBSavePassword_NoAuth(t *testing.T) {
13751343 }
13761344
13771345 // Mock authentication failure
1378- originalGetCredentials := common .GetCredentials
1379- common .GetCredentials = func () (string , string , error ) {
1380- return "" , "" , fmt .Errorf ("not logged in" )
1381- }
1382- defer func () { common .GetCredentials = originalGetCredentials }()
1346+ mockNotLoggedIn (t )
13831347
13841348 // Execute save-password command
13851349 _ , err = executeDBCommand (t .Context (), "db" , "save-password" , "--password=test-password" )
@@ -1427,11 +1391,7 @@ func TestDBSavePassword_PgpassStorage(t *testing.T) {
14271391 }
14281392
14291393 originalGetServiceDetails := getServiceDetailsFunc
1430- originalGetCredentials := common .GetCredentials
1431- common .GetCredentials = func () (string , string , error ) {
1432- return "test-api-key" , "test-project-123" , nil
1433- }
1434- defer func () { common .GetCredentials = originalGetCredentials }()
1394+ mockTestPAT (t )
14351395 getServiceDetailsFunc = func (cmd * cobra.Command , cfg * common.Config , args []string ) (api.Service , error ) {
14361396 return mockService , nil
14371397 }
0 commit comments