github.com/companieshouse/lfp-pay-api@v0.0.0-20230203133422-0ca455cd79f9/service/payment_details_test.go (about) 1 package service 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 "time" 8 9 "github.com/companieshouse/lfp-pay-api-core/models" 10 11 . "github.com/smartystreets/goconvey/convey" 12 ) 13 14 func TestUnitGetPaymentDetailsFromPayableResource(t *testing.T) { 15 16 Convey("Get payment details no transactions - invalid data", t, func() { 17 18 path := "/company/12345678/penalties/late-filing/abcdef/payment" 19 req := httptest.NewRequest(http.MethodGet, path, nil) 20 21 t := time.Now().Truncate(time.Millisecond) 22 23 payable := models.PayableResource{ 24 CompanyNumber: "12345678", 25 Reference: "abcdef", 26 Links: models.PayableResourceLinks{ 27 Self: "/company/12345678/penalties/late-filing/abcdef", 28 Payment: "/company/12345678/penalties/late-filing/abcdef/payment", 29 }, 30 Etag: "qwertyetag1234", 31 CreatedAt: &t, 32 CreatedBy: models.CreatedBy{ 33 Email: "test@user.com", 34 ID: "uz3r1D_H3r3", 35 }, 36 Transactions: []models.TransactionItem{}, 37 Payment: models.Payment{ 38 Amount: "5", 39 Status: "pending", 40 }, 41 } 42 43 service := &PaymentDetailsService{} 44 45 paymentDetails, responseType, err := service.GetPaymentDetailsFromPayableResource(req, &payable) 46 47 So(paymentDetails, ShouldBeNil) 48 So(responseType, ShouldEqual, InvalidData) 49 So(err, ShouldNotBeNil) 50 51 }) 52 53 Convey("Get payment details pending state - success", t, func() { 54 55 path := "/company/12345678/penalties/late-filing/abcdef/payment" 56 req := httptest.NewRequest(http.MethodGet, path, nil) 57 58 t := time.Now().Truncate(time.Millisecond) 59 60 payable := models.PayableResource{ 61 CompanyNumber: "12345678", 62 Reference: "abcdef", 63 Links: models.PayableResourceLinks{ 64 Self: "/company/12345678/penalties/late-filing/abcdef", 65 Payment: "/company/12345678/penalties/late-filing/abcdef/payment", 66 }, 67 Etag: "qwertyetag1234", 68 CreatedAt: &t, 69 CreatedBy: models.CreatedBy{ 70 Email: "test@user.com", 71 ID: "uz3r1D_H3r3", 72 }, 73 Transactions: []models.TransactionItem{ 74 models.TransactionItem{ 75 Amount: 5, 76 Type: "penalty", 77 TransactionID: "0987654321", 78 }, 79 }, 80 Payment: models.Payment{ 81 Amount: "5", 82 Status: "pending", 83 }, 84 } 85 86 service := &PaymentDetailsService{} 87 88 paymentDetails, responseType, err := service.GetPaymentDetailsFromPayableResource(req, &payable) 89 90 expectedCost := models.Cost{ 91 Description: "Late Filing Penalty", 92 Amount: "5", 93 AvailablePaymentMethods: []string{"credit-card"}, 94 ClassOfPayment: []string{"penalty"}, 95 DescriptionIdentifier: "late-filing-penalty", 96 Kind: "cost#cost", 97 ResourceKind: "late-filing-penalty#late-filing-penalty", 98 ProductType: "late-filing-penalty", 99 } 100 101 So(paymentDetails, ShouldNotBeNil) 102 So(paymentDetails.Description, ShouldEqual, "Late Filing Penalty") 103 So(paymentDetails.Kind, ShouldEqual, "payment-details#payment-details") 104 So(paymentDetails.PaymentReference, ShouldEqual, "") 105 So(paymentDetails.Links.Self, ShouldEqual, "/company/12345678/penalties/late-filing/abcdef/payment") 106 So(paymentDetails.Links.Resource, ShouldEqual, "/company/12345678/penalties/late-filing/abcdef") 107 So(paymentDetails.Status, ShouldEqual, "pending") 108 So(paymentDetails.CompanyNumber, ShouldEqual, "12345678") 109 So(paymentDetails.Items[0], ShouldResemble, expectedCost) 110 So(responseType, ShouldEqual, Success) 111 So(err, ShouldBeNil) 112 113 }) 114 115 Convey("Get payment details paid state - success", t, func() { 116 117 path := "/company/12345678/penalties/late-filing/abcdef/payment" 118 req := httptest.NewRequest(http.MethodGet, path, nil) 119 120 t := time.Now().Truncate(time.Millisecond) 121 122 payable := models.PayableResource{ 123 CompanyNumber: "12345678", 124 Reference: "abcdef", 125 Links: models.PayableResourceLinks{ 126 Self: "/company/12345678/penalties/late-filing/abcdef", 127 Payment: "/company/12345678/penalties/late-filing/abcdef/payment", 128 }, 129 Etag: "qwertyetag1234", 130 CreatedAt: &t, 131 CreatedBy: models.CreatedBy{ 132 Email: "test@user.com", 133 ID: "uz3r1D_H3r3", 134 }, 135 Transactions: []models.TransactionItem{ 136 models.TransactionItem{ 137 Amount: 5, 138 Type: "penalty", 139 TransactionID: "0987654321", 140 }, 141 }, 142 Payment: models.Payment{ 143 Amount: "50", 144 Status: "paid", 145 PaidAt: &t, 146 Reference: "payment_id", 147 }, 148 } 149 150 service := &PaymentDetailsService{} 151 152 paymentDetails, responseType, err := service.GetPaymentDetailsFromPayableResource(req, &payable) 153 154 expectedCost := models.Cost{ 155 Description: "Late Filing Penalty", 156 Amount: "5", 157 AvailablePaymentMethods: []string{"credit-card"}, 158 ClassOfPayment: []string{"penalty"}, 159 DescriptionIdentifier: "late-filing-penalty", 160 Kind: "cost#cost", 161 ResourceKind: "late-filing-penalty#late-filing-penalty", 162 ProductType: "late-filing-penalty", 163 } 164 165 So(paymentDetails, ShouldNotBeNil) 166 So(paymentDetails.Description, ShouldEqual, "Late Filing Penalty") 167 So(paymentDetails.Kind, ShouldEqual, "payment-details#payment-details") 168 So(paymentDetails.PaidAt, ShouldEqual, &t) 169 So(paymentDetails.PaymentReference, ShouldEqual, "payment_id") 170 So(paymentDetails.Links.Self, ShouldEqual, "/company/12345678/penalties/late-filing/abcdef/payment") 171 So(paymentDetails.Links.Resource, ShouldEqual, "/company/12345678/penalties/late-filing/abcdef") 172 So(paymentDetails.Status, ShouldEqual, "paid") 173 So(paymentDetails.CompanyNumber, ShouldEqual, "12345678") 174 So(paymentDetails.Items[0], ShouldResemble, expectedCost) 175 So(responseType, ShouldEqual, Success) 176 So(err, ShouldBeNil) 177 178 }) 179 }