Skip to content

Commit

Permalink
Add :max_age to AgeAuthorizationHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
artero authored and xredo committed Oct 16, 2019
1 parent fd7cd56 commit c451a80
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/initializers/decidim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
auth.action_authorizer = 'Decidim::AgeActionAuthorization::Authorizer'
auth.options do |options|
options.attribute :age, type: :string, required: false
options.attribute :max_age, type: :string, required: false
end
end

Expand Down
1 change: 1 addition & 0 deletions config/locales/ca.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ca:
fields:
birthdate: Data de naixement
age: 'Edat mínima'
max_age: 'Edat màxima'
features:
proposals:
actions:
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ en:
fields:
birthdate: Date of birth
age: Minimum age
max_age: Maximum age
features:
proposals:
actions:
Expand Down
1 change: 1 addition & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ es:
fields:
birthdate: Fecha de nacimiento
age: Edad mínima
max_age: Edad máxima
features:
proposals:
actions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ def valid_metadata?
end

def valid_age?
(birthdate + minimum_age.years) <= Date.current
min_date = birthdate + minimum_age.years
max_date = (birthdate + options['max_age'].to_i.years if options.key?('max_age'))

if max_date
(min_date..max_date).cover?(Date.current)
else
min_date <= Date.current
end
end

def birthdate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@
expect(authorizer_without_authorization.authorize).to include(:missing)
end

describe 'when max_age is pressent' do
it 'authorizes if is older that :age and yonger that :max_age' do
birthdate = 21.years.ago.strftime('%Y/%m/%d')
authorizer = authorizer_class.new(authorization_for(birthdate), { 'age' => '20', 'max_age' => '22' }, component, resource)
expect(authorizer.authorize).to include(:ok)
end

it 'authorizes if is older that :age and equal that :max_age' do
birthdate = 22.years.ago.strftime('%Y/%m/%d')
authorizer = authorizer_class.new(authorization_for(birthdate), { 'age' => '20', 'max_age' => '22' }, component, resource)
expect(authorizer.authorize).to include(:ok)
end

it 'not authorizes if is older of :max_age' do
birthdate = 23.years.ago.strftime('%Y/%m/%d')
authorizer = authorizer_class.new(authorization_for(birthdate), { 'age' => '20', 'max_age' => '22' }, component, resource)
expect(authorizer.authorize).to include(:unauthorized)
end

it 'not authorizes if is younger of :age' do
birthdate = 19.years.ago.strftime('%Y/%m/%d')
authorizer = authorizer_class.new(authorization_for(birthdate), { 'age' => '20', 'max_age' => '22' }, component, resource)
expect(authorizer.authorize).to include(:unauthorized)
end
end

def authorization_for(date)
OpenStruct.new(metadata: { 'birthdate' => date },
granted?: true)
Expand Down
1 change: 1 addition & 0 deletions decidim-census/config/locales/ca.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ca:
fields:
birthdate: Data de naixement
age: 'Edat mínima'
max_age: 'Edat máxima'
census:
errors:
messages:
Expand Down
1 change: 1 addition & 0 deletions decidim-census/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ en:
fields:
birthdate: Birthdate
age: 'Minimum age'
age: 'Maximum age'
census:
errors:
messages:
Expand Down
1 change: 1 addition & 0 deletions decidim-census/config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ es:
fields:
birthdate: Fecha de nacimiento
age: 'Edad mínima'
max_age: 'Edad máxima'
census:
errors:
messages:
Expand Down
1 change: 1 addition & 0 deletions decidim-census/lib/decidim/census/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Engine < ::Rails::Engine
auth.action_authorizer = 'Decidim::AgeActionAuthorization::Authorizer'
auth.options do |options|
options.attribute :age, type: :string, required: false
options.attribute :max_age, type: :string, required: false
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Engine < ::Rails::Engine
auth.action_authorizer = 'Decidim::AgeActionAuthorization::Authorizer'
auth.options do |options|
options.attribute :age, type: :string, required: false
options.attribute :max_age, type: :string, required: false
end
end
end
Expand Down

0 comments on commit c451a80

Please sign in to comment.