Skip to content

Commit 54c8b37

Browse files
Test coverage for Orders#show
1 parent 782a934 commit 54c8b37

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

apps/rails_application/.mutant.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ matcher:
2323
- Invoices*
2424
- Shipments*
2525
- VatRates*
26+
- OrdersController#show
27+
- OrdersController#order_belongs_to_different_store?
2628
ignore:
2729
- Coupons::Configuration#call
2830
- Orders::Configuration#call

apps/rails_application/app/controllers/orders_controller.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ def index
44
end
55

66
def show
7-
@order = Orders.find_order(params[:id])
8-
7+
@order = Orders.find_order(params.fetch(:id))
98
return not_found unless @order
10-
return not_found if @order.store_id && @order.store_id != current_store_id
119

12-
@order_header = OrderHeader.find_by_uid(params[:id])
10+
if order_belongs_to_different_store?
11+
not_found
12+
else
13+
@order_header = OrderHeader.find_by_uid(params.fetch(:id))
14+
end
1315
end
1416

1517
def new
@@ -122,6 +124,11 @@ def cancel
122124

123125
private
124126

127+
def order_belongs_to_different_store?
128+
return false unless @order.store_id
129+
!(@order.store_id == current_store_id)
130+
end
131+
125132
def authorize_payment(order_id)
126133
command_bus.call(authorize_payment_cmd(order_id))
127134
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require "test_helper"
2+
3+
class OrdersControllerTest < ActionDispatch::IntegrationTest
4+
cover "OrdersController#show"
5+
6+
def setup
7+
@store_id_a = SecureRandom.uuid
8+
@store_id_b = SecureRandom.uuid
9+
post "/admin/stores", params: { store_id: @store_id_a, name: "Store A" }
10+
post "/admin/stores", params: { store_id: @store_id_b, name: "Store B" }
11+
end
12+
13+
def test_show_returns_not_found_when_order_does_not_exist
14+
post "/switch_store", params: { store_id: @store_id_a }
15+
get "/orders/#{SecureRandom.uuid}"
16+
17+
assert_response(:not_found)
18+
end
19+
20+
def test_show_returns_not_found_when_order_belongs_to_different_store
21+
post "/switch_store", params: { store_id: @store_id_b }
22+
get "/orders/new"
23+
follow_redirect!
24+
order_id = request.path.split('/')[2]
25+
26+
post "/switch_store", params: { store_id: @store_id_a }
27+
get "/orders/#{order_id}"
28+
29+
assert_response(:not_found)
30+
end
31+
32+
def test_show_allows_access_to_order_without_store_id
33+
order_id = SecureRandom.uuid
34+
event_store.publish(Pricing::OfferDrafted.new(data: { order_id: order_id }))
35+
36+
post "/switch_store", params: { store_id: @store_id_a }
37+
get "/orders/#{order_id}"
38+
39+
assert_response(:success)
40+
end
41+
42+
def test_show_allows_access_to_order_in_current_store
43+
post "/switch_store", params: { store_id: @store_id_a }
44+
get "/orders/new"
45+
follow_redirect!
46+
order_id = request.path.split('/')[2]
47+
48+
get "/orders/#{order_id}"
49+
50+
assert_response(:success)
51+
end
52+
53+
private
54+
55+
def event_store
56+
Rails.configuration.event_store
57+
end
58+
end

0 commit comments

Comments
 (0)