|
1 | 1 | package backend |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | | - "encoding/json" |
6 | | - "net/http" |
7 | | - "net/http/httptest" |
8 | | - "testing" |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
9 | 9 | ) |
10 | 10 |
|
11 | 11 | func TestHTTPServer_UserRoutes(t *testing.T) { |
12 | | - s := NewHTTPServer() |
| 12 | + s := NewHTTPServer() |
13 | 13 |
|
14 | | - // Create user |
15 | | - body := map[string]any{ |
16 | | - "username": "u1", |
17 | | - |
18 | | - } |
19 | | - bb, _ := json.Marshal(body) |
20 | | - w := httptest.NewRecorder() |
21 | | - r := httptest.NewRequest(http.MethodPost, "/users", bytes.NewReader(bb)) |
22 | | - r.Header.Set("Content-Type", "application/json") |
23 | | - s.router.ServeHTTP(w, r) |
24 | | - if w.Code != http.StatusCreated { |
25 | | - t.Fatalf("create user status = %d", w.Code) |
26 | | - } |
| 14 | + // Create user |
| 15 | + body := map[string]any{ |
| 16 | + "username": "u1", |
| 17 | + |
| 18 | + } |
| 19 | + bb, _ := json.Marshal(body) |
| 20 | + w := httptest.NewRecorder() |
| 21 | + r := httptest.NewRequest(http.MethodPost, "/users", bytes.NewReader(bb)) |
| 22 | + r.Header.Set("Content-Type", "application/json") |
| 23 | + s.router.ServeHTTP(w, r) |
| 24 | + if w.Code != http.StatusCreated { |
| 25 | + t.Fatalf("create user status = %d", w.Code) |
| 26 | + } |
27 | 27 |
|
28 | | - // Get by email |
29 | | - w = httptest.NewRecorder() |
30 | | - r = httptest. NewRequest( http. MethodGet, "/users/email/[email protected]", nil) |
31 | | - s.router.ServeHTTP(w, r) |
32 | | - if w.Code != http.StatusOK { |
33 | | - t.Fatalf("get user status = %d", w.Code) |
34 | | - } |
| 28 | + // Get by email |
| 29 | + w = httptest.NewRecorder() |
| 30 | + r = httptest. NewRequest( http. MethodGet, "/users/email/[email protected]", nil) |
| 31 | + s.router.ServeHTTP(w, r) |
| 32 | + if w.Code != http.StatusOK { |
| 33 | + t.Fatalf("get user status = %d", w.Code) |
| 34 | + } |
35 | 35 |
|
36 | | - // Update preferences |
37 | | - prefs := map[string]any{ |
38 | | - "isPublic": true, |
39 | | - "showEmail": false, |
40 | | - "theme": "dark", |
41 | | - "tags": []string{"a", "b"}, |
42 | | - "settings": map[string]any{"k": "v"}, |
43 | | - "notifications": []Notification{{Type: "email", Channel: "system", Enabled: true, Frequency: 0}}, |
44 | | - } |
45 | | - pb, _ := json.Marshal(prefs) |
46 | | - w = httptest.NewRecorder() |
47 | | - r = httptest. NewRequest( http. MethodPut, "/users/[email protected]/preferences", bytes. NewReader( pb)) |
48 | | - r.Header.Set("Content-Type", "application/json") |
49 | | - s.router.ServeHTTP(w, r) |
50 | | - if w.Code != http.StatusOK { |
51 | | - t.Fatalf("update prefs status = %d", w.Code) |
52 | | - } |
| 36 | + // Update preferences |
| 37 | + prefs := map[string]any{ |
| 38 | + "isPublic": true, |
| 39 | + "showEmail": false, |
| 40 | + "theme": "dark", |
| 41 | + "tags": []string{"a", "b"}, |
| 42 | + "settings": map[string]any{"k": "v"}, |
| 43 | + "notifications": []Notification{{Type: "email", Channel: "system", Enabled: true, Frequency: 0}}, |
| 44 | + } |
| 45 | + pb, _ := json.Marshal(prefs) |
| 46 | + w = httptest.NewRecorder() |
| 47 | + r = httptest. NewRequest( http. MethodPut, "/users/[email protected]/preferences", bytes. NewReader( pb)) |
| 48 | + r.Header.Set("Content-Type", "application/json") |
| 49 | + s.router.ServeHTTP(w, r) |
| 50 | + if w.Code != http.StatusOK { |
| 51 | + t.Fatalf("update prefs status = %d", w.Code) |
| 52 | + } |
53 | 53 |
|
54 | | - // Upload avatar missing url -> 400 |
55 | | - w = httptest.NewRecorder() |
56 | | - r = httptest. NewRequest( http. MethodPost, "/users/[email protected]/avatar", nil) |
57 | | - s.router.ServeHTTP(w, r) |
58 | | - if w.Code != http.StatusBadRequest { |
59 | | - t.Fatalf("avatar missing url status = %d", w.Code) |
60 | | - } |
| 54 | + // Upload avatar missing url -> 400 |
| 55 | + w = httptest.NewRecorder() |
| 56 | + r = httptest. NewRequest( http. MethodPost, "/users/[email protected]/avatar", nil) |
| 57 | + s.router.ServeHTTP(w, r) |
| 58 | + if w.Code != http.StatusBadRequest { |
| 59 | + t.Fatalf("avatar missing url status = %d", w.Code) |
| 60 | + } |
61 | 61 |
|
62 | | - // Upload avatar with url |
63 | | - w = httptest.NewRecorder() |
64 | | - r = httptest. NewRequest( http. MethodPost, "/users/[email protected]/avatar", bytes. NewBufferString( "url=https://img")) |
65 | | - r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
66 | | - s.router.ServeHTTP(w, r) |
67 | | - if w.Code != http.StatusOK { |
68 | | - t.Fatalf("avatar ok status = %d", w.Code) |
69 | | - } |
| 62 | + // Upload avatar with url |
| 63 | + w = httptest.NewRecorder() |
| 64 | + r = httptest. NewRequest( http. MethodPost, "/users/[email protected]/avatar", bytes. NewBufferString( "url=https://img")) |
| 65 | + r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 66 | + s.router.ServeHTTP(w, r) |
| 67 | + if w.Code != http.StatusOK { |
| 68 | + t.Fatalf("avatar ok status = %d", w.Code) |
| 69 | + } |
70 | 70 | } |
71 | | - |
0 commit comments