@@ -7,122 +7,9 @@ import (
77 "testing"
88
99 "github.com/timescale/tiger-cli/internal/tiger/api"
10- "github.com/timescale/tiger-cli/internal/tiger/api/mocks"
1110 "github.com/timescale/tiger-cli/internal/tiger/config"
12- "github.com/timescale/tiger-cli/internal/tiger/util"
13- "go.uber.org/mock/gomock"
1411)
1512
16- func TestValidateAPIKeyWithClient (t * testing.T ) {
17- tests := []struct {
18- name string
19- setupMock func (* mocks.MockClientWithResponsesInterface )
20- expectedError string
21- }{
22- {
23- name : "valid API key - 200 response" ,
24- setupMock : func (m * mocks.MockClientWithResponsesInterface ) {
25- m .EXPECT ().
26- GetProjectsProjectIdServicesWithResponse (gomock .Any (), "00000000-0000-0000-0000-000000000000" ).
27- Return (& api.GetProjectsProjectIdServicesResponse {
28- HTTPResponse : & http.Response {StatusCode : 200 },
29- }, nil )
30- },
31- expectedError : "" ,
32- },
33- {
34- name : "valid API key - 404 response (project not found)" ,
35- setupMock : func (m * mocks.MockClientWithResponsesInterface ) {
36- m .EXPECT ().
37- GetProjectsProjectIdServicesWithResponse (gomock .Any (), "00000000-0000-0000-0000-000000000000" ).
38- Return (& api.GetProjectsProjectIdServicesResponse {
39- HTTPResponse : & http.Response {StatusCode : 404 },
40- }, nil )
41- },
42- expectedError : "" ,
43- },
44- {
45- name : "invalid API key - 401 response" ,
46- setupMock : func (m * mocks.MockClientWithResponsesInterface ) {
47- m .EXPECT ().
48- GetProjectsProjectIdServicesWithResponse (gomock .Any (), "00000000-0000-0000-0000-000000000000" ).
49- Return (& api.GetProjectsProjectIdServicesResponse {
50- HTTPResponse : & http.Response {StatusCode : 401 },
51- JSON4XX : & api.ClientError {Message : util .Ptr ("Invalid or missing authentication credentials" )},
52- }, nil )
53- },
54- expectedError : "Invalid or missing authentication credentials" ,
55- },
56- {
57- name : "invalid API key - 403 response" ,
58- setupMock : func (m * mocks.MockClientWithResponsesInterface ) {
59- m .EXPECT ().
60- GetProjectsProjectIdServicesWithResponse (gomock .Any (), "00000000-0000-0000-0000-000000000000" ).
61- Return (& api.GetProjectsProjectIdServicesResponse {
62- HTTPResponse : & http.Response {StatusCode : 403 },
63- JSON4XX : & api.ClientError {Message : util .Ptr ("Invalid or missing authentication credentials" )},
64- }, nil )
65- },
66- expectedError : "Invalid or missing authentication credentials" ,
67- },
68- {
69- name : "unexpected response - 500" ,
70- setupMock : func (m * mocks.MockClientWithResponsesInterface ) {
71- m .EXPECT ().
72- GetProjectsProjectIdServicesWithResponse (gomock .Any (), "00000000-0000-0000-0000-000000000000" ).
73- Return (& api.GetProjectsProjectIdServicesResponse {
74- HTTPResponse : & http.Response {StatusCode : 500 },
75- }, nil )
76- },
77- expectedError : "unexpected API response: 500" ,
78- },
79- {
80- name : "network error" ,
81- setupMock : func (m * mocks.MockClientWithResponsesInterface ) {
82- m .EXPECT ().
83- GetProjectsProjectIdServicesWithResponse (gomock .Any (), "00000000-0000-0000-0000-000000000000" ).
84- Return (nil , context .DeadlineExceeded )
85- },
86- expectedError : "API call failed: context deadline exceeded" ,
87- },
88- }
89-
90- for _ , tt := range tests {
91- t .Run (tt .name , func (t * testing.T ) {
92- ctrl := gomock .NewController (t )
93- defer ctrl .Finish ()
94-
95- mockClient := mocks .NewMockClientWithResponsesInterface (ctrl )
96- tt .setupMock (mockClient )
97-
98- err := api .ValidateAPIKeyWithClient (context .Background (), mockClient , "" )
99-
100- if tt .expectedError == "" {
101- if err != nil {
102- t .Errorf ("Expected no error, got: %v" , err )
103- }
104- } else {
105- if err == nil {
106- t .Errorf ("Expected error containing %q, got nil" , tt .expectedError )
107- } else if err .Error () != tt .expectedError {
108- t .Errorf ("Expected error %q, got %q" , tt .expectedError , err .Error ())
109- }
110- }
111- })
112- }
113- }
114-
115- // TestValidateAPIKey_Integration would be an integration test that actually calls the API
116- // This should be run with a real API key for integration testing
117- func TestValidateAPIKey_Integration (t * testing.T ) {
118- if testing .Short () {
119- t .Skip ("Skipping integration test in short mode" )
120- }
121-
122- // This test would require a real API key and network connectivity
123- t .Skip ("Integration test requires real API key - implement when needed" )
124- }
125-
12613func TestNewTigerClientUserAgent (t * testing.T ) {
12714 // Create a test server that captures the User-Agent header
12815 var capturedUserAgent string
0 commit comments