Apply automatic rubocop fixes

This commit is contained in:
Sebastian Serth
2021-05-14 10:51:44 +02:00
parent fe4000916c
commit 6cbecb5b39
440 changed files with 2705 additions and 1853 deletions

View File

@ -19,7 +19,7 @@ describe FileParameters do
describe 'accepts' do
it 'main file of the exercise' do
main_file = hello_world.files.find { |e| e.role = 'main_file' }
main_file = hello_world.files.find {|e| e.role = 'main_file' }
expect(file_accepted?(main_file)).to be true
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
class Controller < AnonymousController
@ -31,7 +33,7 @@ describe Lti do
let(:last_name) { 'Doe' }
let(:full_name) { 'John Doe' }
let(:provider) { double }
let(:provider_full) { double(:lis_person_name_full => full_name) }
let(:provider_full) { double(lis_person_name_full: full_name) }
context 'when a full name is provided' do
it 'returns the full name' do
@ -61,7 +63,8 @@ describe Lti do
describe '#return_to_consumer' do
context 'with a return URL' do
let(:consumer_return_url) { 'http://example.org' }
before(:each) { expect(controller).to receive(:params).and_return(launch_presentation_return_url: consumer_return_url) }
before { expect(controller).to receive(:params).and_return(launch_presentation_return_url: consumer_return_url) }
it 'redirects to the tool consumer' do
expect(controller).to receive(:redirect_to).with(consumer_return_url)
@ -76,7 +79,7 @@ describe Lti do
end
context 'without a return URL' do
before(:each) do
before do
expect(controller).to receive(:params).and_return({})
expect(controller).to receive(:redirect_to).with(:root)
end
@ -101,7 +104,7 @@ describe Lti do
let(:consumer) { FactoryBot.create(:consumer) }
let(:score) { 0.5 }
let(:submission) { FactoryBot.create(:submission) }
let!(:lti_parameter) { FactoryBot.create(:lti_parameter, consumers_id: consumer.id, external_users_id: submission.user_id, exercises_id: submission.exercise_id)}
let!(:lti_parameter) { FactoryBot.create(:lti_parameter, consumers_id: consumer.id, external_users_id: submission.user_id, exercises_id: submission.exercise_id) }
context 'with an invalid score' do
it 'raises an exception' do
@ -112,7 +115,6 @@ describe Lti do
context 'with an valid score' do
context 'with a tool consumer' do
context 'when grading is not supported' do
it 'returns a corresponding status' do
expect_any_instance_of(IMS::LTI::ToolProvider).to receive(:outcome_service?).and_return(false)
@ -124,7 +126,7 @@ describe Lti do
context 'when grading is supported' do
let(:response) { double }
before(:each) do
before do
expect_any_instance_of(IMS::LTI::ToolProvider).to receive(:outcome_service?).and_return(true)
expect_any_instance_of(IMS::LTI::ToolProvider).to receive(:post_replace_result!).with(score).and_return(response)
expect(response).to receive(:response_code).at_least(:once).and_return(200)
@ -170,7 +172,7 @@ describe Lti do
controller.send(:store_lti_session_data, consumer: FactoryBot.build(:consumer), parameters: parameters)
end
it 'it creates an LtiParameter Object' do
it 'creates an LtiParameter Object' do
before_count = LtiParameter.count
controller.instance_variable_set(:@current_user, FactoryBot.create(:external_user))
controller.instance_variable_set(:@exercise, FactoryBot.create(:fibonacci))

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
class Controller < AnonymousController
@ -6,11 +8,13 @@ end
describe SubmissionScoring do
let(:controller) { Controller.new }
before(:all) { @submission = FactoryBot.create(:submission, cause: 'submit') }
before(:each) { controller.instance_variable_set(:@current_user, FactoryBot.create(:external_user)) }
before { controller.instance_variable_set(:@current_user, FactoryBot.create(:external_user)) }
describe '#collect_test_results' do
after(:each) { controller.send(:collect_test_results, @submission) }
after { controller.send(:collect_test_results, @submission) }
it 'executes every teacher-defined test file' do
@submission.collect_files.select(&:teacher_defined_assessment?).each do |file|
@ -20,7 +24,7 @@ describe SubmissionScoring do
end
describe '#score_submission' do
after(:each) { controller.score_submission(@submission) }
after { controller.score_submission(@submission) }
it 'collects the test results' do
expect(controller).to receive(:collect_test_results).and_return([])

View File

@ -1,18 +1,20 @@
# frozen_string_literal: true
require 'rails_helper'
describe Admin::DashboardController do
before(:each) { allow(controller).to receive(:current_user).and_return(FactoryBot.build(:admin)) }
before { allow(controller).to receive(:current_user).and_return(FactoryBot.build(:admin)) }
describe 'GET #show' do
describe 'with format HTML' do
before(:each) { get :show }
before { get :show }
expect_status(200)
expect_template(:show)
end
describe 'with format JSON' do
before(:each) { get :show, format: :json }
before { get :show, format: :json }
expect_json
expect_status(200)

View File

@ -1,10 +1,13 @@
# frozen_string_literal: true
require 'rails_helper'
describe ApplicationController do
describe '#current_user' do
context 'with an external user' do
let(:external_user) { FactoryBot.create(:external_user) }
before(:each) { session[:external_user_id] = external_user.id }
before { session[:external_user_id] = external_user.id }
it 'returns the external user' do
expect(controller.current_user).to eq(external_user)
@ -13,7 +16,8 @@ describe ApplicationController do
context 'without an external user' do
let(:internal_user) { FactoryBot.create(:teacher) }
before(:each) { login_user(internal_user) }
before { login_user(internal_user) }
it 'returns the internal user' do
expect(controller.current_user).to eq(internal_user)
@ -22,7 +26,7 @@ describe ApplicationController do
end
describe '#render_not_authorized' do
before(:each) do
before do
expect(controller).to receive(:welcome) { controller.send(:render_not_authorized) }
get :welcome
end
@ -35,19 +39,19 @@ describe ApplicationController do
let(:locale) { :de }
context 'when specifying a locale' do
before(:each) { allow(session).to receive(:[]=).at_least(:once) }
before { allow(session).to receive(:[]=).at_least(:once) }
context "using the 'custom_locale' parameter" do
it 'overwrites the session' do
expect(session).to receive(:[]=).with(:locale, locale.to_s)
get :welcome, params: { custom_locale: locale }
get :welcome, params: {custom_locale: locale}
end
end
context "using the 'locale' parameter" do
it 'overwrites the session' do
expect(session).to receive(:[]=).with(:locale, locale.to_s)
get :welcome, params: { locale: locale }
get :welcome, params: {locale: locale}
end
end
end
@ -70,7 +74,7 @@ describe ApplicationController do
end
describe 'GET #welcome' do
before(:each) { get :welcome }
before { get :welcome }
expect_status(200)
expect_template(:welcome)

View File

@ -4,14 +4,16 @@ require 'rails_helper'
describe CodeOcean::FilesController do
let(:user) { FactoryBot.create(:admin) }
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
before { allow(controller).to receive(:current_user).and_return(user) }
describe 'POST #create' do
let(:submission) { FactoryBot.create(:submission, user: user) }
context 'with a valid file' do
let(:perform_request) { proc { post :create, params: { code_ocean_file: FactoryBot.build(:file, context: submission).attributes, format: :json } } }
before(:each) do
let(:perform_request) { proc { post :create, params: {code_ocean_file: FactoryBot.build(:file, context: submission).attributes, format: :json} } }
before do
submission.exercise.update(allow_file_creation: true)
perform_request.call
end
@ -27,9 +29,9 @@ describe CodeOcean::FilesController do
end
context 'with an invalid file' do
before(:each) do
before do
submission.exercise.update(allow_file_creation: true)
post :create, params: { code_ocean_file: {context_id: submission.id, context_type: Submission}, format: :json }
post :create, params: {code_ocean_file: {context_id: submission.id, context_type: Submission}, format: :json}
end
expect_assigns(file: CodeOcean::File)
@ -40,8 +42,9 @@ describe CodeOcean::FilesController do
describe 'DELETE #destroy' do
let(:exercise) { FactoryBot.create(:fibonacci) }
let(:perform_request) { proc { delete :destroy, params: { id: exercise.files.first.id } } }
before(:each) { perform_request.call }
let(:perform_request) { proc { delete :destroy, params: {id: exercise.files.first.id} } }
before { perform_request.call }
expect_assigns(file: CodeOcean::File)

View File

@ -1,14 +1,18 @@
# frozen_string_literal: true
require 'rails_helper'
describe ConsumersController do
let(:consumer) { FactoryBot.create(:consumer) }
let(:user) { FactoryBot.create(:admin) }
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
before { allow(controller).to receive(:current_user).and_return(user) }
describe 'POST #create' do
context 'with a valid consumer' do
let(:perform_request) { proc { post :create, params: { consumer: FactoryBot.attributes_for(:consumer) } } }
before(:each) { perform_request.call }
let(:perform_request) { proc { post :create, params: {consumer: FactoryBot.attributes_for(:consumer)} } }
before { perform_request.call }
expect_assigns(consumer: Consumer)
@ -20,7 +24,7 @@ describe ConsumersController do
end
context 'with an invalid consumer' do
before(:each) { post :create, params: { consumer: {} } }
before { post :create, params: {consumer: {}} }
expect_assigns(consumer: Consumer)
expect_status(200)
@ -29,20 +33,20 @@ describe ConsumersController do
end
describe 'DELETE #destroy' do
before(:each) { delete :destroy, params: { id: consumer.id } }
before { delete :destroy, params: {id: consumer.id} }
expect_assigns(consumer: Consumer)
it 'destroys the consumer' do
consumer = FactoryBot.create(:consumer)
expect { delete :destroy, params: { id: consumer.id } }.to change(Consumer, :count).by(-1)
expect { delete :destroy, params: {id: consumer.id} }.to change(Consumer, :count).by(-1)
end
expect_redirect(:consumers)
end
describe 'GET #edit' do
before(:each) { get :edit, params: { id: consumer.id } }
before { get :edit, params: {id: consumer.id} }
expect_assigns(consumer: Consumer)
expect_status(200)
@ -51,7 +55,8 @@ describe ConsumersController do
describe 'GET #index' do
let!(:consumers) { FactoryBot.create_pair(:consumer) }
before(:each) { get :index }
before { get :index }
expect_assigns(consumers: Consumer.all)
expect_status(200)
@ -59,7 +64,7 @@ describe ConsumersController do
end
describe 'GET #new' do
before(:each) { get :new }
before { get :new }
expect_assigns(consumer: Consumer)
expect_status(200)
@ -67,7 +72,7 @@ describe ConsumersController do
end
describe 'GET #show' do
before(:each) { get :show, params: { id: consumer.id } }
before { get :show, params: {id: consumer.id} }
expect_assigns(consumer: :consumer)
expect_status(200)
@ -76,14 +81,14 @@ describe ConsumersController do
describe 'PUT #update' do
context 'with a valid consumer' do
before(:each) { put :update, params: { consumer: FactoryBot.attributes_for(:consumer), id: consumer.id } }
before { put :update, params: {consumer: FactoryBot.attributes_for(:consumer), id: consumer.id} }
expect_assigns(consumer: Consumer)
expect_redirect(:consumer)
end
context 'with an invalid consumer' do
before(:each) { put :update, params: { consumer: {name: ''}, id: consumer.id } }
before { put :update, params: {consumer: {name: ''}, id: consumer.id} }
expect_assigns(consumer: Consumer)
expect_status(200)

View File

@ -1,43 +1,46 @@
# frozen_string_literal: true
require 'rails_helper'
describe ErrorTemplateAttributesController do
let!(:error_template_attribute) { FactoryBot.create(:error_template_attribute) }
let(:user) { FactoryBot.create(:admin) }
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
it "should get index" do
before { allow(controller).to receive(:current_user).and_return(user) }
it 'gets index' do
get :index
expect(response.status).to eq(200)
expect(assigns(:error_template_attributes)).not_to be_nil
end
it "should get new" do
it 'gets new' do
get :new
expect(response.status).to eq(200)
end
it "should create error_template_attribute" do
expect { post :create, params: { error_template_attribute: { } } }.to change(ErrorTemplateAttribute, :count).by(1)
it 'creates error_template_attribute' do
expect { post :create, params: {error_template_attribute: {}} }.to change(ErrorTemplateAttribute, :count).by(1)
expect(response).to redirect_to(error_template_attribute_path(assigns(:error_template_attribute)))
end
it "should show error_template_attribute" do
get :show, params: { id: error_template_attribute }
it 'shows error_template_attribute' do
get :show, params: {id: error_template_attribute}
expect(response.status).to eq(200)
end
it "should get edit" do
get :edit, params: { id: error_template_attribute }
it 'gets edit' do
get :edit, params: {id: error_template_attribute}
expect(response.status).to eq(200)
end
it "should update error_template_attribute" do
patch :update, params: { id: error_template_attribute, error_template_attribute: FactoryBot.attributes_for(:error_template_attribute) }
it 'updates error_template_attribute' do
patch :update, params: {id: error_template_attribute, error_template_attribute: FactoryBot.attributes_for(:error_template_attribute)}
expect(response).to redirect_to(error_template_attribute_path(assigns(:error_template_attribute)))
end
it "should destroy error_template_attribute" do
expect { delete :destroy, params: { id: error_template_attribute } }.to change(ErrorTemplateAttribute, :count).by(-1)
it 'destroys error_template_attribute' do
expect { delete :destroy, params: {id: error_template_attribute} }.to change(ErrorTemplateAttribute, :count).by(-1)
expect(response).to redirect_to(error_template_attributes_path)
end
end

View File

