From c687c1072e2aa834814b3176ddd1685c189bfcb4 Mon Sep 17 00:00:00 2001 From: Ana Date: Mon, 8 Aug 2016 18:01:35 +0200 Subject: [PATCH] Add event schedules and schedules controller specs --- .../admin/event_schedules_controller_spec.rb | 143 ++++++++++++++++++ .../admin/schedules_controller_spec.rb | 63 ++++++++ 2 files changed, 206 insertions(+) create mode 100644 spec/controllers/admin/event_schedules_controller_spec.rb create mode 100644 spec/controllers/admin/schedules_controller_spec.rb diff --git a/spec/controllers/admin/event_schedules_controller_spec.rb b/spec/controllers/admin/event_schedules_controller_spec.rb new file mode 100644 index 00000000..d432e974 --- /dev/null +++ b/spec/controllers/admin/event_schedules_controller_spec.rb @@ -0,0 +1,143 @@ +require 'spec_helper' + +describe Admin::EventSchedulesController do + let(:venue) { create(:venue) } + let(:conference) { create(:conference, venue: venue) } + let(:schedule) { create(:schedule, program: conference.program)} + let(:event_schedule) { create(:event_schedule, schedule: schedule)} + let!(:organizer_role) { Role.find_by(name: 'organizer', resource: conference) } + let(:organizer) { create(:user, role_ids: organizer_role.id) } + + context 'logged in as an organizer' do + before :each do + sign_in(organizer) + event_schedule + end + + describe 'POST #create' do + context 'with valid attributes' do + it 'saves the event schedule to the database' do + expected = expect do + post :create, conference_id: conference.short_title, event_schedule: + attributes_for(:event_schedule, + schedule_id: schedule.id, + event_id: create(:event, program: conference.program).id, + room_id: create(:room, venue: venue).id, + start_time: conference.start_date) + end + expected.to change { EventSchedule.count }.by 1 + end + + it 'renders JSON without errors' do + post :create, conference_id: conference.short_title, event_schedule: + attributes_for(:event_schedule, + schedule_id: schedule.id, + event_id: create(:event, program: conference.program).id, + room_id: create(:room, venue: venue).id, + start_time: conference.start_date) + + expect(response).to be_success + expect(JSON.parse(response.body)['status']).to eq('ok') + end + end + + context 'with invalid attributes' do + it 'does not save the event schedule to the database' do + expected = expect do + post :create, conference_id: conference.short_title, event_schedule: + attributes_for(:event_schedule, + schedule_id: schedule.id, + event_id: nil, + room_id: nil, + start_time: nil) + end + expected.to_not change { EventSchedule.count } + end + + it 'renders JSON with error' do + post :create, conference_id: conference.short_title, event_schedule: + attributes_for(:event_schedule, + schedule_id: schedule.id, + event_id: nil, + room_id: nil, + start_time: nil) + + expect(JSON.parse(response.body)['status']).to_not eq('ok') + end + end + end + + describe 'POST #update' do + context 'with valid attributes' do + it 'changes event schedule attributes' do + event = create(:event, program: conference.program) + room = create(:room, venue: venue) + patch :update, id: event_schedule.id, conference_id: conference.short_title, event_schedule: + attributes_for(:event_schedule, + schedule_id: schedule.id, + event_id: event.id, + room_id: room.id, + start_time: conference.start_date) + event_schedule.reload + expect(event_schedule.schedule_id).to eq(schedule.id) + expect(event_schedule.event_id).to eq(event.id) + expect(event_schedule.room_id).to eq(room.id) + expect(event_schedule.start_time).to eq(conference.start_date) + end + + it 'renders JSON without errors' do + patch :update, id: event_schedule.id, conference_id: conference.short_title, event_schedule: + attributes_for(:event_schedule, + schedule_id: schedule.id, + event_id: create(:event, program: conference.program).id, + room_id: create(:room, venue: venue).id, + start_time: conference.start_date) + + expect(response).to be_success + expect(JSON.parse(response.body)['status']).to eq('ok') + end + end + + context 'with invalid attributes' do + it 'does not save the event schedule to the database' do + expected = expect do + patch :update, id: event_schedule.id, conference_id: conference.short_title, event_schedule: + attributes_for(:event_schedule, + schedule_id: schedule.id, + event_id: nil, + room_id: nil, + start_time: nil) + end + expected.to_not change { event_schedule } + end + + it 'renders JSON with error' do + patch :update, id: event_schedule.id, conference_id: conference.short_title, event_schedule: + attributes_for(:event_schedule, + schedule_id: schedule.id, + event_id: nil, + room_id: nil, + start_time: nil) + + expect(JSON.parse(response.body)['status']).to_not eq('ok') + end + end + end + + describe 'DELETE #destroy' do + it 'deletes the event schedule' do + expected = expect do + delete :destroy, id: event_schedule.id, conference_id: conference.short_title + end + + expected.to change { EventSchedule.count }.by(-1) + end + it 'renders JSON without errors' do + delete :destroy, id: event_schedule.id, conference_id: conference.short_title + + expect(response).to be_success + expect(JSON.parse(response.body)['status']).to eq('ok') + end + end + end +end diff --git a/spec/controllers/admin/schedules_controller_spec.rb b/spec/controllers/admin/schedules_controller_spec.rb new file mode 100644 index 00000000..143ba042 --- /dev/null +++ b/spec/controllers/admin/schedules_controller_spec.rb @@ -0,0 +1,63 @@ +require 'spec_helper' + +describe Admin::SchedulesController do + let(:conference) { create(:conference) } + let(:schedule) { create(:schedule, program: conference.program)} + let!(:organizer_role) { Role.find_by(name: 'organizer', resource: conference) } + let(:organizer) { create(:user, role_ids: organizer_role.id) } + + context 'logged in as an organizer' do + before :each do + sign_in(organizer) + schedule + end + + describe 'GET #index' do + it 'renders the index template' do + get :index, conference_id: conference.short_title + expect(response).to render_template :index + end + end + + describe 'POST #create' do + it 'saves the schedule to the database' do + expected = expect do + post :create, conference_id: conference.short_title + end + expected.to change { Schedule.count }.by 1 + end + + it 'redirects to schedules#show' do + post :create, conference_id: conference.short_title + + expect(response).to redirect_to admin_conference_schedule_path( + conference.short_title, assigns[:schedule]) + end + end + + describe 'GET #show' do + it 'assigns the requested schedule to schedule' do + get :show, id: schedule.id, conference_id: conference.short_title + expect(assigns(:schedule)).to eq schedule + end + + it 'renders the show template' do + get :show, id: schedule.id, conference_id: conference.short_title + expect(response).to render_template :show + end + end + + describe 'DELETE #destroy' do + it 'deletes the schedule' do + expected = expect do + delete :destroy, id: schedule.id, conference_id: conference.short_title + end + expected.to change { Schedule.count }.by(-1) + end + it 'redirects to schedules#index' do + delete :destroy, id: schedule.id, conference_id: conference.short_title + expect(response).to redirect_to admin_conference_schedules_path(conference.short_title) + end + end + end +end