|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/cookiejar" |
| 9 | + "net/http/httptest" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/ProtonMail/go-proton-api" |
| 13 | +) |
| 14 | + |
| 15 | +// testSession creates a minimal Session pointing at the given test server. |
| 16 | +// It overrides proton.DefaultHostURL for the duration of the test. |
| 17 | +func testSession(t *testing.T, serverURL string) *Session { |
| 18 | + t.Helper() |
| 19 | + jar, _ := cookiejar.New(nil) |
| 20 | + return &Session{ |
| 21 | + Auth: proton.Auth{ |
| 22 | + UID: "test-uid-123", |
| 23 | + AccessToken: "test-token-abc", |
| 24 | + }, |
| 25 | + cookieJar: jar, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestDoJSON_SuccessGet(t *testing.T) { |
| 30 | + type payload struct { |
| 31 | + Name string `json:"Name"` |
| 32 | + ID int `json:"ID"` |
| 33 | + } |
| 34 | + |
| 35 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 36 | + if r.Method != http.MethodGet { |
| 37 | + t.Fatalf("expected GET, got %s", r.Method) |
| 38 | + } |
| 39 | + if r.Header.Get("x-pm-uid") != "test-uid-123" { |
| 40 | + t.Fatalf("missing x-pm-uid header") |
| 41 | + } |
| 42 | + if r.Header.Get("Authorization") != "Bearer test-token-abc" { |
| 43 | + t.Fatalf("missing Authorization header") |
| 44 | + } |
| 45 | + w.Header().Set("Content-Type", "application/json") |
| 46 | + json.NewEncoder(w).Encode(map[string]any{ |
| 47 | + "Code": 1000, |
| 48 | + "Name": "test-share", |
| 49 | + "ID": 42, |
| 50 | + }) |
| 51 | + })) |
| 52 | + defer srv.Close() |
| 53 | + |
| 54 | + s := testSession(t, srv.URL) |
| 55 | + var result payload |
| 56 | + err := s.DoJSON(context.Background(), "GET", srv.URL+"/test", nil, &result) |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("DoJSON GET: %v", err) |
| 59 | + } |
| 60 | + if result.Name != "test-share" || result.ID != 42 { |
| 61 | + t.Fatalf("unexpected result: %+v", result) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestDoJSON_SuccessPost(t *testing.T) { |
| 66 | + type reqBody struct { |
| 67 | + Email string `json:"Email"` |
| 68 | + } |
| 69 | + |
| 70 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 71 | + if r.Method != http.MethodPost { |
| 72 | + t.Fatalf("expected POST, got %s", r.Method) |
| 73 | + } |
| 74 | + if r.Header.Get("Content-Type") != "application/json" { |
| 75 | + t.Fatalf("missing Content-Type header") |
| 76 | + } |
| 77 | + body, _ := io.ReadAll(r.Body) |
| 78 | + var req reqBody |
| 79 | + if err := json.Unmarshal(body, &req); err != nil { |
| 80 | + t.Fatalf("unmarshal request body: %v", err) |
| 81 | + } |
| 82 | + if req.Email != "user@example.com" { |
| 83 | + t.Fatalf("unexpected email: %s", req.Email) |
| 84 | + } |
| 85 | + w.Header().Set("Content-Type", "application/json") |
| 86 | + json.NewEncoder(w).Encode(map[string]any{"Code": 1000}) |
| 87 | + })) |
| 88 | + defer srv.Close() |
| 89 | + |
| 90 | + s := testSession(t, srv.URL) |
| 91 | + err := s.DoJSON(context.Background(), "POST", srv.URL+"/invite", reqBody{Email: "user@example.com"}, nil) |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("DoJSON POST: %v", err) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestDoJSON_APIError(t *testing.T) { |
| 98 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 99 | + w.WriteHeader(http.StatusUnprocessableEntity) |
| 100 | + json.NewEncoder(w).Encode(map[string]any{ |
| 101 | + "Code": 2011, |
| 102 | + "Error": "Share not found", |
| 103 | + }) |
| 104 | + })) |
| 105 | + defer srv.Close() |
| 106 | + |
| 107 | + s := testSession(t, srv.URL) |
| 108 | + err := s.DoJSON(context.Background(), "GET", srv.URL+"/test", nil, nil) |
| 109 | + if err == nil { |
| 110 | + t.Fatal("expected error, got nil") |
| 111 | + } |
| 112 | + |
| 113 | + apiErr, ok := err.(*APIError) |
| 114 | + if !ok { |
| 115 | + t.Fatalf("expected *APIError, got %T: %v", err, err) |
| 116 | + } |
| 117 | + if apiErr.Status != http.StatusUnprocessableEntity { |
| 118 | + t.Fatalf("expected status 422, got %d", apiErr.Status) |
| 119 | + } |
| 120 | + if apiErr.Code != 2011 { |
| 121 | + t.Fatalf("expected code 2011, got %d", apiErr.Code) |
| 122 | + } |
| 123 | + if apiErr.Message != "Share not found" { |
| 124 | + t.Fatalf("expected message 'Share not found', got %q", apiErr.Message) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestDoJSON_AuthHeaders(t *testing.T) { |
| 129 | + var gotUID, gotAuth string |
| 130 | + |
| 131 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 132 | + gotUID = r.Header.Get("x-pm-uid") |
| 133 | + gotAuth = r.Header.Get("Authorization") |
| 134 | + json.NewEncoder(w).Encode(map[string]any{"Code": 1000}) |
| 135 | + })) |
| 136 | + defer srv.Close() |
| 137 | + |
| 138 | + s := testSession(t, srv.URL) |
| 139 | + _ = s.DoJSON(context.Background(), "GET", srv.URL+"/test", nil, nil) |
| 140 | + |
| 141 | + if gotUID != "test-uid-123" { |
| 142 | + t.Fatalf("x-pm-uid = %q, want %q", gotUID, "test-uid-123") |
| 143 | + } |
| 144 | + if gotAuth != "Bearer test-token-abc" { |
| 145 | + t.Fatalf("Authorization = %q, want %q", gotAuth, "Bearer test-token-abc") |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestDoJSON_CookiesAttached(t *testing.T) { |
| 150 | + // First request sets a cookie, second request should send it back. |
| 151 | + var gotCookie string |
| 152 | + |
| 153 | + callCount := 0 |
| 154 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 155 | + callCount++ |
| 156 | + if callCount == 1 { |
| 157 | + http.SetCookie(w, &http.Cookie{Name: "Session-Id", Value: "abc123", Path: "/"}) |
| 158 | + } else { |
| 159 | + c, err := r.Cookie("Session-Id") |
| 160 | + if err != nil { |
| 161 | + gotCookie = "" |
| 162 | + } else { |
| 163 | + gotCookie = c.Value |
| 164 | + } |
| 165 | + } |
| 166 | + json.NewEncoder(w).Encode(map[string]any{"Code": 1000}) |
| 167 | + })) |
| 168 | + defer srv.Close() |
| 169 | + |
| 170 | + s := testSession(t, srv.URL) |
| 171 | + |
| 172 | + // First call — server sets cookie. |
| 173 | + _ = s.DoJSON(context.Background(), "GET", srv.URL+"/first", nil, nil) |
| 174 | + // Second call — cookie should be sent. |
| 175 | + _ = s.DoJSON(context.Background(), "GET", srv.URL+"/second", nil, nil) |
| 176 | + |
| 177 | + if gotCookie != "abc123" { |
| 178 | + t.Fatalf("cookie not attached on second request: got %q", gotCookie) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +func TestDoJSON_NilBody(t *testing.T) { |
| 183 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 184 | + if r.Body != nil { |
| 185 | + body, _ := io.ReadAll(r.Body) |
| 186 | + if len(body) > 0 { |
| 187 | + t.Fatalf("expected empty body for GET, got %d bytes", len(body)) |
| 188 | + } |
| 189 | + } |
| 190 | + if r.Header.Get("Content-Type") != "" { |
| 191 | + t.Fatalf("Content-Type should not be set for nil body, got %q", r.Header.Get("Content-Type")) |
| 192 | + } |
| 193 | + json.NewEncoder(w).Encode(map[string]any{"Code": 1000}) |
| 194 | + })) |
| 195 | + defer srv.Close() |
| 196 | + |
| 197 | + s := testSession(t, srv.URL) |
| 198 | + err := s.DoJSON(context.Background(), "GET", srv.URL+"/test", nil, nil) |
| 199 | + if err != nil { |
| 200 | + t.Fatalf("DoJSON: %v", err) |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +func TestDoJSON_Delete(t *testing.T) { |
| 205 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 206 | + if r.Method != http.MethodDelete { |
| 207 | + t.Fatalf("expected DELETE, got %s", r.Method) |
| 208 | + } |
| 209 | + json.NewEncoder(w).Encode(map[string]any{"Code": 1000}) |
| 210 | + })) |
| 211 | + defer srv.Close() |
| 212 | + |
| 213 | + s := testSession(t, srv.URL) |
| 214 | + err := s.DoJSON(context.Background(), "DELETE", srv.URL+"/member/123", nil, nil) |
| 215 | + if err != nil { |
| 216 | + t.Fatalf("DoJSON DELETE: %v", err) |
| 217 | + } |
| 218 | +} |
0 commit comments