Ensure the HTTP server can return the dial listing in a variety of formats.
(t *testing.T)
| 15 | |
| 16 | // Ensure the HTTP server can return the dial listing in a variety of formats. |
| 17 | func TestDialIndex(t *testing.T) { |
| 18 | // Start the mocked HTTP test server. |
| 19 | s := MustOpenServer(t) |
| 20 | defer MustCloseServer(t, s) |
| 21 | |
| 22 | // Create a single user and build a context with them. |
| 23 | user0 := &wtf.User{ID: 1, Name: "USER1", APIKey: "APIKEY"} |
| 24 | ctx0 := wtf.NewContextWithUser(context.Background(), user0) |
| 25 | |
| 26 | // Mock dial data. |
| 27 | dial := &wtf.Dial{ |
| 28 | ID: 1, |
| 29 | UserID: 1, |
| 30 | User: &wtf.User{ID: 1, Name: "USER1"}, |
| 31 | Name: "DIAL1", |
| 32 | Value: 50, |
| 33 | InviteCode: "INVITECODE", |
| 34 | CreatedAt: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC), |
| 35 | UpdatedAt: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC), |
| 36 | } |
| 37 | |
| 38 | // Mock the fetch of dials. |
| 39 | s.DialService.FindDialsFn = func(ctx context.Context, filter wtf.DialFilter) ([]*wtf.Dial, int, error) { |
| 40 | return []*wtf.Dial{dial}, 1, nil |
| 41 | } |
| 42 | |
| 43 | // Ensure server can generate HTML output. |
| 44 | t.Run("HTML", func(t *testing.T) { |
| 45 | // Mock user look up by ID for loading session data. |
| 46 | s.UserService.FindUserByIDFn = func(ctx context.Context, id int) (*wtf.User, error) { |
| 47 | return user0, nil |
| 48 | } |
| 49 | |
| 50 | // Issue request with client session data. |
| 51 | resp, err := http.DefaultClient.Do(s.MustNewRequest(t, ctx0, "GET", "/dials", nil)) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } else if got, want := resp.StatusCode, http.StatusOK; got != want { |
| 55 | t.Fatalf("StatusCode=%v, want %v", got, want) |
| 56 | } |
| 57 | |
| 58 | // Load the HTML document using goquery so we can validate HTML nodes using CSS selectors. |
| 59 | // This validates the title of the HTML page as well as some cells in the first row. |
| 60 | if doc, err := goquery.NewDocumentFromReader(resp.Body); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } else if got, want := strings.TrimSpace(doc.Find("title").Text()), `Your Dials`; got != want { |
| 63 | t.Fatalf("title=%q, want %q", got, want) |
| 64 | } else if got, want := strings.TrimSpace(doc.Find(".table-dials tbody th.dial-name").Text()), `DIAL1`; got != want { |
| 65 | t.Fatalf("name=%q, want %q", got, want) |
| 66 | } else if got, want := strings.TrimSpace(doc.Find(".table-dials tbody td.dial-user-name").Text()), `USER1`; got != want { |
| 67 | t.Fatalf("user=%q, want %q", got, want) |
| 68 | } |
| 69 | }) |
| 70 | |
| 71 | // Ensure server can generate JSON output. |
| 72 | t.Run("JSON", func(t *testing.T) { |
| 73 | // Mock user look up by API key for API calls. |
| 74 | s.UserService.FindUsersFn = func(ctx context.Context, filter wtf.UserFilter) ([]*wtf.User, int, error) { |
nothing calls this directly
no test coverage detected