Commit e9349a502423cf24b1df91a9d2e6a1e0bca73826
1 parent
39a8494c
Exists in
colab
and in
4 other branches
Improved ownership_authentication_spec coverage
Showing
2 changed files
with
54 additions
and
4 deletions
Show diff stats
app/controllers/concerns/ownership_authentication.rb
spec/controllers/concerns/ownership_authentication_spec.rb
| 1 | 1 | require 'spec_helper' |
| 2 | 2 | |
| 3 | -describe OwnershipAuthentication do | |
| 3 | +describe OwnershipAuthentication, type: :controller do | |
| 4 | 4 | #TODO: test other methods |
| 5 | 5 | |
| 6 | 6 | describe 'reading_group_owner?' do |
| 7 | + let(:reading_group) { FactoryGirl.build(:reading_group) } | |
| 8 | + | |
| 7 | 9 | context 'Not ReadingGroupsController nor ReadingsController' do |
| 10 | + let!(:projects_controller) { ProjectsController.new } | |
| 11 | + | |
| 8 | 12 | before do |
| 9 | - @projects_controller = ProjectsController.new # let doesn't work in here | |
| 10 | - @projects_controller.extend(OwnershipAuthentication) | |
| 13 | + projects_controller.extend(OwnershipAuthentication) | |
| 11 | 14 | end |
| 12 | 15 | |
| 13 | 16 | it 'should raise an exception' do |
| 14 | - expect { @projects_controller.reading_group_owner? }.to raise_error("Not supported") | |
| 17 | + expect { projects_controller.reading_group_owner? }.to raise_error("Not supported") | |
| 18 | + end | |
| 19 | + end | |
| 20 | + | |
| 21 | + context 'within ReadingsController' do | |
| 22 | + let! (:readings_controller) { ReadingsController.new } | |
| 23 | + | |
| 24 | + before do | |
| 25 | + readings_controller.params = {} | |
| 26 | + readings_controller.params[:reading_group_id] = reading_group.id | |
| 27 | + end | |
| 28 | + | |
| 29 | + context 'with a user logged in' do | |
| 30 | + let! (:current_user) { FactoryGirl.create(:user) } | |
| 31 | + | |
| 32 | + before do | |
| 33 | + readings_controller.expects(:current_user).returns(current_user) | |
| 34 | + end | |
| 35 | + | |
| 36 | + context 'when the user owns the ReadingGroup' do | |
| 37 | + let!(:reading_group_ownership) { FactoryGirl.build(:reading_group_ownership, {user_id: current_user.id, reading_group_id: reading_group.id}) } | |
| 38 | + | |
| 39 | + before do | |
| 40 | + reading_group_ownerships = Object.new | |
| 41 | + reading_group_ownerships.expects(:find_by_reading_group_id).with(reading_group.id).returns(reading_group_ownership) | |
| 42 | + current_user.expects(:reading_group_ownerships).returns(reading_group_ownerships) | |
| 43 | + end | |
| 44 | + | |
| 45 | + it 'should return true' do | |
| 46 | + readings_controller.reading_group_owner?.should be_true | |
| 47 | + end | |
| 48 | + end | |
| 49 | + | |
| 50 | + context 'when the user does not own the ReadingGroup' do | |
| 51 | + before do | |
| 52 | + reading_group_ownerships = Object.new | |
| 53 | + reading_group_ownerships.expects(:find_by_reading_group_id).with(reading_group.id).returns(nil) | |
| 54 | + current_user.expects(:reading_group_ownerships).returns(reading_group_ownerships) | |
| 55 | + end | |
| 56 | + | |
| 57 | + it 'should respond' do # FIXME: this is not the best test, but it it's the closest we can do I think | |
| 58 | + readings_controller.expects(:respond_to) | |
| 59 | + | |
| 60 | + readings_controller.reading_group_owner? | |
| 61 | + end | |
| 62 | + end | |
| 15 | 63 | end |
| 16 | 64 | end |
| 17 | 65 | end | ... | ... |