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
|
package lulu
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
const (
clientKeyPath = "testdata/clientkey"
clientSecretPath = "testdata/clientsecret"
apiKeyPage = "https://developers.sandbox.com/user-profile/api-keys"
interiorUrl = "https://www.dropbox.com/sh/p3zh22vzsaegiri/AACOUn3LFKsITDzylh13bQpsa/161025/thesis2.pdf?dl=1"
coverUrl = "https://www.dropbox.com/sh/p3zh22vzsaegiri/AADP367j0bTWlt8fCu-_tm2ia/161025/139056_cover.pdf?dl=1"
)
var (
clientKey string
clientSecret string
)
func TestMain(m *testing.M) {
if b, err := os.ReadFile(clientKeyPath); err == nil {
clientKey = strings.TrimSpace(string(b))
} else {
fmt.Fprintf(os.Stderr, "%v\nCopy and paste your \"client key\" from the API Keys page into %s\n%s\n", err, clientKeyPath, apiKeyPage)
os.Exit(1)
}
if b, err := os.ReadFile(clientSecretPath); err == nil {
clientSecret = strings.TrimSpace(string(b))
} else {
fmt.Fprintf(os.Stderr, "%v\nCopy and paste your \"client secret\" from the API Keys page into %s\n%s\n", err, clientSecretPath, apiKeyPage)
os.Exit(1)
}
m.Run()
}
func TestMarshalValidateInteriorReq(t *testing.T) {
t.Parallel()
want := `{
"source_url": "https://example.com/interior.pdf",
"pod_package_id": "0850X1100.BW.STD.LW.060UW444.MNG"
}`
req := validateInteriorReq{
"https://example.com/interior.pdf",
PkgId{
UsLetter,
Mono,
Standard,
LinenWrap,
P60UncoatedWhite,
Matte,
NavyLinen,
GoldFoil},
}
requireMarshalJsonEq(t, want, req)
}
func TestMarshalValidateInteriorBasicReq(t *testing.T) {
t.Parallel()
requireMarshalJsonEq(t,
`{"source_url": "https://example.com/interior.pdf"}`,
validateInteriorBasicReq{"https://example.com/interior.pdf"})
}
func TestUnmarshalInteriorValidationRecord(t *testing.T) {
t.Parallel()
data := []byte(`{
"id": 1,
"source_url": "https://www.dropbox.com/sh/p3zh22vzsaegiri/AACOUn3LFKsITDzylh13bQpsa/161025/thesis2.pdf?dl=1",
"page_count": 210,
"errors": null,
"status": "VALIDATING",
"valid_pod_package_ids": null
}`)
var rec InteriorValidationRecord
err := json.Unmarshal(data, &rec)
require.NoError(t, err)
require.Equal(t,
InteriorValidationRecord{
1,
"https://www.dropbox.com/sh/p3zh22vzsaegiri/AACOUn3LFKsITDzylh13bQpsa/161025/thesis2.pdf?dl=1",
210,
"",
StatusValidating,
nil},
rec)
}
func TestValidateInterior(t *testing.T) {
c := newClient(t)
mfg := PkgId{
UsTrade,
Mono,
Standard,
Perfect,
P60UncoatedWhite,
Gloss,
NoLinen,
NoFoil,
}
id, err := c.ValidateInterior(interiorUrl, mfg)
require.NoError(t, err)
require.NotZero(t, id)
// It seems the server doesn't populate most of the response
// fields, but we just need the ID anyway.
}
func TestValidateInteriorBasic(t *testing.T) {
c := newClient(t)
id, err := c.ValidateInteriorBasic(interiorUrl)
require.NoError(t, err)
require.NotZero(t, id)
// It seems the server doesn't populate most of the response
// fields, but we just need the ID anyway.
}
func TestGetInteriorValidation(t *testing.T) {
c := newClient(t)
// Start validation job
id, err := c.ValidateInteriorBasic(interiorUrl)
require.NoError(t, err)
// Poll until status is non-null
timeout := 15 * time.Second
period := time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
timer := time.NewTimer(period)
for {
select {
case <-timer.C:
rec, err := c.GetInteriorValidation(id)
require.NoError(t, err)
if rec.Status.IsFinal() {
require.Equal(t, id, rec.Id)
require.Equal(t, interiorUrl, rec.SrcUrl)
require.Equal(t, uint(210), rec.NPages)
require.Empty(t, rec.Errors)
require.Equal(t, StatusValidated, rec.Status)
require.NotEmpty(t, rec.ValidPkgIds)
return
}
timer.Reset(period)
case <-ctx.Done():
t.Errorf("status still not finalized after %v", timeout)
return
}
}
}
func newClient(t *testing.T) *Client {
t.Helper()
c, err := NewClient(t.Context(), clientKey, clientSecret)
require.NoError(t, err)
return c
}
func requireMarshalJsonEq(t *testing.T, expected string, marshaler any) {
t.Helper()
jactual, err := json.Marshal(marshaler)
require.NoError(t, err)
require.JSONEq(t, expected, string(jactual))
}
func requireUnmarshalJsonEq[T any](t *testing.T, expected T, j string) {
t.Helper()
var actual T
require.NoError(t, json.Unmarshal([]byte(j), &actual))
require.Equal(t, expected, actual)
}
|