Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement posting age requirement #308

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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