@ -1,43 +1,46 @@
# frozen_string_literal: true
require 'rails_helper'
describe ErrorTemplatesController do
let!(:error_template) { FactoryBot.create(:error_template) }
let(:user) { FactoryBot.create(:admin) }
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
it "should get index" do
before { allow(controller).to receive(:current_user).and_return(user) }
it 'gets index' do
get :index
expect(response.status).to eq(200)
expect(assigns(:error_templates)).not_to be_nil
end
it "should get new" do
it 'gets new' do
get :new
expect(response.status).to eq(200)
end
it "should create error_template" do
expect { post :create, params: {error_template: { execution_environment_id: error_template.execution_environment.id } } }.to change(ErrorTemplate, :count).by(1)
it 'creates error_template' do
expect { post :create, params: {error_template: {execution_environment_id: error_template.execution_environment.id}} }.to change(ErrorTemplate, :count).by(1)
expect(response).to redirect_to(error_template_path(assigns(:error_template)))
end
it "should show error_template" do
get :show, params: { id: error_template }
it 'shows error_template' do
get :show, params: {id: error_template}
expect(response.status).to eq(200)
end
it "should get edit" do
get :edit, params: { id: error_template }
it 'gets edit' do
get :edit, params: {id: error_template}
expect(response.status).to eq(200)
end
it "should update error_template" do
patch :update, params: { id: error_template, error_template: FactoryBot.attributes_for(:error_template) }
it 'updates error_template' do
patch :update, params: {id: error_template, error_template: FactoryBot.attributes_for(:error_template)}
expect(response).to redirect_to(error_template_path(assigns(:error_template)))
end
it "should destroy error_template" do
expect { delete :destroy, params: { id: error_template } }.to change(ErrorTemplate, :count).by(-1)
it 'destroys error_template' do
expect { delete :destroy, params: {id: error_template} }.to change(ErrorTemplate, :count).by(-1)
expect(response).to redirect_to(error_templates_path)
end
end

View File

@ -1,14 +1,18 @@
# frozen_string_literal: true
require 'rails_helper'
describe EventsController do
let(:user) { FactoryBot.create(:admin) }
let(:exercise) {FactoryBot.create(:fibonacci)}
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
let(:exercise) { FactoryBot.create(:fibonacci) }
before { allow(controller).to receive(:current_user).and_return(user) }
describe 'POST #create' do
context 'with a valid event' do
let(:perform_request) { proc { post :create, params: { event: {category: 'foo', data: 'bar', exercise_id: exercise.id, file_id: exercise.files[0].id} } } }
before(:each) { perform_request.call }
let(:perform_request) { proc { post :create, params: {event: {category: 'foo', data: 'bar', exercise_id: exercise.id, file_id: exercise.files[0].id}} } }
before { perform_request.call }
expect_assigns(event: Event)
@ -20,13 +24,15 @@ describe EventsController do
end
context 'with an invalid event' do
before(:each) { post :create, params: { event: {exercise_id: 847482} } }
before { post :create, params: {event: {exercise_id: 847_482}} }
expect_assigns(event: Event)
expect_status(422)
end
context 'with no event' do
before(:each) { post :create }
before { post :create }
expect_status(422)
end
end

View File

@ -1,16 +1,20 @@
# frozen_string_literal: true
require 'rails_helper'
describe ExecutionEnvironmentsController do
let(:execution_environment) { FactoryBot.create(:ruby) }
let(:user) { FactoryBot.create(:admin) }
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
before { allow(controller).to receive(:current_user).and_return(user) }
describe 'POST #create' do
before(:each) { expect(DockerClient).to receive(:image_tags).at_least(:once).and_return([]) }
before { expect(DockerClient).to receive(:image_tags).at_least(:once).and_return([]) }
context 'with a valid execution environment' do
let(:perform_request) { proc { post :create, params: { execution_environment: FactoryBot.build(:ruby).attributes } } }
before(:each) { perform_request.call }
let(:perform_request) { proc { post :create, params: {execution_environment: FactoryBot.build(:ruby).attributes} } }
before { perform_request.call }
expect_assigns(docker_images: Array)
expect_assigns(execution_environment: ExecutionEnvironment)
@ -23,7 +27,7 @@ describe ExecutionEnvironmentsController do
end
context 'with an invalid execution environment' do
before(:each) { post :create, params: { execution_environment: {} } }
before { post :create, params: {execution_environment: {}} }
expect_assigns(execution_environment: ExecutionEnvironment)
expect_status(200)
@ -32,22 +36,22 @@ describe ExecutionEnvironmentsController do
end
describe 'DELETE #destroy' do
before(:each) { delete :destroy, params: { id: execution_environment.id } }
before { delete :destroy, params: {id: execution_environment.id} }
expect_assigns(execution_environment: :execution_environment)
it 'destroys the execution environment' do
execution_environment = FactoryBot.create(:ruby)
expect { delete :destroy, params: { id: execution_environment.id } }.to change(ExecutionEnvironment, :count).by(-1)
expect { delete :destroy, params: {id: execution_environment.id} }.to change(ExecutionEnvironment, :count).by(-1)
end
expect_redirect(:execution_environments)
end
describe 'GET #edit' do
before(:each) do
before do
expect(DockerClient).to receive(:image_tags).at_least(:once).and_return([])
get :edit, params: { id: execution_environment.id }
get :edit, params: {id: execution_environment.id}
end
expect_assigns(docker_images: Array)
@ -59,10 +63,10 @@ describe ExecutionEnvironmentsController do
describe 'POST #execute_command' do
let(:command) { 'which ruby' }
before(:each) do
before do
expect(DockerClient).to receive(:new).with(execution_environment: execution_environment).and_call_original
expect_any_instance_of(DockerClient).to receive(:execute_arbitrary_command).with(command)
post :execute_command, params: { command: command, id: execution_environment.id }
post :execute_command, params: {command: command, id: execution_environment.id}
end
expect_assigns(docker_client: DockerClient)
@ -73,7 +77,8 @@ describe ExecutionEnvironmentsController do
describe 'GET #index' do
before(:all) { FactoryBot.create_pair(:ruby) }
before(:each) { get :index }
before { get :index }
expect_assigns(execution_environments: ExecutionEnvironment.all)
expect_status(200)
@ -81,7 +86,7 @@ describe ExecutionEnvironmentsController do
end
describe 'GET #new' do
before(:each) do
before do
expect(DockerClient).to receive(:image_tags).at_least(:once).and_return([])
get :new
end
@ -96,7 +101,7 @@ describe ExecutionEnvironmentsController do
context 'when Docker is available' do
let(:docker_images) { [1, 2, 3] }
before(:each) do
before do
expect(DockerClient).to receive(:check_availability!).at_least(:once)
expect(DockerClient).to receive(:image_tags).and_return(docker_images)
controller.send(:set_docker_images)
@ -108,7 +113,7 @@ describe ExecutionEnvironmentsController do
context 'when Docker is unavailable' do
let(:error_message) { 'Docker is unavailable' }
before(:each) do
before do
expect(DockerClient).to receive(:check_availability!).at_least(:once).and_raise(DockerClient::Error.new(error_message))
controller.send(:set_docker_images)
end
@ -123,7 +128,7 @@ describe ExecutionEnvironmentsController do
end
describe 'GET #shell' do
before(:each) { get :shell, params: { id: execution_environment.id } }
before { get :shell, params: {id: execution_environment.id} }
expect_assigns(execution_environment: :execution_environment)
expect_status(200)
@ -131,7 +136,7 @@ describe ExecutionEnvironmentsController do
end
describe 'GET #statistics' do
before(:each) { get :statistics, params: { id: execution_environment.id } }
before { get :statistics, params: {id: execution_environment.id} }
expect_assigns(execution_environment: :execution_environment)
expect_status(200)
@ -139,7 +144,7 @@ describe ExecutionEnvironmentsController do
end
describe 'GET #show' do
before(:each) { get :show, params: { id: execution_environment.id } }
before { get :show, params: {id: execution_environment.id} }
expect_assigns(execution_environment: :execution_environment)
expect_status(200)
@ -148,9 +153,9 @@ describe ExecutionEnvironmentsController do
describe 'PUT #update' do
context 'with a valid execution environment' do
before(:each) do
before do
expect(DockerClient).to receive(:image_tags).at_least(:once).and_return([])
put :update, params: { execution_environment: FactoryBot.attributes_for(:ruby), id: execution_environment.id }
put :update, params: {execution_environment: FactoryBot.attributes_for(:ruby), id: execution_environment.id}
end
expect_assigns(docker_images: Array)
@ -159,7 +164,7 @@ describe ExecutionEnvironmentsController do
end
context 'with an invalid execution environment' do
before(:each) { put :update, params: { execution_environment: {name: ''}, id: execution_environment.id } }
before { put :update, params: {execution_environment: {name: ''}, id: execution_environment.id} }
expect_assigns(execution_environment: ExecutionEnvironment)
expect_status(200)

View File

@ -5,12 +5,14 @@ require 'rails_helper'
describe ExercisesController do
let(:exercise) { FactoryBot.create(:dummy) }
let(:user) { FactoryBot.create(:admin) }
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
before { allow(controller).to receive(:current_user).and_return(user) }
describe 'PUT #batch_update' do
let(:attributes) { { 'public': 'true'} }
let(:perform_request) { proc { put :batch_update, params: { exercises: {0 => attributes.merge(id: exercise.id)} } } }
before(:each) { perform_request.call }
let(:attributes) { {public: 'true'} }
let(:perform_request) { proc { put :batch_update, params: {exercises: {0 => attributes.merge(id: exercise.id)}} } }
before { perform_request.call }
it 'updates the exercises' do
expect_any_instance_of(Exercise).to receive(:update).with(attributes)
@ -22,10 +24,10 @@ describe ExercisesController do
end
describe 'POST #clone' do
let(:perform_request) { proc { post :clone, params: { id: exercise.id } } }
let(:perform_request) { proc { post :clone, params: {id: exercise.id} } }
context 'when saving succeeds' do
before(:each) { perform_request.call }
before { perform_request.call }
expect_assigns(exercise: Exercise)
@ -42,7 +44,7 @@ describe ExercisesController do
end
context 'when saving fails' do
before(:each) do
before do
expect_any_instance_of(Exercise).to receive(:save).and_return(false)
perform_request.call
end
@ -57,8 +59,9 @@ describe ExercisesController do
let(:exercise_attributes) { FactoryBot.build(:dummy).attributes }
context 'with a valid exercise' do
let(:perform_request) { proc { post :create, params: { exercise: exercise_attributes } } }
before(:each) { perform_request.call }
let(:perform_request) { proc { post :create, params: {exercise: exercise_attributes} } }
before { perform_request.call }
expect_assigns(exercise: Exercise)
@ -70,7 +73,7 @@ describe ExercisesController do
end
context 'when including a file' do
let(:perform_request) { proc { post :create, params: { exercise: exercise_attributes.merge(files_attributes: files_attributes) } } }
let(:perform_request) { proc { post :create, params: {exercise: exercise_attributes.merge(files_attributes: files_attributes)} } }
context 'when specifying the file content within the form' do
let(:files_attributes) { {'0' => FactoryBot.build(:file).attributes} }
@ -116,7 +119,7 @@ describe ExercisesController do
end
context 'with an invalid exercise' do
before(:each) { post :create, params: { exercise: { } } }
before { post :create, params: {exercise: {}} }
expect_assigns(exercise: Exercise)
expect_status(200)
@ -125,20 +128,20 @@ describe ExercisesController do
end
describe 'DELETE #destroy' do
before(:each) { delete :destroy, params: { id: exercise.id } }
before { delete :destroy, params: {id: exercise.id} }
expect_assigns(exercise: :exercise)
it 'destroys the exercise' do
exercise = FactoryBot.create(:dummy)
expect { delete :destroy, params: { id: exercise.id } }.to change(Exercise, :count).by(-1)
expect { delete :destroy, params: {id: exercise.id} }.to change(Exercise, :count).by(-1)
end
expect_redirect(:exercises)
end
describe 'GET #edit' do
before(:each) { get :edit, params: { id: exercise.id } }
before { get :edit, params: {id: exercise.id} }
expect_assigns(exercise: :exercise)
expect_status(200)
@ -146,11 +149,12 @@ describe ExercisesController do
end
describe 'GET #implement' do
let(:perform_request) { proc { get :implement, params: { id: exercise.id } } }
let(:perform_request) { proc { get :implement, params: {id: exercise.id} } }
context 'with an exercise with visible files' do
let(:exercise) { FactoryBot.create(:fibonacci) }
before(:each) { perform_request.call }
before { perform_request.call }
expect_assigns(exercise: :exercise)
@ -174,7 +178,7 @@ describe ExercisesController do
end
context 'with an exercise without visible files' do
before(:each) { perform_request.call }
before { perform_request.call }
expect_assigns(exercise: :exercise)
expect_flash_message(:alert, :'exercises.implement.no_files')
@ -184,8 +188,10 @@ describe ExercisesController do
describe 'GET #index' do
let(:scope) { Pundit.policy_scope!(user, Exercise) }
before(:all) { FactoryBot.create_pair(:dummy) }
before(:each) { get :index }
before { get :index }
expect_assigns(exercises: :scope)
expect_status(200)
@ -193,7 +199,7 @@ describe ExercisesController do
end
describe 'GET #new' do
before(:each) { get :new }
before { get :new }
expect_assigns(execution_environments: ExecutionEnvironment.all, exercise: Exercise)
expect_assigns(exercise: Exercise)
@ -203,7 +209,7 @@ describe ExercisesController do
describe 'GET #show' do
context 'as admin' do
before(:each) { get :show, params: { id: exercise.id } }
before { get :show, params: {id: exercise.id} }
expect_assigns(exercise: :exercise)
expect_status(200)
@ -213,7 +219,7 @@ describe ExercisesController do
describe 'GET #reload' do
context 'as anyone' do
before(:each) { get :reload, format: :json, params: { id: exercise.id } }
before { get :reload, format: :json, params: {id: exercise.id} }
expect_assigns(exercise: :exercise)
expect_status(200)
@ -222,7 +228,7 @@ describe ExercisesController do
end
describe 'GET #statistics' do
before(:each) { get :statistics, params: { id: exercise.id } }
before { get :statistics, params: {id: exercise.id} }
expect_assigns(exercise: :exercise)
expect_status(200)
@ -231,23 +237,23 @@ describe ExercisesController do
describe 'POST #submit' do
let(:output) { {} }
let(:perform_request) { post :submit, format: :json, params: { id: exercise.id, submission: {cause: 'submit', exercise_id: exercise.id} } }
let(:perform_request) { post :submit, format: :json, params: {id: exercise.id, submission: {cause: 'submit', exercise_id: exercise.id}} }
let(:user) { FactoryBot.create(:external_user) }
let!(:lti_parameter) { FactoryBot.create(:lti_parameter, external_user: user, exercise: exercise) }
before(:each) do
before do
allow_any_instance_of(Submission).to receive(:normalized_score).and_return(1)
expect(controller).to receive(:collect_test_results).and_return([{score: 1, weight: 1}])
expect(controller).to receive(:score_submission).and_call_original
end
context 'when LTI outcomes are supported' do
before(:each) do
before do
expect(controller).to receive(:lti_outcome_service?).and_return(true)
end
context 'when the score transmission succeeds' do
before(:each) do
before do
expect(controller).to receive(:send_score).and_return(status: 'success')
perform_request
end
@ -263,7 +269,7 @@ describe ExercisesController do
end
context 'when the score transmission fails' do
before(:each) do
before do
expect(controller).to receive(:send_score).and_return(status: 'unsupported')
perform_request
end
@ -280,7 +286,7 @@ describe ExercisesController do
end
context 'when LTI outcomes are not supported' do
before(:each) do
before do
expect(controller).to receive(:lti_outcome_service?).and_return(false)
expect(controller).not_to receive(:send_score)
perform_request
@ -300,14 +306,15 @@ describe ExercisesController do
describe 'PUT #update' do
context 'with a valid exercise' do
let(:exercise_attributes) { FactoryBot.build(:dummy).attributes }
before(:each) { put :update, params: { exercise: exercise_attributes, id: exercise.id } }
before { put :update, params: {exercise: exercise_attributes, id: exercise.id} }
expect_assigns(exercise: Exercise)
expect_redirect(:exercise)
end
context 'with an invalid exercise' do
before(:each) { put :update, params: { exercise: {title: ''}, id: exercise.id } }
before { put :update, params: {exercise: {title: ''}, id: exercise.id} }
expect_assigns(exercise: Exercise)
expect_status(200)
@ -321,7 +328,7 @@ describe ExercisesController do
describe 'POST #export_external_check' do
render_views
let(:post_request) { post :export_external_check, params: { id: exercise.id } }
let(:post_request) { post :export_external_check, params: {id: exercise.id} }
let!(:codeharbor_link) { FactoryBot.create(:codeharbor_link, user: user) }
let(:external_check_hash) { {message: message, exercise_found: true, update_right: update_right, error: error} }
let(:message) { 'message' }
@ -509,7 +516,7 @@ describe ExercisesController do
end
context 'when the imported exercise is invalid' do
before { allow(ProformaService::Import).to receive(:call) { imported_exercise.tap { |e| e.files = [] }.tap { |e| e.title = nil } } }
before { allow(ProformaService::Import).to receive(:call) { imported_exercise.tap {|e| e.files = [] }.tap {|e| e.title = nil } } }
it 'responds with correct status code' do
expect { post_request }.not_to(change { imported_exercise.reload.files.count })

