This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
Rearrange, not change the abilities in Ability #997
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
d00277c
WIP Rearrange, not change the abilities in Ability, cf recommended be…
emcoding 27ead4d
Clean file, without todo's and check comments
emcoding e1b56f1
First examples of the New And Complete ability specs. Now with update…
emcoding 6b4664e
Halfway
emcoding bfaeb99
WIP All things processed and annotated
emcoding f6da595
Cleanup file ; see previous commit for annotations
emcoding b172a3f
hot fix for failing spec MailingsController (because of wrong authori…
emcoding ec3c037
Finetuning
emcoding f84460e
Finetuning
emcoding dad17d8
Write tests for desired behaviour of student ability whether or not t…
emcoding ff1e8ba
Write tests for desired behaviour of whether a supervisor can or cann…
emcoding 975877f
Restore old code for can read_email and can users_info. Add tests for…
emcoding 1e5db7d
WIP Split the ability specs in separate files per role - thanks to @k…
emcoding cabcae9
WIP First try feature test guest user access
emcoding 6e7d601
FIX issue 1003; couldn't solve the abilities and pass the specs witho…
emcoding 9a0f39b
Remove one level of nesting in the ability specs, removed the repitio…
emcoding e765a2b
:cop:
emcoding 9d685da
Revert changes to authorize method in controllers that are not needed…
emcoding 419bdf7
Declutter
emcoding cb126b9
Add scope to solve failing specs.
emcoding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
# frozen_string_literal: true | ||
class MailingsController < ApplicationController | ||
|
||
load_and_authorize_resource except: :index | ||
load_and_authorize_resource | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
def index | ||
@mailings = Mailing.order('id DESC').page(params[:page]) | ||
authorize! :read, :mailing | ||
end | ||
|
||
# These actions are here to enable the cancancan 'not authorised' notice | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,11 @@ | |
|
||
let(:valid_attributes) { build(:team).attributes.merge(roles_attributes: [{ name: 'coach', github_handle: 'tobias' }]) } | ||
|
||
before do | ||
user.roles.create(name: 'student', team: team) | ||
end | ||
|
||
describe "GET index" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that the new ability rule prevents students from creating a second team, the specs can't assign two teams either. Moved the one global |
||
before do | ||
user.roles.create(name: 'student', team: team) | ||
end | ||
|
||
context 'before acceptance letters are sent' do | ||
let(:last_season) { Season.create name: Date.today.year - 1 } | ||
let!(:invisble_team) { create :team, :in_current_season, kind: nil, invisible: true } | ||
|
@@ -103,6 +103,10 @@ | |
end | ||
|
||
describe "GET edit" do | ||
before do | ||
user.roles.create(name: 'student', team: team) | ||
end | ||
|
||
context "their own team" do | ||
let(:team) { create(:team) } | ||
|
||
|
@@ -165,7 +169,10 @@ | |
end | ||
|
||
describe "PATCH update" do | ||
before { sign_in user } | ||
before do | ||
sign_in user | ||
user.roles.create(name: 'student', team: team) | ||
end | ||
|
||
context "their own team" do | ||
let(:team) { create(:team) } | ||
|
@@ -267,7 +274,10 @@ | |
end | ||
|
||
describe "DELETE destroy" do | ||
before { sign_in user } | ||
before do | ||
sign_in user | ||
user.roles.create(name: 'student', team: team) | ||
end | ||
|
||
context "their own team" do | ||
let(:params) { { id: team.to_param } } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,8 @@ | |
factory :season do | ||
sequence(:name, '2000') | ||
end | ||
|
||
trait :past do | ||
name '2010' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
FactoryBot.define do | ||
factory :user, aliases: [:member] do | ||
github_handle { FFaker::InternetSE.user_name_variant_short } | ||
github_handle { FFaker::InternetSE.unique.user_name_variant_short } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes flickering specs because of failing uniqueness validation on gh handle. |
||
name { FFaker::Name.name } | ||
email { FFaker::Internet.email } | ||
location { FFaker::Address.city } | ||
|
@@ -84,5 +84,9 @@ | |
create(:reviewer_role, user: user) | ||
end | ||
end | ||
|
||
trait :unconfirmed do | ||
confirmed_at nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'Guest User', type: :feature do | ||
|
||
let!(:activity) { create(:status_update, :published, team: team1) } | ||
let!(:other_user) { create(:user) } | ||
let!(:project) { create(:project, :in_current_season, :accepted, submitter: other_user) } | ||
let!(:team1) { create(:team, :in_current_season, name: 'Cheesy forever', project_name: project.name, project_id: project.id) } | ||
let!(:out_of_season) { Season.current.starts_at - 1.week } | ||
let!(:summer_season) { Season.current.starts_at + 1.week } | ||
|
||
context "when visiting public pages" do | ||
|
||
context 'All Year' do | ||
before { Timecop.travel(out_of_season) } | ||
after { Timecop.return } | ||
|
||
it 'can view Activities' do | ||
visit root_path | ||
expect(page).to have_css('h1', text: 'Activities') | ||
find('.title', match: :smart).click | ||
expect(page).to have_content(activity.title) | ||
expect(page).to have_content('You must be logged in to add a comment.') | ||
end | ||
|
||
it 'can view Community and User' do | ||
visit community_path | ||
expect(page).to have_css('h1', text: 'Community') | ||
find_link(other_user.name, match: :smart).click | ||
expect(page).to have_content("About me") | ||
expect(page).to have_link("All participants") | ||
expect(page).not_to have_link("Edit") # check | ||
end | ||
|
||
it 'can view projects' do | ||
visit projects_path | ||
expect(page).to have_css('h1', text: 'Projects') # can be empty table | ||
end | ||
|
||
it 'has a nav menu with public links' do | ||
visit root_path | ||
expect(page).to have_link("Activities") | ||
find_link("Summer of Code").click | ||
expect(page).to have_link("Teams") | ||
expect(page).to have_link("Community") | ||
expect(page).to have_link("Help") | ||
end | ||
|
||
it 'has access to sign in link' do | ||
visit root_path | ||
expect(page).to have_link('Sign in') | ||
end | ||
end | ||
|
||
context 'in season' do | ||
before do | ||
Timecop.travel(summer_season) | ||
end | ||
after { Timecop.return } | ||
|
||
it "can view the current season's accepted and selected projects" do | ||
visit projects_path | ||
expect(page).to have_css('h1', text: 'Projects') | ||
find_link(project.name, match: :smart).click | ||
expect(page).to have_content project.description | ||
expect(page).not_to have_link("Edit") | ||
end | ||
end | ||
end | ||
# continuing story in: sign_in_unconfirmed_user || sign_in_confirmed_user || sign_in_fail | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'rails_helper' | ||
require 'cancan/matchers' | ||
|
||
# Run this file with | ||
# $ rspec spec/models/ability_spec.rb -fd | ||
# to see the output of specs running inside the shared examples [mdv] | ||
RSpec.describe Ability, type: :model do | ||
|
||
let(:admin) { create(:user) } | ||
subject(:ability) { Ability.new(admin) } | ||
|
||
let(:other_user) { build_stubbed(:user, hide_email: true) } | ||
|
||
describe "Admin" do | ||
before { allow(admin).to receive(:admin?).and_return true } | ||
|
||
it { expect(subject).not_to be_able_to(:create, User.new) } # happens only via GitHub | ||
# it "has access to almost everything else" | ||
# Only test the most exclusive, the most sensitive and the 'cannots': | ||
it { expect(subject).to be_able_to(:crud, Team) } | ||
it { expect(subject).to be_able_to([:read, :update, :destroy], User) } | ||
it { expect(subject).to be_able_to(:read_email, other_user) } | ||
it { expect(subject).to be_able_to(:read, :users_info, other_user) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'rails_helper' | ||
require 'cancan/matchers' | ||
|
||
# Run this file with | ||
# $ rspec spec/models/ability_spec.rb -fd | ||
# to see the output of specs running inside the shared examples [mdv] | ||
RSpec.describe Ability, type: :model do | ||
|
||
let(:user) { build_stubbed(:user) } | ||
subject(:ability) { Ability.new(user) } | ||
let(:other_user) { build_stubbed(:user) } | ||
|
||
describe "Confirmed user" do | ||
|
||
it_behaves_like 'has access to public features' | ||
|
||
# same as unconfirmed: | ||
it "can modify own account" do | ||
expect(subject).to be_able_to([:update, :destroy], user) | ||
expect(subject).to be_able_to(:resend_confirmation_instruction, User, id: user.id) | ||
end | ||
it { expect(subject).not_to be_able_to([:update, :destroy], other_user) } | ||
|
||
# the perks of confirming | ||
it { expect(subject).to be_able_to([:join, :create], Team) } | ||
it { expect(subject).to be_able_to(:create, Comment) } # TODO needs work for polymorphism | ||
it { expect(subject).to be_able_to(:create, Project) } | ||
it { expect(subject).to be_able_to(:index, Mailing) } | ||
it { expect(subject).to be_able_to(:read, Mailing, recipient: user )} | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏎 The 'not authorised' method will now show up on the page where the user tried to do something forbidden. We couldn't
redirect :back
before, because it would mess up if there wasn't a referer present.