Introduce new enabled option for CodeHarbor

* Fix tests to ensure they work independent of config option
This commit is contained in:
Sebastian Serth
2021-02-16 14:24:35 +01:00
parent bcaa3244f4
commit 293bcccc80
8 changed files with 125 additions and 62 deletions

View File

@ -1,11 +1,23 @@
# frozen_string_literal: true
require 'rails_helper'
describe CodeharborLinksController do
let(:user) { FactoryBot.create(:teacher) }
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
describe 'GET #new' do
before { get :new }
let(:codeocean_config) { instance_double(CodeOcean::Config) }
let(:codeharbor_config) { {codeharbor: {enabled: true, url: 'http://test.url'}} }
before do
allow(CodeOcean::Config).to receive(:new).with(:code_ocean).and_return(codeocean_config)
allow(codeocean_config).to receive(:read).and_return(codeharbor_config)
allow(controller).to receive(:current_user).and_return(user)
end
context 'GET #new' do
before do
get :new
end
expect_status(200)
end
@ -13,7 +25,7 @@ describe CodeharborLinksController do
describe 'GET #edit' do
let(:codeharbor_link) { FactoryBot.create(:codeharbor_link, user: user) }
before { get :edit, params: { id: codeharbor_link.id } }
before { get :edit, params: {id: codeharbor_link.id} }
expect_status(200)
end
@ -52,9 +64,11 @@ describe CodeharborLinksController do
it 'updates push_url' do
expect { put_request }.to change { codeharbor_link.reload.push_url }.to('http://foo.bar/push')
end
it 'updates check_uuid_url' do
expect { put_request }.to change { codeharbor_link.reload.check_uuid_url }.to('http://foo.bar/check')
end
it 'updates api_key' do
expect { put_request }.to change { codeharbor_link.reload.api_key }.to('api_key')
end
@ -67,7 +81,7 @@ describe CodeharborLinksController do
let(:params) { {push_url: '', check_uuid_url: '', api_key: ''} }
it 'does not change codeharbor_link' do
expect { put_request }.not_to change { codeharbor_link.reload.attributes }
expect { put_request }.not_to(change { codeharbor_link.reload.attributes })
end
it 'redirects to user show' do
@ -90,4 +104,3 @@ describe CodeharborLinksController do
end
end
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe CodeharborLinkPolicy do
@ -5,37 +7,73 @@ describe CodeharborLinkPolicy do
let(:codeharbor_link) { FactoryBot.create(:codeharbor_link) }
%i[index? show?].each do |action|
permissions(action) do
it 'does not grant access any user' do
%i[external_user teacher admin].each do |factory_name|
expect(policy).not_to permit(FactoryBot.create(factory_name), codeharbor_link)
context 'when CodeHarbor link is enabled' do
let(:codeocean_config) { instance_double(CodeOcean::Config) }
let(:codeharbor_config) { {codeharbor: {enabled: true}} }
before do
allow(CodeOcean::Config).to receive(:new).with(:code_ocean).and_return(codeocean_config)
allow(codeocean_config).to receive(:read).and_return(codeharbor_config)
end
%i[index? show?].each do |action|
permissions(action) do
it 'does not grant access any user' do
%i[external_user teacher admin].each do |factory_name|
expect(policy).not_to permit(FactoryBot.create(factory_name), codeharbor_link)
end
end
end
end
end
%i[new? create?].each do |action|
permissions(action) do
it 'grants access to teachers' do
%i[teacher admin].each do |factory_name|
%i[new? create?].each do |action|
permissions(action) do
it 'grants access to teachers' do
%i[teacher admin].each do |factory_name|
expect(policy).to permit(FactoryBot.create(factory_name), codeharbor_link)
end
end
it 'does not grant access to all other users' do
expect(policy).not_to permit(FactoryBot.create(:external_user), codeharbor_link)
end
end
end
%i[destroy? edit? update?].each do |action|
permissions(action) do
it 'grants access to the owner of the link' do
expect(policy).to permit(codeharbor_link.user, codeharbor_link)
end
it 'does not grant access to arbitrary users' do
%i[external_user admin teacher].each do |factory_name|
expect(policy).not_to permit(FactoryBot.create(factory_name), codeharbor_link)
end
end
end
end
permissions(:enabled?) do
it 'reflects the config option' do
%i[external_user admin teacher].each do |factory_name|
expect(policy).to permit(FactoryBot.create(factory_name), codeharbor_link)
end
end
it 'does not grant access to all other users' do
expect(policy).not_to permit(FactoryBot.create(:external_user), codeharbor_link)
end
end
end
%i[destroy? edit? update?].each do |action|
permissions(action) do
it 'grants access to the owner of the link' do
expect(policy).to permit(codeharbor_link.user, codeharbor_link)
end
context 'when CodeHabor link is disabled' do
let(:codeocean_config) { instance_double(CodeOcean::Config) }
let(:codeharbor_config) { {codeharbor: {enabled: false}} }
it 'does not grant access to arbitrary users' do
before do
allow(CodeOcean::Config).to receive(:new).with(:code_ocean).and_return(codeocean_config)
allow(codeocean_config).to receive(:read).and_return(codeharbor_config)
end
permissions(:enabled?) do
it 'reflects the config option' do
%i[external_user admin teacher].each do |factory_name|
expect(policy).not_to permit(FactoryBot.create(factory_name), codeharbor_link)
end