Skip to content

Commit

Permalink
Merge pull request #308 from zooniverse/posting-age-requirement
Browse files Browse the repository at this point in the history
Implement posting age requirement
  • Loading branch information
zwolf authored Sep 28, 2023
2 parents fb1b246 + ec83f0d commit c1886b0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/policies/application_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ def has_role?(role)
true
end

def of_posting_age?
return true unless ENV['POSTING_AGE_REQUIREMENT']

user.created_at < (Time.now - age_requirement)
end

def age_requirement
quant = ENV.fetch('POSTING_AGE_REQUIREMENT', '24')
quant.to_i.hours
end

def roles_in(section)
user_roles.fetch section, []
end
Expand Down
6 changes: 5 additions & 1 deletion app/policies/comment_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ def show?
end

def create?
logged_in? && !locked? && writable?
if Array.wrap(record).compact.any? { |a| a.section == 'zooniverse' }
logged_in? && !locked? && writable? && of_posting_age?
else
logged_in? && !locked? && writable?
end
end

def update?
Expand Down
1 change: 1 addition & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
email { "#{ login }@example.com" }
admin false
banned false
created_at Time.now - 1.year

factory :moderator do
transient do
Expand Down
15 changes: 15 additions & 0 deletions spec/policies/application_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,25 @@
end

context 'with a user' do
ENV['POSTING_AGE_REQUIREMENT'] = '24'
let(:user){ create :user }
let(:record){ OpenStruct.new user_id: user.id + 1, section: 'project-1' }

it{ is_expected.to be_logged_in }
it{ is_expected.to be_of_posting_age }
it{ is_expected.to_not be_owner }
it{ is_expected.to_not be_moderator }
it{ is_expected.to_not be_admin }
it{ is_expected.to_not be_team }
it{ is_expected.to have_attributes user_roles: { } }
end

context 'with a brand new user' do
ENV['POSTING_AGE_REQUIREMENT'] = '24'
let(:user){ create :user, created_at: Time.now }

it{ is_expected.to be_logged_in }
it{ is_expected.to_not be_of_posting_age }
it{ is_expected.to_not be_owner }
it{ is_expected.to_not be_moderator }
it{ is_expected.to_not be_admin }
Expand Down
16 changes: 16 additions & 0 deletions spec/policies/comment_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@
it_behaves_like 'a policy permitting', :index, :show, :create, :move, :upvote, :remove_upvote
it_behaves_like 'a policy forbidding', :update, :destroy
end

context 'with a new account' do
let(:user){ create :user, created_at: Time.now }
ENV['POSTING_AGE_REQUIREMENT'] = '24'

context 'on a project board' do
it_behaves_like 'a policy permitting', :index, :show, :create, :upvote, :remove_upvote
it_behaves_like 'a policy forbidding', :update, :destroy, :move
end

context 'on the zooniverse board' do
let(:board){ create :board, section: 'zooniverse', permissions: { read: 'all', write: 'all' } }
it_behaves_like 'a policy permitting', :index, :show, :upvote, :remove_upvote
it_behaves_like 'a policy forbidding', :create, :update, :destroy, :move
end
end
end

context 'with permissions read:team write:team' do
Expand Down

0 comments on commit c1886b0

Please sign in to comment.