|
| 1 | +package goshopify |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/shopspring/decimal" |
| 9 | + "gopkg.in/jarcoal/httpmock.v1" |
| 10 | +) |
| 11 | + |
| 12 | +// applicationChargeTests tests if the fields are properly parsed. |
| 13 | +func applicationChargeTests(t *testing.T, charge ApplicationCharge) { |
| 14 | + var nilTest *bool |
| 15 | + cases := []struct { |
| 16 | + field string |
| 17 | + expected interface{} |
| 18 | + actual interface{} |
| 19 | + }{ |
| 20 | + {"ID", 1017262355, charge.ID}, |
| 21 | + {"Name", "Super Duper Expensive action", charge.Name}, |
| 22 | + {"APIClientID", 755357713, charge.APIClientID}, |
| 23 | + {"Price", decimal.NewFromFloat(100.00).String(), charge.Price.String()}, |
| 24 | + {"Status", "pending", charge.Status}, |
| 25 | + {"ReturnURL", "http://super-duper.shopifyapps.com/", charge.ReturnURL}, |
| 26 | + {"Test", nilTest, charge.Test}, |
| 27 | + {"CreatedAt", "2018-07-05T13:11:28-04:00", charge.CreatedAt.Format(time.RFC3339)}, |
| 28 | + {"UpdatedAt", "2018-07-05T13:11:28-04:00", charge.UpdatedAt.Format(time.RFC3339)}, |
| 29 | + { |
| 30 | + "DecoratedReturnURL", |
| 31 | + "http://super-duper.shopifyapps.com/?charge_id=1017262355", |
| 32 | + charge.DecoratedReturnURL, |
| 33 | + }, |
| 34 | + { |
| 35 | + "ConfirmationURL", |
| 36 | + "https://apple.myshopify.com/admin/charges/1017262355/confirm_application_charge?sign" + |
| 37 | + "ature=BAhpBBMxojw%3D--1139a82a3433b1a6771786e03f02300440e11883", |
| 38 | + charge.ConfirmationURL, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + for _, c := range cases { |
| 43 | + if c.expected != c.actual { |
| 44 | + t.Errorf("ApplicationCharge.%s returned %v, expected %v", c.field, c.actual, c.expected) |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestApplicationChargeServiceOp_Create(t *testing.T) { |
| 50 | + setup() |
| 51 | + defer teardown() |
| 52 | + |
| 53 | + httpmock.RegisterResponder( |
| 54 | + "POST", |
| 55 | + "https://fooshop.myshopify.com/admin/application_charges.json", |
| 56 | + httpmock.NewBytesResponder(200, loadFixture("applicationcharge.json")), |
| 57 | + ) |
| 58 | + |
| 59 | + p := decimal.NewFromFloat(100.00) |
| 60 | + charge := ApplicationCharge{ |
| 61 | + Name: "Super Duper Expensive action", |
| 62 | + Price: &p, |
| 63 | + ReturnURL: "http://super-duper.shopifyapps.com", |
| 64 | + } |
| 65 | + |
| 66 | + returnedCharge, err := client.ApplicationCharge.Create(charge) |
| 67 | + if err != nil { |
| 68 | + t.Errorf("ApplicationCharge.Create returned an error: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + applicationChargeTests(t, *returnedCharge) |
| 72 | +} |
| 73 | + |
| 74 | +func TestApplicationChargeServiceOp_Get(t *testing.T) { |
| 75 | + setup() |
| 76 | + defer teardown() |
| 77 | + |
| 78 | + httpmock.RegisterResponder( |
| 79 | + "GET", |
| 80 | + "https://fooshop.myshopify.com/admin/application_charges/1.json", |
| 81 | + httpmock.NewStringResponder(200, `{"application_charge": {"id":1}}`), |
| 82 | + ) |
| 83 | + |
| 84 | + charge, err := client.ApplicationCharge.Get(1, nil) |
| 85 | + if err != nil { |
| 86 | + t.Errorf("ApplicationCharge.Get returned an error: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + expected := &ApplicationCharge{ID: 1} |
| 90 | + if !reflect.DeepEqual(charge, expected) { |
| 91 | + t.Errorf("ApplicationCharge.Get returned %+v, expected %+v", charge, expected) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestApplicationChargeServiceOp_List(t *testing.T) { |
| 96 | + setup() |
| 97 | + defer teardown() |
| 98 | + |
| 99 | + httpmock.RegisterResponder( |
| 100 | + "GET", |
| 101 | + "https://fooshop.myshopify.com/admin/application_charges.json", |
| 102 | + httpmock.NewStringResponder(200, `{"application_charges": [{"id":1},{"id":2}]}`), |
| 103 | + ) |
| 104 | + |
| 105 | + charges, err := client.ApplicationCharge.List(nil) |
| 106 | + if err != nil { |
| 107 | + t.Errorf("ApplicationCharge.List returned an error: %v", err) |
| 108 | + } |
| 109 | + |
| 110 | + expected := []ApplicationCharge{{ID: 1}, {ID: 2}} |
| 111 | + if !reflect.DeepEqual(charges, expected) { |
| 112 | + t.Errorf("ApplicationCharge.List returned %+v, expected %+v", charges, expected) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestApplicationChargeServiceOp_Activate(t *testing.T) { |
| 117 | + setup() |
| 118 | + defer teardown() |
| 119 | + |
| 120 | + httpmock.RegisterResponder( |
| 121 | + "POST", |
| 122 | + "https://fooshop.myshopify.com/admin/application_charges/455696195/activate.json", |
| 123 | + httpmock.NewStringResponder( |
| 124 | + 200, |
| 125 | + `{"application_charge":{"id":455696195,"status":"active"}}`, |
| 126 | + ), |
| 127 | + ) |
| 128 | + |
| 129 | + charge := ApplicationCharge{ |
| 130 | + ID: 455696195, |
| 131 | + Status: "accepted", |
| 132 | + } |
| 133 | + |
| 134 | + returnedCharge, err := client.ApplicationCharge.Activate(charge) |
| 135 | + if err != nil { |
| 136 | + t.Errorf("ApplicationCharge.Activate returned an error: %v", err) |
| 137 | + } |
| 138 | + |
| 139 | + expected := &ApplicationCharge{ID: 455696195, Status: "active"} |
| 140 | + if !reflect.DeepEqual(returnedCharge, expected) { |
| 141 | + t.Errorf("ApplicationCharge.Activate returned %+v, expected %+v", charge, expected) |
| 142 | + } |
| 143 | +} |
0 commit comments