diff --git a/bin/rails b/bin/rails index 8a217f6a..8b245672 100755 --- a/bin/rails +++ b/bin/rails @@ -1,10 +1,4 @@ #!/usr/bin/env ruby -if ENV["RAILS_ENV"] == "test" - require "simplecov" - SimpleCov.start "rails" - puts "required simplecov" -end - APP_PATH = File.expand_path("../../config/application", __FILE__) require_relative "../config/boot" require "rails/commands" diff --git a/spec/controllers/admin_controller_spec.rb b/spec/controllers/admin_controller_spec.rb index a5306f66..ce01c551 100644 --- a/spec/controllers/admin_controller_spec.rb +++ b/spec/controllers/admin_controller_spec.rb @@ -4,7 +4,7 @@ include AuthHelper describe "as an admin user" do - let(:an_application) { create(:application) } + let!(:an_application) { create(:application) } describe "GET applications" do it "allows user to view admin applications index" do @@ -15,31 +15,79 @@ end describe "PATCH approve" do - describe "with good params" do - let(:application_params) { {application: {id: an_application.id}} } + subject { patch :approve, params: {application: {id: an_application.id}} } - before do - # Shortcircuit actually mailing the approval email - allow(ApplicationsMailer).to receive_message_chain(:approved, :deliver_now) - end + before(:each) do + # Shortcircuit actually mailing the approval email + allow(ApplicationsMailer).to receive_message_chain(:approved, :deliver_now) + + login_as(:voting_member, is_admin: true) + end + describe "with valid params" do it "should approve the relevant application" do - login_as(:voting_member, is_admin: true) expect { - patch :approve, params: application_params + subject }.to change { an_application.reload.state }.from("submitted").to("approved") end + + it "sends a flash message" do + subject + expect(flash[:message]).to be_present + end + end + + describe "with invalid params" do + before(:each) do + allow_any_instance_of(Application).to receive(:save).and_return(false) + end + + it "renders applications" do + subject + expect(response).to render_template :applications + end + + it "sends a flash error" do + subject + expect(flash[:error]).to be_present + end end end describe "PATCH reject" do - let(:application_params) { {application: {id: an_application.id}} } + subject { patch :reject, params: {application: {id: an_application.id}} } - it "should reject the relevant application" do + before(:each) do login_as(:voting_member, is_admin: true) - expect { - patch :reject, params: application_params - }.to change { an_application.reload.state }.from("submitted").to("rejected") + end + + describe "with valid params" do + it "should reject the relevant application" do + expect { + subject + }.to change { an_application.reload.state }.from("submitted").to("rejected") + end + + it "sends a flash message" do + subject + expect(flash[:message]).to be_present + end + end + + describe "with invalid params" do + before(:each) do + allow_any_instance_of(Application).to receive(:save).and_return(false) + end + + it "renders applications" do + subject + expect(response).to render_template :applications + end + + it "sends a flash error" do + subject + expect(flash[:error]).to be_present + end end end @@ -53,18 +101,29 @@ describe "POST setup_complete" do let!(:member) { create(:member) } - let(:params) { {user: {id: member.id}} } - subject { post :setup_complete, params: params } + subject { post :setup_complete, params: {user: {id: member.id}} } - before do + before(:each) do member.update(email_for_google: "bananas@example.com") + login_as(:voting_member, is_admin: true) end - it "allows the admin to mark user setup as complete" do - login_as(:voting_member, is_admin: true) + context "with valid parameters" do + it "allows the admin to mark user setup as complete" do + expect { subject }.to change { member.reload.setup_complete }.from(nil).to(true) + end + end - expect { subject }.to change { member.reload.setup_complete }.from(nil).to(true) + context "with an invalid user" do + before(:each) do + allow_any_instance_of(User).to receive(:save).and_return(false) + end + + it "sends a flash message" do + subject + expect(flash[:message]).to be_present + end end end @@ -72,11 +131,29 @@ let!(:member) { create(:member) } let(:params) { {user: {id: member.id, membership_note: "beeeep"}} } - it "allows the admin to make notes on the new user" do + subject { post :save_membership_note, params: params } + + before(:each) do login_as(:voting_member, is_admin: true) - expect { - post :save_membership_note, params: params - }.to change { member.reload.membership_note }.from(nil).to("beeeep") + end + + context "with valid params" do + it "allows the admin to make notes on the new user" do + expect { + subject + }.to change { member.reload.membership_note }.from(nil).to("beeeep") + end + end + + context "with an invalid user" do + before(:each) do + allow_any_instance_of(User).to receive(:save).and_return(false) + end + + it "sends a flash message" do + subject + expect(flash[:notice]).to be_present + end end end diff --git a/spec/controllers/members/comments_controller_spec.rb b/spec/controllers/members/comments_controller_spec.rb new file mode 100644 index 00000000..7c0962b9 --- /dev/null +++ b/spec/controllers/members/comments_controller_spec.rb @@ -0,0 +1,66 @@ +require "spec_helper" + +describe Members::CommentsController do + include AuthHelper + + let(:member) { create :member } + let(:application) { create :application } + + describe "GET index" do + subject { get :index } + + it_should_behave_like "deny non-members", [:visitor, :applicant] + it_should_behave_like "allow members", [:member, :voting_member] + end + + describe "POST create" do + let(:params) { + { + user_id: member.id, + application_id: application.id + } + } + + subject { post :create, params: params } + + it_should_behave_like "deny non-members", [:visitor, :applicant] + + context "when logged in as a member" do + before do + login_as(member) + end + + context "with a valid comment" do + let(:params) { + { + comment: {application_id: application.id, body: "test body"}, + application_id: application.id, + user_id: member.id + } + } + + it "creates a comment" do + subject + expect(Comment.last.application_id).to eq application.id + expect(Comment.last.user_id).to eq member.id + expect(Comment.last.body).to eq "test body" + end + end + + context "with an invalid comment" do + let(:params) { + { + comment: {application_id: application.id, body: nil}, + application_id: application.id, + user_id: member.id + } + } + + it "does not create a comment" do + subject + expect(Comment.last).to be_nil + end + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 933f9137..69ecd311 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,6 @@ # This file is copied to spec/ when you run 'rails generate rspec:install' require "simplecov" -SimpleCov.start +SimpleCov.start 'rails' ENV["RAILS_ENV"] ||= "test" require File.expand_path("../../config/environment", __FILE__)