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