View File

@ -1,12 +1,15 @@
# frozen_string_literal: true
require 'rails_helper'
describe ExternalUsersController do
let(:user) { FactoryBot.build(:admin) }
let!(:users) { FactoryBot.create_pair(:external_user) }
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
before { allow(controller).to receive(:current_user).and_return(user) }
describe 'GET #index' do
before(:each) { get :index }
before { get :index }
expect_assigns(users: ExternalUser.all)
expect_status(200)
@ -14,7 +17,7 @@ describe ExternalUsersController do
end
describe 'GET #show' do
before(:each) { get :show, params: { id: users.first.id } }
before { get :show, params: {id: users.first.id} }
expect_assigns(user: ExternalUser)
expect_status(200)

View File

@ -1,14 +1,18 @@
# frozen_string_literal: true
require 'rails_helper'
describe FileTypesController do
let(:file_type) { FactoryBot.create(:dot_rb) }
let(:user) { FactoryBot.create(:admin) }
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
before { allow(controller).to receive(:current_user).and_return(user) }
describe 'POST #create' do
context 'with a valid file type' do
let(:perform_request) { proc { post :create, params: { file_type: FactoryBot.attributes_for(:dot_rb) } } }
before(:each) { perform_request.call }
let(:perform_request) { proc { post :create, params: {file_type: FactoryBot.attributes_for(:dot_rb)} } }
before { perform_request.call }
expect_assigns(editor_modes: Array)
expect_assigns(file_type: FileType)
@ -21,7 +25,7 @@ describe FileTypesController do
end
context 'with an invalid file type' do
before(:each) { post :create, params: { file_type: { } } }
before { post :create, params: {file_type: {}} }
expect_assigns(editor_modes: Array)
expect_assigns(file_type: FileType)
@ -31,20 +35,20 @@ describe FileTypesController do
end
describe 'DELETE #destroy' do
before(:each) { delete :destroy, params: { id: file_type.id } }
before { delete :destroy, params: {id: file_type.id} }
expect_assigns(file_type: FileType)
it 'destroys the file type' do
file_type = FactoryBot.create(:dot_rb)
expect { delete :destroy, params: { id: file_type.id } }.to change(FileType, :count).by(-1)
expect { delete :destroy, params: {id: file_type.id} }.to change(FileType, :count).by(-1)
end
expect_redirect(:file_types)
end
describe 'GET #edit' do
before(:each) { get :edit, params: { id: file_type.id } }
before { get :edit, params: {id: file_type.id} }
expect_assigns(editor_modes: Array)
expect_assigns(file_type: FileType)
@ -54,7 +58,8 @@ describe FileTypesController do
describe 'GET #index' do
before(:all) { FactoryBot.create_pair(:dot_rb) }
before(:each) { get :index }
before { get :index }
expect_assigns(file_types: FileType.all)
expect_status(200)
@ -62,7 +67,7 @@ describe FileTypesController do
end
describe 'GET #new' do
before(:each) { get :new }
before { get :new }
expect_assigns(editor_modes: Array)
expect_assigns(file_type: FileType)
@ -71,7 +76,7 @@ describe FileTypesController do
end
describe 'GET #show' do
before(:each) { get :show, params: { id: file_type.id } }
before { get :show, params: {id: file_type.id} }
expect_assigns(file_type: :file_type)
expect_status(200)
@ -80,7 +85,7 @@ describe FileTypesController do
describe 'PUT #update' do
context 'with a valid file type' do
before(:each) { put :update, params: { file_type: FactoryBot.attributes_for(:dot_rb), id: file_type.id } }
before { put :update, params: {file_type: FactoryBot.attributes_for(:dot_rb), id: file_type.id} }
expect_assigns(editor_modes: Array)
expect_assigns(file_type: FileType)
@ -88,7 +93,7 @@ describe FileTypesController do
end
context 'with an invalid file type' do
before(:each) { put :update, params: { file_type: {name: ''}, id: file_type.id } }
before { put :update, params: {file_type: {name: ''}, id: file_type.id} }
expect_assigns(editor_modes: Array)
expect_assigns(file_type: FileType)

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe InternalUsersController do
@ -7,28 +9,28 @@ describe InternalUsersController do
describe 'GET #activate' do
let(:user) { InternalUser.create(FactoryBot.attributes_for(:teacher)) }
before(:each) do
before do
user.send(:setup_activation)
user.save(validate: false)
end
context 'without a valid activation token' do
before(:each) { get :activate, params: { id: user.id } }
before { get :activate, params: {id: user.id} }
expect_redirect(:root)
end
context 'with an already activated user' do
before(:each) do
before do
user.activate!
get :activate, params: { id: user.id, token: user.activation_token }
get :activate, params: {id: user.id, token: user.activation_token}
end
expect_redirect(:root)
end
context 'with valid preconditions' do
before(:each) { get :activate, params: { id: user.id, token: user.activation_token } }
before { get :activate, params: {id: user.id, token: user.activation_token} }
expect_assigns(user: InternalUser)
expect_status(200)
@ -40,29 +42,29 @@ describe InternalUsersController do
let(:user) { InternalUser.create(FactoryBot.build(:teacher).attributes) }
let(:password) { SecureRandom.hex }
before(:each) do
before do
user.send(:setup_activation)
user.save(validate: false)
expect(user.activation_token).to be_present
end
context 'without a valid activation token' do
before(:each) { put :activate, params: { id: user.id } }
before { put :activate, params: {id: user.id} }
expect_redirect(:root)
end
context 'with an already activated user' do
before(:each) do
before do
user.activate!
put :activate, params: { id: user.id, internal_user: {activation_token: user.activation_token, password: password, password_confirmation: password} }
put :activate, params: {id: user.id, internal_user: {activation_token: user.activation_token, password: password, password_confirmation: password}}
end
expect_redirect(:root)
end
context 'without a password' do
before(:each) { put :activate, params: { id: user.id, internal_user: {activation_token: user.activation_token} } }
before { put :activate, params: {id: user.id, internal_user: {activation_token: user.activation_token}} }
expect_assigns(user: InternalUser)
@ -74,7 +76,7 @@ describe InternalUsersController do
end
context 'without a valid password confirmation' do
before(:each) { put :activate, params: { id: user.id, internal_user: {activation_token: user.activation_token, password: password, password_confirmation: ''} } }
before { put :activate, params: {id: user.id, internal_user: {activation_token: user.activation_token, password: password, password_confirmation: ''}} }
expect_assigns(user: InternalUser)
@ -86,7 +88,7 @@ describe InternalUsersController do
end
context 'with valid preconditions' do
before(:each) { put :activate, params: { id: user.id, internal_user: {activation_token: user.activation_token, password: password, password_confirmation: password} } }
before { put :activate, params: {id: user.id, internal_user: {activation_token: user.activation_token, password: password, password_confirmation: password}} }
expect_assigns(user: InternalUser)
@ -100,11 +102,12 @@ describe InternalUsersController do
end
describe 'POST #create' do
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
before { allow(controller).to receive(:current_user).and_return(user) }
context 'with a valid internal user' do
let(:perform_request) { proc { post :create, params: { internal_user: FactoryBot.build(:teacher).attributes } } }
before(:each) { perform_request.call }
let(:perform_request) { proc { post :create, params: {internal_user: FactoryBot.build(:teacher).attributes} } }
before { perform_request.call }
expect_assigns(user: InternalUser)
@ -129,7 +132,7 @@ describe InternalUsersController do
end
context 'with an invalid internal user' do
before(:each) { post :create, params: { internal_user: {} } }
before { post :create, params: {internal_user: {}} }
expect_assigns(user: InternalUser)
expect_status(200)
@ -138,24 +141,24 @@ describe InternalUsersController do
end
describe 'DELETE #destroy' do
before(:each) do
before do
allow(controller).to receive(:current_user).and_return(user)
delete :destroy, params: { id: users.first.id }
delete :destroy, params: {id: users.first.id}
end
expect_assigns(user: InternalUser)
it 'destroys the internal user' do
expect { delete :destroy, params: { id: InternalUser.last.id } }.to change(InternalUser, :count).by(-1)
expect { delete :destroy, params: {id: InternalUser.last.id} }.to change(InternalUser, :count).by(-1)
end
expect_redirect(:internal_users)
end
describe 'GET #edit' do
before(:each) do
before do
allow(controller).to receive(:current_user).and_return(user)
get :edit, params: { id: users.first.id }
get :edit, params: {id: users.first.id}
end
expect_assigns(user: InternalUser)
@ -165,7 +168,7 @@ describe InternalUsersController do
describe 'GET #forgot_password' do
context 'when no user is logged in' do
before(:each) do
before do
allow(controller).to receive(:set_sentry_context).and_return(nil)
expect(controller).to receive(:current_user).and_return(nil)
@ -177,7 +180,7 @@ describe InternalUsersController do
end
context 'when a user is already logged in' do
before(:each) do
before do
allow(controller).to receive(:set_sentry_context).and_return(nil)
expect(controller).to receive(:current_user).and_return(user)
@ -191,8 +194,9 @@ describe InternalUsersController do
describe 'POST #forgot_password' do
context 'with an email address' do
let(:perform_request) { proc { post :forgot_password, params: { email: user.email } } }
before(:each) { perform_request.call }
let(:perform_request) { proc { post :forgot_password, params: {email: user.email} } }
before { perform_request.call }
it 'delivers instructions to reset the password' do
expect(InternalUser).to receive(:where).and_return([user])
@ -204,7 +208,7 @@ describe InternalUsersController do
end
context 'without an email address' do
before(:each) { post :forgot_password }
before { post :forgot_password }
expect_status(200)
expect_template(:forgot_password)
@ -212,7 +216,7 @@ describe InternalUsersController do
end
describe 'GET #index' do
before(:each) do
before do
allow(controller).to receive(:current_user).and_return(user)
get :index
end
@ -223,7 +227,7 @@ describe InternalUsersController do
end
describe 'GET #new' do
before(:each) do
before do
allow(controller).to receive(:current_user).and_return(user)
get :new
end
@ -237,15 +241,15 @@ describe InternalUsersController do
let(:user) { users.first }
context 'without a valid password reset token' do
before(:each) { get :reset_password, params: { id: user.id } }
before { get :reset_password, params: {id: user.id} }
expect_redirect(:root)
end
context 'with a valid password reset token' do
before(:each) do
before do
user.deliver_reset_password_instructions!
get :reset_password, params: { id: user.id, token: user.reset_password_token }
get :reset_password, params: {id: user.id, token: user.reset_password_token}
end
expect_assigns(user: :user)
@ -256,10 +260,11 @@ describe InternalUsersController do
describe 'PUT #reset_password' do
let(:user) { users.first }
before(:each) { user.deliver_reset_password_instructions! }
before { user.deliver_reset_password_instructions! }
context 'without a valid password reset token' do
before(:each) { put :reset_password, params: { id: user.id } }
before { put :reset_password, params: {id: user.id} }
expect_redirect(:root)
end
@ -268,8 +273,9 @@ describe InternalUsersController do
let(:password) { 'foo' }
context 'with a matching password confirmation' do
let(:perform_request) { proc { put :reset_password, params: { internal_user: {password: password, password_confirmation: password}, id: user.id, token: user.reset_password_token } } }
before(:each) { perform_request.call }
let(:perform_request) { proc { put :reset_password, params: {internal_user: {password: password, password_confirmation: password}, id: user.id, token: user.reset_password_token} } }
before { perform_request.call }
expect_assigns(user: :user)
@ -281,8 +287,8 @@ describe InternalUsersController do
end
context 'without a matching password confirmation' do
before(:each) do
put :reset_password, params: { internal_user: {password: password, password_confirmation: ''}, id: users.first.id, token: user.reset_password_token }
before do
put :reset_password, params: {internal_user: {password: password, password_confirmation: ''}, id: users.first.id, token: user.reset_password_token}
end
expect_assigns(user: :user)
@ -293,9 +299,9 @@ describe InternalUsersController do
end
describe 'GET #show' do
before(:each) do
before do
allow(controller).to receive(:current_user).and_return(user)
get :show, params: { id: users.first.id }
get :show, params: {id: users.first.id}
end
expect_assigns(user: InternalUser)
@ -304,17 +310,17 @@ describe InternalUsersController do
end
describe 'PUT #update' do
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
before { allow(controller).to receive(:current_user).and_return(user) }
context 'with a valid internal user' do
before(:each) { put :update, params: { internal_user: FactoryBot.attributes_for(:teacher), id: users.first.id } }
before { put :update, params: {internal_user: FactoryBot.attributes_for(:teacher), id: users.first.id} }
expect_assigns(user: InternalUser)
expect_redirect { user }
end
context 'with an invalid internal user' do
before(:each) { put :update, params: { internal_user: {email: ''}, id: users.first.id } }
before { put :update, params: {internal_user: {email: ''}, id: users.first.id} }
expect_assigns(user: InternalUser)
expect_status(200)

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe RequestForCommentsController do
@ -24,7 +26,7 @@ describe RequestForCommentsController do
rfc_other_study_group.user.update(study_groups: [another_study_group])
rfc_other_study_group.submission.update(study_group: another_study_group)
get :index, params: { "q[submission_study_group_id_in][]": my_study_group.id }
get :index, params: {"q[submission_study_group_id_in][]": my_study_group.id}
expect(assigns(:request_for_comments)).to eq([rfc_within_my_study_group])
end
@ -47,7 +49,7 @@ describe RequestForCommentsController do
describe 'GET #get_rfcs_for_exercise' do
before do
exercise = FactoryBot.create(:even_odd)
get :get_rfcs_for_exercise, params: { exercise_id: exercise.id }
get :get_rfcs_for_exercise, params: {exercise_id: exercise.id}
end
expect_status(200)

