Skip to content

Commit d482b73

Browse files
committed
Fields definitions: Update/add tests
(cherry picked from commit 2586962)
1 parent 2c1f9fb commit d482b73

6 files changed

Lines changed: 127 additions & 0 deletions

File tree

features/developer_portal/admin/applications/extra_fields.feature

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ Feature: Developer portal application extra fields
6363
| Plate number |
6464
| Stealth |
6565

66+
Scenario: Read-only extra fields are ignored if received
67+
Given the following application:
68+
| Buyer | Name | Product |
69+
| Jane | Jane's App | The API |
70+
And the application has the following extra fields:
71+
| Description | It's a car |
72+
| Engine | 120 |
73+
| Wheels | 4 |
74+
| Color | White |
75+
When the buyer logs in
76+
And the provider has the field "plate_number" for applications as editable
77+
And they go to application "Jane's App" dev portal edit page
78+
And the provider has the field "plate_number" for applications as read only
79+
And the form is submitted with:
80+
| Description | It's a car |
81+
| Engine | 120 |
82+
| Wheels | 4 |
83+
| Color | White |
84+
| Plate number | edit me |
85+
Then they should see the flash message "Application was successfully updated"
86+
And they should not see "Plate number"
87+
6688
Scenario: Extra fields validation
6789
When they go to the dev portal new application page
6890
And the form is submitted with:

features/old/accounts/buyers/account_fields.feature

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ Feature: Buyer side, account fields
4646
And I press "Update"
4747
Then I should see "The account information was updated."
4848

49+
Scenario: Read-only extra fields are ignored on update
50+
Given I log in as "bob" on foo.3scale.localhost
51+
And provider "foo.3scale.localhost" has the field "non_editable" for accounts as editable
52+
Then I go to the account edit page
53+
And provider "foo.3scale.localhost" has the field "non_editable" for accounts as read only
54+
And the form is submitted with:
55+
| Required field | value |
56+
| False field | |
57+
| Choices field | 3 |
58+
| Non editable | edited |
59+
Then I should see "The account information was updated."
60+
Then I should not see "Non editable"
4961

5062
Scenario: Viewing account with extra fields
5163
#TODO ugly table change this

features/step_definitions/provider_steps.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ def import_simple_layout(provider)
6161
a.save!
6262
end
6363

64+
Given "{provider} has the field {string} for {field_definition_target} as {read_only_status}" do |provider, name, target, read_only|
65+
a = provider.fields_definitions.by_target(target).find { |fd| fd.name == name }
66+
a.read_only = read_only
67+
a.save!
68+
end
69+
6470
Given "{provider} allows to change account plan {change_plan_permission}" do |provider, plan_permission|
6571
provider.set_change_account_plan_permission! plan_permission
6672
end

features/support/parameter_types.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,14 @@ def find_metric(metrics, name)
469469
regexp: /(.*)/,
470470
transformer: ->(selector) { selector_for(selector) }
471471
)
472+
473+
ParameterType(
474+
name: 'read_only_status',
475+
regexp: /(editable|read only)/,
476+
transformer: ->(value) do
477+
{
478+
'editable' => false,
479+
'read only' => true,
480+
}[value]
481+
end
482+
)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
module DeveloperPortal
6+
class BaseControllerTest < ActionDispatch::IntegrationTest
7+
8+
class FilterReadOnlyParamsTest < BaseControllerTest
9+
class TestController < DeveloperPortal::BaseController
10+
skip_before_action :login_required
11+
12+
def create
13+
render plain: filter_readonly_params(params[:user], User)
14+
end
15+
end
16+
17+
test 'filters out read-only fields' do
18+
account = FactoryBot.create(:simple_provider)
19+
ro_fields = FactoryBot.create_list(:fields_definition, 2, account:, read_only: true)
20+
editable_fields = FactoryBot.create_list(:fields_definition, 3, account:)
21+
22+
ro_params = fields_to_hash(ro_fields)
23+
editable_params = fields_to_hash(editable_fields)
24+
25+
TestController.any_instance.expects(:site_account).at_least_once.returns(account)
26+
27+
with_test_routes do
28+
post '/test/create', params: { user: {**ro_params, **editable_params} }
29+
30+
assert_response :success
31+
assert_equal editable_params.to_s, response.body
32+
end
33+
end
34+
end
35+
36+
private
37+
38+
def fields_to_hash(fields)
39+
fields.each_with_object({}) { |fd, p| p[fd.name]=SecureRandom.hex }
40+
end
41+
42+
def with_test_routes
43+
Rails.application.routes.draw do
44+
post '/test/create' => 'developer_portal/base_controller_test/filter_read_only_params_test/test#create'
45+
end
46+
yield
47+
ensure
48+
Rails.application.routes_reloader.reload!
49+
end
50+
end
51+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'test_helper'
2+
3+
module DeveloperPortal
4+
class BaseControllerTest < ActiveSupport::TestCase
5+
6+
class TestController < DeveloperPortal::BaseController; end
7+
8+
class FilterReadOnlyParamsTest < BaseControllerTest
9+
10+
test 'filters out read-only fields' do
11+
account = FactoryBot.create(:simple_provider)
12+
ro_fields = FactoryBot.create_list(:fields_definition, 2, account:, read_only: true)
13+
FactoryBot.create_list(:fields_definition, 3, account:)
14+
params = account.fields_definitions.each_with_object({}) { |fd, p| p[fd.name]=SecureRandom.hex }
15+
TestController.any_instance.expects(:site_account).returns(account)
16+
17+
result = TestController.new.send(:filter_readonly_params, params, User)
18+
19+
assert_equal 3, result.size
20+
assert_not_includes result, ro_fields.map(&:name)
21+
end
22+
23+
end
24+
end
25+
end

0 commit comments

Comments
 (0)