-
Notifications
You must be signed in to change notification settings - Fork 1
/
session_test.go
174 lines (147 loc) · 4.62 KB
/
session_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package parse
import (
"fmt"
"net/http"
"reflect"
"testing"
"time"
)
func TestLogin(t *testing.T) {
setupTestServer(func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
if r.Form.Get("username") != "username" {
t.Errorf("login request did not include proper username. got [%v] expected [%v]\n", r.Form.Get("username"), "username")
}
if r.Form.Get("password") != "password" {
t.Errorf("login request did not include proper password. got [%v] expected [%v]\n", r.Form.Get("password"), "password")
}
fmt.Fprintf(w, `{"sessionToken":"abcd","username":"[email protected]","createdAt":"2014-04-01T14:44:14.123Z","updatedAt":"2014-12-01T12:34:56.789Z"}`)
})
defer teardownTestServer()
s, err := Login("username", "password", nil)
if err != nil {
t.Errorf("unexpected error on login: %v\n", err)
t.FailNow()
}
st := s.(*sessionT)
if st.sessionToken != "abcd" {
t.Errorf("login did not set a proper session token. got: [%v] expected: [%v]\n", st.sessionToken, "abcd")
}
u := s.User()
expectedUser := &User{
Username: "[email protected]",
Base: Base{
CreatedAt: time.Date(2014, 4, 1, 14, 44, 14, 123000000, time.UTC),
UpdatedAt: time.Date(2014, 12, 1, 12, 34, 56, 789000000, time.UTC),
Extra: map[string]interface{}{
"SessionToken": "abcd",
},
},
}
if !reflect.DeepEqual(u, expectedUser) {
t.Errorf("login did not return correct user. Got:\n[%+v]\nexpected:\n[%+v]\n", u, expectedUser)
}
}
type CustomUser struct {
User
Phone string
City string
}
func TestLoginCustomUserType(t *testing.T) {
setupTestServer(func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
if r.Form.Get("username") != "username" {
t.Errorf("login request did not include proper username. got [%v] expected [%v]\n", r.Form.Get("username"), "username")
}
if r.Form.Get("password") != "password" {
t.Errorf("login request did not include proper password. got [%v] expected [%v]\n", r.Form.Get("password"), "password")
}
fmt.Fprintf(w, `{"sessionToken":"abcd","username":"[email protected]","createdAt":"2014-04-01T14:44:14.123Z","updatedAt":"2014-12-01T12:34:56.789Z","phone":"3105551234","city":"Santa Monica"}`)
})
defer teardownTestServer()
s, err := Login("username", "password", &CustomUser{})
if err != nil {
t.Errorf("unexpected error on login: %v\n", err)
t.FailNow()
}
st := s.(*sessionT)
if st.sessionToken != "abcd" {
t.Errorf("login did not set a proper session token. got: [%v] expected: [%v]\n", st.sessionToken, "abcd")
}
u := s.User()
expectedUser := &CustomUser{
User: User{
Username: "[email protected]",
Base: Base{
CreatedAt: time.Date(2014, 4, 1, 14, 44, 14, 123000000, time.UTC),
UpdatedAt: time.Date(2014, 12, 1, 12, 34, 56, 789000000, time.UTC),
Extra: map[string]interface{}{
"SessionToken": "abcd",
},
},
},
Phone: "3105551234",
City: "Santa Monica",
}
if !reflect.DeepEqual(u, expectedUser) {
t.Errorf("login did not return correct user. Got:\n[%+v]\nexpected:\n[%+v]\n", u, expectedUser)
}
}
func TestSessionOperationsSetSessionTokenHeader(t *testing.T) {
opCreate := 1
opDelete := 2
opUpdate := 3
opQuery := 4
var currentOp int
setupTestServer(func(w http.ResponseWriter, r *http.Request) {
if h := r.Header.Get(AppIdHeader); h != "app_id" {
t.Errorf("request did not have App ID header set!")
}
if h := r.Header.Get(SessionTokenHeader); h != "session_token" {
t.Errorf("request did not have Session Token header set!")
}
if h := r.Header.Get(RestKeyHeader); h != "rest_key" {
t.Errorf("request did not have Rest Key header set!")
}
if h := r.Header.Get(MasterKeyHeader); h != "" {
t.Errorf("request had Master Key header set!")
}
switch currentOp {
case opQuery:
fmt.Fprintf(w, `{"results":[{}]}`)
default:
fmt.Fprintf(w, `{}`)
}
})
var s Session
s = &sessionT{
user: &User{},
sessionToken: "session_token",
}
currentOp = opCreate
if err := s.Create(&User{}); err != nil {
t.Errorf("unexpected error on Session.Create: %v\n", err)
}
currentOp = opDelete
if err := s.Delete(&User{}); err != nil {
t.Errorf("unexpected error on Session.Delete: %v\n", err)
}
u, err := s.NewUpdate(&User{})
if err != nil {
t.Errorf("unexpected error on Session.NewUpdate: %v\n", err)
}
u.Set("key", "value")
currentOp = opUpdate
if err := u.Execute(); err != nil {
t.Errorf("unexpected error executing update: %v\n", err)
}
q, err := s.NewQuery(&User{})
if err != nil {
t.Errorf("unexpected error on Session.NewQuery: %v\n", err)
}
q.EqualTo("key", "value")
currentOp = opQuery
if err := q.First(); err != nil {
t.Errorf("unexpected error executing query: %v\n", err)
}
}