transferred Code Ocean from original repository to GitHub

This commit is contained in:
Hauke Klement
2015-01-22 09:51:49 +01:00
commit 4cbf9970b1
683 changed files with 11979 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
require 'rails_helper'
describe ApplicationPolicy do
describe '#initialize' do
context 'without a user' do
it 'raises an error' do
expect { ApplicationPolicy.new(nil, nil) }.to raise_error(Pundit::NotAuthorizedError)
end
end
end
end

View File

@@ -0,0 +1,73 @@
require 'rails_helper'
describe CodeOcean::FilePolicy do
subject { CodeOcean::FilePolicy }
let(:file) { FactoryGirl.build(:file) }
let(:exercise) { FactoryGirl.create(:fibonacci) }
let(:submission) { FactoryGirl.create(:submission) }
permissions :create? do
context 'as part of an exercise' do
before(:each) { file.context = exercise }
it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), file)
end
it 'grants access to authors' do
expect(subject).to permit(exercise.author, file)
end
it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), file)
end
end
end
context 'as part of a submission' do
before(:each) { file.context = submission }
it 'grants access to authors' do
expect(subject).to permit(submission.author, file)
end
it 'does not grant access to all other users' do
[:admin, :external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), file)
end
end
end
end
permissions :destroy? do
context 'as part of an exercise' do
before(:each) { file.context = exercise }
it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), file)
end
it 'grants access to authors' do
expect(subject).to permit(exercise.author, file)
end
it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), file)
end
end
end
context 'as part of a submission' do
before(:each) { file.context = submission }
it 'does not grant access to anyone' do
[:admin, :external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), file)
end
end
end
end
end

View File

@@ -0,0 +1,16 @@
require 'rails_helper'
describe ConsumerPolicy do
subject { ConsumerPolicy }
[:create?, :destroy?, :edit?, :index?, :new?, :show?, :update?].each do |action|
permissions(action) do
it 'grants access to admins only' do
expect(subject).to permit(FactoryGirl.build(:admin), Consumer.new)
[:external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), Consumer.new)
end
end
end
end
end

View File

@@ -0,0 +1,37 @@
require 'rails_helper'
describe ErrorPolicy do
subject { ErrorPolicy }
let(:error) { FactoryGirl.build(:error) }
permissions :index? do
it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), error)
end
it 'grants access to teachers' do
expect(subject).to permit(FactoryGirl.build(:teacher), error)
end
it 'does not grant access to external users' do
expect(subject).not_to permit(FactoryGirl.build(:external_user), error)
end
end
permissions :show? do
it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), error)
end
it 'grants access to authors' do
expect(subject).to permit(error.execution_environment.author, error)
end
it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), error)
end
end
end
end

View File

@@ -0,0 +1,41 @@
require 'rails_helper'
describe ExecutionEnvironmentPolicy do
subject { ExecutionEnvironmentPolicy }
let(:execution_environment) { FactoryGirl.build(:ruby) }
[:create?, :index?, :new?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), execution_environment)
end
it 'grants access to teachers' do
expect(subject).to permit(FactoryGirl.build(:teacher), execution_environment)
end
it 'does not grant access to external users' do
expect(subject).not_to permit(FactoryGirl.build(:external_user), execution_environment)
end
end
end
[:destroy?, :edit?, :execute_command?, :shell?, :show?, :update?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), execution_environment)
end
it 'grants access to authors' do
expect(subject).to permit(execution_environment.author, execution_environment)
end
it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), execution_environment)
end
end
end
end
end

View File

@@ -0,0 +1,99 @@
require 'rails_helper'
describe ExercisePolicy do
subject { ExercisePolicy }
let(:exercise) { FactoryGirl.build(:fibonacci) }
[:create?, :index?, :new?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), exercise)
end
it 'grants access to teachers' do
expect(subject).to permit(FactoryGirl.build(:teacher), exercise)
end
it 'does not grant access to external users' do
expect(subject).not_to permit(FactoryGirl.build(:external_user), exercise)
end
end
end
[:destroy?, :edit?, :show?, :update?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), exercise)
end
it 'grants access to authors' do
expect(subject).to permit(exercise.author, exercise)
end
it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), exercise)
end
end
end
end
[:implement?, :submit?].each do |action|
permissions(action) do
it 'grants access to anyone' do
[:admin, :external_user, :teacher].each do |factory_name|
expect(subject).to permit(FactoryGirl.build(factory_name), Exercise.new)
end
end
end
end
describe ExercisePolicy::Scope do
describe '#resolve' do
let(:admin) { FactoryGirl.create(:admin) }
let(:external_user) { FactoryGirl.create(:external_user) }
let(:teacher) { FactoryGirl.create(:teacher) }
before(:each) do
[admin, teacher].each do |user|
[true, false].each do |public|
FactoryGirl.create(:fibonacci, public: public, user_id: user.id, user_type: InternalUser.class.name)
end
end
end
context 'for admins' do
let(:scope) { Pundit.policy_scope!(admin, Exercise) }
it 'returns all exercises' do
expect(scope.map(&:id)).to include(*Exercise.all.map(&:id))
end
end
context 'for external users' do
let(:scope) { Pundit.policy_scope!(external_user, Exercise) }
it 'returns only public exercises' do
expect(scope.map(&:id)).to include(*Exercise.where(public: true).map(&:id))
end
end
context 'for teachers' do
let(:scope) { Pundit.policy_scope!(teacher, Exercise) }
it 'includes all public exercises' do
expect(scope.map(&:id)).to include(*Exercise.where(public: true).map(&:id))
end
it 'includes all authored non-public exercises' do
expect(scope.map(&:id)).to include(*Exercise.where(public: false, user_id: teacher.id).map(&:id))
end
it "does not include other authors' non-public exercises" do
expect(scope.map(&:id)).not_to include(*Exercise.where(public: false).where("user_id <> #{teacher.id}").map(&:id))
end
end
end
end
end

