github.com/companieshouse/lfp-pay-api@v0.0.0-20230203133422-0ca455cd79f9/service/payment_service_test.go (about) 1 package service 2 3 import ( 4 "context" 5 "net/http" 6 "testing" 7 8 "github.com/companieshouse/go-session-handler/httpsession" 9 "github.com/companieshouse/go-session-handler/session" 10 "github.com/companieshouse/lfp-pay-api-core/constants" 11 "github.com/companieshouse/lfp-pay-api-core/validators" 12 "github.com/jarcoal/httpmock" 13 . "github.com/smartystreets/goconvey/convey" 14 ) 15 16 var paymentsResourceResponse = ` 17 { 18 "amount": "150", 19 "reference": "123", 20 "status": "paid", 21 "created_by": { 22 "email": "test@example.com", 23 "forename": "test", 24 "surname": "user", 25 "id": "123" 26 } 27 } 28 ` 29 30 var paymentDetailsResponse = ` 31 { 32 "card_type": "VISA", 33 "external_payment_id": "1234567890", 34 "transaction_id": "1234", 35 "payment_status": "paid" 36 } 37 ` 38 39 func TestUnitPaymentInformation(t *testing.T) { 40 Convey("IsPaid()", t, func() { 41 Convey("is not paid if status is not 'paid'", func() { 42 p := &validators.PaymentInformation{} 43 So(p.IsPaid(), ShouldBeFalse) 44 }) 45 46 Convey("is paid is status is 'paid", func() { 47 p := &validators.PaymentInformation{Status: constants.Paid.String()} 48 So(p.IsPaid(), ShouldBeTrue) 49 }) 50 }) 51 52 Convey("PaymentAmount()", t, func() { 53 Convey("parses string float successfully", func() { 54 amounts := make(map[string]float64, 2) 55 amounts["150"] = 150 56 amounts["150.50"] = 150.50 57 58 p := validators.PaymentInformation{} 59 60 for s, f := range amounts { 61 p.Amount = s 62 So(p.PaymentAmount(), ShouldEqual, f) 63 } 64 }) 65 66 Convey("negative number returned when failing to parse float", func() { 67 p := validators.PaymentInformation{Amount: "foo"} 68 So(p.PaymentAmount(), ShouldEqual, -1) 69 }) 70 }) 71 } 72 73 func TestUnitGetPaymentInformation(t *testing.T) { 74 paymentsAPIURL := "https://api-payments.companieshouse.gov.uk" 75 76 Convey("GetPaymentResourceFromPaymentAPI", t, func() { 77 78 httpmock.Activate() 79 defer httpmock.DeactivateAndReset() 80 ctx := context.WithValue(context.Background(), httpsession.ContextKeySession, &session.Session{}) 81 r := &http.Request{} 82 r = r.WithContext(ctx) 83 84 Convey("payment resource error", func() { 85 defer httpmock.Reset() 86 httpmock.RegisterResponder(http.MethodGet, paymentsAPIURL+"/payments/123", httpmock.NewStringResponder(http.StatusTeapot, "")) 87 88 resp, err := GetPaymentInformation("123", &http.Request{}) 89 So(resp, ShouldBeNil) 90 So(err, ShouldNotBeNil) 91 So(err.Error(), ShouldEqual, `ch-api: got HTTP response code 418 with body: `) 92 }) 93 94 Convey("payment details error", func() { 95 defer httpmock.Reset() 96 httpmock.RegisterResponder(http.MethodGet, paymentsAPIURL+"/payments/123", httpmock.NewStringResponder(http.StatusOK, paymentsResourceResponse)) 97 httpmock.RegisterResponder(http.MethodGet, paymentsAPIURL+"/private/payments/123/payment-details", httpmock.NewStringResponder(http.StatusTeapot, "")) 98 99 resp, err := GetPaymentInformation("123", r) 100 So(resp, ShouldBeNil) 101 So(err, ShouldNotBeNil) 102 So(err.Error(), ShouldEqual, `ch-api: got HTTP response code 418 with body: `) 103 }) 104 105 Convey("it returns a serialised version of the response", func() { 106 defer httpmock.Reset() 107 httpmock.RegisterResponder(http.MethodGet, paymentsAPIURL+"/payments/123", httpmock.NewStringResponder(http.StatusOK, paymentsResourceResponse)) 108 httpmock.RegisterResponder(http.MethodGet, paymentsAPIURL+"/private/payments/123/payment-details", httpmock.NewStringResponder(http.StatusOK, paymentDetailsResponse)) 109 110 resp, err := GetPaymentInformation("123", r) 111 112 So(err, ShouldBeNil) 113 So(resp.Amount, ShouldEqual, "150") 114 So(resp.CreatedBy, ShouldEqual, "test@example.com") 115 So(resp.Status, ShouldEqual, "paid") 116 So(resp.Reference, ShouldEqual, "123") 117 So(resp.CardType, ShouldEqual, "VISA") 118 So(resp.ExternalPaymentID, ShouldEqual, "1234567890") 119 }) 120 }) 121 }