View File

@ -11,9 +11,9 @@ describe SessionsController do
let(:user_attributes) { FactoryBot.build(:teacher).attributes }
context 'with valid credentials' do
before(:each) do
before do
user.activate!
post :create, params: { email: user.email, password: password, remember_me: 1 }
post :create, params: {email: user.email, password: password, remember_me: 1}
end
expect_flash_message(:notice, :'sessions.create.success')
@ -21,7 +21,7 @@ describe SessionsController do
end
context 'with invalid credentials' do
before(:each) { post :create, params: { email: user.email, password: '', remember_me: 1 } }
before { post :create, params: {email: user.email, password: '', remember_me: 1} }
expect_flash_message(:danger, :'sessions.create.failure')
expect_template(:new)
@ -32,7 +32,8 @@ describe SessionsController do
let(:exercise) { FactoryBot.create(:dummy) }
let(:exercise2) { FactoryBot.create(:dummy) }
let(:nonce) { SecureRandom.hex }
before(:each) { I18n.locale = I18n.default_locale }
before { I18n.locale = I18n.default_locale }
context 'without OAuth parameters' do
it 'refuses the LTI launch' do
@ -44,14 +45,14 @@ describe SessionsController do
context 'without a valid consumer key' do
it 'refuses the LTI launch' do
expect(controller).to receive(:refuse_lti_launch).with(message: I18n.t('sessions.oauth.invalid_consumer')).and_call_original
post :create_through_lti, params: { oauth_consumer_key: SecureRandom.hex, oauth_signature: SecureRandom.hex }
post :create_through_lti, params: {oauth_consumer_key: SecureRandom.hex, oauth_signature: SecureRandom.hex}
end
end
context 'with an invalid OAuth signature' do
it 'refuses the LTI launch' do
expect(controller).to receive(:refuse_lti_launch).with(message: I18n.t('sessions.oauth.invalid_signature')).and_call_original
post :create_through_lti, params: { oauth_consumer_key: consumer.oauth_key, oauth_signature: SecureRandom.hex }
post :create_through_lti, params: {oauth_consumer_key: consumer.oauth_key, oauth_signature: SecureRandom.hex}
end
end
@ -60,7 +61,7 @@ describe SessionsController do
expect_any_instance_of(IMS::LTI::ToolProvider).to receive(:valid_request?).and_return(true)
expect(NonceStore).to receive(:has?).with(nonce).and_return(true)
expect(controller).to receive(:refuse_lti_launch).with(message: I18n.t('sessions.oauth.used_nonce')).and_call_original
post :create_through_lti, params: { oauth_consumer_key: consumer.oauth_key, oauth_nonce: nonce, oauth_signature: SecureRandom.hex }
post :create_through_lti, params: {oauth_consumer_key: consumer.oauth_key, oauth_nonce: nonce, oauth_signature: SecureRandom.hex}
end
end
@ -68,15 +69,16 @@ describe SessionsController do
it 'refuses the LTI launch' do
expect_any_instance_of(IMS::LTI::ToolProvider).to receive(:valid_request?).and_return(true)
expect(controller).to receive(:refuse_lti_launch).with(message: I18n.t('sessions.oauth.invalid_exercise_token')).and_call_original
post :create_through_lti, params: { custom_token: '', oauth_consumer_key: consumer.oauth_key, oauth_nonce: nonce, oauth_signature: SecureRandom.hex, user_id: '123' }
post :create_through_lti, params: {custom_token: '', oauth_consumer_key: consumer.oauth_key, oauth_nonce: nonce, oauth_signature: SecureRandom.hex, user_id: '123'}
end
end
context 'with valid launch parameters' do
let(:locale) { :de }
let(:perform_request) { post :create_through_lti, params: { custom_locale: locale, custom_token: exercise.token, oauth_consumer_key: consumer.oauth_key, oauth_nonce: nonce, oauth_signature: SecureRandom.hex, user_id: user.external_id } }
let(:perform_request) { post :create_through_lti, params: {custom_locale: locale, custom_token: exercise.token, oauth_consumer_key: consumer.oauth_key, oauth_nonce: nonce, oauth_signature: SecureRandom.hex, user_id: user.external_id} }
let(:user) { FactoryBot.create(:external_user, consumer_id: consumer.id) }
before(:each) { expect_any_instance_of(IMS::LTI::ToolProvider).to receive(:valid_request?).and_return(true) }
before { expect_any_instance_of(IMS::LTI::ToolProvider).to receive(:valid_request?).and_return(true) }
it 'assigns the current user' do
perform_request
@ -96,7 +98,7 @@ describe SessionsController do
end
it 'stores LTI parameters in the session' do
#Todo replace session with lti_parameter /should be done already
# Todo replace session with lti_parameter /should be done already
expect(controller).to receive(:store_lti_session_data)
perform_request
end
@ -109,7 +111,7 @@ describe SessionsController do
context 'when LTI outcomes are supported' do
let(:message) { I18n.t('sessions.create_through_lti.session_with_outcome', consumer: consumer) }
before(:each) do
before do
expect(controller).to receive(:lti_outcome_service?).and_return(true)
perform_request
end
@ -120,7 +122,7 @@ describe SessionsController do
context 'when LTI outcomes are not supported' do
let(:message) { I18n.t('sessions.create_through_lti.session_without_outcome', consumer: consumer) }
before(:each) do
before do
expect(controller).to receive(:lti_outcome_service?).and_return(false)
perform_request
end
@ -135,7 +137,7 @@ describe SessionsController do
it 'redirects to recommended exercise if requested token of proxy exercise' do
FactoryBot.create(:proxy_exercise, exercises: [exercise])
post :create_through_lti, params: { custom_locale: locale, custom_token: ProxyExercise.first.token, oauth_consumer_key: consumer.oauth_key, oauth_nonce: nonce, oauth_signature: SecureRandom.hex, user_id: user.external_id }
post :create_through_lti, params: {custom_locale: locale, custom_token: ProxyExercise.first.token, oauth_consumer_key: consumer.oauth_key, oauth_nonce: nonce, oauth_signature: SecureRandom.hex, user_id: user.external_id}
expect(controller).to redirect_to(implement_exercise_path(exercise.id))
end
@ -146,7 +148,7 @@ describe SessionsController do
exercise.save
exercise2.expected_difficulty = 1
exercise2.save
post :create_through_lti, params: { custom_locale: locale, custom_token: ProxyExercise.first.token, oauth_consumer_key: consumer.oauth_key, oauth_nonce: nonce, oauth_signature: SecureRandom.hex, user_id: user.external_id }
post :create_through_lti, params: {custom_locale: locale, custom_token: ProxyExercise.first.token, oauth_consumer_key: consumer.oauth_key, oauth_nonce: nonce, oauth_signature: SecureRandom.hex, user_id: user.external_id}
expect(controller).to redirect_to(implement_exercise_path(exercise2.id))
end
end
@ -154,13 +156,14 @@ describe SessionsController do
describe 'DELETE #destroy' do
let(:user) { double }
before(:each) {
before do
allow(controller).to receive(:set_sentry_context).and_return(nil)
expect(controller).to receive(:current_user).at_least(:once).and_return(user)
}
end
context 'with an internal user' do
before(:each) do
before do
allow(user).to receive(:external_user?).and_return(false)
allow(user).to receive(:forget_me!)
delete :destroy
@ -177,13 +180,13 @@ describe SessionsController do
end
context 'with an external user' do
before(:each) do
before do
allow(user).to receive(:external_user?).and_return(true)
delete :destroy
end
it 'clears the session' do
#Todo replace session with lti_parameter /should be done already
# Todo replace session with lti_parameter /should be done already
expect(controller).to receive(:clear_lti_session_data)
delete :destroy
end
@ -195,19 +198,11 @@ describe SessionsController do
end
describe 'GET #destroy_through_lti' do
let(:perform_request) { proc { get :destroy_through_lti, params: { consumer_id: consumer.id, submission_id: submission.id } } }
let(:submission) { FactoryBot.create(:submission, exercise: FactoryBot.create(:dummy)) }
before(:each) do
#Todo replace session with lti_parameter
#Todo create LtiParameter Object
# session[:lti_parameters] = {}
end
before(:each) { perform_request.call }
let(:perform_request) { proc { get :destroy_through_lti, params: {consumer_id: consumer.id, submission_id: submission.id} } }
let(:submission) { FactoryBot.create(:submission, exercise: FactoryBot.create(:dummy)) } before { perform_request.call }
it 'clears the session' do
#Todo replace session with lti_parameter /should be done already
# Todo replace session with lti_parameter /should be done already
expect(controller).to receive(:clear_lti_session_data)
perform_request.call
end
@ -218,7 +213,7 @@ describe SessionsController do
describe 'GET #new' do
context 'when no user is logged in' do
before(:each) do
before do
allow(controller).to receive(:set_sentry_context).and_return(nil)
expect(controller).to receive(:current_user).and_return(nil)
@ -230,7 +225,7 @@ describe SessionsController do
end
context 'when a user is already logged in' do
before(:each) do
before do
allow(controller).to receive(:set_sentry_context).and_return(nil)
expect(controller).to receive(:current_user).and_return(FactoryBot.build(:teacher))

View File

@ -1,34 +1,36 @@
# frozen_string_literal: true
require 'rails_helper'
describe StatisticsController do
let(:user) { FactoryBot.create(:admin) }
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
[:show, :graphs].each do |route|
before { allow(controller).to receive(:current_user).and_return(user) }
%i[show graphs].each do |route|
describe "GET ##{route}" do
before(:each) { get route }
before { get route }
expect_status(200)
expect_template(route)
end
end
[:user_activity_history, :rfc_activity_history].each do |route|
%i[user_activity_history rfc_activity_history].each do |route|
describe "GET ##{route}" do
before(:each) { get route }
before { get route }
expect_status(200)
expect_template(:activity_history)
end
end
[:show, :user_activity, :user_activity_history, :rfc_activity, :rfc_activity_history].each do |route|
%i[show user_activity user_activity_history rfc_activity rfc_activity_history].each do |route|
describe "GET ##{route}.json" do
before(:each) { get route, format: :json }
before { get route, format: :json }
expect_status(200)
expect_json
end
end
end

View File

