Skip to content

Commit

Permalink
Add home controller test (#972)
Browse files Browse the repository at this point in the history
Co-authored-by: viehlieb <pf@pragma-shift.net>
Co-authored-by: Tobias Kneuker <tk@pragma-shift.net>
  • Loading branch information
3 people authored Mar 25, 2023
1 parent debce2a commit 503ed6c
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def ordergroup
# cancel personal memberships direct from the myProfile-page
def cancel_membership
if params[:membership_id]
membership = @current_user.memberships.find!(params[:membership_id])
membership = @current_user.memberships.find(params[:membership_id])
else
membership = @current_user.memberships.find_by_group_id!(params[:group_id])
end
Expand Down
162 changes: 162 additions & 0 deletions spec/controllers/home_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# frozen_string_literal: true

require 'spec_helper'

describe HomeController, type: :controller do
let(:user) { create :user }

describe 'GET index' do
describe 'NOT logged in' do
it 'redirects' do
get_with_defaults :profile
expect(response).to have_http_status(:redirect)
expect(response).to redirect_to(login_path)
end
end

describe 'logged in' do
before { login user }

it 'succeeds' do
get_with_defaults :index
expect(response).to have_http_status(:success)
end
end
end

describe 'GET profile' do
before { login user }

it 'succeeds' do
get_with_defaults :profile
expect(response).to have_http_status(:success)
end
end

describe 'GET reference_calculator' do
describe 'with simple user' do
before { login user }

it 'redirects to home' do
get_with_defaults :reference_calculator
expect(response).to have_http_status(:redirect)
expect(response).to redirect_to(root_path)
end
end

describe 'with ordergroup user' do
let(:og_user) { create :user, :ordergroup }

before { login og_user }

it 'succeeds' do
get_with_defaults :reference_calculator
expect(response).to have_http_status(:success)
end
end
end

describe 'GET update_profile' do
describe 'with simple user' do
let(:unchanged_attributes) { user.attributes.slice('first_name', 'last_name', 'email') }
let(:changed_attributes) { attributes_for :user }
let(:invalid_attributes) { { email: 'e.mail.com' } }

before { login user }

it 'stays on profile after update with invalid attributes' do
get_with_defaults :update_profile, params: { user: invalid_attributes }
expect(response).to have_http_status(:success)
end

it 'redirects to profile after update with unchanged attributes' do
get_with_defaults :update_profile, params: { user: unchanged_attributes }
expect(response).to have_http_status(:redirect)
expect(response).to redirect_to(my_profile_path)
end

it 'redirects to profile after update' do
patch :update_profile, params: { foodcoop: FoodsoftConfig[:default_scope], user: changed_attributes }
expect(response).to have_http_status(:redirect)
expect(response).to redirect_to(my_profile_path)
expect(flash[:notice]).to match(/#{I18n.t('home.changes_saved')}/)
expect(user.reload.attributes.slice(:first_name, :last_name, :email)).to eq(changed_attributes.slice('first_name', 'last_name', 'email'))
end
end

describe 'with ordergroup user' do
let(:og_user) { create :user, :ordergroup }
let(:unchanged_attributes) { og_user.attributes.slice('first_name', 'last_name', 'email') }
let(:changed_attributes) { unchanged_attributes.merge({ ordergroup: { contact_address: 'new Adress 7' } }) }

before { login og_user }

it 'redirects to home after update' do
get_with_defaults :update_profile, params: { user: changed_attributes }
expect(response).to have_http_status(:redirect)
expect(response).to redirect_to(my_profile_path)
expect(og_user.reload.ordergroup.contact_address).to eq('new Adress 7')
end
end
end

describe 'GET ordergroup' do
describe 'with simple user' do
before { login user }

it 'redirects to home' do
get_with_defaults :ordergroup
expect(response).to have_http_status(:redirect)
expect(response).to redirect_to(root_path)
end
end

describe 'with ordergroup user' do
let(:og_user) { create :user, :ordergroup }

before { login og_user }

it 'succeeds' do
get_with_defaults :ordergroup
expect(response).to have_http_status(:success)
end
end
end

describe 'GET cancel_membership' do
describe 'with simple user without group' do
before { login user }

it 'fails' do
expect do
get_with_defaults :cancel_membership
end.to raise_error(ActiveRecord::RecordNotFound)
expect do
get_with_defaults :cancel_membership, params: { membership_id: 424242 }
end.to raise_error(ActiveRecord::RecordNotFound)
end
end

describe 'with ordergroup user' do
let(:fin_user) { create :user, :role_finance }

before { login fin_user }

it 'removes user from group' do
membership = fin_user.memberships.first
get_with_defaults :cancel_membership, params: { group_id: fin_user.groups.first.id }
expect(response).to have_http_status(:redirect)
expect(response).to redirect_to(my_profile_path)
expect(flash[:notice]).to match(/#{I18n.t('home.ordergroup_cancelled', group: membership.group.name)}/)
end

it 'removes user membership' do
membership = fin_user.memberships.first
get_with_defaults :cancel_membership, params: { membership_id: membership.id }
expect(response).to have_http_status(:redirect)
expect(response).to redirect_to(my_profile_path)
expect(flash[:notice]).to match(/#{I18n.t('home.ordergroup_cancelled', group: membership.group.name)}/)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
# --seed 1234
config.order = "random"

config.include SpecTestHelper, type: :controller
config.include SessionHelper, type: :feature

# Automatically determine spec from directory structure, see:
# https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/directory-structure
config.infer_spec_type_from_file_location!
Expand Down
23 changes: 23 additions & 0 deletions spec/support/spec_test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module SpecTestHelper
def login(user)
user = User.find_by_nick(user.nick)
session[:user_id] = user.id
session[:scope] = FoodsoftConfig[:default_scope] # Save scope in session to not allow switching between foodcoops with one account
session[:locale] = user.locale
end

def current_user
User.find(session[:user_id])
end

def get_with_defaults(action, params: {}, xhr: false, format: nil)
params['foodcoop'] = FoodsoftConfig[:default_scope]
get action, params: params, xhr: xhr, format: format
end
end

RSpec.configure do |config|
config.include SpecTestHelper, type: :controller
end

0 comments on commit 503ed6c

Please sign in to comment.