Skip to content

Commit acbfed3

Browse files
Vat rates - controller level security
1 parent d3ed3f1 commit acbfed3

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

rails_application/app/controllers/available_vat_rates_controller.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ def create
3434
end
3535

3636
def index
37-
@available_vat_rates = VatRates::AvailableVatRate.all
37+
@available_vat_rates = VatRates.available_vat_rates_for_store(current_store_id)
3838
end
3939

4040
def destroy
41+
vat_rate = VatRates::AvailableVatRate.find_by(code: params[:vat_rate_code], store_id: current_store_id)
42+
43+
unless vat_rate
44+
return redirect_to available_vat_rates_path, alert: "VAT rate does not exist"
45+
end
46+
4147
remove_available_vat_rate(params[:vat_rate_code])
4248
redirect_to available_vat_rates_path, notice: "VAT rate was successfully removed"
4349
rescue Taxes::VatRateNotExists

rails_application/app/read_models/vat_rates/configuration.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ class AvailableVatRate < ApplicationRecord
33
self.table_name = "available_vat_rates"
44
end
55

6+
def self.available_vat_rates_for_store(store_id)
7+
AvailableVatRate.where(store_id: store_id)
8+
end
9+
610
class Configuration
711
def call(event_store)
812
event_store.subscribe(AddAvailableVatRate, to: [Taxes::AvailableVatRateAdded])
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require "test_helper"
2+
3+
class VatRatesSecurityTest < InMemoryRESIntegrationTestCase
4+
def test_user_can_only_see_vat_rates_from_their_current_store
5+
store_1_id = register_store("Store 1")
6+
store_2_id = register_store("Store 2")
7+
8+
switch_to_store(store_1_id)
9+
add_available_vat_rate(20, "vat20")
10+
11+
switch_to_store(store_2_id)
12+
add_available_vat_rate(10, "vat10")
13+
14+
assert_equal(0, VatRates.available_vat_rates_for_store(store_1_id).where(code: "vat10").count)
15+
assert_equal(1, VatRates.available_vat_rates_for_store(store_1_id).where(code: "vat20").count)
16+
assert_equal(1, VatRates.available_vat_rates_for_store(store_2_id).where(code: "vat10").count)
17+
assert_equal(0, VatRates.available_vat_rates_for_store(store_2_id).where(code: "vat20").count)
18+
end
19+
20+
def test_user_cannot_delete_vat_rate_from_another_store
21+
store_1_id = register_store("Store 1")
22+
store_2_id = register_store("Store 2")
23+
24+
add_available_vat_rate(20, "vat20")
25+
vat_rate_code = "vat20"
26+
27+
switch_to_store(store_2_id)
28+
29+
delete available_vat_rates_path, params: { vat_rate_code: vat_rate_code }
30+
31+
assert_redirected_to available_vat_rates_path
32+
follow_redirect!
33+
assert_select "#alert", text: /does not exist/
34+
end
35+
36+
def test_user_can_delete_vat_rate_from_their_own_store
37+
store_1_id = register_store("Store 1")
38+
39+
add_available_vat_rate(20, "vat20")
40+
41+
delete available_vat_rates_path, params: { vat_rate_code: "vat20" }
42+
43+
assert_redirected_to available_vat_rates_path
44+
follow_redirect!
45+
assert_select "td", text: "vat20", count: 0
46+
end
47+
48+
private
49+
50+
def switch_to_store(store_id)
51+
cookies[:current_store_id] = store_id
52+
end
53+
end

0 commit comments

Comments
 (0)