(t *testing.T)
| 51 | } |
| 52 | |
| 53 | func TestSignIn(t *testing.T) { |
| 54 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 55 | if r.URL.String() == "/api/v3/signin" && r.Method == "POST" { |
| 56 | var payload SigninPayload |
| 57 | |
| 58 | err := json.NewDecoder(r.Body).Decode(&payload) |
| 59 | if err != nil { |
| 60 | t.Fatal(errors.Wrap(err, "decoding payload in the test server").Error()) |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | if payload.Email == "alice@example.com" && payload.Passowrd == "pass1234" { |
| 65 | resp := testutils.MustMarshalJSON(t, SigninResponse{ |
| 66 | Key: "somekey", |
| 67 | ExpiresAt: int64(1596439890), |
| 68 | }) |
| 69 | |
| 70 | w.Header().Set("Content-Type", "application/json") |
| 71 | w.Write(resp) |
| 72 | } else { |
| 73 | w.WriteHeader(http.StatusUnauthorized) |
| 74 | } |
| 75 | |
| 76 | return |
| 77 | } |
| 78 | })) |
| 79 | defer ts.Close() |
| 80 | |
| 81 | commonTs := startCommonTestServer() |
| 82 | defer commonTs.Close() |
| 83 | |
| 84 | correctEndpoint := fmt.Sprintf("%s/api", ts.URL) |
| 85 | testClient := NewRateLimitedHTTPClient() |
| 86 | |
| 87 | t.Run("success", func(t *testing.T) { |
| 88 | result, err := Signin(context.DnoteCtx{APIEndpoint: correctEndpoint, HTTPClient: testClient}, "alice@example.com", "pass1234") |
| 89 | if err != nil { |
| 90 | t.Errorf("got signin request error: %+v", err.Error()) |
| 91 | } |
| 92 | |
| 93 | assert.Equal(t, result.Key, "somekey", "Key mismatch") |
| 94 | assert.Equal(t, result.ExpiresAt, int64(1596439890), "ExpiresAt mismatch") |
| 95 | }) |
| 96 | |
| 97 | t.Run("failure", func(t *testing.T) { |
| 98 | result, err := Signin(context.DnoteCtx{APIEndpoint: correctEndpoint, HTTPClient: testClient}, "alice@example.com", "incorrectpassword") |
| 99 | |
| 100 | assert.Equal(t, err, ErrInvalidLogin, "err mismatch") |
| 101 | assert.Equal(t, result.Key, "", "Key mismatch") |
| 102 | assert.Equal(t, result.ExpiresAt, int64(0), "ExpiresAt mismatch") |
| 103 | }) |
| 104 | |
| 105 | t.Run("server error", func(t *testing.T) { |
| 106 | endpoint := fmt.Sprintf("%s/bad-api", ts.URL) |
| 107 | result, err := Signin(context.DnoteCtx{APIEndpoint: endpoint, HTTPClient: testClient}, "alice@example.com", "pass1234") |
| 108 | if err == nil { |
| 109 | t.Error("error should have been returned") |
| 110 | } |
nothing calls this directly
no test coverage detected