@ -1,19 +1,23 @@
# frozen_string_literal: true
require 'rails_helper'
describe SubmissionsController do
let(:submission) { FactoryBot.create(:submission) }
let(:user) { FactoryBot.create(:admin) }
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
before { allow(controller).to receive(:current_user).and_return(user) }
describe 'POST #create' do
before(:each) do
before do
controller.request.accept = 'application/json'
end
context 'with a valid submission' do
let(:exercise) { FactoryBot.create(:hello_world) }
let(:perform_request) { proc { post :create, format: :json, params: { submission: FactoryBot.attributes_for(:submission, exercise_id: exercise.id) } } }
before(:each) { perform_request.call }
let(:perform_request) { proc { post :create, format: :json, params: {submission: FactoryBot.attributes_for(:submission, exercise_id: exercise.id)} } }
before { perform_request.call }
expect_assigns(submission: Submission)
@ -26,7 +30,7 @@ describe SubmissionsController do
end
context 'with an invalid submission' do
before(:each) { post :create, params: { submission: { } } }
before { post :create, params: {submission: {}} }
expect_assigns(submission: Submission)
expect_json
@ -36,17 +40,18 @@ describe SubmissionsController do
describe 'GET #download_file' do
context 'with an invalid filename' do
before(:each) { get :download_file, params: { filename: SecureRandom.hex, id: submission.id } }
before { get :download_file, params: {filename: SecureRandom.hex, id: submission.id} }
expect_status(404)
end
context 'with a valid binary filename' do
let(:submission) { FactoryBot.create(:submission, exercise: FactoryBot.create(:sql_select)) }
before(:each) { get :download_file, params: { filename: file.name_with_extension, id: submission.id } }
before { get :download_file, params: {filename: file.name_with_extension, id: submission.id} }
context 'for a binary file' do
let(:file) { submission.collect_files.detect { |file| file.name == 'exercise' && file.file_type.file_extension == '.sql' } }
let(:file) { submission.collect_files.detect {|file| file.name == 'exercise' && file.file_type.file_extension == '.sql' } }
expect_assigns(file: :file)
expect_assigns(submission: :submission)
@ -61,10 +66,11 @@ describe SubmissionsController do
context 'with a valid filename' do
let(:submission) { FactoryBot.create(:submission, exercise: FactoryBot.create(:audio_video)) }
before(:each) { get :download_file, params: { filename: file.name_with_extension, id: submission.id } }
before { get :download_file, params: {filename: file.name_with_extension, id: submission.id} }
context 'for a binary file' do
let(:file) { submission.collect_files.detect { |file| file.file_type.file_extension == '.mp4' } }
let(:file) { submission.collect_files.detect {|file| file.file_type.file_extension == '.mp4' } }
expect_assigns(file: :file)
expect_assigns(submission: :submission)
@ -77,7 +83,7 @@ describe SubmissionsController do
end
context 'for a non-binary file' do
let(:file) { submission.collect_files.detect { |file| file.file_type.file_extension == '.js' } }
let(:file) { submission.collect_files.detect {|file| file.file_type.file_extension == '.js' } }
expect_assigns(file: :file)
expect_assigns(submission: :submission)
@ -93,7 +99,8 @@ describe SubmissionsController do
describe 'GET #index' do
before(:all) { FactoryBot.create_pair(:submission) }
before(:each) { get :index }
before { get :index }
expect_assigns(submissions: Submission.all)
expect_status(200)
@ -104,17 +111,18 @@ describe SubmissionsController do
let(:file) { submission.files.first }
context 'with an invalid filename' do
before(:each) { get :render_file, params: { filename: SecureRandom.hex, id: submission.id } }
before { get :render_file, params: {filename: SecureRandom.hex, id: submission.id} }
expect_status(404)
end
context 'with a valid filename' do
let(:submission) { FactoryBot.create(:submission, exercise: FactoryBot.create(:audio_video)) }
before(:each) { get :render_file, params: { filename: file.name_with_extension, id: submission.id } }
before { get :render_file, params: {filename: file.name_with_extension, id: submission.id} }
context 'for a binary file' do
let(:file) { submission.collect_files.detect { |file| file.file_type.file_extension == '.mp4' } }
let(:file) { submission.collect_files.detect {|file| file.file_type.file_extension == '.mp4' } }
expect_assigns(file: :file)
expect_assigns(submission: :submission)
@ -127,7 +135,7 @@ describe SubmissionsController do
end
context 'for a non-binary file' do
let(:file) { submission.collect_files.detect { |file| file.file_type.file_extension == '.js' } }
let(:file) { submission.collect_files.detect {|file| file.file_type.file_extension == '.js' } }
expect_assigns(file: :file)
expect_assigns(submission: :submission)
@ -143,24 +151,24 @@ describe SubmissionsController do
describe 'GET #run' do
let(:filename) { submission.collect_files.detect(&:main_file?).name_with_extension }
let(:perform_request) { get :run, params: { filename: filename , id: submission.id } }
let(:perform_request) { get :run, params: {filename: filename, id: submission.id} }
before(:each) do
before do
expect_any_instance_of(ActionController::Live::SSE).to receive(:write).at_least(3).times
end
context 'when no errors occur during execution' do
before(:each) do
before do
expect_any_instance_of(DockerClient).to receive(:execute_run_command).with(submission, filename).and_return({})
perform_request
end
pending("todo")
pending('todo')
end
end
describe 'GET #show' do
before(:each) { get :show, params: { id: submission.id } }
before { get :show, params: {id: submission.id} }
expect_assigns(submission: :submission)
expect_status(200)
@ -172,11 +180,12 @@ describe SubmissionsController do
# https://github.com/rails/jbuilder/issues/32
render_views
before(:each) { get :show, params: { id: submission.id }, format: :json }
before { get :show, params: {id: submission.id}, format: :json }
expect_assigns(submission: :submission)
expect_status(200)
[:render, :run, :test].each do |action|
%i[render run test].each do |action|
describe "##{action}_url" do
let(:url) { JSON.parse(response.body).with_indifferent_access.fetch("#{action}_url") }
@ -186,45 +195,47 @@ describe SubmissionsController do
end
it 'ends with a placeholder' do
expect(url).to end_with(Submission::FILENAME_URL_PLACEHOLDER + '.json')
expect(url).to end_with("#{Submission::FILENAME_URL_PLACEHOLDER}.json")
end
end
end
describe "#score_url" do
let(:url) { JSON.parse(response.body).with_indifferent_access.fetch("score_url") }
describe '#score_url' do
let(:url) { JSON.parse(response.body).with_indifferent_access.fetch('score_url') }
it "corresponds to the score path" do
it 'corresponds to the score path' do
expect(url).to eq(Rails.application.routes.url_helpers.score_submission_path(submission, format: :json))
end
end
end
describe 'GET #score' do
let(:perform_request) { proc { get :score, params: { id: submission.id } } }
before(:each) { perform_request.call }
let(:perform_request) { proc { get :score, params: {id: submission.id} } }
pending("todo: mock puma webserver or encapsulate tubesock call (Tubesock::HijackNotAvailable)")
before { perform_request.call }
pending('todo: mock puma webserver or encapsulate tubesock call (Tubesock::HijackNotAvailable)')
end
describe 'GET #test' do
let(:filename) { submission.collect_files.detect(&:teacher_defined_assessment?).name_with_extension }
let(:output) { {} }
before(:each) do
before do
expect_any_instance_of(DockerClient).to receive(:execute_test_command).with(submission, filename)
get :test, filename: filename, id: submission.id
get :test, params: {filename: filename, id: submission.id}
end
pending("todo")
pending('todo')
end
describe '#with_server_sent_events' do
let(:response) { ActionDispatch::TestResponse.new }
before(:each) { allow(controller).to receive(:response).and_return(response) }
before { allow(controller).to receive(:response).and_return(response) }
context 'when no error occurs' do
after(:each) { controller.send(:with_server_sent_events) }
after { controller.send(:with_server_sent_events) }
it 'uses server-sent events' do
expect(ActionController::Live::SSE).to receive(:new).and_call_original
@ -246,7 +257,7 @@ describe SubmissionsController do
end
context 'when an error occurs' do
after(:each) { controller.send(:with_server_sent_events) { fail } }
after { controller.send(:with_server_sent_events) { raise } }
it 'uses server-sent events' do
expect(ActionController::Live::SSE).to receive(:new).and_call_original

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'seeds_helper'
module CodeOcean

View File

@ -1,8 +1,10 @@
# frozen_string_literal: true
FactoryBot.define do
factory :codeharbor_link do
user { build(:teacher) }
push_url { 'http://push.url' }
check_uuid_url { 'http://check-uuid.url' }
sequence(:api_key) { |n| "api_key#{n}" }
sequence(:api_key) {|n| "api_key#{n}" }
end
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
FactoryBot.define do
factory :consumer do
name { 'openHPI' }

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true
FactoryBot.define do
factory :error_template_attribute do
key { "MyString" }
regex { "MyString" }
key { 'MyString' }
regex { 'MyString' }
end
end

View File

@ -1,7 +1,9 @@
# frozen_string_literal: true
FactoryBot.define do
factory :error_template do
association :execution_environment, factory: :ruby
name { "MyString" }
signature { "MyString" }
name { 'MyString' }
signature { 'MyString' }
end
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
FactoryBot.define do
factory :coffee_script, class: 'ExecutionEnvironment' do
created_by_teacher

View File

@ -4,13 +4,13 @@ require 'seeds_helper'
def create_seed_file(exercise, path, file_attributes = {})
file_extension = File.extname(path)
file_type = FactoryBot.create(file_attributes[:file_type] || :"dot_#{file_extension.gsub('.', '')}")
file_type = FactoryBot.create(file_attributes[:file_type] || :"dot_#{file_extension.delete('.')}")
name = File.basename(path).gsub(file_extension, '')
file_attributes.merge!(file_type: file_type, name: name, path: path.split('/')[1..-2].join('/'), role: file_attributes[:role] || 'regular_file')
if file_type.binary?
file_attributes.merge!(native_file: File.open(SeedsHelper.seed_file_path(path), 'r'))
file_attributes[:native_file] = File.open(SeedsHelper.seed_file_path(path), 'r')
else
file_attributes.merge!(content: SeedsHelper.read_seed_file(path))
file_attributes[:content] = SeedsHelper.read_seed_file(path)
end
exercise.add_file!(file_attributes)
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
FactoryBot.define do
factory :external_user do
association :consumer

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
FactoryBot.define do
factory :dot_coffee, class: 'FileType' do
created_by_admin

View File

@ -1,10 +1,11 @@
FactoryBot.define do
# frozen_string_literal: true
FactoryBot.define do
LTI_PARAMETERS = {
lis_result_sourcedid: "c2db0c7c-4411-4b27-a52b-ddfc3dc32065",
lis_outcome_service_url: "http://172.16.54.235:3000/courses/0132156a-9afb-434d-83cc-704780104105/sections/21c6c6f4-1fb6-43b4-af3c-04fdc098879e/items/999b1fe6-d4b6-47b7-a577-ea2b4b1041ec/tool_grading",
launch_presentation_return_url: "http://172.16.54.235:3000/courses/0132156a-9afb-434d-83cc-704780104105/sections/21c6c6f4-1fb6-43b4-af3c-04fdc098879e/items/999b1fe6-d4b6-47b7-a577-ea2b4b1041ec/tool_return"
}
lis_result_sourcedid: 'c2db0c7c-4411-4b27-a52b-ddfc3dc32065',
lis_outcome_service_url: 'http://172.16.54.235:3000/courses/0132156a-9afb-434d-83cc-704780104105/sections/21c6c6f4-1fb6-43b4-af3c-04fdc098879e/items/999b1fe6-d4b6-47b7-a577-ea2b4b1041ec/tool_grading',
launch_presentation_return_url: 'http://172.16.54.235:3000/courses/0132156a-9afb-434d-83cc-704780104105/sections/21c6c6f4-1fb6-43b4-af3c-04fdc098879e/items/999b1fe6-d4b6-47b7-a577-ea2b4b1041ec/tool_return',
}.freeze
factory :lti_parameter do
association :consumer

View File

@ -1,8 +1,9 @@
# frozen_string_literal: true
FactoryBot.define do
factory :proxy_exercise, class: 'ProxyExercise' do
created_by_teacher
token { 'dummytoken' }
title { 'Dummy' }
end
end

View File

@ -1,12 +1,14 @@
# frozen_string_literal: true
FactoryBot.define do
[:admin, :external_user, :teacher].each do |factory_name|
%i[admin external_user teacher].each do |factory_name|
trait :"created_by_#{factory_name}" do
association :user, factory: factory_name
end
end
trait :generated_email do
email { "#{name.underscore.gsub(' ', '.')}@example.org" }
email { "#{name.underscore.tr(' ', '.')}@example.org" }
end
trait :generated_user_name do

View File

@ -1,7 +1,9 @@
# frozen_string_literal: true
FactoryBot.define do
factory :structured_error_attribute do
association :structured_error
association :error_template_attribute
value { "MyString" }
value { 'MyString' }
end
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
FactoryBot.define do
factory :structured_error do
association :error_template

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
FactoryBot.define do
factory :study_group, class: 'StudyGroup' do
association :consumer

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
FactoryBot.define do
factory :submission do
cause { 'save' }

View File

@ -1,8 +1,9 @@
# frozen_string_literal: true
FactoryBot.define do
factory :user_exercise_feedback, class: 'UserExerciseFeedback' do
created_by_external_user
feedback_text { 'Most suitable exercise ever' }
association :exercise, factory: :math
end
end

View File

@ -7,7 +7,7 @@ describe 'Authentication' do
let(:password) { FactoryBot.attributes_for(:admin)[:password] }
context 'when signed out' do
before(:each) { visit(root_path) }
before { visit(root_path) }
it 'displays a sign in link' do
expect(page).to have_content(I18n.t('sessions.new.link'))
@ -35,7 +35,7 @@ describe 'Authentication' do
end
context 'when signed in' do
before(:each) do
before do
sign_in(user, password)
visit(root_path)
end

View File

@ -5,7 +5,8 @@ require 'rails_helper'
describe 'Authorization' do
context 'as an admin' do
let(:user) { FactoryBot.create(:admin) }
before(:each) { allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) }
before { allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) }
[Consumer, ExecutionEnvironment, Exercise, FileType, InternalUser].each do |model|
expect_permitted_path(:"new_#{model.model_name.singular}_path")
@ -14,7 +15,8 @@ describe 'Authorization' do
context 'as an external user' do
let(:user) { FactoryBot.create(:external_user) }
before(:each) { allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) }
before { allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) }
[Consumer, ExecutionEnvironment, Exercise, FileType, InternalUser].each do |model|
expect_forbidden_path(:"new_#{model.model_name.singular}_path")
@ -23,7 +25,8 @@ describe 'Authorization' do
context 'as a teacher' do
let(:user) { FactoryBot.create(:teacher) }
before(:each) { allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) }
before { allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) }
[Consumer, InternalUser, ExecutionEnvironment, FileType].each do |model|
expect_forbidden_path(:"new_#{model.model_name.singular}_path")

View File

