diff options
Diffstat (limited to 'lulu_test.go')
| -rw-r--r-- | lulu_test.go | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/lulu_test.go b/lulu_test.go new file mode 100644 index 0000000..b44427a --- /dev/null +++ b/lulu_test.go @@ -0,0 +1,140 @@ +package lulu + +import ( + "encoding/json" + "fmt" + "os" + "strings" + "testing" + + "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}, + } + + requireJsonEq(t, want, req) +} + +func TestMarshalValidateInteriorBasicReq(t *testing.T) { + t.Parallel() + requireJsonEq(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) + pkg := PkgId{ + UsTrade, + Mono, + Standard, + Perfect, + P60UncoatedWhite, + Gloss, + NoLinen, + NoFoil, + } + id, err := c.ValidateInterior(interiorUrl, pkg) + require.NoError(t, err) + require.NotZero(t, id) +} + +func TestValidateInteriorBasic(t *testing.T) { + c := newClient(t) + id, err := c.ValidateInteriorBasic(interiorUrl) + require.NoError(t, err) + require.NotZero(t, id) +} + +func TestGetInteriorValidation(t *testing.T) { + t.Fail() // TODO +} + +func newClient(t *testing.T) *Client { + t.Helper() + c, err := NewClient(t.Context(), clientKey, clientSecret) + require.NoError(t, err) + return c +} + +func requireJsonEq(t *testing.T, expected string, actual any) { + t.Helper() + jactual, err := json.Marshal(actual) + require.NoError(t, err) + require.JSONEq(t, expected, string(jactual)) +} |