add controller specs
This commit is contained in:
93
spec/controllers/codeharbor_links_controller_spec.rb
Normal file
93
spec/controllers/codeharbor_links_controller_spec.rb
Normal file
@ -0,0 +1,93 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe CodeharborLinksController do
|
||||
let(:user) { FactoryBot.create(:teacher) }
|
||||
before(:each) { allow(controller).to receive(:current_user).and_return(user) }
|
||||
|
||||
describe 'GET #new' do
|
||||
before { get :new }
|
||||
|
||||
expect_status(200)
|
||||
end
|
||||
|
||||
describe 'GET #edit' do
|
||||
let(:codeharbor_link) { FactoryBot.create(:codeharbor_link, user: user) }
|
||||
|
||||
before { get :edit, params: { id: codeharbor_link.id } }
|
||||
|
||||
expect_status(200)
|
||||
end
|
||||
|
||||
describe 'POST #create' do
|
||||
let(:post_request) { post :create, params: {codeharbor_link: params} }
|
||||
let(:params) { {push_url: 'http://foo.bar/push', check_uuid_url: 'http://foo.bar/check', api_key: 'api_key'} }
|
||||
|
||||
it 'creates a codeharbor_link' do
|
||||
expect { post_request }.to change(CodeharborLink, :count).by(1)
|
||||
end
|
||||
|
||||
it 'redirects to user show' do
|
||||
expect(post_request).to redirect_to(user)
|
||||
end
|
||||
|
||||
context 'with invalid params' do
|
||||
let(:params) { {push_url: '', check_uuid_url: '', api_key: ''} }
|
||||
|
||||
it 'does not create a codeharbor_link' do
|
||||
expect { post_request }.not_to change(CodeharborLink, :count)
|
||||
end
|
||||
|
||||
it 'redirects to user show' do
|
||||
post_request
|
||||
expect(response).to render_template(:new)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT #update' do
|
||||
let(:codeharbor_link) { FactoryBot.create(:codeharbor_link, user: user) }
|
||||
let(:put_request) { patch :update, params: {id: codeharbor_link.id, codeharbor_link: params} }
|
||||
let(:params) { {push_url: 'http://foo.bar/push', check_uuid_url: 'http://foo.bar/check', api_key: 'api_key'} }
|
||||
|
||||
it 'updates push_url' do
|
||||
expect { put_request }.to change { codeharbor_link.reload.push_url }.to('http://foo.bar/push')
|
||||
end
|
||||
it 'updates check_uuid_url' do
|
||||
expect { put_request }.to change { codeharbor_link.reload.check_uuid_url }.to('http://foo.bar/check')
|
||||
end
|
||||
it 'updates api_key' do
|
||||
expect { put_request }.to change { codeharbor_link.reload.api_key }.to('api_key')
|
||||
end
|
||||
|
||||
it 'redirects to user show' do
|
||||
expect(put_request).to redirect_to(user)
|
||||
end
|
||||
|
||||
context 'with invalid params' do
|
||||
let(:params) { {push_url: '', check_uuid_url: '', api_key: ''} }
|
||||
|
||||
it 'does not change codeharbor_link' do
|
||||
expect { put_request }.not_to change { codeharbor_link.reload.attributes }
|
||||
end
|
||||
|
||||
it 'redirects to user show' do
|
||||
put_request
|
||||
expect(response).to render_template(:edit)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE #destroy' do
|
||||
let!(:codeharbor_link) { FactoryBot.create(:codeharbor_link, user: user) }
|
||||
let(:destroy_request) { delete :destroy, params: {id: codeharbor_link.id} }
|
||||
|
||||
it 'deletes codeharbor_link' do
|
||||
expect { destroy_request }.to change(CodeharborLink, :count).by(-1)
|
||||
end
|
||||
|
||||
it 'redirects to user show' do
|
||||
expect(destroy_request).to redirect_to(user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -313,4 +313,203 @@ describe ExercisesController do
|
||||
expect_template(:edit)
|
||||
end
|
||||
end
|
||||
|
||||
RSpec::Matchers.define_negated_matcher :not_include, :include
|
||||
# RSpec::Support::ObjectFormatter.default_instance.max_formatted_output_length = 99999
|
||||
|
||||
describe 'POST #export_external_check' do
|
||||
render_views
|
||||
|
||||
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' }
|
||||
let(:update_right) { true }
|
||||
let(:error) {}
|
||||
|
||||
before { allow(ExerciseService::CheckExternal).to receive(:call).with(uuid: exercise.uuid, codeharbor_link: codeharbor_link).and_return(external_check_hash) }
|
||||
|
||||
it 'renders the correct contents as json' do
|
||||
post_request
|
||||
expect(JSON.parse(response.body).symbolize_keys[:message]).to eq('message')
|
||||
expect(JSON.parse(response.body).symbolize_keys[:actions]).to(
|
||||
include('button').and(include('Abort').and(include('Export')))
|
||||
)
|
||||
expect(JSON.parse(response.body).symbolize_keys[:actions]).to(
|
||||
not_include('Retry').and(not_include('Hide'))
|
||||
)
|
||||
end
|
||||
|
||||
context 'when there is an error' do
|
||||
let(:error) { 'error' }
|
||||
|
||||
it 'renders the correct contents as json' do
|
||||
post_request
|
||||
expect(JSON.parse(response.body).symbolize_keys[:message]).to eq('message')
|
||||
expect(JSON.parse(response.body).symbolize_keys[:actions]).to(
|
||||
include('button').and(include('Abort')).and(include('Retry'))
|
||||
)
|
||||
expect(JSON.parse(response.body).symbolize_keys[:actions]).to(
|
||||
not_include('Create new').and(not_include('Export')).and(not_include('Hide'))
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when update_right is false' do
|
||||
let(:update_right) { false }
|
||||
|
||||
it 'renders the correct contents as json' do
|
||||
post_request
|
||||
expect(JSON.parse(response.body).symbolize_keys[:message]).to eq('message')
|
||||
expect(JSON.parse(response.body).symbolize_keys[:actions]).to(
|
||||
include('button').and(include('Abort')).and(include('Create new'))
|
||||
)
|
||||
expect(JSON.parse(response.body).symbolize_keys[:actions]).to(
|
||||
not_include('Retry').and(not_include('Export')).and(not_include('Hide'))
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#export_external_confirm' do
|
||||
render_views
|
||||
|
||||
let!(:codeharbor_link) { FactoryBot.create(:codeharbor_link, user: user) }
|
||||
let(:post_request) { post :export_external_confirm, params: {push_type: push_type, id: exercise.id, codeharbor_link: codeharbor_link.id} }
|
||||
let(:push_type) { 'create_new' }
|
||||
let(:error) {}
|
||||
let(:zip) { 'zip' }
|
||||
|
||||
before do
|
||||
allow(ProformaService::ExportTask).to receive(:call).with(exercise: exercise).and_return(zip)
|
||||
allow(ExerciseService::PushExternal).to receive(:call).with(zip: zip, codeharbor_link: codeharbor_link).and_return(error)
|
||||
end
|
||||
|
||||
it 'renders correct response' do
|
||||
post_request
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(JSON.parse(response.body).symbolize_keys[:message]).to(include('successfully exported'))
|
||||
expect(JSON.parse(response.body).symbolize_keys[:status]).to(eql('success'))
|
||||
expect(JSON.parse(response.body).symbolize_keys[:actions]).to(include('button').and(include('Close')))
|
||||
expect(JSON.parse(response.body).symbolize_keys[:actions]).to(not_include('Retry').and(not_include('Abort')))
|
||||
end
|
||||
|
||||
context 'when an error occurs' do
|
||||
let(:error) { 'exampleerror' }
|
||||
|
||||
it 'renders correct response' do
|
||||
post_request
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(JSON.parse(response.body).symbolize_keys[:message]).to(include('failed').and(include('exampleerror')))
|
||||
expect(JSON.parse(response.body).symbolize_keys[:status]).to(eql('fail'))
|
||||
expect(JSON.parse(response.body).symbolize_keys[:actions]).to(include('button').and(include('Retry')).and(include('Close')))
|
||||
expect(JSON.parse(response.body).symbolize_keys[:actions]).to(not_include('Abort'))
|
||||
end
|
||||
end
|
||||
|
||||
context 'without push_type' do
|
||||
let(:push_type) {}
|
||||
|
||||
it 'responds with status 500' do
|
||||
post_request
|
||||
expect(response).to have_http_status(:internal_server_error)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#import_uuid_check' do
|
||||
let(:exercise) { FactoryBot.create(:dummy, uuid: SecureRandom.uuid) }
|
||||
let!(:codeharbor_link) { FactoryBot.create(:codeharbor_link, user: user) }
|
||||
let(:uuid) { exercise.reload.uuid }
|
||||
let(:post_request) { post :import_uuid_check, params: {uuid: uuid} }
|
||||
let(:headers) { {'Authorization' => "Bearer #{codeharbor_link.api_key}"} }
|
||||
|
||||
before { request.headers.merge! headers }
|
||||
|
||||
it 'renders correct response' do
|
||||
post_request
|
||||
expect(response).to have_http_status(:success)
|
||||
|
||||
expect(JSON.parse(response.body).symbolize_keys[:exercise_found]).to be true
|
||||
expect(JSON.parse(response.body).symbolize_keys[:update_right]).to be true
|
||||
expect(JSON.parse(response.body).symbolize_keys[:message]).to(include('has been found').and(include('Overwrite')))
|
||||
end
|
||||
|
||||
context 'when api_key is incorrect' do
|
||||
let(:headers) { {'Authorization' => 'Bearer XXXXXX'} }
|
||||
|
||||
it 'renders correct response' do
|
||||
post_request
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the user is cannot update the exercise' do
|
||||
let(:codeharbor_link) { FactoryBot.create(:codeharbor_link, api_key: 'anotherkey') }
|
||||
|
||||
it 'renders correct response' do
|
||||
post_request
|
||||
expect(response).to have_http_status(:success)
|
||||
|
||||
expect(JSON.parse(response.body).symbolize_keys[:exercise_found]).to be true
|
||||
expect(JSON.parse(response.body).symbolize_keys[:update_right]).to be false
|
||||
expect(JSON.parse(response.body).symbolize_keys[:message]).to(include('has been found').and(not_include('Overwrite')))
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the searched exercise does not exist' do
|
||||
let(:uuid) { 'anotheruuid' }
|
||||
|
||||
it 'renders correct response' do
|
||||
post_request
|
||||
expect(response).to have_http_status(:success)
|
||||
|
||||
expect(JSON.parse(response.body).symbolize_keys[:exercise_found]).to be false
|
||||
expect(JSON.parse(response.body).symbolize_keys[:message]).to(include('No corresponding exercise'))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST #import_exercise' do
|
||||
let(:codeharbor_link) { FactoryBot.create(:codeharbor_link, user: user) }
|
||||
let(:imported_exercise) { exercise }
|
||||
let(:post_request) { post :import_exercise, body: zip_file_content }
|
||||
let(:zip_file_content) { 'zipped task xml' }
|
||||
let(:headers) { {'Authorization' => "Bearer #{codeharbor_link.api_key}"} }
|
||||
|
||||
before do
|
||||
request.headers.merge! headers
|
||||
allow(ProformaService::Import).to receive(:call).and_return(imported_exercise)
|
||||
end
|
||||
|
||||
it 'responds with correct status code' do
|
||||
post_request
|
||||
expect(response).to have_http_status(:created)
|
||||
end
|
||||
|
||||
it 'calls service' do
|
||||
post_request
|
||||
expect(ProformaService::Import).to have_received(:call).with(zip: be_a(Tempfile).and(has_content(zip_file_content)), user: user)
|
||||
end
|
||||
|
||||
context 'when import fails with ProformaError' do
|
||||
before { allow(ProformaService::Import).to receive(:call).and_raise(Proforma::PreImportValidationError) }
|
||||
|
||||
it 'responds with correct status code' do
|
||||
post_request
|
||||
expect(response).to have_http_status(:bad_request)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when import fails due to another error' do
|
||||
before { allow(ProformaService::Import).to receive(:call).and_raise(StandardError) }
|
||||
|
||||
it 'responds with correct status code' do
|
||||
post_request
|
||||
expect(response).to have_http_status(:internal_server_error)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
Reference in New Issue
Block a user