Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage #776

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions bin/rails
Original file line number Diff line number Diff line change
@@ -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"
125 changes: 101 additions & 24 deletions spec/controllers/admin_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -53,30 +101,59 @@

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

describe "POST add_membership_note" do
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

Expand Down
66 changes: 66 additions & 0 deletions spec/controllers/members/comments_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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__)
Expand Down