Skip to content

Commit

Permalink
add order checkout flow tests, add go pay mocker test module
Browse files Browse the repository at this point in the history
see #3
  • Loading branch information
dedekm committed May 12, 2022
1 parent 4b88648 commit 05144b9
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 37 deletions.
2 changes: 1 addition & 1 deletion app/controllers/boutique/go_pay_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 6 additions & 17 deletions test/controllers/boutique/go_pay_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "test_helper"

class Boutique::GoPayControllerTest < Boutique::ControllerTest
include Boutique::Test::GoPayApiMocker

def setup
super

Expand All @@ -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)
Expand All @@ -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
25 changes: 6 additions & 19 deletions test/controllers/boutique/orders_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "test_helper"

class Boutique::OrdersControllerTest < Boutique::ControllerTest
include Boutique::Test::GoPayApiMocker

test "add" do
product = create(:boutique_product)

Expand All @@ -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

Expand All @@ -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: {
Expand All @@ -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?
Expand All @@ -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
Expand All @@ -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
34 changes: 34 additions & 0 deletions test/go_pay_api_mocker.rb
Original file line number Diff line number Diff line change
@@ -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
106 changes: 106 additions & 0 deletions test/integration/boutique/order_checkout_flow_test.rb
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
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: "[email protected]",
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
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down

0 comments on commit 05144b9

Please sign in to comment.