@ -19,7 +19,7 @@ describe 'Editor', js: true do
score: 1.0,
filename: 'index.html_spec.rb',
message: 'Well done.',
weight: 2.0
weight: 2.0,
}]
end
let(:user) { FactoryBot.create(:teacher) }
@ -60,7 +60,7 @@ describe 'Editor', js: true do
context 'when selecting a binary file' do
context 'when selecting an audio file' do
let(:file) { exercise.files.detect { |file| file.file_type.audio? } }
let(:file) { exercise.files.detect {|file| file.file_type.audio? } }
it 'contains an <audio> tag' do
expect(page).to have_css("audio[src='#{file.native_file.url}']")
@ -68,7 +68,7 @@ describe 'Editor', js: true do
end
context 'when selecting an image file' do
let(:file) { exercise.files.detect { |file| file.file_type.image? } }
let(:file) { exercise.files.detect {|file| file.file_type.image? } }
it 'contains an <img> tag' do
expect(page).to have_css("img[src='#{file.native_file.url}']")
@ -76,7 +76,7 @@ describe 'Editor', js: true do
end
context 'when selecting a video file' do
let(:file) { exercise.files.detect { |file| file.file_type.video? } }
let(:file) { exercise.files.detect {|file| file.file_type.video? } }
it 'contains a <video> tag' do
expect(page).to have_css("video[src='#{file.native_file.url}']")
@ -85,7 +85,7 @@ describe 'Editor', js: true do
end
context 'when selecting a non-binary file' do
let(:file) { exercise.files.detect { |file| !file.file_type.binary? && !file.hidden? } }
let(:file) { exercise.files.detect {|file| !file.file_type.binary? && !file.hidden? } }
it "displays the file's code" do
expect(page).to have_css(".frame[data-filename='#{file.name_with_extension}']")

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'Factories' do

View File

@ -3,7 +3,6 @@
require 'rails_helper'
describe Prometheus::Controller do
let(:codeocean_config) { instance_double(CodeOcean::Config) }
let(:prometheus_config) { {prometheus_exporter: {enabled: true}} }

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'Request_for_Comments' do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe Admin::DashboardHelper do
@ -8,7 +10,7 @@ describe Admin::DashboardHelper do
end
describe '#docker_data' do
before(:each) { FactoryBot.create(:ruby) }
before { FactoryBot.create(:ruby) }
it 'contains an entry for every execution environment' do
expect(docker_data.length).to eq(ExecutionEnvironment.count)

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe ApplicationHelper do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe ExerciseHelper do

View File

@ -5,7 +5,7 @@ require 'yaml'
describe 'yaml config files' do
Find.find(__dir__, 'config') do |path|
next unless path =~ /.*.\.yml/
next unless /.*.\.yml/.match?(path)
it "loads #{path} without syntax error" do
expect { YAML.load_file(path) }.not_to raise_error

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe Assessor do
@ -8,7 +10,7 @@ describe Assessor do
let(:stdout) { "Finished in 0.1 seconds (files took 0.1 seconds to load)\n2 examples, 1 failure" }
context 'when an error occurs' do
before(:each) do
before do
expect_any_instance_of(TestingFrameworkAdapter).to receive(:test_outcome).and_raise
end
@ -22,7 +24,7 @@ describe Assessor do
end
context 'when no error occurs' do
after(:each) { assess }
after { assess }
it 'utilizes the testing framework adapter' do
expect(assessor.instance_variable_get(:@testing_framework_adapter)).to receive(:test_outcome)
@ -40,7 +42,7 @@ describe Assessor do
let(:test_outcome) { {count: count, passed: passed} }
it 'returns the correct score' do
expect(assessor.send(:calculate_score, test_outcome)).to eq(passed.to_f / count.to_f)
expect(assessor.send(:calculate_score, test_outcome)).to eq(passed.to_f / count)
end
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe CodeOcean::Config do
@ -10,8 +12,9 @@ describe CodeOcean::Config do
let(:read) { described_class.new(filename).read }
context 'when the file is present' do
before(:each) { File.write(path, {Rails.env.to_s => content}.to_yaml) }
after(:each) { FileUtils.rm(path) }
before { File.write(path, {Rails.env.to_s => content}.to_yaml) }
after { FileUtils.rm(path) }
it 'returns the environment-specific content' do
expect(read).to eq(content.with_indifferent_access)
@ -30,8 +33,9 @@ describe CodeOcean::Config do
let(:read) { described_class.new(filename).read(erb: true) }
context 'when the file is present' do
before(:each) { File.write(path, {Rails.env.to_s => content}.to_yaml) }
after(:each) { FileUtils.rm(path) }
before { File.write(path, {Rails.env.to_s => content}.to_yaml) }
after { FileUtils.rm(path) }
it 'returns the environment-specific content' do
expect(read).to eq(content.with_indifferent_access)

View File

@ -1,4 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
require 'seeds_helper'
@ -85,7 +86,7 @@ describe DockerClient, docker: true do
FileUtils.mkdir_p(workspace_path)
allow(described_class).to receive(:generate_local_workspace_path).and_return(local_workspace_path)
expect(described_class).to receive(:container_creation_options).with(execution_environment, local_workspace_path)
.and_wrap_original do |original_method, *args, &block|
.and_wrap_original do |original_method, *args, &block|
result = original_method.call(*args, &block)
result['NanoCPUs'] = 2 * 1_000_000_000 # CPU quota in units of 10^-9 CPUs.
result
@ -113,7 +114,7 @@ describe DockerClient, docker: true do
let(:error) { Docker::Error::NotFoundError.new }
context 'when retries are left' do
before(:each) do
before do
expect(described_class).to receive(:mapped_directories).and_raise(error).and_call_original
end
@ -123,7 +124,7 @@ describe DockerClient, docker: true do
end
context 'when no retries are left' do
before(:each) do
before do
expect(described_class).to receive(:mapped_directories).exactly(DockerClient::RETRY_COUNT + 1).times.and_raise(error)
end
@ -138,24 +139,24 @@ describe DockerClient, docker: true do
describe '#create_workspace_files' do
let(:container) { double }
before(:each) do
before do
expect(container).to receive(:binds).at_least(:once).and_return(["#{workspace_path}:#{DockerClient::CONTAINER_WORKSPACE_PATH}"])
end
after(:each) { docker_client.send(:create_workspace_files, container, submission) }
after { docker_client.send(:create_workspace_files, container, submission) }
it 'creates submission-specific directories' do
expect(Dir).to receive(:mkdir).at_least(:once).and_call_original
end
it 'copies binary files' do
submission.collect_files.select { |file| file.file_type.binary? }.each do |file|
submission.collect_files.select {|file| file.file_type.binary? }.each do |file|
expect(docker_client).to receive(:copy_file_to_workspace).with(container: container, file: file)
end
end
it 'creates non-binary files' do
submission.collect_files.reject { |file| file.file_type.binary? }.each do |file|
submission.collect_files.reject {|file| file.file_type.binary? }.each do |file|
expect(docker_client).to receive(:create_workspace_file).with(container: container, file: file)
end
end
@ -164,7 +165,8 @@ describe DockerClient, docker: true do
describe '#create_workspace_file' do
let(:file) { FactoryBot.build(:file, content: 'puts 42') }
let(:file_path) { File.join(workspace_path, file.name_with_extension) }
after(:each) { File.delete(file_path) }
after { File.delete(file_path) }
it 'creates a file' do
expect(described_class).to receive(:local_workspace_path).at_least(:once).and_return(workspace_path)
@ -177,7 +179,8 @@ describe DockerClient, docker: true do
describe '.destroy_container' do
let(:container) { described_class.create_container(execution_environment) }
after(:each) { described_class.destroy_container(container) }
after { described_class.destroy_container(container) }
it 'kills running processes' do
expect(container).to receive(:kill).and_return(container)
@ -190,8 +193,8 @@ describe DockerClient, docker: true do
it 'removes the mapped directory' do
expect(described_class).to receive(:local_workspace_path).at_least(:once).and_return(workspace_path)
#!TODO Fix this
#expect(PathName).to receive(:rmtree).with(workspace_path)
# !TODO Fix this
# expect(PathName).to receive(:rmtree).with(workspace_path)
end
it 'deletes the container' do
@ -216,9 +219,9 @@ describe DockerClient, docker: true do
let(:error) { Excon::Errors::SocketError.new(SocketError.new) }
context 'when retries are left' do
let(:result) { {status: "ok", stdout: 42} }
let(:result) { {status: 'ok', stdout: 42} }
before(:each) do
before do
expect(docker_client).to receive(:send_command).and_raise(error).and_return(result)
end
@ -228,43 +231,45 @@ describe DockerClient, docker: true do
end
context 'when no retries are left' do
before(:each) do
before do
expect(docker_client).to receive(:send_command).exactly(DockerClient::RETRY_COUNT + 1).times.and_raise(error)
end
it 'raises the error' do
pending("retries are disabled")
#!TODO Retries is disabled
#expect { execute_arbitrary_command }.to raise_error(error)
pending('retries are disabled')
# !TODO Retries is disabled
# expect { execute_arbitrary_command }.to raise_error(error)
end
end
end
end
describe '#execute_run_command' do
let(:filename) { submission.exercise.files.detect { |file| file.role == 'main_file' }.name_with_extension }
after(:each) { docker_client.send(:execute_run_command, submission, filename) }
let(:filename) { submission.exercise.files.detect {|file| file.role == 'main_file' }.name_with_extension }
after { docker_client.send(:execute_run_command, submission, filename) }
it 'takes a container from the pool' do
pending("todo in the future")
pending('todo in the future')
expect(DockerContainerPool).to receive(:get_container).with(submission.execution_environment).and_call_original
end
it 'creates the workspace files' do
pending("todo in the future")
pending('todo in the future')
expect(docker_client).to receive(:create_workspace_files)
end
it 'executes the run command' do
pending("todo in the future")
pending('todo in the future')
expect(submission.execution_environment).to receive(:run_command).and_call_original
expect(docker_client).to receive(:send_command).with(kind_of(String), kind_of(Docker::Container))
end
end
describe '#execute_test_command' do
let(:filename) { submission.exercise.files.detect { |file| file.role == 'teacher_defined_test' || file.role == 'teacher_defined_linter' }.name_with_extension }
after(:each) { docker_client.send(:execute_test_command, submission, filename) }
let(:filename) { submission.exercise.files.detect {|file| file.role == 'teacher_defined_test' || file.role == 'teacher_defined_linter' }.name_with_extension }
after { docker_client.send(:execute_test_command, submission, filename) }
it 'takes a container from the pool' do
expect(DockerContainerPool).to receive(:get_container).with(submission.execution_environment).and_call_original
@ -300,7 +305,7 @@ describe DockerClient, docker: true do
end
context 'with incomplete configuration' do
before(:each) { expect(described_class).to receive(:config).at_least(:once).and_return({}) }
before { expect(described_class).to receive(:config).at_least(:once).and_return({}) }
it 'raises an error' do
expect { described_class.initialize_environment }.to raise_error(DockerClient::Error)
@ -331,7 +336,7 @@ describe DockerClient, docker: true do
describe '.mapped_ports' do
context 'with exposed ports' do
before(:each) { execution_environment.exposed_ports = '3000' }
before { execution_environment.exposed_ports = '3000' }
it 'returns a mapping' do
expect(described_class.mapped_ports(execution_environment)).to be_a(Hash)
@ -355,30 +360,31 @@ describe DockerClient, docker: true do
let(:block) { proc {} }
let(:container) { described_class.create_container(execution_environment) }
let(:send_command) { docker_client.send(:send_command, command, container, &block) }
after(:each) { send_command }
after { send_command }
it 'limits the execution time' do
expect(Timeout).to receive(:timeout).at_least(:once).with(kind_of(Numeric)).and_call_original
end
it 'provides the command to be executed as input' do
pending("we are currently not using any input and for output server send events instead of attach.")
pending('we are currently not using any input and for output server send events instead of attach.')
expect(container).to receive(:attach).with(stdin: kind_of(StringIO))
end
it 'calls the block' do
pending("block is no longer called, see revision 4cbf9970b13362efd4588392cafe4f7fd7cb31c3 to get information how it was done before.")
pending('block is no longer called, see revision 4cbf9970b13362efd4588392cafe4f7fd7cb31c3 to get information how it was done before.')
expect(block).to receive(:call)
end
context 'when a timeout occurs' do
before(:each) do
before do
expect(container).to receive(:exec).once.and_raise(Timeout::Error)
expect(container).to receive(:exec).twice.and_return([[], []])
end
it 'destroys the container asynchronously' do
pending("Container is destroyed, but not as expected in this test. ToDo update this test.")
pending('Container is destroyed, but not as expected in this test. ToDo update this test.')
expect(Concurrent::Future).to receive(:execute)
end
@ -389,7 +395,7 @@ describe DockerClient, docker: true do
context 'when the container terminates timely' do
it 'destroys the container asynchronously' do
pending("Container is destroyed, but not as expected in this test. ToDo update this test.")
pending('Container is destroyed, but not as expected in this test. ToDo update this test.')
expect(Concurrent::Future).to receive(:execute)
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe DockerContainerMixin do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe FileTree do
@ -161,7 +163,7 @@ describe FileTree do
context 'for intermediary nodes' do
let(:node) do
root.add(Tree::TreeNode.new('').tap { |node| node.add(Tree::TreeNode.new('')) })
root.add(Tree::TreeNode.new('').tap {|node| node.add(Tree::TreeNode.new('')) })
end
it 'is a folder icon' do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails/generators'
require 'generators/testing_framework_adapter_generator'
require 'rails_helper'
@ -10,11 +12,11 @@ describe TestingFrameworkAdapterGenerator do
let(:path) { Rails.root.join('lib', "#{name.underscore}_adapter.rb") }
let(:spec_path) { Rails.root.join('spec', 'lib', "#{name.underscore}_adapter_spec.rb") }
before(:each) do
before do
silenced { Rails::Generators.invoke('testing_framework_adapter', [name]) }
end
after(:each) do
after do
File.delete(path)
File.delete(spec_path)
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe JunitAdapter do

View File

@ -1,10 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
describe MochaAdapter do
let(:adapter) { described_class.new }
let(:count) { 42 }
let(:failed) { 25 }
let(:stdout) { "#{count-failed} passing (20ms)\n\n#{failed} failing" }
let(:stdout) { "#{count - failed} passing (20ms)\n\n#{failed} failing" }
describe '#parse_output' do
it 'returns the correct numbers' do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe NonceStore do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe PortPool do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe PyUnitAdapter do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe RspecAdapter do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe SqlResultSetComparatorAdapter do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe TestingFrameworkAdapter do

View File

