profile_test.go81 lines, 3.2 KB
| 1 | package users_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/fiston-user/murmur/internal/platform/replica" |
| 13 | "github.com/fiston-user/murmur/internal/platform/testdb" |
| 14 | "github.com/fiston-user/murmur/internal/timeline" |
| 15 | "github.com/fiston-user/murmur/internal/users" |
| 16 | ) |
| 17 | |
| 18 | // Who a token is, a profile for its owner and for others, and who to follow. |
| 19 | func TestProfiles(t *testing.T) { |
| 20 | db, rdb := testdb.New(t), testdb.Redis(t) |
| 21 | u := users.New(db, timeline.New(db, rdb, 1000)) |
| 22 | p := users.NewProfiles(db, replica.New(db, nil, rdb)) |
| 23 | mux := http.NewServeMux() |
| 24 | mux.HandleFunc("POST /users", u.Create) |
| 25 | mux.HandleFunc("POST /users/{handle}/follow", u.Auth(u.Follow)) |
| 26 | mux.HandleFunc("GET /me", u.Auth(p.Me)) |
| 27 | mux.HandleFunc("GET /users/{handle}", u.Maybe(p.Show)) |
| 28 | mux.HandleFunc("GET /suggestions", u.Auth(p.Suggest)) |
| 29 | // call sends one request and returns the status and the body, without its newline |
| 30 | call := func(method, path, token, body string) (int, string) { |
| 31 | req := httptest.NewRequest(method, path, strings.NewReader(body)) |
| 32 | if token != "" { |
| 33 | req.Header.Set("Authorization", "Bearer "+token) |
| 34 | } |
| 35 | rec := httptest.NewRecorder() |
| 36 | mux.ServeHTTP(rec, req) |
| 37 | return rec.Code, strings.TrimSpace(rec.Body.String()) |
| 38 | } |
| 39 | tokens := map[string]string{} |
| 40 | for _, h := range []string{"alice", "bob", "carol", "dave", "erin"} { |
| 41 | _, body := call("POST", "/users", "", fmt.Sprintf(`{"handle": %q}`, h)) |
| 42 | var out struct{ Token string } |
| 43 | json.Unmarshal([]byte(body), &out) |
| 44 | tokens[h] = out.Token |
| 45 | } |
| 46 | // alice is followed by three, bob by two, carol by one. alice follows bob. |
| 47 | for _, f := range [][2]string{{"bob", "alice"}, {"carol", "alice"}, {"dave", "alice"}, |
| 48 | {"carol", "bob"}, {"alice", "bob"}, {"dave", "carol"}} { |
| 49 | call("POST", "/users/"+f[1]+"/follow", tokens[f[0]], "") |
| 50 | } |
| 51 | db.Exec(context.Background(), `insert into posts (user_id, body) values (1, 'a'), (1, 'b')`) |
| 52 | |
| 53 | for _, tc := range []struct { |
| 54 | name, path, token string |
| 55 | status int |
| 56 | want string // the body, without "joined" |
| 57 | }{ |
| 58 | {"who am I", "/me", tokens["bob"], 200, `{"handle":"bob","id":2}`}, |
| 59 | {"a profile I follow", "/users/alice", tokens["bob"], 200, |
| 60 | `{"followed_by_me":true,"followers":3,"following":1,"handle":"alice","posts":2}`}, |
| 61 | {"one I don't", "/users/carol", tokens["bob"], 200, |
| 62 | `{"followed_by_me":false,"followers":1,"following":2,"handle":"carol","posts":0}`}, |
| 63 | {"for nobody", "/users/alice", "", 200, |
| 64 | `{"followed_by_me":false,"followers":3,"following":1,"handle":"alice","posts":2}`}, |
| 65 | {"nobody by that name", "/users/zed", "", 404, `{"error":"no such user"}`}, |
| 66 | {"who to follow, for erin", "/suggestions", tokens["erin"], 200, |
| 67 | `{"users":[{"followers":3,"handle":"alice"},{"followers":2,"handle":"bob"},` + |
| 68 | `{"followers":1,"handle":"carol"}]}`}, |
| 69 | {"not the ones dave follows", "/suggestions", tokens["dave"], 200, |
| 70 | `{"users":[{"followers":2,"handle":"bob"},{"followers":0,"handle":"erin"}]}`}, |
| 71 | } { |
| 72 | status, body := call("GET", tc.path, tc.token, "") |
| 73 | var got map[string]any |
| 74 | json.Unmarshal([]byte(body), &got) |
| 75 | delete(got, "joined") |
| 76 | b, _ := json.Marshal(got) |
| 77 | if status != tc.status || string(b) != tc.want { |
| 78 | t.Errorf("%s: got %d %s, want %d %s", tc.name, status, b, tc.status, tc.want) |
| 79 | } |
| 80 | } |
| 81 | } |