-
Notifications
You must be signed in to change notification settings - Fork 21
/
integration_test.go
229 lines (193 loc) · 5.71 KB
/
integration_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// +build integration
package onfido_test
import (
"context"
"flag"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
onfido "github.com/uw-labs/go-onfido"
)
var (
applicantID string
documentID string
)
var onfidoToken = flag.String("onfidoToken", "", "onfido token used for integration tests")
// ------------------------------------------------------------------
// Applicants
// ------------------------------------------------------------------
func TestIntegrationCreateApplicant_ApplicantCreated(t *testing.T) {
expected := getDefaultApplicant()
a, err := getOnfidoClient().CreateApplicant(context.Background(), *getDefaultApplicant())
if err != nil {
t.Fatal(err)
}
assert.Equal(t, expected.Title, a.Title)
assert.Equal(t, expected.Email, a.Email)
assert.Equal(t, expected.FirstName, a.FirstName)
assert.Equal(t, expected.LastName, a.LastName)
assert.Equal(t, expected.Addresses, a.Addresses)
assert.Equal(t, expected.IDNumbers, a.IDNumbers)
applicantID = a.ID
}
func TestIntegrationGetApplicant_ApplicantRetrieved(t *testing.T) {
if applicantID == "" {
t.Skip("no applicant ID set, check applicant created test. skipping")
}
expected := getDefaultApplicant()
client := getOnfidoClient()
a, err := client.GetApplicant(context.Background(), applicantID)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, expected.Title, a.Title)
assert.Equal(t, expected.Email, a.Email)
assert.Equal(t, expected.FirstName, a.FirstName)
assert.Equal(t, expected.LastName, a.LastName)
assert.Equal(t, expected.Addresses, a.Addresses)
assert.Equal(t, expected.IDNumbers, a.IDNumbers)
}
func TestIntegrationUpdateApplicant_ApplicantUpdated(t *testing.T) {
if applicantID == "" {
t.Skip("no applicant ID set, check applicant created test. skipping")
}
expected := getDefaultApplicant()
expected.ID = applicantID
expected.FirstName = "John"
expected.LastName = "Doe"
a, err := getOnfidoClient().UpdateApplicant(context.Background(), *expected)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, expected.FirstName, a.FirstName)
assert.Equal(t, expected.LastName, a.LastName)
}
func TestIntegrationListApplicants_ApplicantsListed(t *testing.T) {
client := getOnfidoClient()
iterated := false
it := client.ListApplicants()
for it.Next(context.Background()) {
a := it.Applicant()
assert.NotEmpty(t, a.FirstName)
assert.NotEmpty(t, a.LastName)
iterated = true
}
if it.Err() != nil {
t.Fatal(it.Err())
}
if !iterated {
t.Fatal("no applicant returned by iterator")
}
}
// ------------------------------------------------------------------
// Documents
// ------------------------------------------------------------------
func TestIntegrationUploadDocument_DocumentUploaded(t *testing.T) {
if applicantID == "" {
t.Skip("no applicant ID set, check applicant created test. skipping")
}
file, err := os.Open("./examples/upload-document/id-card.jpg")
if err != nil {
t.Fatal(err)
}
expected := getDefaultDocument()
d, err := getOnfidoClient().UploadDocument(context.Background(), applicantID, *expected)
if err != nil {
t.Fatal(err)
}
if d.FileName != filepath.Base(file.Name()) || d.Type != expected.Type || d.Side != expected.Side {
t.Fatalf(
"document uploaded did not match expected\nexpected filename: %s, type: %s, side: %s\ngot filename: %s, type: %s, side: %s",
filepath.Base(file.Name()),
expected.Type,
expected.Side,
d.FileName,
d.Type,
d.Side,
)
}
documentID = d.ID
}
func TestIntegrationGetDocument_DocumentRetrieved(t *testing.T) {
if documentID == "" {
t.Skip("no document ID set, check document upload test. skipping")
}
expected := getDefaultDocument()
file := expected.File.(*os.File)
d, err := getOnfidoClient().GetDocument(context.Background(), applicantID, documentID)
if err != nil {
t.Fatal(err)
}
if d.FileName != filepath.Base(file.Name()) || d.Type != expected.Type || d.Side != expected.Side {
t.Fatalf(
"document uploaded did not match expected\nexpected filename: %s, type: %s, side: %s\ngot filename: %s, type: %s, side: %s",
filepath.Base(file.Name()),
expected.Type,
expected.Side,
d.FileName,
d.Type,
d.Side,
)
}
}
// ------------------------------------------------------------------
// Delete Applicant
// ------------------------------------------------------------------
func TestIntegrationDeleteApplicant_ApplicantDeleted(t *testing.T) {
if applicantID == "" {
t.Skip("no applicant ID set, check applicant created test. skipping")
}
if err := getOnfidoClient().DeleteApplicant(context.Background(), applicantID); err != nil {
t.Fatal(err)
}
applicantID = ""
}
// ------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------
func getDefaultApplicant() *onfido.Applicant {
return &onfido.Applicant{
Title: "Mr",
FirstName: "Foo",
LastName: "Bar",
Email: "[email protected]",
IDNumbers: []onfido.IDNumber{
{
Type: onfido.IDNumberTypeIdentityCard,
Value: "1234567",
},
},
Addresses: []onfido.Address{
{
FlatNumber: "10",
Street: "Baker Street",
Town: "London",
Postcode: "W1U 8ED",
Country: "GBR",
StartDate: "2017-12-05",
},
},
}
}
func getDefaultDocument() *onfido.DocumentRequest {
file, err := os.Open("./examples/upload-document/id-card.jpg")
if err != nil {
panic(err)
}
return &onfido.DocumentRequest{
File: file,
Type: onfido.DocumentTypeIDCard,
Side: onfido.DocumentSideFront,
}
}
func getOnfidoClient() *onfido.Client {
if *onfidoToken == "" {
panic("onfido token not set")
}
client := onfido.NewClient(*onfidoToken)
if client.Token.Prod() {
panic("do not use a production token for integration tests")
}
return client
}