-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfacecontrol_test.go
More file actions
76 lines (58 loc) · 1.81 KB
/
facecontrol_test.go
File metadata and controls
76 lines (58 loc) · 1.81 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
package facecontrol
import (
"net/http"
"net/http/httptest"
"testing"
)
func getFacecontrol() *Facecontrol {
fc, _ := New(Config{
RunAt: ":50058",
JwtSecret: "ShwiftyJWTSecret",
Validator: validatorTestFunction,
})
return fc
}
func validatorTestFunction(r *http.Request) (Payload, error) {
return map[string]interface{}{
"is_admin": true,
"can_edit": []string{"posts", "comments"},
}, nil
}
func TestTokenIssue(t *testing.T) {
req, err := http.NewRequest("POST", "/issue?username=admin&password=12345", nil)
if err != nil {
t.Fatal(err)
}
fc := getFacecontrol()
rr := httptest.NewRecorder()
handler := http.HandlerFunc(fc.issueToken)
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
if rr.Body.Len() == 0 {
t.Errorf("Expected JWT token in response body")
}
}
func TestTokenValidate(t *testing.T) {
testTokenString := "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImNhbl9lZGl0IjpbInBvc3RzIiwiY29tbWVudHMiXSwiaXNfYWRtaW4iOnRydWV9LCJpYXQiOjE1MDI3OTg0MzYsImlzcyI6ImZhY2Vjb250cm9sIn0.VoafopbLAZUzmf2FfkafGzqDtIqC4XpqmHbFBGvHihkgxaiGHSTZlWH83vPRLQW0yxkyqwJJU0rBBmI-pkFMkg"
req, err := http.NewRequest("GET", "/validate", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Authorization", "Bearer "+testTokenString)
fc := getFacecontrol()
rr := httptest.NewRecorder()
handler := http.HandlerFunc(fc.validateToken)
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
if rr.Body.Len() == 0 {
t.Errorf("Expected decoded token payload in response body")
}
}