Skip to content

Commit 7d87350

Browse files
authored
Merge pull request #4100 from 3scale/fixed-order-backend-mapping-rules
THREESCALE-11879: Order backend API mapping rules
2 parents 0e24c96 + 8ca4412 commit 7d87350

3 files changed

Lines changed: 43 additions & 5 deletions

File tree

app/models/backend_api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class BackendApi < ApplicationRecord
1919
after_create :create_default_metrics
2020
before_destroy :avoid_destruction
2121

22-
has_many :proxy_rules, as: :owner, dependent: :destroy, inverse_of: :owner
22+
has_many :proxy_rules, -> { order(position: :asc) }, as: :owner, dependent: :destroy, inverse_of: :owner
2323
has_many :metrics, as: :owner, dependent: :destroy, inverse_of: :owner
2424
alias_method :all_metrics, :metrics
2525

test/integration/admin/api/backend_apis/mapping_rules_controller_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@ def setup
114114
delete admin_api_backend_api_mapping_rule_path(backend_api, mapping_rule), params: { access_token: access_token_value }
115115
assert_response :not_found
116116
end
117+
118+
test 'mapping rules are ordered by position' do
119+
backend_api.proxy_rules.destroy_all
120+
121+
FactoryBot.create(:proxy_rule, owner: backend_api, pattern: '/two')
122+
rule_3 = FactoryBot.create(:proxy_rule, owner: backend_api, pattern: '/three')
123+
rule_1 = FactoryBot.create(:proxy_rule, owner: backend_api, pattern: '/one')
124+
rule_1.move_to_top
125+
rule_3.move_to_bottom
126+
127+
get admin_api_backend_api_mapping_rules_path(backend_api), params: { access_token: access_token_value }
128+
129+
assert_response :success
130+
assert(response_mapping_rules = JSON.parse(response.body)['mapping_rules'])
131+
132+
patterns = response_mapping_rules.map { |mapping_rule| mapping_rule.dig('mapping_rule', 'pattern') }
133+
assert_equal %w[/one /two /three], patterns
134+
end
117135
end
118136

119137
class MemberPermission < self

test/unit/apicast/proxy_rules_source_test.rb

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
class Apicast::ProxyRulesSourceTest < ActiveSupport::TestCase
44

5+
def setup
6+
@proxy = FactoryBot.create(:proxy)
7+
backend_api_config = FactoryBot.create(:backend_api_config, service: @proxy.service, path: '/test/path/')
8+
@backend_api = backend_api_config.backend_api
9+
@metric = FactoryBot.create(:metric, owner: backend_api, description: 'My awesome metric', system_name: 'my-metric')
10+
end
11+
12+
attr_reader :backend_api, :metric, :proxy
13+
514
def test_to_hash
6-
proxy = FactoryBot.create(:proxy)
715
rule_1 = FactoryBot.create(:proxy_rule, proxy: proxy, last: true)
816
rule_2 = FactoryBot.create(:proxy_rule, proxy: proxy)
917

10-
backend_api_config = FactoryBot.create(:backend_api_config, service: proxy.service, path: '/test/path/')
11-
backend_api = backend_api_config.backend_api
12-
metric = FactoryBot.create(:metric, owner: backend_api, description: 'My awesome metric', system_name: 'my-metric')
1318
rule_3 = FactoryBot.create(:proxy_rule, owner: backend_api, pattern: '/create')
1419
rule_4 = FactoryBot.create(:proxy_rule, owner: backend_api, pattern: '/delete', metric: metric)
1520
rule_5 = FactoryBot.create(:proxy_rule, owner: backend_api, pattern: '/list?filter=a', metric: metric)
@@ -33,6 +38,21 @@ def test_to_hash
3338
assert_equal ['username'], rule_hash_6['parameters']
3439
end
3540

41+
test 'backend api mapping rules are ordered by position' do
42+
proxy.proxy_rules.destroy_all
43+
44+
assert backend_api.proxy_rules.empty?
45+
46+
FactoryBot.create(:proxy_rule, owner: backend_api, pattern: '/two')
47+
rule_3 = FactoryBot.create(:proxy_rule, owner: backend_api, pattern: '/three')
48+
rule_1 = FactoryBot.create(:proxy_rule, owner: backend_api, pattern: '/one')
49+
rule_1.move_to_top
50+
rule_3.move_to_bottom
51+
52+
source = Apicast::ProxyRulesSource.new(proxy).to_hash
53+
assert_equal %w[/test/path/one /test/path/two /test/path/three], source.map { |rule| rule['pattern'] }
54+
end
55+
3656
private
3757

3858
def find_by_id_in_hash(id, hash)

0 commit comments

Comments
 (0)