flamingo.me/flamingo-commerce/v3@v3.11.0/test/integrationtest/projecttest/tests/frontend/checkout_test.go (about) 1 //go:build integration 2 // +build integration 3 4 package frontend_test 5 6 import ( 7 "net/http" 8 "testing" 9 10 "flamingo.me/flamingo-commerce/v3/payment/domain" 11 "flamingo.me/flamingo-commerce/v3/test/integrationtest" 12 "flamingo.me/flamingo-commerce/v3/test/integrationtest/projecttest/modules/payment" 13 "flamingo.me/flamingo-commerce/v3/test/integrationtest/projecttest/modules/placeorder" 14 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func Test_Checkout_SubmitCheckoutAction(t *testing.T) { 19 t.Run("empty cart should lead to an error", func(t *testing.T) { 20 e := integrationtest.NewHTTPExpect(t, "http://"+FlamingoURL) 21 response := e.GET(routeCheckoutSubmit).Expect() 22 response.Status(http.StatusOK).Body().IsEqual("null\n") 23 }) 24 25 t.Run("cart and valid form should lead to redirect to review page", func(t *testing.T) { 26 e := integrationtest.NewHTTPExpect(t, "http://"+FlamingoURL) 27 // prepare cart 28 CartAddProduct(t, e, "fake_simple", 5, "", "inflight") 29 30 response := SubmitCheckoutForm(t, e, map[string]interface{}{ 31 "billingAddress": map[string]interface{}{ 32 "firstname": "firstname", 33 "lastname": "lastname", 34 "email": "test@test.com", 35 }, 36 "deliveries": map[string]interface{}{ 37 "inflight": map[string]interface{}{ 38 "deliveryAddress": map[string]interface{}{ 39 "firstname": "firstname", 40 "lastname": "lastname", 41 "email": "test@test.com", 42 }, 43 }, 44 }, 45 "payment": map[string]interface{}{ 46 "gateway": payment.FakePaymentGateway, 47 "method": domain.PaymentFlowStatusCompleted, 48 }, 49 }) 50 51 assert.Equal(t, routeCheckoutReview, response.Raw().Request.URL.RequestURI()) 52 }) 53 54 t.Run("checkout with invalid form should lead to page with form errors", func(t *testing.T) { 55 e := integrationtest.NewHTTPExpect(t, "http://"+FlamingoURL) 56 // prepare cart 57 CartAddProduct(t, e, "fake_simple", 5, "", "inflight") 58 59 response := SubmitCheckoutForm(t, e, nil) 60 61 assert.Equal(t, routeCheckoutSubmit, response.Raw().Request.URL.RequestURI()) 62 63 form := response.JSON().Object().Value("Form").Object() 64 form.Value("BillingAddressForm").Object().Value("ValidationInfo").Object().Value("IsValid").Boolean().IsFalse() 65 form.Value("DeliveryForms").Object().Value("inflight").Object().Value("ValidationInfo").Object().Value("IsValid").Boolean().IsFalse() 66 form.Value("SimplePaymentForm").Object().Value("ValidationInfo").Object().Value("IsValid").Boolean().IsFalse() 67 }) 68 69 t.Run("checkout with cart requires payment", func(t *testing.T) { 70 e := integrationtest.NewHTTPExpect(t, "http://"+FlamingoURL) 71 // prepare cart 72 CartAddProduct(t, e, "fake_simple", 5, "", "inflight") 73 74 response := SubmitCheckoutForm(t, e, map[string]interface{}{ 75 "billingAddress": map[string]interface{}{ 76 "firstname": "firstname", 77 "lastname": "lastname", 78 "email": "test@test.com", 79 }, 80 "deliveries": map[string]interface{}{ 81 "inflight": map[string]interface{}{ 82 "deliveryAddress": map[string]interface{}{ 83 "firstname": "firstname", 84 "lastname": "lastname", 85 "email": "test@test.com", 86 }, 87 }, 88 }, 89 }) 90 91 response.Status(http.StatusOK) 92 assert.Equal(t, routeCheckoutSubmit, response.Raw().Request.URL.RequestURI()) 93 94 form := response.JSON().Object().Value("Form").Object() 95 form.Value("SimplePaymentForm").Object().Value("ValidationInfo").Object().Value("IsValid").Boolean().IsFalse() 96 }) 97 98 t.Run("checkout with zero cart possible without payment", func(t *testing.T) { 99 e := integrationtest.NewHTTPExpect(t, "http://"+FlamingoURL) 100 // prepare cart 101 CartAddProduct(t, e, "fake_simple", 5, "", "inflight") 102 CartApplyVoucher(t, e, "100-percent-off") 103 104 response := SubmitCheckoutForm(t, e, map[string]interface{}{ 105 "billingAddress": map[string]interface{}{ 106 "firstname": "firstname", 107 "lastname": "lastname", 108 "email": "test@test.com", 109 }, 110 "deliveries": map[string]interface{}{ 111 "inflight": map[string]interface{}{ 112 "deliveryAddress": map[string]interface{}{ 113 "firstname": "firstname", 114 "lastname": "lastname", 115 "email": "test@test.com", 116 }, 117 }, 118 }, 119 }) 120 121 assert.Equal(t, routeCheckoutReview, response.Raw().Request.URL.RequestURI()) 122 }) 123 } 124 125 func Test_Checkout_ReviewActionAndPlaceOrderAction(t *testing.T) { 126 t.Run("valid payment should lead to success page", func(t *testing.T) { 127 e := integrationtest.NewHTTPExpect(t, "http://"+FlamingoURL) 128 // prepare cart 129 CartAddProduct(t, e, "fake_simple", 5, "", "inflight") 130 131 // submit checkout form 132 response := SubmitCheckoutForm(t, e, map[string]interface{}{ 133 "billingAddress": map[string]interface{}{ 134 "firstname": "firstname", 135 "lastname": "lastname", 136 "email": "test@test.com", 137 }, 138 "deliveries": map[string]interface{}{ 139 "inflight": map[string]interface{}{ 140 "deliveryAddress": map[string]interface{}{ 141 "firstname": "firstname", 142 "lastname": "lastname", 143 "email": "test@test.com", 144 }, 145 }, 146 }, 147 "payment": map[string]interface{}{ 148 "gateway": payment.FakePaymentGateway, 149 "method": domain.PaymentFlowStatusCompleted, 150 }, 151 }) 152 153 assert.Equal(t, routeCheckoutReview, response.Raw().Request.URL.RequestURI()) 154 155 // submit review form 156 response = SubmitReviewForm(t, e, map[string]interface{}{ 157 "proceed": "1", 158 "termsAndConditions": "1", 159 "privacyPolicy": "1", 160 }) 161 162 assert.Equal(t, routeCheckoutSuccess, response.Raw().Request.URL.RequestURI()) 163 response.JSON().Object().Value("PaymentInfos").NotNull() 164 response.JSON().Object().Value("PlacedOrderInfos").Array().Value(0).Object().Value("OrderNumber").String().NotEmpty() 165 }) 166 167 t.Run("zero cart without payment should lead to success page", func(t *testing.T) { 168 e := integrationtest.NewHTTPExpect(t, "http://"+FlamingoURL) 169 // prepare cart 170 CartAddProduct(t, e, "fake_simple", 5, "", "inflight") 171 CartApplyVoucher(t, e, "100-percent-off") 172 173 // submit checkout form 174 response := SubmitCheckoutForm(t, e, map[string]interface{}{ 175 "billingAddress": map[string]interface{}{ 176 "firstname": "firstname", 177 "lastname": "lastname", 178 "email": "test@test.com", 179 }, 180 "deliveries": map[string]interface{}{ 181 "inflight": map[string]interface{}{ 182 "deliveryAddress": map[string]interface{}{ 183 "firstname": "firstname", 184 "lastname": "lastname", 185 "email": "test@test.com", 186 }, 187 }, 188 }, 189 }) 190 191 assert.Equal(t, routeCheckoutReview, response.Raw().Request.URL.RequestURI()) 192 193 // submit review form 194 response = SubmitReviewForm(t, e, map[string]interface{}{ 195 "proceed": "1", 196 "termsAndConditions": "1", 197 "privacyPolicy": "1", 198 }) 199 200 assert.Equal(t, routeCheckoutSuccess, response.Raw().Request.URL.RequestURI()) 201 response.JSON().Object().Value("PaymentInfos").IsNull() 202 }) 203 204 t.Run("error during payment should lead to checkout page", func(t *testing.T) { 205 e := integrationtest.NewHTTPExpect(t, "http://"+FlamingoURL) 206 // prepare cart 207 CartAddProduct(t, e, "fake_simple", 5, "", "inflight") 208 209 // submit checkout form 210 response := SubmitCheckoutForm(t, e, map[string]interface{}{ 211 "billingAddress": map[string]interface{}{ 212 "firstname": "firstname", 213 "lastname": "lastname", 214 "email": "test@test.com", 215 }, 216 "deliveries": map[string]interface{}{ 217 "inflight": map[string]interface{}{ 218 "deliveryAddress": map[string]interface{}{ 219 "firstname": "firstname", 220 "lastname": "lastname", 221 "email": "test@test.com", 222 }, 223 }, 224 }, 225 "payment": map[string]interface{}{ 226 "gateway": payment.FakePaymentGateway, 227 "method": domain.PaymentFlowStatusFailed, 228 }, 229 }) 230 231 assert.Equal(t, routeCheckoutReview, response.Raw().Request.URL.RequestURI()) 232 233 // submit review form 234 response = SubmitReviewForm(t, e, map[string]interface{}{ 235 "proceed": "1", 236 "termsAndConditions": "1", 237 "privacyPolicy": "1", 238 }) 239 240 assert.Equal(t, routeCheckoutSubmit, response.Raw().Request.URL.RequestURI()) 241 response.JSON().Object().Value("ErrorInfos").Object().Value("HasPaymentError").Boolean().IsTrue() 242 response.JSON().Object().Value("ErrorInfos").Object().Value("HasError").Boolean().IsTrue() 243 response.JSON().Object().Value("ErrorInfos").Object().Value("ErrorMessage").String().IsEqual(domain.PaymentErrorCodeFailed) 244 }) 245 246 t.Run("error during place order should lead to checkout page", func(t *testing.T) { 247 e := integrationtest.NewHTTPExpect(t, "http://"+FlamingoURL) 248 // prepare cart 249 CartAddProduct(t, e, "fake_simple", 5, "", "inflight") 250 251 // submit checkout form 252 response := SubmitCheckoutForm(t, e, map[string]interface{}{ 253 "billingAddress": map[string]interface{}{ 254 "firstname": "firstname", 255 "lastname": "lastname", 256 "email": "test@test.com", 257 }, 258 "personalData": map[string]interface{}{ 259 placeorder.CustomAttributesKeyPlaceOrderError: "generic error during place order", 260 }, 261 "deliveries": map[string]interface{}{ 262 "inflight": map[string]interface{}{ 263 "deliveryAddress": map[string]interface{}{ 264 "firstname": "firstname", 265 "lastname": "lastname", 266 "email": "test@test.com", 267 }, 268 }, 269 }, 270 "payment": map[string]interface{}{ 271 "gateway": payment.FakePaymentGateway, 272 "method": domain.PaymentFlowStatusCompleted, 273 }, 274 }) 275 276 assert.Equal(t, routeCheckoutReview, response.Raw().Request.URL.RequestURI()) 277 278 // submit review form 279 response = SubmitReviewForm(t, e, map[string]interface{}{ 280 "proceed": "1", 281 "termsAndConditions": "1", 282 "privacyPolicy": "1", 283 }) 284 285 assert.Equal(t, routeCheckoutSubmit, response.Raw().Request.URL.RequestURI()) 286 response.JSON().Object().Value("ErrorInfos").Object().Value("HasError").Boolean().IsTrue() 287 response.JSON().Object().Value("ErrorInfos").Object().Value("HasPaymentError").Boolean().IsFalse() 288 response.JSON().Object().Value("ErrorInfos").Object().Value("ErrorMessage").String().IsEqual("generic error during place order") 289 }) 290 }