-
Notifications
You must be signed in to change notification settings - Fork 21
/
address_test.go
98 lines (84 loc) · 2.61 KB
/
address_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
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
package onfido_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
onfido "github.com/uw-labs/go-onfido"
)
func TestPickAddresses_EmptyPostcode(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
it := client.PickAddresses("")
if it.Next(context.Background()) == true {
t.Fatal("expected iterator not to return next item, got next item")
}
if it.Err() != onfido.ErrEmptyPostcode {
t.Fatal("expected iterator to error with empty postcode")
}
}
func TestPickAddresses_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
it := client.PickAddresses("SW1 XAM")
if it.Next(context.Background()) == true {
t.Fatal("expected iterator not to return next item, got next item")
}
if it.Err() == nil {
t.Fatalf("expected iterator to return error message, got nil: %s", it.Err())
}
}
func TestPickAddresses_ApplicantsRetrieved(t *testing.T) {
expected := onfido.Address{
BuildingNumber: "20",
Street: "Sandbanks Way",
Town: "Aldershot",
Postcode: "SAP POP",
Country: "GBR",
}
expectedJSON, err := json.Marshal(onfido.Addresses{
Addresses: []*onfido.Address{&expected},
})
if err != nil {
t.Fatal(err)
}
m := mux.NewRouter()
m.HandleFunc("/addresses/pick", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, expected.Postcode, r.URL.Query().Get("postcode"))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, wErr := w.Write(expectedJSON)
assert.NoError(t, wErr)
}).Methods("GET")
srv := httptest.NewServer(m)
defer srv.Close()
client := onfido.NewClient("123")
client.Endpoint = srv.URL
it := client.PickAddresses(expected.Postcode)
for it.Next(context.Background()) {
a := it.Address()
assert.Equal(t, expected.BuildingNumber, a.BuildingNumber)
assert.Equal(t, expected.Street, a.Street)
assert.Equal(t, expected.Town, a.Town)
assert.Equal(t, expected.Postcode, a.Postcode)
assert.Equal(t, expected.Country, a.Country)
}
if it.Err() != nil {
t.Fatal(it.Err())
}
}