@ -1,5 +1,7 @@
# frozen_string_literal: true
class UserMailerPreview < ActionMailer::Preview
def exercise_anomaly_detected()
def exercise_anomaly_detected
collection = ExerciseCollection.new(name: 'Hello World', user: FactoryBot.create(:admin))
anomalies = {49 => 879.325828, 51 => 924.870057, 31 => 1031.21233, 69 => 2159.182116}
UserMailer.exercise_anomaly_detected(collection, anomalies)

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe UserMailer do
@ -6,7 +8,7 @@ describe UserMailer do
describe '#activation_needed_email' do
let(:mail) { described_class.activation_needed_email(user) }
before(:each) do
before do
user.send(:setup_activation)
user.save(validate: false)
end
@ -37,7 +39,7 @@ describe UserMailer do
describe '#reset_password_email' do
let(:mail) { described_class.reset_password_email(user) }
before(:each) do
before do
user.send(:setup_activation)
user.save(validate: false)
end

View File

@ -1,7 +1,9 @@
# frozen_string_literal: true
require 'rails_helper'
describe CodeOcean::File do
let(:file) { described_class.create.tap { |file| file.update(content: nil, hidden: nil, read_only: nil) } }
let(:file) { described_class.create.tap {|file| file.update(content: nil, hidden: nil, read_only: nil) } }
it 'validates the presence of a file type' do
expect(file.errors[:file_type_id]).to be_present
@ -24,7 +26,7 @@ describe CodeOcean::File do
end
context 'as a teacher-defined test' do
before(:each) { file.update(role: 'teacher_defined_test') }
before { file.update(role: 'teacher_defined_test') }
it 'validates the presence of a feedback message' do
expect(file.errors[:feedback_message]).to be_present
@ -41,7 +43,7 @@ describe CodeOcean::File do
end
context 'with another file type' do
before(:each) { file.update(role: 'regular_file') }
before { file.update(role: 'regular_file') }
it 'validates the absence of a feedback message' do
file.update(feedback_message: 'Your solution is not correct yet.')

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe CodeharborLink do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe Consumer do

View File

@ -1,7 +1,9 @@
# frozen_string_literal: true
require 'rails_helper'
describe ExecutionEnvironment do
let(:execution_environment) { described_class.create.tap { |execution_environment| execution_environment.update(network_enabled: nil) } }
let(:execution_environment) { described_class.create.tap {|execution_environment| execution_environment.update(network_enabled: nil) } }
it 'validates that the Docker image works', docker: true do
expect(execution_environment).to receive(:validate_docker_image?).and_return(true)
@ -69,7 +71,7 @@ describe ExecutionEnvironment do
describe '#valid_test_setup?' do
context 'with a test command and a testing framework' do
before(:each) { execution_environment.update(test_command: FactoryBot.attributes_for(:ruby)[:test_command], testing_framework: FactoryBot.attributes_for(:ruby)[:testing_framework]) }
before { execution_environment.update(test_command: FactoryBot.attributes_for(:ruby)[:test_command], testing_framework: FactoryBot.attributes_for(:ruby)[:testing_framework]) }
it 'is valid' do
expect(execution_environment.errors[:test_command]).to be_blank
@ -77,7 +79,7 @@ describe ExecutionEnvironment do
end
context 'with a test command but no testing framework' do
before(:each) { execution_environment.update(test_command: FactoryBot.attributes_for(:ruby)[:test_command], testing_framework: nil) }
before { execution_environment.update(test_command: FactoryBot.attributes_for(:ruby)[:test_command], testing_framework: nil) }
it 'is invalid' do
expect(execution_environment.errors[:test_command]).to be_present
@ -85,7 +87,7 @@ describe ExecutionEnvironment do
end
context 'with no test command but a testing framework' do
before(:each) { execution_environment.update(test_command: nil, testing_framework: FactoryBot.attributes_for(:ruby)[:testing_framework]) }
before { execution_environment.update(test_command: nil, testing_framework: FactoryBot.attributes_for(:ruby)[:testing_framework]) }
it 'is invalid' do
expect(execution_environment.errors[:test_command]).to be_present
@ -93,7 +95,7 @@ describe ExecutionEnvironment do
end
context 'with no test command and no testing framework' do
before(:each) { execution_environment.update(test_command: nil, testing_framework: nil) }
before { execution_environment.update(test_command: nil, testing_framework: nil) }
it 'is valid' do
expect(execution_environment.errors[:test_command]).to be_blank
@ -121,7 +123,8 @@ describe ExecutionEnvironment do
describe '#working_docker_image?', docker: true do
let(:working_docker_image?) { execution_environment.send(:working_docker_image?) }
before(:each) { expect(DockerClient).to receive(:find_image_by_tag).and_return(Object.new) }
before { expect(DockerClient).to receive(:find_image_by_tag).and_return(Object.new) }
it 'instantiates a Docker client' do
expect(DockerClient).to receive(:new).with(execution_environment: execution_environment).and_call_original

View File

@ -1,13 +1,13 @@
# frozen_string_literal: true
require 'rails_helper'
describe Exercise do
let(:exercise) { described_class.create.tap { |exercise| exercise.update(public: nil, token: nil) } }
let(:exercise) { described_class.create.tap {|exercise| exercise.update(public: nil, token: nil) } }
let(:users) { FactoryBot.create_list(:external_user, 10) }
def create_submissions
10.times do
FactoryBot.create(:submission, cause: 'submit', exercise: exercise, score: Forgery(:basic).number, user: users.sample)
end
FactoryBot.create_list(:submission, 10, cause: 'submit', exercise: exercise, score: Forgery(:basic).number, user: users.sample)
end
it 'validates the number of main files' do
@ -75,10 +75,10 @@ describe Exercise do
end
context 'with submissions' do
before(:each) { create_submissions }
before { create_submissions }
it 'returns the average score expressed as a percentage' do
maximum_percentages = exercise.submissions.group_by(&:user_id).values.map { |submission| submission.sort_by(&:score).last.score / exercise.maximum_score * 100 }
maximum_percentages = exercise.submissions.group_by(&:user_id).values.map {|submission| submission.max_by(&:score).score / exercise.maximum_score * 100 }
expect(exercise.average_percentage).to eq(maximum_percentages.average.round(2))
end
end
@ -94,10 +94,10 @@ describe Exercise do
end
context 'with submissions' do
before(:each) { create_submissions }
before { create_submissions }
it "returns the average of all users' maximum scores" do
maximum_scores = exercise.submissions.group_by(&:user_id).values.map { |submission| submission.sort_by(&:score).last.score }
maximum_scores = exercise.submissions.group_by(&:user_id).values.map {|submission| submission.max_by(&:score).score }
expect(exercise.average_score).to be_within(0.1).of(maximum_scores.average)
end
end
@ -105,7 +105,8 @@ describe Exercise do
describe '#duplicate' do
let(:exercise) { FactoryBot.create(:fibonacci) }
after(:each) { exercise.duplicate }
after { exercise.duplicate }
it 'duplicates the exercise' do
expect(exercise).to receive(:dup).and_call_original

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe ExternalUser do

View File

@ -1,7 +1,9 @@
# frozen_string_literal: true
require 'rails_helper'
describe FileType do
let(:file_type) { described_class.create.tap { |file_type| file_type.update(binary: nil, executable: nil, renderable: nil) } }
let(:file_type) { described_class.create.tap {|file_type| file_type.update(binary: nil, executable: nil, renderable: nil) } }
it 'validates the presence of the binary flag' do
expect(file_type.errors[:binary]).to be_present
@ -10,7 +12,7 @@ describe FileType do
end
context 'when binary' do
before(:each) { file_type.update(binary: true) }
before { file_type.update(binary: true) }
it 'does not validate the presence of an editor mode' do
expect(file_type.errors[:editor_mode]).not_to be_present
@ -22,7 +24,7 @@ describe FileType do
end
context 'when not binary' do
before(:each) { file_type.update(binary: false) }
before { file_type.update(binary: false) }
it 'validates the presence of an editor mode' do
expect(file_type.errors[:editor_mode]).to be_present

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe InternalUser do
@ -16,7 +18,7 @@ describe InternalUser do
context 'when not activated' do
let(:user) { FactoryBot.create(:teacher) }
before(:each) do
before do
user.send(:setup_activation)
user.send(:send_activation_needed_email!)
end
@ -34,7 +36,8 @@ describe InternalUser do
context 'with a pending password reset' do
let(:user) { FactoryBot.create(:teacher) }
before(:each) { user.deliver_reset_password_instructions! }
before { user.deliver_reset_password_instructions! }
it 'validates the confirmation of the password' do
user.update(password: password, password_confirmation: '')

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe Submission do
@ -28,7 +30,8 @@ describe Submission do
describe '#normalized_score' do
context 'with a score' do
let(:submission) { FactoryBot.create(:submission) }
before(:each) { submission.score = submission.exercise.maximum_score / 2 }
before { submission.score = submission.exercise.maximum_score / 2 }
it 'returns the score as a value between 0 and 1' do
expect(0..1).to include(submission.normalized_score)
@ -36,7 +39,7 @@ describe Submission do
end
context 'without a score' do
before(:each) { submission.score = nil }
before { submission.score = nil }
it 'returns 0' do
expect(submission.normalized_score).to be 0
@ -47,7 +50,8 @@ describe Submission do
describe '#percentage' do
context 'with a score' do
let(:submission) { FactoryBot.create(:submission) }
before(:each) { submission.score = submission.exercise.maximum_score / 2 }
before { submission.score = submission.exercise.maximum_score / 2 }
it 'returns the score expressed as a percentage' do
expect(0..100).to include(submission.percentage)
@ -55,7 +59,7 @@ describe Submission do
end
context 'without a score' do
before(:each) { submission.score = nil }
before { submission.score = nil }
it 'returns 0' do
expect(submission.percentage).to be 0
@ -67,7 +71,7 @@ describe Submission do
let(:siblings) { described_class.find_by(user: user).siblings }
let(:user) { FactoryBot.create(:external_user) }
before(:each) do
before do
10.times.each_with_index do |_, index|
FactoryBot.create(:submission, exercise: submission.exercise, user: (index.even? ? user : FactoryBot.create(:external_user)))
end
@ -87,11 +91,10 @@ describe Submission do
end
describe '#redirect_to_feedback?' do
context 'with no exercise feedback' do
let(:exercise) {FactoryBot.create(:dummy)}
let(:user) {FactoryBot.build(:external_user, id: (11 - exercise.created_at.to_i % 10) % 10)}
let(:submission) {FactoryBot.build(:submission, exercise: exercise, user: user)}
let(:exercise) { FactoryBot.create(:dummy) }
let(:user) { FactoryBot.build(:external_user, id: (11 - exercise.created_at.to_i % 10) % 10) }
let(:submission) { FactoryBot.build(:submission, exercise: exercise, user: user) }
it 'sends 10% of users to feedback page' do
expect(submission.send(:redirect_to_feedback?)).to be_truthy
@ -106,9 +109,9 @@ describe Submission do
end
context 'with little exercise feedback' do
let(:exercise) {FactoryBot.create(:dummy_with_user_feedbacks)}
let(:user) {FactoryBot.build(:external_user, id: (11 - exercise.created_at.to_i % 10) % 10)}
let(:submission) {FactoryBot.build(:submission, exercise: exercise, user: user)}
let(:exercise) { FactoryBot.create(:dummy_with_user_feedbacks) }
let(:user) { FactoryBot.build(:external_user, id: (11 - exercise.created_at.to_i % 10) % 10) }
let(:submission) { FactoryBot.build(:submission, exercise: exercise, user: user) }
it 'sends 10% of users to feedback page' do
expect(submission.send(:redirect_to_feedback?)).to be_truthy
@ -123,15 +126,15 @@ describe Submission do
end
context 'with enough exercise feedback' do
let(:exercise) {FactoryBot.create(:dummy_with_user_feedbacks, user_feedbacks_count: 42)}
let(:user) {FactoryBot.create(:external_user)}
let(:exercise) { FactoryBot.create(:dummy_with_user_feedbacks, user_feedbacks_count: 42) }
let(:user) { FactoryBot.create(:external_user) }
before do
allow_any_instance_of(described_class).to receive(:redirect_to_feedback?).and_return(false)
end
it 'sends nobody to feedback page' do
30.times do |i|
30.times do |_i|
submission = FactoryBot.create(:submission, exercise: exercise, user: FactoryBot.create(:external_user))
expect(submission.send(:redirect_to_feedback?)).to be_falsey
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe Admin::DashboardPolicy do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe ApplicationPolicy do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe CodeOcean::FilePolicy do
@ -19,7 +21,7 @@ describe CodeOcean::FilePolicy do
end
it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name|
%i[external_user teacher].each do |factory_name|
expect(subject).not_to permit(FactoryBot.build(factory_name), file)
end
end
@ -49,7 +51,7 @@ describe CodeOcean::FilePolicy do
end
it 'does not grant access to all other users' do
[:admin, :external_user, :teacher].each do |factory_name|
%i[admin external_user teacher].each do |factory_name|
expect(subject).not_to permit(FactoryBot.build(factory_name), file)
end
end
@ -69,7 +71,7 @@ describe CodeOcean::FilePolicy do
end
it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name|
%i[external_user teacher].each do |factory_name|
expect(subject).not_to permit(FactoryBot.build(factory_name), file)
end
end
@ -79,7 +81,7 @@ describe CodeOcean::FilePolicy do
let(:file) { submission.files.first }
it 'does not grant access to anyone' do
[:admin, :external_user, :teacher].each do |factory_name|
%i[admin external_user teacher].each do |factory_name|
expect(subject).not_to permit(FactoryBot.build(factory_name), file)
end
end

View File

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

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe ExecutionEnvironmentPolicy do
@ -21,7 +23,7 @@ describe ExecutionEnvironmentPolicy do
end
end
[:execute_command?, :shell?, :statistics?, :show?].each do |action|
%i[execute_command? shell? statistics? show?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryBot.build(:admin), execution_environment)
@ -32,14 +34,14 @@ describe ExecutionEnvironmentPolicy do
end
it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name|
%i[external_user teacher].each do |factory_name|
expect(subject).not_to permit(FactoryBot.build(factory_name), execution_environment)
end
end
end
end
[:destroy?, :edit?, :update?, :new?, :create?].each do |action|
%i[destroy? edit? update? new? create?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryBot.build(:admin), execution_environment)
@ -50,7 +52,7 @@ describe ExecutionEnvironmentPolicy do
end
it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name|
%i[external_user teacher].each do |factory_name|
expect(subject).not_to permit(FactoryBot.build(factory_name), execution_environment)
end
end

