Skip to content

Commit

Permalink
Merge pull request #18 from FireflyArtsCollective/fix-admin-controlle…
Browse files Browse the repository at this point in the history
…r-specs

Fix admin controller specs
  • Loading branch information
Katee authored Apr 2, 2017
2 parents a384872 + 34833d6 commit 1061b70
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/controllers/admins_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ def create

if @admin.save
# Only assign the session to the new account if it's the first one.
flash[:success] = "New admin <#{@admin.email}> created."

unless session[:admin_id].present?
session[:admin_id] = @admin.id
redirect_to root_path
return
end

flash[:success] = "New admin <#{@admin.email}> created."

# reset @admin so form is empty
@admin = Admin.new
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Admin < ActiveRecord::Base
private

def normalize_email
self.email = email.downcase.strip
self.email = email&.downcase&.strip
end

def activate
Expand Down
27 changes: 13 additions & 14 deletions spec/controllers/admins_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ def go!
end

context 'with valid params' do
let(:admin_params) { FactoryGirl.attributes_for(:admin) }

it 'creates Admin' do
expect { go! }.to change { Admin.count }.by(1)
expect(response).to redirect_to(root_path)
it 'is ok' do
go!
expect(response).to render_template('index')
expect(response).to be_ok
end

context 'when an Admin already exists' do
Expand All @@ -24,10 +23,10 @@ def go!
sign_in admin
end

it 'creates Admin' do
expect { go! }.to change { Admin.count }.by(1)
expect(response).to render_template('new')
expect(flash.keys).to include('success')
it 'is ok' do
go!
expect(response).to render_template('index')
expect(response).to be_ok
end
end
end
Expand All @@ -53,13 +52,14 @@ def go!

it 'creates Admin' do
expect { go! }.to change { Admin.count }.by(1)
expect(flash[:success]).to be_present
expect(response).to redirect_to(root_path)
end

context 'when an Admin already exists' do
let!(:admin) { FactoryGirl.create(:admin) }

it { go!; is_expected.to be_forbidden}
it { go!; is_expected.to be_forbidden }

context 'when admin signed in' do
before do
Expand All @@ -75,8 +75,7 @@ def go!
end

context 'with invalid params' do
let!(:existing_admin) { FactoryGirl.create(:admin) }
let(:admin_params) { FactoryGirl.attributes_for(:admin, email: existing_admin.email) }
let(:admin_params) { FactoryGirl.attributes_for(:admin, email: nil) }

it 'displays form' do
expect { go! }.not_to change { Admin.count }
Expand All @@ -91,9 +90,9 @@ def go!
end

context 'logged out' do
it 'redirects' do
it 'is forbidden' do
go!
expect(response).to redirect_to('/')
expect(response).to be_forbidden
end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/models/admin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@
expect { subject.save! }.to change { subject.activated }
expect(subject.activated).to eq(true)
end

context 'nil email' do
subject { FactoryGirl.build(:admin, email: nil) }
its(:valid?) { is_expected.to eq(false) }
end
end
end

0 comments on commit 1061b70

Please sign in to comment.