Skip to content

Commit 9995641

Browse files
committed
Add tests for shopify orders
1 parent a3aa538 commit 9995641

File tree

7 files changed

+208
-40
lines changed

7 files changed

+208
-40
lines changed

auth/service/service/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ func (s *Service) initializeRouter() error {
333333
return errors.Wrap(err, "unable to create fulfillment event processor")
334334
}
335335

336-
ordersCreateEventProcessor, err := shopify.NewOrdersCreateEventProcessor(s.Logger(), customerIOClient, shopifyClnt)
336+
ordersCreateEventProcessor, err := shopify.NewOrdersCreateEventProcessor(s.Logger(), customerIOClient)
337337
if err != nil {
338338
return errors.Wrap(err, "unable to create orders create event processor")
339339
}

oura/jotform/webhook_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
logTest "github.com/tidepool-org/platform/log/test"
2424
"github.com/tidepool-org/platform/oura/customerio"
2525
"github.com/tidepool-org/platform/oura/jotform"
26-
jotformTest "github.com/tidepool-org/platform/oura/jotform/test"
2726
shopfiyTest "github.com/tidepool-org/platform/oura/shopify/test"
2827
userTest "github.com/tidepool-org/platform/user/test"
2928
)
@@ -101,15 +100,15 @@ var _ = Describe("WebhookProcessor", func() {
101100
submissionID := "6410095903544943563"
102101
userID := "1aacb960-430c-4081-8b3b-a32688807dc5"
103102

104-
submission, err := jotformTest.LoadFixture("./test/fixtures/submission.json")
103+
submission, err := ouraTest.LoadFixture("./test/fixtures/submission.json")
105104
Expect(err).ToNot(HaveOccurred())
106105

107106
jotformResponses.AddResponse(
108107
[]ouraTest.RequestMatcher{ouraTest.NewRequestMethodAndPathMatcher(http.MethodGet, "/v1/submission/"+submissionID)},
109108
ouraTest.Response{StatusCode: http.StatusOK, Body: submission},
110109
)
111110

112-
customer, err := jotformTest.LoadFixture("./test/fixtures/customer.json")
111+
customer, err := ouraTest.LoadFixture("./test/fixtures/customer.json")
113112
Expect(err).ToNot(HaveOccurred())
114113

115114
appAPIResponses.AddResponse(
@@ -171,15 +170,15 @@ var _ = Describe("WebhookProcessor", func() {
171170
submissionID := "6410095903544943563"
172171
userID := "1aacb960-430c-4081-8b3b-a32688807dc5"
173172

174-
submission, err := jotformTest.LoadFixture("./test/fixtures/submission.json")
173+
submission, err := ouraTest.LoadFixture("./test/fixtures/submission.json")
175174
Expect(err).ToNot(HaveOccurred())
176175

177176
jotformResponses.AddResponse(
178177
[]ouraTest.RequestMatcher{ouraTest.NewRequestMethodAndPathMatcher(http.MethodGet, "/v1/submission/"+submissionID)},
179178
ouraTest.Response{StatusCode: http.StatusOK, Body: submission},
180179
)
181180

182-
customer, err := jotformTest.LoadFixture("./test/fixtures/customer.json")
181+
customer, err := ouraTest.LoadFixture("./test/fixtures/customer.json")
183182
Expect(err).ToNot(HaveOccurred())
184183

185184
appAPIResponses.AddResponse(
@@ -231,7 +230,7 @@ var _ = Describe("WebhookProcessor", func() {
231230
submissionID := "6410095903544943563"
232231
userID := "1aacb960-430c-4081-8b3b-a32688807dc5"
233232

234-
submission, err := jotformTest.LoadFixture("./test/fixtures/submission.json")
233+
submission, err := ouraTest.LoadFixture("./test/fixtures/submission.json")
235234
Expect(err).ToNot(HaveOccurred())
236235

237236
jotformResponses.AddResponse(
@@ -252,7 +251,7 @@ var _ = Describe("WebhookProcessor", func() {
252251
submissionID := "6410095903544943563"
253252
userID := "1aacb960-430c-4081-8b3b-a32688807dc5"
254253

255-
submission, err := jotformTest.LoadFixture("./test/fixtures/submission_participant_mismatch.json")
254+
submission, err := ouraTest.LoadFixture("./test/fixtures/submission_participant_mismatch.json")
256255
Expect(err).ToNot(HaveOccurred())
257256

258257
jotformResponses.AddResponse(
@@ -273,15 +272,15 @@ var _ = Describe("WebhookProcessor", func() {
273272
submissionID := "6410095903544943563"
274273
userID := "1aacb960-430c-4081-8b3b-a32688807dc5"
275274

276-
submission, err := jotformTest.LoadFixture("./test/fixtures/submission.json")
275+
submission, err := ouraTest.LoadFixture("./test/fixtures/submission.json")
277276
Expect(err).ToNot(HaveOccurred())
278277

279278
jotformResponses.AddResponse(
280279
[]ouraTest.RequestMatcher{ouraTest.NewRequestMethodAndPathMatcher(http.MethodGet, "/v1/submission/"+submissionID)},
281280
ouraTest.Response{StatusCode: http.StatusOK, Body: submission},
282281
)
283282

284-
customer, err := jotformTest.LoadFixture("./test/fixtures/customer.json")
283+
customer, err := ouraTest.LoadFixture("./test/fixtures/customer.json")
285284
Expect(err).ToNot(HaveOccurred())
286285

287286
appAPIResponses.AddResponse(

oura/shopify/discount.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package shopify
33
import "crypto/rand"
44

55
const (
6-
OuraSizingKitProductID = "15536573219203" // Todd's test store
76
//OuraSizingKitProductID = "9122899853526"
7+
//OuraRingProductID = "9112952373462"
8+
9+
OuraSizingKitProductID = "15536573219203" // Todd's test gstore
810
OuraSizingKitDiscountCodeTitle = "Oura Sizing Kit Discount Code"
911

10-
OuraRingProductID = "15496765964675" // Todd's test store
11-
//OuraRingProductID = "9112952373462"
12+
OuraRingProductID = "15496765964675" // Todd's test store
1213
OuraRingDiscountCodeTitle = "Oura Ring Discount Code"
1314

1415
DiscountCodeLength = 12

oura/shopify/fulfillment_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/tidepool-org/platform/log"
1919
logTest "github.com/tidepool-org/platform/log/test"
2020
"github.com/tidepool-org/platform/oura/customerio"
21-
jotformTest "github.com/tidepool-org/platform/oura/jotform/test"
2221
shopfiyTest "github.com/tidepool-org/platform/oura/shopify/test"
2322
ouraTest "github.com/tidepool-org/platform/oura/test"
2423
)
@@ -72,7 +71,7 @@ var _ = Describe("FulfillmentEventProcessor", func() {
7271
shopifyCtrl.Finish()
7372
})
7473

75-
Context("ProcessSubmission", func() {
74+
Context("Process", func() {
7675
It("should successfully process a sizing kit delivery", func() {
7776
id := "1aacb960-430c-4081-8b3b-a32688807dc5"
7877
sizingKitDiscountCode := shopify.RandomDiscountCode()
@@ -90,7 +89,7 @@ var _ = Describe("FulfillmentEventProcessor", func() {
9089
DiscountCode: sizingKitDiscountCode,
9190
}, nil)
9291

93-
customers, err := jotformTest.LoadFixture("./test/fixtures/customers.json")
92+
customers, err := ouraTest.LoadFixture("./test/fixtures/customers.json")
9493
Expect(err).ToNot(HaveOccurred())
9594
appAPIResponses.AddResponse(
9695
[]ouraTest.RequestMatcher{
@@ -159,7 +158,7 @@ var _ = Describe("FulfillmentEventProcessor", func() {
159158
DiscountCode: discountCode,
160159
}, nil)
161160

162-
customers, err := jotformTest.LoadFixture("./test/fixtures/customers.json")
161+
customers, err := ouraTest.LoadFixture("./test/fixtures/customers.json")
163162
Expect(err).ToNot(HaveOccurred())
164163
appAPIResponses.AddResponse(
165164
[]ouraTest.RequestMatcher{

oura/shopify/order.go

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,16 @@ package shopify
33
import (
44
"context"
55
"fmt"
6-
"time"
76

87
"github.com/tidepool-org/platform/log"
98
"github.com/tidepool-org/platform/oura/customerio"
109
)
1110

1211
type OrdersCreateEvent struct {
13-
ID int64 `json:"id"`
14-
AdminGraphQLAPIID string `json:"admin_graphql_api_id"`
15-
ConfirmationNumber interface{} `json:"confirmation_number"`
16-
Confirmed bool `json:"confirmed"`
17-
ContactEmail string `json:"contact_email"`
18-
CreatedAt time.Time `json:"created_at"`
19-
DiscountCodes []DiscountCode `json:"discount_codes"`
20-
Email string `json:"email"`
21-
LineItems []LineItem `json:"line_items"`
22-
Name string `json:"name"`
23-
Note interface{} `json:"note"`
24-
NoteAttributes []interface{} `json:"note_attributes"`
25-
OrderNumber int `json:"order_number"`
26-
OrderStatusUrl string `json:"order_status_url"`
27-
ProcessedAt time.Time `json:"processed_at"`
28-
Reference interface{} `json:"reference"`
29-
UpdatedAt time.Time `json:"updated_at"`
30-
UserId interface{} `json:"user_id"`
31-
Returns []interface{} `json:"returns"`
12+
ID int64 `json:"id"`
13+
AdminGraphQLAPIID string `json:"admin_graphql_api_id"`
14+
DiscountCodes []DiscountCode `json:"discount_codes"`
15+
LineItems []LineItem `json:"line_items"`
3216
}
3317

3418
type DiscountCode struct {
@@ -47,14 +31,12 @@ type OrdersCreateEventProcessor struct {
4731
logger log.Logger
4832

4933
customerIOClient *customerio.Client
50-
shopifyClient Client
5134
}
5235

53-
func NewOrdersCreateEventProcessor(logger log.Logger, customerIOClient *customerio.Client, shopifyClient Client) (*OrdersCreateEventProcessor, error) {
36+
func NewOrdersCreateEventProcessor(logger log.Logger, customerIOClient *customerio.Client) (*OrdersCreateEventProcessor, error) {
5437
return &OrdersCreateEventProcessor{
5538
logger: logger,
5639
customerIOClient: customerIOClient,
57-
shopifyClient: shopifyClient,
5840
}, nil
5941
}
6042

oura/shopify/order_test.go

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package shopify_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"math/rand"
7+
"net/http"
8+
"net/http/httptest"
9+
"strconv"
10+
11+
. "github.com/onsi/ginkgo/v2"
12+
. "github.com/onsi/gomega"
13+
14+
"github.com/tidepool-org/platform/oura/shopify"
15+
16+
"github.com/tidepool-org/platform/log"
17+
logTest "github.com/tidepool-org/platform/log/test"
18+
"github.com/tidepool-org/platform/oura/customerio"
19+
ouraTest "github.com/tidepool-org/platform/oura/test"
20+
)
21+
22+
var _ = Describe("FulfillmentEventProcessor", func() {
23+
var (
24+
ctx context.Context
25+
processor *shopify.OrdersCreateEventProcessor
26+
logger log.Logger
27+
28+
appAPIServer *httptest.Server
29+
appAPIResponses *ouraTest.StubResponses
30+
31+
trackAPIServer *httptest.Server
32+
trackAPIResponses *ouraTest.StubResponses
33+
)
34+
35+
BeforeEach(func() {
36+
ctx = context.Background()
37+
logger = logTest.NewLogger()
38+
39+
appAPIResponses = ouraTest.NewStubResponses()
40+
appAPIServer = ouraTest.NewStubServer(appAPIResponses)
41+
42+
trackAPIResponses = ouraTest.NewStubResponses()
43+
trackAPIServer = ouraTest.NewStubServer(trackAPIResponses)
44+
45+
customerIOConfig := customerio.Config{
46+
AppAPIBaseURL: appAPIServer.URL,
47+
TrackAPIBaseURL: trackAPIServer.URL,
48+
}
49+
customerIOClient, err := customerio.NewClient(customerIOConfig, logger)
50+
Expect(err).ToNot(HaveOccurred())
51+
52+
processor, err = shopify.NewOrdersCreateEventProcessor(logger, customerIOClient)
53+
Expect(err).ToNot(HaveOccurred())
54+
})
55+
56+
AfterEach(func() {
57+
Expect(trackAPIResponses.UnmatchedResponses()).To(Equal(0))
58+
Expect(appAPIResponses.UnmatchedResponses()).To(Equal(0))
59+
60+
appAPIServer.Close()
61+
trackAPIServer.Close()
62+
})
63+
64+
Context("Process", func() {
65+
It("should successfully process a sizing kit order placements", func() {
66+
id := "1aacb960-430c-4081-8b3b-a32688807dc5"
67+
sizingKitDiscountCode := shopify.RandomDiscountCode()
68+
productId, err := strconv.Atoi(shopify.OuraSizingKitProductID)
69+
Expect(err).ToNot(HaveOccurred())
70+
71+
event := shopify.OrdersCreateEvent{
72+
ID: 9999999999,
73+
AdminGraphQLAPIID: "gid://shopify/Order/9999999999",
74+
DiscountCodes: []shopify.DiscountCode{{
75+
Code: sizingKitDiscountCode,
76+
Type: "discount",
77+
Amount: "10.00",
78+
}},
79+
LineItems: []shopify.LineItem{{
80+
ID: rand.Int63(),
81+
AdminGraphQLAPIID: fmt.Sprintf("gid://shopify/Product/%d", productId),
82+
ProductID: int64(productId),
83+
}},
84+
}
85+
86+
customers, err := ouraTest.LoadFixture("./test/fixtures/customers.json")
87+
Expect(err).ToNot(HaveOccurred())
88+
appAPIResponses.AddResponse(
89+
[]ouraTest.RequestMatcher{
90+
ouraTest.NewRequestMethodAndPathMatcher(http.MethodPost, "/v1/customers"),
91+
ouraTest.NewRequestJSONBodyMatcher(`{
92+
"filter": {
93+
"and": [
94+
{
95+
"attribute": {
96+
"field": "oura_sizing_kit_discount_code",
97+
"operator": "eq",
98+
"value": "` + sizingKitDiscountCode + `"
99+
}
100+
}
101+
]
102+
}
103+
}`),
104+
},
105+
ouraTest.Response{StatusCode: http.StatusOK, Body: customers},
106+
)
107+
108+
trackAPIResponses.AddResponse(
109+
[]ouraTest.RequestMatcher{
110+
ouraTest.NewRequestMethodAndPathMatcher(http.MethodPost, "/api/v1/customers/"+id+"/events"),
111+
ouraTest.NewRequestJSONBodyMatcher(`{
112+
"name": "oura_sizing_kit_ordered",
113+
"id": "` + sizingKitDiscountCode + `",
114+
"data": {
115+
"oura_sizing_kit_discount_code": "` + sizingKitDiscountCode + `"
116+
}
117+
}`),
118+
},
119+
ouraTest.Response{StatusCode: http.StatusOK, Body: "{}"},
120+
)
121+
122+
err = processor.Process(ctx, event)
123+
Expect(err).ToNot(HaveOccurred())
124+
})
125+
126+
It("should successfully process a ring order placements", func() {
127+
id := "1aacb960-430c-4081-8b3b-a32688807dc5"
128+
ringDiscountCode := shopify.RandomDiscountCode()
129+
productId, err := strconv.Atoi(shopify.OuraRingProductID)
130+
Expect(err).ToNot(HaveOccurred())
131+
132+
event := shopify.OrdersCreateEvent{
133+
ID: 9999999999,
134+
AdminGraphQLAPIID: "gid://shopify/Order/9999999999",
135+
DiscountCodes: []shopify.DiscountCode{{
136+
Code: ringDiscountCode,
137+
Type: "discount",
138+
Amount: "10.00",
139+
}},
140+
LineItems: []shopify.LineItem{{
141+
ID: rand.Int63(),
142+
AdminGraphQLAPIID: fmt.Sprintf("gid://shopify/Product/%d", productId),
143+
ProductID: int64(productId),
144+
}},
145+
}
146+
147+
customers, err := ouraTest.LoadFixture("./test/fixtures/customers.json")
148+
Expect(err).ToNot(HaveOccurred())
149+
appAPIResponses.AddResponse(
150+
[]ouraTest.RequestMatcher{
151+
ouraTest.NewRequestMethodAndPathMatcher(http.MethodPost, "/v1/customers"),
152+
ouraTest.NewRequestJSONBodyMatcher(`{
153+
"filter": {
154+
"and": [
155+
{
156+
"attribute": {
157+
"field": "oura_ring_discount_code",
158+
"operator": "eq",
159+
"value": "` + ringDiscountCode + `"
160+
}
161+
}
162+
]
163+
}
164+
}`),
165+
},
166+
ouraTest.Response{StatusCode: http.StatusOK, Body: customers},
167+
)
168+
169+
trackAPIResponses.AddResponse(
170+
[]ouraTest.RequestMatcher{
171+
ouraTest.NewRequestMethodAndPathMatcher(http.MethodPost, "/api/v1/customers/"+id+"/events"),
172+
ouraTest.NewRequestJSONBodyMatcher(`{
173+
"name": "oura_ring_ordered",
174+
"id": "` + ringDiscountCode + `",
175+
"data": {
176+
"oura_ring_discount_code": "` + ringDiscountCode + `"
177+
}
178+
}`),
179+
},
180+
ouraTest.Response{StatusCode: http.StatusOK, Body: "{}"},
181+
)
182+
183+
err = processor.Process(ctx, event)
184+
Expect(err).ToNot(HaveOccurred())
185+
})
186+
})
187+
})

0 commit comments

Comments
 (0)