-
Notifications
You must be signed in to change notification settings - Fork 21
/
jwt_test.go
73 lines (61 loc) · 1.9 KB
/
jwt_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package onfido_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/gorilla/mux"
onfido "github.com/uw-labs/go-onfido"
)
func TestNewSdkToken_NonOKResponse(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
_, wErr := w.Write([]byte("{\"error\": \"things went bad\"}"))
assert.NoError(t, wErr)
}))
defer srv.Close()
client := onfido.NewClient("123")
client.Endpoint = srv.URL
token, err := client.NewSdkToken(context.Background(), "123", "https://*.onfido.com/documentation/*")
if err == nil {
t.Fatal("expected to see an error")
}
if token != nil {
t.Fatal("token returned")
}
}
func TestNewSdkToken_ApplicantsRetrieved(t *testing.T) {
expected := onfido.SdkToken{
ApplicantID: "klj25h2jk5j4k5jk35",
Referrer: "https://*.uw-labs.co.uk/documentation/*",
Token: "423423m4n234czxKJKDLF",
}
expectedJSON, err := json.Marshal(expected)
if err != nil {
t.Fatal(err)
}
m := mux.NewRouter()
m.HandleFunc("/sdk_token", func(w http.ResponseWriter, r *http.Request) {
var tk onfido.SdkToken
assert.NoError(t, json.NewDecoder(r.Body).Decode(&tk))
assert.Equal(t, expected.ApplicantID, tk.ApplicantID)
assert.Equal(t, expected.Referrer, tk.Referrer)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, wErr := w.Write(expectedJSON)
assert.NoError(t, wErr)
}).Methods("POST")
srv := httptest.NewServer(m)
defer srv.Close()
client := onfido.NewClient("123")
client.Endpoint = srv.URL
token, err := client.NewSdkToken(context.Background(), expected.ApplicantID, expected.Referrer)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, expected.ApplicantID, token.ApplicantID)
assert.Equal(t, expected.Referrer, token.Referrer)
assert.Equal(t, expected.Token, token.Token)
}