View File

@@ -0,0 +1,16 @@
require 'rails_helper'
describe ExternalUserPolicy do
subject { ExternalUserPolicy }
[:create?, :destroy?, :edit?, :index?, :new?, :show?, :update?].each do |action|
permissions(action) do
it 'grants access to admins only' do
expect(subject).to permit(FactoryGirl.build(:admin), ExternalUser.new)
[:external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), ExternalUser.new)
end
end
end
end
end

View File

@@ -0,0 +1,41 @@
require 'rails_helper'
describe FileTypePolicy do
subject { FileTypePolicy }
let(:file_type) { FactoryGirl.build(:dot_rb) }
[:create?, :index?, :new?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), file_type)
end
it 'grants access to teachers' do
expect(subject).to permit(FactoryGirl.build(:teacher), file_type)
end
it 'does not grant access to external users' do
expect(subject).not_to permit(FactoryGirl.build(:external_user), file_type)
end
end
end
[:destroy?, :edit?, :show?, :update?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), file_type)
end
it 'grants access to authors' do
expect(subject).to permit(file_type.author, file_type)
end
it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), file_type)
end
end
end
end
end

View File

@@ -0,0 +1,41 @@
require 'rails_helper'
describe HintPolicy do
subject { HintPolicy }
let(:hint) { FactoryGirl.build(:ruby_no_method_error) }
[:create?, :index?, :new?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), hint)
end
it 'grants access to teachers' do
expect(subject).to permit(FactoryGirl.build(:teacher), hint)
end
it 'does not grant access to external users' do
expect(subject).not_to permit(FactoryGirl.build(:external_user), hint)
end
end
end
[:destroy?, :edit?, :show?, :update?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), hint)
end
it 'grants access to authors' do
expect(subject).to permit(hint.execution_environment.author, hint)
end
it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), hint)
end
end
end
end
end

View File

@@ -0,0 +1,35 @@
require 'rails_helper'
describe InternalUserPolicy do
subject { InternalUserPolicy }
[:create?, :edit?, :index?, :new?, :show?, :update?].each do |action|
permissions(action) do
it 'grants access to admins only' do
expect(subject).to permit(FactoryGirl.build(:admin), InternalUser.new)
[:external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), InternalUser.new)
end
end
end
end
permissions :destroy? do
context 'with an admin user' do
it 'grants access to no one' do
[:admin, :external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), FactoryGirl.build(:admin))
end
end
end
context 'with a non-admin user' do
it 'grants access to admins only' do
expect(subject).to permit(FactoryGirl.build(:admin), InternalUser.new)
[:external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), FactoryGirl.build(:teacher))
end
end
end
end
end

View File

@@ -0,0 +1,35 @@
require 'rails_helper'
describe SubmissionPolicy do
subject { SubmissionPolicy }
permissions :create? do
it 'grants access to anyone' do
[:admin, :external_user, :teacher].each do |factory_name|
expect(subject).to permit(FactoryGirl.build(factory_name), Submission.new)
end
end
end
[:download_file?, :render_file?, :run?, :score?, :show?, :statistics?, :stop?, :test?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryGirl.build(:admin), Submission.new)
end
it 'grants access to authors' do
user = FactoryGirl.create(:external_user)
expect(subject).to permit(user, FactoryGirl.build(:submission, user_id: user.id, user_type: user.class.name))
end
end
end
permissions :index? do
it 'grants access to admins only' do
expect(subject).to permit(FactoryGirl.build(:admin), Submission.new)
[:external_user, :teacher].each do |factory_name|
expect(subject).not_to permit(FactoryGirl.build(factory_name), Submission.new)
end
end
end
end