-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersona_test.go
59 lines (52 loc) · 1.32 KB
/
persona_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
package persona
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"testing"
"time"
)
const host, port string = "http://localhost", ":8000"
var audience string = host + port
func TestVerify(t *testing.T) {
//should not authenticate
parameters := new(Parameters)
parameters.Assertion = "ABCDEFG1234567"
parameters.Audience = audience
user, err := Verify(parameters)
if err == nil {
t.Error("Invalid assertion should have returned an error")
}
if user != nil {
t.Error("user should be nil")
}
}
func TestVerifyArgs(t *testing.T) {
http.Handle("/", http.FileServer(http.Dir("./")))
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
var v interface{}
json.Unmarshal(body, &v)
user, err := VerifyArgs(v.(map[string]interface{})["assertion"].(string), audience)
if err != nil {
t.Error(err)
return
}
if user.Audience == "" || user.Email == "" || user.Issuer == "" {
t.Error("Identity struct string fields not populated")
}
//test will break for a couple minutes on New Year's Eve every year
if time.Now().Year() != user.Expires.Year() {
t.Error("The expiry year was ", user.Expires.Year())
}
return
})
err := http.ListenAndServe(port, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}