diff --git a/app/controllers/concerns/ownership_authentication.rb b/app/controllers/concerns/ownership_authentication.rb index 7ff3dd8..718790b 100644 --- a/app/controllers/concerns/ownership_authentication.rb +++ b/app/controllers/concerns/ownership_authentication.rb @@ -47,6 +47,8 @@ module OwnershipAuthentication format.json { head :no_content } end end + + return true end def check_mezuro_configuration_ownership(id) diff --git a/spec/controllers/concerns/ownership_authentication_spec.rb b/spec/controllers/concerns/ownership_authentication_spec.rb index e1d5fdb..311ff53 100644 --- a/spec/controllers/concerns/ownership_authentication_spec.rb +++ b/spec/controllers/concerns/ownership_authentication_spec.rb @@ -1,17 +1,65 @@ require 'spec_helper' -describe OwnershipAuthentication do +describe OwnershipAuthentication, type: :controller do #TODO: test other methods describe 'reading_group_owner?' do + let(:reading_group) { FactoryGirl.build(:reading_group) } + context 'Not ReadingGroupsController nor ReadingsController' do + let!(:projects_controller) { ProjectsController.new } + before do - @projects_controller = ProjectsController.new # let doesn't work in here - @projects_controller.extend(OwnershipAuthentication) + projects_controller.extend(OwnershipAuthentication) end it 'should raise an exception' do - expect { @projects_controller.reading_group_owner? }.to raise_error("Not supported") + expect { projects_controller.reading_group_owner? }.to raise_error("Not supported") + end + end + + context 'within ReadingsController' do + let! (:readings_controller) { ReadingsController.new } + + before do + readings_controller.params = {} + readings_controller.params[:reading_group_id] = reading_group.id + end + + context 'with a user logged in' do + let! (:current_user) { FactoryGirl.create(:user) } + + before do + readings_controller.expects(:current_user).returns(current_user) + end + + context 'when the user owns the ReadingGroup' do + let!(:reading_group_ownership) { FactoryGirl.build(:reading_group_ownership, {user_id: current_user.id, reading_group_id: reading_group.id}) } + + before do + reading_group_ownerships = Object.new + reading_group_ownerships.expects(:find_by_reading_group_id).with(reading_group.id).returns(reading_group_ownership) + current_user.expects(:reading_group_ownerships).returns(reading_group_ownerships) + end + + it 'should return true' do + readings_controller.reading_group_owner?.should be_true + end + end + + context 'when the user does not own the ReadingGroup' do + before do + reading_group_ownerships = Object.new + reading_group_ownerships.expects(:find_by_reading_group_id).with(reading_group.id).returns(nil) + current_user.expects(:reading_group_ownerships).returns(reading_group_ownerships) + end + + it 'should respond' do # FIXME: this is not the best test, but it it's the closest we can do I think + readings_controller.expects(:respond_to) + + readings_controller.reading_group_owner? + end + end end end end -- libgit2 0.21.2