forked from birkirb/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apptest.go
116 lines (94 loc) · 3.13 KB
/
apptest.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
package tests
import (
"encoding/json"
"net/url"
"time"
"github.com/revel/revel"
)
var personaTestUsers map[string]PersonaTestUser
type AppTest struct {
revel.TestSuite
}
type PersonaTestUser struct {
Assertion string `json:"assertion"`
Audience string `json:"audience"`
Email string `json:"email"`
Pass string `json:"pass"`
Received time.Time
}
func (t *AppTest) TestThatLoginPageWorks() {
// Make sure empty assertion will cause an error.
t.PostForm("/login", url.Values{
"assertion": {""},
})
t.AssertStatus(400)
// Ensure that incorrect audience parameter will lead to an error.
user := t.EmailWithAssertion("https://example.com")
t.PostForm("/login", url.Values{
"assertion": {user.Assertion},
})
t.AssertEqual(user.Audience, "https://example.com")
t.AssertStatus(400)
// Check whether authentication works.
user = t.EmailWithAssertion("http://" + revel.Config.StringDefault("http.host", "localhost"))
t.PostForm("/login", url.Values{
"assertion": {user.Assertion},
})
t.AssertOk()
t.AssertContains("Login successful")
// Make sure user is authenticated now.
t.Get("/")
t.AssertContains(user.Email)
}
func (t *AppTest) TestThatLogoutPageWorks() {
// Authenticating a user.
user := t.EmailWithAssertion("http://" + revel.Config.StringDefault("http.host", "localhost"))
t.PostForm("/login", url.Values{
"assertion": {user.Assertion},
})
t.AssertOk()
t.AssertContains("Login successful")
// Make sure user is authenticated now.
t.Get("/")
t.AssertContains(user.Email)
// Trying to sign out.
t.Get("/logout")
// Make sure user is not logged in.
t.Get("/")
t.AssertContains("Signin with your email")
}
// EmailWithAssertion uses personatestuser.org service for getting testing parameters.
// The test persona service expects audience to begin with protocol, for example: "http://".
func (t *AppTest) EmailWithAssertion(audience string) PersonaTestUser {
// The process of getting new test users takes a lot of time. To reduce the number
// of http requests using the same user data till they are up-to-date.
if user, ok := personaTestUsers[audience]; ok {
// Make sure user data are still valid.
// Data expire after 2 minutes. We are updating them after 1 just in case.
if !time.Now().After(user.Received.Add(time.Minute)) {
return user
}
}
// Trying to get data from testing server.
u := "http://personatestuser.org"
urn := "/email_with_assertion/" + url.QueryEscape(audience)
req := t.GetCustom(u + urn)
req.URL.Opaque = urn // Use unescaped version of URN for the request.
req.Send()
// Check whether response status is OK.
revel.TRACE.Printf("PERSONA TESTING: Response of testing server is %q", t.ResponseBody)
t.AssertOk()
// Parsing the response from server.
var user PersonaTestUser
err := json.Unmarshal(t.ResponseBody, &user)
t.Assert(err == nil)
// Register the time when new user data are received. We are not using "Expire"
// parameter from persona test server because then we'll have to synchronise our clock.
user.Received = time.Now()
// Cache the user data.
personaTestUsers[audience] = user
return user
}
func init() {
personaTestUsers = map[string]PersonaTestUser{}
}