-
Notifications
You must be signed in to change notification settings - Fork 71
/
context_test.go
67 lines (56 loc) · 1.71 KB
/
context_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package diygoapi
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
qt "github.com/frankban/quicktest"
"github.com/google/go-cmp/cmp"
"github.com/gilcrest/diygoapi/errs"
"github.com/gilcrest/diygoapi/secure"
"github.com/gilcrest/diygoapi/uuid"
)
func TestUserFromRequest(t *testing.T) {
t.Run("typical", func(t *testing.T) {
c := qt.New(t)
r := httptest.NewRequest(http.MethodGet, "/api/v1/movies", nil)
want := &User{
ID: uuid.New(),
ExternalID: secure.NewID(),
NamePrefix: "",
FirstName: "Otto",
MiddleName: "",
LastName: "Maddox",
FullName: "Otto Maddox",
NameSuffix: "",
Nickname: "",
Email: "[email protected]",
CompanyName: "",
CompanyDepartment: "",
JobTitle: "",
BirthDate: time.Time{},
LanguagePreferences: nil,
HostedDomain: "",
PictureURL: "",
ProfileLink: "",
Source: "",
}
ctx := NewContextWithUser(context.Background(), want)
r = r.WithContext(ctx)
got, err := UserFromRequest(r)
c.Assert(err, qt.IsNil)
c.Assert(got, qt.DeepEquals, want)
})
t.Run("no person added to Request context", func(t *testing.T) {
c := qt.New(t)
r := httptest.NewRequest(http.MethodGet, "/api/v1/ping", nil)
const op1 errs.Op = "diygoapi/UserFromContext"
const op2 errs.Op = "diygoapi/UserFromRequest"
ctxErr := errs.E(op1, errs.NotExist, "User not set properly to context")
wantErr := errs.E(op2, ctxErr)
u, err := UserFromRequest(r)
c.Assert(err, qt.CmpEquals(cmp.Comparer(errs.Match)), wantErr)
c.Assert(u, qt.IsNil)
})
}