diff --git a/app/controllers/boutique/go_pay_controller.rb b/app/controllers/boutique/go_pay_controller.rb index 2efd96b7..3f9fa8ff 100644 --- a/app/controllers/boutique/go_pay_controller.rb +++ b/app/controllers/boutique/go_pay_controller.rb @@ -7,7 +7,7 @@ def comeback if @payment.paid? flash[:success] = t(".success") - if @payment.order.user.invitation_accepted_at.nil? + if @payment.order.user.created_by_invite? && !@payment.order.user.invitation_accepted? session[:folio_user_invited_email] = @payment.order.user.email redirect_to main_app.user_invitation_path else diff --git a/test/controllers/boutique/go_pay_controller_test.rb b/test/controllers/boutique/go_pay_controller_test.rb index 5a9cebae..819fe6b2 100644 --- a/test/controllers/boutique/go_pay_controller_test.rb +++ b/test/controllers/boutique/go_pay_controller_test.rb @@ -3,6 +3,8 @@ require "test_helper" class Boutique::GoPayControllerTest < Boutique::ControllerTest + include Boutique::Test::GoPayApiMocker + def setup super @@ -11,16 +13,16 @@ def setup end test "comeback with successful payment" do - go_pay_api_call_mock(result_state: "PAID") + go_pay_find_payment_api_call_mock get comeback_go_pay_url(id: 123) - assert_redirected_to order_url(@order.secret_hash) + assert_redirected_to main_app.user_invitation_url assert @payment.reload.paid? assert @order.reload.paid? end test "comeback with failed payment" do - go_pay_api_call_mock(result_state: "CANCELED") + go_pay_find_payment_api_call_mock(state: "CANCELED") get comeback_go_pay_url(id: 123) assert_redirected_to order_url(@order.secret_hash) @@ -29,22 +31,9 @@ def setup end test "notify" do - go_pay_api_call_mock(result_state: "PAID") + go_pay_find_payment_api_call_mock get notify_go_pay_url(id: 123) assert_response :success end - - private - def go_pay_api_call_mock(result_state:) - result = { - "id" => 123, - "payment_instrument" => "PAYMENT_CARD", - "state" => result_state - } - - Boutique::GoPay::Api.any_instance - .expects(:find_payment) - .returns(result) - end end diff --git a/test/controllers/boutique/orders_controller_test.rb b/test/controllers/boutique/orders_controller_test.rb index 578e1704..c298912e 100644 --- a/test/controllers/boutique/orders_controller_test.rb +++ b/test/controllers/boutique/orders_controller_test.rb @@ -3,6 +3,8 @@ require "test_helper" class Boutique::OrdersControllerTest < Boutique::ControllerTest + include Boutique::Test::GoPayApiMocker + test "add" do product = create(:boutique_product) @@ -16,9 +18,6 @@ class Boutique::OrdersControllerTest < Boutique::ControllerTest end test "edit" do - create(:folio_page, type: "Dummy::Page::DataProtection") - create(:folio_page, type: "Dummy::Page::Terms") - get edit_order_url assert_redirected_to main_app.root_url @@ -33,7 +32,7 @@ class Boutique::OrdersControllerTest < Boutique::ControllerTest assert_redirected_to main_app.root_url create_order_with_current_session_id - go_pay_api_call_mock + go_pay_create_payment_api_call_mock params = { order: { @@ -45,7 +44,7 @@ class Boutique::OrdersControllerTest < Boutique::ControllerTest } post confirm_order_url, params: params - assert_redirected_to "https://test.gopay.com" + assert_redirected_to mocked_go_pay_payment_gateway_url assert @order.reload.confirmed? assert @order.payments.present? assert @order.primary_address.present? @@ -69,10 +68,10 @@ class Boutique::OrdersControllerTest < Boutique::ControllerTest assert_raises(ActiveRecord::RecordNotFound) { get order_url(order.secret_hash) } order.confirm! - go_pay_api_call_mock + go_pay_create_payment_api_call_mock post payment_order_url(order.secret_hash) - assert_redirected_to "https://test.gopay.com" + assert_redirected_to mocked_go_pay_payment_gateway_url assert order.payments.present? assert order.primary_address.present? end @@ -83,16 +82,4 @@ def create_order_with_current_session_id post add_order_url, params: { product_variant_id: product.master_variant.id } @order = Boutique::Order.find_by(web_session_id: session.id.public_id) end - - def go_pay_api_call_mock - result = { - "id" => 123, - "payment_instrument" => "PAYMENT_CARD", - "gw_url" => "https://test.gopay.com", - } - - Boutique::GoPay::Api.any_instance - .expects(:create_payment) - .returns(result) - end end diff --git a/test/go_pay_api_mocker.rb b/test/go_pay_api_mocker.rb new file mode 100644 index 00000000..e7465a98 --- /dev/null +++ b/test/go_pay_api_mocker.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +module Boutique::Test + module GoPayApiMocker + private + def go_pay_create_payment_api_call_mock + result = { + "id" => 123, + "payment_instrument" => "PAYMENT_CARD", + "gw_url" => mocked_go_pay_payment_gateway_url, + } + + Boutique::GoPay::Api.any_instance + .expects(:create_payment) + .returns(result) + end + + def go_pay_find_payment_api_call_mock(state: "PAID") + result = { + "id" => 123, + "payment_instrument" => "PAYMENT_CARD", + "state" => state, + } + + Boutique::GoPay::Api.any_instance + .expects(:find_payment) + .returns(result) + end + + def mocked_go_pay_payment_gateway_url + "https://test.gopay.com" + end + end +end diff --git a/test/integration/boutique/order_checkout_flow_test.rb b/test/integration/boutique/order_checkout_flow_test.rb new file mode 100644 index 00000000..1b0908bd --- /dev/null +++ b/test/integration/boutique/order_checkout_flow_test.rb @@ -0,0 +1,106 @@ +# frozen_string_literal: true + +require "test_helper" + +class Boutique::OrderCheckoutFlowTest < Boutique::ControllerTest + include Boutique::Test::GoPayApiMocker + include Devise::Test::IntegrationHelpers + + def setup + super + + @product = create(:boutique_product) + go_pay_create_payment_api_call_mock + end + + test "anonymous - successful payment" do + post add_order_url, params: { product_variant_id: @product.master_variant.id } + assert_redirected_to edit_order_url + assert_equal 1, Boutique::Order.count + + params = { + order: { + first_name: "John", + last_name: "Doe", + email: "order@test.test", + primary_address_attributes: build(:boutique_folio_primary_address).serializable_hash, + } + } + + post confirm_order_url, params: params + assert_redirected_to mocked_go_pay_payment_gateway_url + + go_pay_find_payment_api_call_mock + get comeback_go_pay_url(id: 123) + assert_redirected_to main_app.user_invitation_url + end + + test "anonymous - unsuccessful payment" do + post add_order_url, params: { product_variant_id: @product.master_variant.id } + assert_redirected_to edit_order_url + assert_equal 1, Boutique::Order.count + + params = { + order: { + first_name: "John", + last_name: "Doe", + email: "order@test.test", + primary_address_attributes: build(:boutique_folio_primary_address).serializable_hash, + } + } + + post confirm_order_url, params: params + assert_redirected_to mocked_go_pay_payment_gateway_url + + go_pay_find_payment_api_call_mock(state: "CANCELED") + + get comeback_go_pay_url(id: 123) + assert_redirected_to order_url(Boutique::Order.first.secret_hash) + end + + test "user - successful payment" do + user = create(:folio_user) + sign_in user + + post add_order_url, params: { product_variant_id: @product.master_variant.id } + assert_redirected_to edit_order_url + assert_equal 1, Boutique::Order.count + + params = { + order: { + primary_address_attributes: build(:boutique_folio_primary_address).serializable_hash, + } + } + + post confirm_order_url, params: params + assert_redirected_to mocked_go_pay_payment_gateway_url + + go_pay_find_payment_api_call_mock + + get comeback_go_pay_url(id: 123) + assert_redirected_to order_url(Boutique::Order.first.secret_hash) + end + + test "user - unsuccessful payment" do + user = create(:folio_user) + sign_in user + + post add_order_url, params: { product_variant_id: @product.master_variant.id } + assert_redirected_to edit_order_url + assert_equal 1, Boutique::Order.count + + params = { + order: { + primary_address_attributes: build(:boutique_folio_primary_address).serializable_hash, + } + } + + post confirm_order_url, params: params + assert_redirected_to mocked_go_pay_payment_gateway_url + + go_pay_find_payment_api_call_mock + + get comeback_go_pay_url(id: 123) + assert_redirected_to order_url(Boutique::Order.first.secret_hash) + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 8301c626..ab84c6b8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,6 +5,7 @@ require_relative "../test/dummy/config/environment" require Folio::Engine.root.join("test/test_helper_base") +require Boutique::Engine.root.join("test/go_pay_api_mocker") ActiveRecord::Migrator.migrations_paths = [File.expand_path("../test/dummy/db/migrate", __dir__)] ActiveRecord::Migrator.migrations_paths << File.expand_path("../db/migrate", __dir__)