From 71eb7dc9c9fa116702f77e85b1a03caf2e44f3cf Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Wed, 27 Mar 2024 13:51:44 -0700 Subject: [PATCH] Adding a two more tests to EventsController --- app/controllers/events_controller.rb | 58 ++++++++--- app/views/layouts/application.html.haml | 8 +- lib/extensions/time.rb | 4 +- spec/controllers/events_controller_spec.rb | 112 +++++++++++++++++++++ 4 files changed, 163 insertions(+), 19 deletions(-) diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 9f37c349..55ef6627 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -31,9 +31,8 @@ def edit end def create - populate_permitted_params_event(permitted_params) - - @event = Event.new(permitted_params[:event]) + create_params = populate_permitted_params_event(converted_params[:event]) + @event = Event.new(create_params) if @event.save redirect_to @event @@ -43,9 +42,8 @@ def create end def update - populate_permitted_params_event(permitted_params) - - if @event.update(permitted_params[:event]) + update_params = populate_permitted_params_event(converted_params[:event]) + if @event.update(update_params) redirect_to @event else render action: 'edit' @@ -111,12 +109,17 @@ def download_guest_list private - def populate_permitted_params_event(permitted_params) - permitted_params[:event][:start_time] = Time.from_picker(permitted_params.delete(:start_time)) - permitted_params[:event][:end_time] = Time.from_picker(permitted_params.delete(:end_time)) - permitted_params[:event][:ticket_sales_start_time] = Time.from_picker(permitted_params.delete(:ticket_sales_start_time)) - permitted_params[:event][:ticket_sales_end_time] = Time.from_picker(permitted_params.delete(:ticket_sales_end_time)) - permitted_params[:event][:ticket_requests_end_time] = Time.from_picker(permitted_params.delete(:ticket_requests_end_time)) + def converted_params + @converted_params ||= permitted_params.to_h.tap(&:symbolize_keys!) + end + + def populate_permitted_params_event(permitted_parameters) + permitted_parameters[:start_time] = Time.from_picker(permitted_parameters.delete(:start_time)) + permitted_parameters[:end_time] = Time.from_picker(permitted_parameters.delete(:end_time)) + permitted_parameters[:ticket_sales_start_time] = Time.from_picker(permitted_parameters.delete(:ticket_sales_start_time)) + permitted_parameters[:ticket_sales_end_time] = Time.from_picker(permitted_parameters.delete(:ticket_sales_end_time)) + permitted_parameters[:ticket_requests_end_time] = Time.from_picker(permitted_parameters.delete(:ticket_requests_end_time)) + permitted_parameters end def completed_ticket_requests @@ -133,6 +136,35 @@ def set_event end def permitted_params - params.permit(:id, :event, :user_email, :user_id, :start_time, :end_time, :ticket_sales_start_time, :ticket_sales_end_time, :ticket_requests_end_time) + params.permit( + :id, + :user_email, + :user_id, + :start_time, + :end_time, + :ticket_sales_start_time, + :ticket_sales_end_time, + :ticket_requests_end_time, + event: %i[ + adult_ticket_price + allow_donations + allow_financial_assistance + cabin_price + early_arrival_price + end_time + kid_ticket_price + late_departure_price + max_adult_tickets_per_request + max_cabin_requests + max_cabins_per_request + max_kid_tickets_per_request + name + require_mailing_address + start_time + tickets_require_approval + ] + ) + .to_hash + .with_indifferent_access end end diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index c8b75101..5aa081c1 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -18,7 +18,7 @@ = yield :head %body - .navbar.navbar-expand-lg.navbar-dark.bg-primary + .navbar.navbar-expand-md.navbar-dark.bg-primary .container-fluid %a.navbar-brand{ href: root_path } .h2 @@ -27,14 +27,14 @@ %span.navbar-toggler-icon .collapse.navbar-collapse#navbarSupportedContent - %ul.navbar-nav.me-auto.mb-2.mb-lg-0.justify-content-center.navbar-nav-scroll{style: "--bs-scroll-height: 100px;" } + %ul.navbar-nav.justify-content-center - if user_signed_in? %li.nav-item %a.nav-link{ href: edit_user_registration_path } Edit Profile %li.nav-item = link_to destroy_user_session_path, method: :delete, class: 'nav-link' do - Logouts + Log Out - else %li.nav-item.d-flex @@ -46,8 +46,6 @@ %input.form-control.me-2{"aria-label" => "Search", :placeholder => "Search", :type => "search"}/ %button.btn.btn-info{:type => "submit", onclick: 'document.alert("Not yet implemented")'} Search - - .container = bootstrap_flash = yield diff --git a/lib/extensions/time.rb b/lib/extensions/time.rb index 7a65b2e1..d9f72ec1 100644 --- a/lib/extensions/time.rb +++ b/lib/extensions/time.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class Time + TIME_FORMAT = '%Y/%m/%d %H:%M:%S' + DATE_FORMATS.merge!( friendly: '%A, %B %-d, %Y %l:%M %p %Z', month_day: '%B %-d', @@ -9,6 +11,6 @@ class Time ) def self.from_picker(datetimepicker_string) - strptime(datetimepicker_string, '%Y/%m/%d %H:%M:%S').to_time if datetimepicker_string.present? + strptime(datetimepicker_string, TIME_FORMAT).to_time if datetimepicker_string.present? end end diff --git a/spec/controllers/events_controller_spec.rb b/spec/controllers/events_controller_spec.rb index 67df77eb..9edd9267 100644 --- a/spec/controllers/events_controller_spec.rb +++ b/spec/controllers/events_controller_spec.rb @@ -29,4 +29,116 @@ end end end + + describe 'GET #new' do + before { get :new } + + context 'when the user is not signed in' do + it { redirects_to new_user_session_path } + end + + context 'when the user is signed in' do + context 'and is not a site admin' do + let(:viewer) { create(:user) } + + it { succeeds } + end + + context 'and is a site admin' do + let(:viewer) { create(:site_admin).user } + + it { succeeds } + end + end + end + + describe 'POST #create' do + let(:new_event) { build(:event) } + let(:new_event_params) do + new_event.as_json.tap do |h| + h[:start_time] = new_event.start_time.strftime(Time::TIME_FORMAT) + h[:end_time] = new_event.end_time.strftime(Time::TIME_FORMAT) + end + end + let(:make_request) { -> { post :create, params: { event: new_event_params } } } + + # rubocop: disable RSpec/AnyInstance + before do + allow_any_instance_of(described_class).to receive(:params).and_return(ActionController::Parameters.new(event: new_event_params)) + end + # rubocop: enable RSpec/AnyInstance + + describe '#permitted_params' do + subject(:permitted_keys) { permitted_event_keys } + + let(:permitted_event_params) { described_class.new.send(:permitted_params)[:event].to_h.symbolize_keys } + let(:expected_keys) do + %i[ + adult_ticket_price + allow_donations + allow_financial_assistance + cabin_price + early_arrival_price + end_time + kid_ticket_price + late_departure_price + max_adult_tickets_per_request + max_cabin_requests + max_cabins_per_request + max_kid_tickets_per_request + name + require_mailing_address + start_time + tickets_require_approval + ] + end + let(:permitted_event_keys) { permitted_event_params.keys.sort } + + it { expect(permitted_keys).to eql(expected_keys) } + end + + context 'when the user is not signed in' do + before { make_request[] } + + it { redirects_to new_user_session_path } + end + + context 'when the user is signed in' do + context 'and is not a site admin' do + let(:viewer) { create(:user) } + + before { make_request[] } + + it { redirects_to root_path } + end + + context 'and is a site admin' do + let(:viewer) { create(:site_admin).user } + + describe 'flash messages' do + before { make_request[] } + + its(:flash) { is_expected.to be_empty } + end + + describe 'newly created event' do + subject { Event.last } + + before { make_request[] } + + it { is_expected.to be_valid } + end + + describe 'event creation' do + subject(:response) { make_request[] } + + let(:created_event) { subject.instance_variable_get(:@event) } + + it { expect { response }.to change(Event, :count) } + it { is_expected.to have_http_status(:redirect) } + it { is_expected.to redirect_to(event_path(Event.last)) } + end + end + end + end end