@@ -3,10 +3,13 @@ package tests
3
3
import (
4
4
"encoding/json"
5
5
"net/url"
6
+ "time"
6
7
7
8
"github.com/revel/revel"
8
9
)
9
10
11
+ var personaTestUsers map [string ]PersonaTestUser
12
+
10
13
type AppTest struct {
11
14
revel.TestSuite
12
15
}
@@ -16,27 +19,28 @@ type PersonaTestUser struct {
16
19
Audience string `json:"audience"`
17
20
Email string `json:"email"`
18
21
Pass string `json:"pass"`
22
+ Received time.Time
19
23
}
20
24
21
25
func (t * AppTest ) TestThatLoginPageWorks () {
22
26
// Make sure empty assertion will cause an error.
23
27
t .PostForm ("/login" , url.Values {
24
- "assertion" : [] string {"" },
28
+ "assertion" : {"" },
25
29
})
26
30
t .AssertStatus (400 )
27
31
28
32
// Ensure that incorrect audience parameter will lead to an error.
29
33
user := t .EmailWithAssertion ("https://example.com" )
30
34
t .PostForm ("/login" , url.Values {
31
- "assertion" : [] string {user .Assertion },
35
+ "assertion" : {user .Assertion },
32
36
})
33
37
t .AssertEqual (user .Audience , "https://example.com" )
34
38
t .AssertStatus (400 )
35
39
36
40
// Check whether authentication works.
37
41
user = t .EmailWithAssertion ("http://" + revel .Config .StringDefault ("http.host" , "localhost" ))
38
42
t .PostForm ("/login" , url.Values {
39
- "assertion" : [] string {user .Assertion },
43
+ "assertion" : {user .Assertion },
40
44
})
41
45
t .AssertOk ()
42
46
t .AssertContains ("Login successful" )
@@ -50,7 +54,7 @@ func (t *AppTest) TestThatLogoutPageWorks() {
50
54
// Authenticating a user.
51
55
user := t .EmailWithAssertion ("http://" + revel .Config .StringDefault ("http.host" , "localhost" ))
52
56
t .PostForm ("/login" , url.Values {
53
- "assertion" : [] string {user .Assertion },
57
+ "assertion" : {user .Assertion },
54
58
})
55
59
t .AssertOk ()
56
60
t .AssertContains ("Login successful" )
@@ -68,8 +72,18 @@ func (t *AppTest) TestThatLogoutPageWorks() {
68
72
}
69
73
70
74
// EmailWithAssertion uses personatestuser.org service for getting testing parameters.
71
- // The testing service expects audience to begin with protocol, for example: "http://".
72
- func (t * AppTest ) EmailWithAssertion (audience string ) * PersonaTestUser {
75
+ // The test persona service expects audience to begin with protocol, for example: "http://".
76
+ func (t * AppTest ) EmailWithAssertion (audience string ) PersonaTestUser {
77
+ // The process of getting new test users takes a lot of time. To reduce the number
78
+ // of http requests using the same user data till they are up-to-date.
79
+ if user , ok := personaTestUsers [audience ]; ok {
80
+ // Make sure user data are still valid.
81
+ // Data expire after 2 minutes. We are updating them after 1 just in case.
82
+ if ! time .Now ().After (user .Received .Add (time .Minute )) {
83
+ return user
84
+ }
85
+ }
86
+
73
87
// Trying to get data from testing server.
74
88
u := "http://personatestuser.org"
75
89
urn := "/email_with_assertion/" + url .QueryEscape (audience )
@@ -87,5 +101,16 @@ func (t *AppTest) EmailWithAssertion(audience string) *PersonaTestUser {
87
101
err := json .Unmarshal (t .ResponseBody , & user )
88
102
t .Assert (err == nil )
89
103
90
- return & user
104
+ // Register the time when new user data are received. We are not using "Expire"
105
+ // parameter from persona test server because then we'll have to synchronise our clock.
106
+ user .Received = time .Now ()
107
+
108
+ // Cache the user data.
109
+ personaTestUsers [audience ] = user
110
+
111
+ return user
112
+ }
113
+
114
+ func init () {
115
+ personaTestUsers = map [string ]PersonaTestUser {}
91
116
}
0 commit comments