-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_test.go
More file actions
197 lines (161 loc) · 5.51 KB
/
Copy pathhandler_test.go
File metadata and controls
197 lines (161 loc) · 5.51 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// setupBridge creates a MailxClient backed by a mock Mailx server and returns
// the bridge's ServeMux ready for testing.
func setupBridge(t *testing.T, aliasStatus int, aliasName string) *http.ServeMux {
t.Helper()
srv := newTestMailxServer(http.StatusOK, aliasStatus, aliasName, nil)
t.Cleanup(srv.Close)
cfg := testConfig(srv.URL)
client := NewMailxClient(cfg, srv.Client())
if err := client.Authenticate(context.Background()); err != nil {
t.Fatalf("setup auth failed: %v", err)
}
mux := http.NewServeMux()
registerHandlers(mux, client)
return mux
}
func TestHealth(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("GET /health", handleHealth)
req := httptest.NewRequest(http.MethodGet, "/health", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("expected 200, got %d", rec.Code)
}
var body map[string]string
if err := json.NewDecoder(rec.Body).Decode(&body); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if body["status"] != "ok" {
t.Errorf("expected status 'ok', got %q", body["status"])
}
}
func TestCreateAlias_ValidRequest(t *testing.T) {
mux := setupBridge(t, http.StatusCreated, "new.alias@example.com")
req := httptest.NewRequest(http.MethodPost, "/api/v1/aliases",
strings.NewReader(`{"domain":"github.com","description":"Generated by Bitwarden."}`))
req.Header.Set("Authorization", "Bearer test-bridge-key")
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusCreated {
t.Fatalf("expected 201, got %d: %s", rec.Code, rec.Body.String())
}
var resp struct {
Data struct {
Email string `json:"email"`
} `json:"data"`
}
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if resp.Data.Email != "new.alias@example.com" {
t.Errorf("expected email 'new.alias@example.com', got %q", resp.Data.Email)
}
}
func TestCreateAlias_MissingAuth(t *testing.T) {
mux := setupBridge(t, http.StatusCreated, "alias@example.com")
req := httptest.NewRequest(http.MethodPost, "/api/v1/aliases",
strings.NewReader(`{"domain":"test.com"}`))
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusUnauthorized {
t.Errorf("expected 401, got %d", rec.Code)
}
}
func TestCreateAlias_WrongToken(t *testing.T) {
mux := setupBridge(t, http.StatusCreated, "alias@example.com")
req := httptest.NewRequest(http.MethodPost, "/api/v1/aliases",
strings.NewReader(`{"domain":"test.com"}`))
req.Header.Set("Authorization", "Bearer wrong-key")
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusUnauthorized {
t.Errorf("expected 401, got %d", rec.Code)
}
}
func TestCreateAlias_NoBearerPrefix(t *testing.T) {
mux := setupBridge(t, http.StatusCreated, "alias@example.com")
req := httptest.NewRequest(http.MethodPost, "/api/v1/aliases",
strings.NewReader(`{"domain":"test.com"}`))
req.Header.Set("Authorization", "test-bridge-key")
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusUnauthorized {
t.Errorf("expected 401 without Bearer prefix, got %d", rec.Code)
}
}
func TestCreateAlias_EmptyDomain(t *testing.T) {
mux := setupBridge(t, http.StatusCreated, "alias@example.com")
req := httptest.NewRequest(http.MethodPost, "/api/v1/aliases",
strings.NewReader(`{"domain":"","description":"test"}`))
req.Header.Set("Authorization", "Bearer test-bridge-key")
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusUnprocessableEntity {
t.Errorf("expected 422 for empty domain, got %d", rec.Code)
}
}
func TestCreateAlias_InvalidJSON(t *testing.T) {
mux := setupBridge(t, http.StatusCreated, "alias@example.com")
req := httptest.NewRequest(http.MethodPost, "/api/v1/aliases",
strings.NewReader(`not json`))
req.Header.Set("Authorization", "Bearer test-bridge-key")
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rec.Code)
}
}
func TestCreateAlias_BodyTooLarge(t *testing.T) {
mux := setupBridge(t, http.StatusCreated, "alias@example.com")
largeBody := `{"domain":"` + strings.Repeat("x", maxBodySize+100) + `"}`
req := httptest.NewRequest(http.MethodPost, "/api/v1/aliases",
strings.NewReader(largeBody))
req.Header.Set("Authorization", "Bearer test-bridge-key")
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("expected 400 for oversized body, got %d", rec.Code)
}
}
func TestCreateAlias_UpstreamFailure(t *testing.T) {
mux := setupBridge(t, http.StatusInternalServerError, "")
req := httptest.NewRequest(http.MethodPost, "/api/v1/aliases",
strings.NewReader(`{"domain":"test.com"}`))
req.Header.Set("Authorization", "Bearer test-bridge-key")
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusBadGateway {
t.Errorf("expected 502, got %d", rec.Code)
}
}
func TestSanitize(t *testing.T) {
tests := []struct {
name string
input string
maxLen int
want string
}{
{"normal", "github.com", 200, "github.com"},
{"truncate", "abcdef", 3, "abc"},
{"control chars", "a\nb\tc", 200, "a?b?c"},
{"empty", "", 200, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := sanitize(tt.input, tt.maxLen)
if got != tt.want {
t.Errorf("sanitize(%q, %d) = %q, want %q", tt.input, tt.maxLen, got, tt.want)
}
})
}
}