View File

@ -1,20 +1,22 @@
# frozen_string_literal: true
require 'rails_helper'
describe ExercisePolicy do
subject { described_class }
let(:exercise) { FactoryBot.build(:dummy, public: true) }
let(:exercise) { FactoryBot.build(:dummy, public: true) }
permissions :batch_update? do
it 'grants access to admins only' do
expect(subject).to permit(FactoryBot.build(:admin), exercise)
[:external_user, :teacher].each do |factory_name|
%i[external_user teacher].each do |factory_name|
expect(subject).not_to permit(FactoryBot.build(factory_name), exercise)
end
end
end
[:create?, :index?, :new?, :statistics?, :feedback?, :get_rfcs_for_exercise?].each do |action|
%i[create? index? new? statistics? feedback? get_rfcs_for_exercise?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryBot.build(:admin), exercise)
@ -30,7 +32,7 @@ let(:exercise) { FactoryBot.build(:dummy, public: true) }
end
end
[:clone?, :destroy?, :edit?, :update?].each do |action|
%i[clone? destroy? edit? update?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryBot.build(:admin), exercise)
@ -41,14 +43,14 @@ let(:exercise) { FactoryBot.build(:dummy, public: true) }
end
it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name|
%i[external_user teacher].each do |factory_name|
expect(subject).not_to permit(FactoryBot.build(factory_name), exercise)
end
end
end
end
[:export_external_check?, :export_external_confirm?].each do |action|
%i[export_external_check? export_external_confirm?].each do |action|
permissions(action) do
context 'when user is author' do
let(:user) { exercise.author }
@ -82,7 +84,7 @@ let(:exercise) { FactoryBot.build(:dummy, public: true) }
end
end
[:external_user, :teacher].each do |factory_name|
%i[external_user teacher].each do |factory_name|
context "when user is #{factory_name}" do
let(:user) { FactoryBot.build(factory_name) }
@ -110,10 +112,10 @@ let(:exercise) { FactoryBot.build(:dummy, public: true) }
end
end
[:implement?, :submit?].each do |action|
%i[implement? submit?].each do |action|
permissions(action) do
it 'grants access to anyone' do
[:admin, :external_user, :teacher].each do |factory_name|
%i[admin external_user teacher].each do |factory_name|
expect(subject).to permit(FactoryBot.build(factory_name), Exercise.new)
end
end
@ -151,7 +153,6 @@ let(:exercise) { FactoryBot.build(:dummy, public: true) }
end
context 'for teachers' do
let(:scope) { Pundit.policy_scope!(@teacher, Exercise) }
it 'includes all public exercises' do

View File

@ -1,13 +1,15 @@
# frozen_string_literal: true
require 'rails_helper'
describe ExternalUserPolicy do
subject { described_class }
[:create?, :destroy?, :edit?, :new?, :show?, :update?].each do |action|
%i[create? destroy? edit? new? show? update?].each do |action|
permissions(action) do
it 'grants access to admins only' do
expect(subject).to permit(FactoryBot.build(:admin), ExternalUser.new)
[:external_user, :teacher].each do |factory_name|
%i[external_user teacher].each do |factory_name|
expect(subject).not_to permit(FactoryBot.build(factory_name), ExternalUser.new)
end
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe FileTypePolicy do
@ -5,7 +7,7 @@ describe FileTypePolicy do
let(:file_type) { FactoryBot.build(:dot_rb) }
[:destroy?, :edit?, :update?, :new?, :create?, :index?, :show?].each do |action|
%i[destroy? edit? update? new? create? index? show?].each do |action|
permissions(action) do
it 'grants access to admins' do
expect(subject).to permit(FactoryBot.build(:admin), file_type)
@ -16,7 +18,7 @@ describe FileTypePolicy do
end
it 'does not grant access to all other users' do
[:external_user, :teacher].each do |factory_name|
%i[external_user teacher].each do |factory_name|
expect(subject).not_to permit(FactoryBot.build(factory_name), file_type)
end
end

View File

@ -1,13 +1,15 @@
# frozen_string_literal: true
require 'rails_helper'
describe InternalUserPolicy do
subject { described_class }
[:create?, :edit?, :index?, :new?, :show?, :update?].each do |action|
%i[create? edit? index? new? show? update?].each do |action|
permissions(action) do
it 'grants access to admins only' do
expect(subject).to permit(FactoryBot.build(:admin), InternalUser.new)
[:external_user, :teacher].each do |factory_name|
%i[external_user teacher].each do |factory_name|
expect(subject).not_to permit(FactoryBot.build(factory_name), InternalUser.new)
end
end
@ -17,7 +19,7 @@ describe InternalUserPolicy do
permissions :destroy? do
context 'with an admin user' do
it 'grants access to no one' do
[:admin, :external_user, :teacher].each do |factory_name|
%i[admin external_user teacher].each do |factory_name|
expect(subject).not_to permit(FactoryBot.build(factory_name), FactoryBot.build(:admin))
end
end
@ -26,7 +28,7 @@ describe InternalUserPolicy do
context 'with a non-admin user' do
it 'grants access to admins only' do
expect(subject).to permit(FactoryBot.build(:admin), InternalUser.new)
[:external_user, :teacher].each do |factory_name|
%i[external_user teacher].each do |factory_name|
expect(subject).not_to permit(FactoryBot.build(factory_name), FactoryBot.build(:teacher))
end
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe SubmissionPolicy do
@ -5,13 +7,13 @@ describe SubmissionPolicy do
permissions :create? do
it 'grants access to anyone' do
[:admin, :external_user, :teacher].each do |factory_name|
%i[admin external_user teacher].each do |factory_name|
expect(subject).to permit(FactoryBot.build(factory_name), Submission.new)
end
end
end
[:download_file?, :render_file?, :run?, :score?, :show?, :statistics?, :stop?, :test?].each do |action|
%i[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(FactoryBot.build(:admin), Submission.new)
@ -27,7 +29,7 @@ describe SubmissionPolicy do
permissions :index? do
it 'grants access to admins only' do
expect(subject).to permit(FactoryBot.build(:admin), Submission.new)
[:external_user, :teacher].each do |factory_name|
%i[external_user teacher].each do |factory_name|
expect(subject).not_to permit(FactoryBot.build(factory_name), Submission.new)
end
end

View File

@ -15,7 +15,7 @@ require 'pundit/rspec'
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
Dir[Rails.root.join('spec/support/**/*.rb')].sort.each { |f| require f }
Dir[Rails.root.join('spec/support/**/*.rb')].sort.each {|f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.

View File

@ -19,9 +19,9 @@ RSpec.describe ProformaService::ConvertExerciseToTask do
let(:convert_to_task) { described_class.new(exercise: exercise) }
let(:exercise) do
FactoryBot.create(:dummy,
instructions: 'instruction',
uuid: SecureRandom.uuid,
files: files + tests)
instructions: 'instruction',
uuid: SecureRandom.uuid,
files: files + tests)
end
let(:files) { [] }
let(:tests) { [] }

View File

@ -3,7 +3,7 @@
require 'rails_helper'
describe ProformaService::ConvertTaskToExercise do
# ToDo: Add teacher_defined_linter for tests
# TODO: Add teacher_defined_linter for tests
describe '.new' do
subject(:convert_to_exercise_service) { described_class.new(task: task, user: user, exercise: exercise) }
@ -232,7 +232,7 @@ describe ProformaService::ConvertTaskToExercise do
meta_data: {
'feedback-message' => 'feedback-message',
'testing-framework' => 'testing-framework',
'testing-framework-version' => 'testing-framework-version'
'testing-framework-version' => 'testing-framework-version',
}
)
end
@ -252,11 +252,11 @@ describe ProformaService::ConvertTaskToExercise do
end
it 'creates an exercise with a test' do
expect(convert_to_exercise_service.files.select { |file| file.role == 'teacher_defined_test' }).to have(1).item
expect(convert_to_exercise_service.files.select {|file| file.role == 'teacher_defined_test' }).to have(1).item
end
it 'creates an exercise with a test with correct attributes' do
expect(convert_to_exercise_service.files.select { |file| file.role == 'teacher_defined_test' }.first).to have_attributes(
expect(convert_to_exercise_service.files.find {|file| file.role == 'teacher_defined_test' }).to have_attributes(
feedback_message: 'feedback-message',
content: 'testfile-content',
name: 'testfile',
@ -275,7 +275,7 @@ describe ProformaService::ConvertTaskToExercise do
meta_data: {
'feedback-message' => 'feedback-message',
'testing-framework' => 'testing-framework',
'testing-framework-version' => 'testing-framework-version'
'testing-framework-version' => 'testing-framework-version',
}
)
end
@ -294,7 +294,7 @@ describe ProformaService::ConvertTaskToExercise do
end
it 'creates an exercise with two test' do
expect(convert_to_exercise_service.files.select { |file| file.role == 'teacher_defined_test' }).to have(2).items
expect(convert_to_exercise_service.files.select {|file| file.role == 'teacher_defined_test' }).to have(2).items
end
end
end
@ -355,7 +355,7 @@ describe ProformaService::ConvertTaskToExercise do
meta_data: {
'feedback-message' => 'feedback-message',
'testing-framework' => 'testing-framework',
'testing-framework-version' => 'testing-framework-version'
'testing-framework-version' => 'testing-framework-version',
}
)
end

View File

@ -26,11 +26,11 @@ describe ProformaService::Import do
let(:zip_file) { Tempfile.new('proforma_test_zip_file', encoding: 'ascii-8bit') }
let(:exercise) do
FactoryBot.create(:dummy,
instructions: 'instruction',
execution_environment: execution_environment,
files: files + tests,
uuid: uuid,
user: user)
instructions: 'instruction',
execution_environment: execution_environment,
files: files + tests,
uuid: uuid,
user: user)
end
let(:uuid) {}
@ -156,7 +156,7 @@ describe ProformaService::Import do
let(:import_user) { FactoryBot.create(:teacher) }
it 'raises a proforma error' do
expect { imported_exercise.save! } .to raise_error Proforma::ExerciseNotOwned
expect { imported_exercise.save! }.to raise_error Proforma::ExerciseNotOwned
end
end
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause this
@ -16,8 +18,8 @@
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
unless RUBY_PLATFORM == 'java'
require 'simplecov'
SimpleCov.start('rails')
require 'simplecov'
SimpleCov.start('rails')
end
require 'webmock/rspec'

View File

@ -1,10 +1,11 @@
# frozen_string_literal: true
class AnonymousController < ApplicationController
def flash
@flash ||= {}
end
def redirect_to(*)
end
def redirect_to(*); end
def session
@session ||= {}

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module Authentication
def sign_in(user, password)
page.driver.post(sessions_url, email: user.email, password: password)

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
def expect_assigns(pairs)
pairs.each do |key, value|
it "assigns @#{key}" do
@ -61,12 +63,12 @@ end
def obtain_object(object)
case object
when Proc
object.call
when Symbol
send(object)
else
object
when Proc
object.call
when Symbol
send(object)
else
object
end
end
private :obtain_object

View File

@ -3,7 +3,6 @@
require 'capybara/rspec'
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
CONTAINER = Docker::Container.send(:new, Docker::Connection.new('http://example.org', {}), 'id' => SecureRandom.hex)
IMAGE = Docker::Image.new(Docker::Connection.new('http://example.org', {}), 'id' => SecureRandom.hex, 'RepoTags' => [FactoryBot.attributes_for(:ruby)[:docker_image]])
@ -16,7 +18,7 @@ RSpec.configure do |config|
config.after(:suite) do
examples = RSpec.world.filtered_examples.values.flatten
has_docker_tests = examples.any? { |example| example.metadata[:docker] }
has_docker_tests = examples.any? {|example| example.metadata[:docker] }
next unless has_docker_tests
FileUtils.rm_rf(Rails.root.join('tmp', 'files', 'test'))

View File

@ -31,7 +31,7 @@ RSpec::Matchers.define :be_an_equal_exercise_as do |exercise|
return true if object == other # for []
return false if object.length != other.length
object.to_a.product(other.to_a).map { |k, v| equal?(k, v) }.any?
object.to_a.product(other.to_a).map {|k, v| equal?(k, v) }.any?
end
def attributes_and_associations(object)

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'factory_bot'
# Use "old" FactoryBot default to allow auto-creating associations for #build

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
def expect_forbidden_path(path_name)
it "forbids to access the #{path_name.to_s.split('_').join(' ')}" do
visit(send(path_name))

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'capybara/rspec'
require 'selenium/webdriver'

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module Silencer
def silenced
@stdout = $stdout

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
module WaitForAjax
def wait_for_ajax
Timeout.timeout(Capybara.default_wait_time) do

View File

@ -1,11 +1,14 @@
# frozen_string_literal: true
require 'rails_helper'
describe FileUploader do
let(:file_path) { Rails.root.join('db', 'seeds', 'fibonacci', 'exercise.rb') }
let(:uploader) { described_class.new(FactoryBot.create(:file)) }
before(:each) { uploader.store!(File.open(file_path, 'r')) }
after(:each) { uploader.remove! }
before { uploader.store!(File.open(file_path, 'r')) }
after { uploader.remove! }
it 'uses the specified storage directory' do
expect(uploader.file.path).to start_with(Rails.root.join('public', uploader.store_dir).to_s)

View File

@ -1,9 +1,11 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'execution_environments/shell.html.slim' do
let(:execution_environment) { FactoryBot.create(:ruby) }
before(:each) do
before do
assign(:execution_environment, execution_environment)
render
end

View File

@ -1,11 +1,13 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'exercises/implement.html.slim' do
let(:exercise) { FactoryBot.create(:fibonacci) }
let(:files) { exercise.files.visible }
let(:non_binary_files) { files.reject { |file| file.file_type.binary? } }
let(:non_binary_files) { files.reject {|file| file.file_type.binary? } }
before(:each) do
before do
assign(:current_user, FactoryBot.create(:admin))
assign(:exercise, exercise)
assign(:files, files)