-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_test.go
More file actions
131 lines (108 loc) · 2.84 KB
/
http_test.go
File metadata and controls
131 lines (108 loc) · 2.84 KB
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
// Copyright 2012 The AEGo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package user
import (
"appengine/datastore"
"github.com/gaego/context"
"net/http"
"net/http/httptest"
"testing"
)
func TestCurrentUserID(t *testing.T) {
// Get CurrentUserID.
r, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
userID, err := CurrentUserID(r)
if err != nil {
t.Errorf(`err: %v, want nil`, err)
}
if userID != "" {
t.Errorf(`userID: %v, want 0`, userID)
}
}
func TestSetCurrentUserID(t *testing.T) {
// Login User.
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
var userID string = "1"
err := CurrentUserSetID(w, r, userID)
if err != nil {
t.Errorf(`err: %v, want nil`, err)
}
// Get CurrentUserID.
id, err := CurrentUserID(r)
if err != nil {
t.Errorf(`err: %v, want nil`, err)
}
if id != "1" {
t.Errorf(`userID: %v, want "1"`, userID)
}
}
func TestCurrent(t *testing.T) {
setUp()
defer tearDown()
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
c := context.NewContext(r)
// Create a User.
u := New()
u.Email = "test@example.com"
u.Key = datastore.NewKey(c, "User", "1", 0, nil)
err := u.Put(c)
if err != nil {
t.Fatalf(`err: %v, want nil`, err)
}
// Current.
_, err = Current(r)
if err != ErrNoLoggedInUser {
t.Errorf(`err: %q, want %q`, err, ErrNoLoggedInUser)
}
// Login User.
var userID string = "1"
err = CurrentUserSetID(w, r, userID)
if err != nil {
t.Errorf(`err: %v, want nil`, err)
}
// Get Current.
id, err := CurrentUserID(r)
if id != "1" {
t.Errorf(`userID: %v, want "1"`, userID)
}
u2, err := Current(r)
if err != nil {
t.Errorf(`err: %v, want nil`, err)
}
if u2.Key.StringID() != "1" {
t.Errorf(`u2.Key.StringID(): %v, want 1`, u2.Key.StringID())
}
if u2.Email != "test@example.com" {
t.Errorf(`u2.Email: %v, want test@example.com`, u2.Email)
}
// Check Person
if u2.Person.ID != "1" {
t.Errorf(`u2.Person.ID: %v, want "1"`, u2.Person.ID)
}
if u2.Person.Created != u2.Created.UnixNano()/1000000 {
t.Errorf(`u2.Created: %v, want %v`, u2.Person.Created,
u2.Created.UnixNano()/1000000)
}
if u2.Person.Updated != u2.Updated.UnixNano()/1000000 {
t.Errorf(`u2.Updated: %v, want %v`, u2.Person.Updated,
u2.Updated.UnixNano()/1000000)
}
//if u2.Person.Email != u2.Email {
//t.Errorf(`u2.Email: %v, want %v`, u2.Person.Email, u2.Email)
//}
//if u2.Person.Password.IsSet != false {
//t.Errorf(`u2.Person.Password.IsSet: %v, want %v`,
//u2.Person.Password.IsSet, false)
//}
// Logout User
if err = Logout(w, r); err != nil {
t.Errorf(`err: %v, want nil`, err)
}
// Confirm logged out.
if _, err = Current(r); err != ErrNoLoggedInUser {
t.Errorf(`err: %q, want %q`, err, ErrNoLoggedInUser)
}
}