Add home controller test

Co-authored-by: viehlieb <pf@pragma-shift.net>
Co-authored-by: Tobias Kneuker <tk@pragma-shift.net>
This commit is contained in:
Philipp Rothmann 2022-12-05 10:13:02 +01:00
parent 3f114af193
commit d16aa19300
6 changed files with 236 additions and 4 deletions

View file

@ -115,6 +115,7 @@ group :test do
gem 'rspec-core' gem 'rspec-core'
gem 'rspec-rerun' gem 'rspec-rerun'
gem 'i18n-spec' gem 'i18n-spec'
gem 'rails-controller-testing'
# code coverage # code coverage
gem 'simplecov', require: false gem 'simplecov', require: false
gem 'simplecov-lcov', require: false gem 'simplecov-lcov', require: false

View file

@ -343,6 +343,10 @@ GEM
sprockets-rails (>= 2.0.0) sprockets-rails (>= 2.0.0)
rails-assets-listjs (0.2.0.beta.4) rails-assets-listjs (0.2.0.beta.4)
railties (>= 3.1) railties (>= 3.1)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
actionview (>= 5.0.1.rc1)
activesupport (>= 5.0.1.rc1)
rails-dom-testing (2.0.3) rails-dom-testing (2.0.3)
activesupport (>= 4.2.0) activesupport (>= 4.2.0)
nokogiri (>= 1.6) nokogiri (>= 1.6)
@ -608,6 +612,7 @@ DEPENDENCIES
rack-cors rack-cors
rails (~> 5.2) rails (~> 5.2)
rails-assets-listjs (= 0.2.0.beta.4) rails-assets-listjs (= 0.2.0.beta.4)
rails-controller-testing
rails-i18n rails-i18n
rails-settings-cached (= 0.4.3) rails-settings-cached (= 0.4.3)
rails_tokeninput rails_tokeninput

View file

@ -18,7 +18,7 @@ class HomeController < ApplicationController
@bank_accounts = @types.includes(:bank_account).map(&:bank_account).uniq.compact @bank_accounts = @types.includes(:bank_account).map(&:bank_account).uniq.compact
@bank_accounts = [BankAccount.last] if @bank_accounts.empty? @bank_accounts = [BankAccount.last] if @bank_accounts.empty?
else else
redirect_to root_url, alert: I18n.t('group_orders.errors.no_member') redirect_to root_path, alert: I18n.t('group_orders.errors.no_member')
end end
end end
@ -26,7 +26,7 @@ class HomeController < ApplicationController
if @current_user.update(user_params) if @current_user.update(user_params)
@current_user.ordergroup.update(ordergroup_params) if ordergroup_params @current_user.ordergroup.update(ordergroup_params) if ordergroup_params
session[:locale] = @current_user.locale session[:locale] = @current_user.locale
redirect_to my_profile_url, notice: I18n.t('home.changes_saved') redirect_to my_profile_path, notice: I18n.t('home.changes_saved')
else else
render :profile render :profile
end end
@ -64,7 +64,7 @@ class HomeController < ApplicationController
# cancel personal memberships direct from the myProfile-page # cancel personal memberships direct from the myProfile-page
def cancel_membership def cancel_membership
if params[:membership_id] if params[:membership_id]
membership = @current_user.memberships.find!(params[:membership_id]) membership = @current_user.memberships.find(params[:membership_id])
else else
membership = @current_user.memberships.find_by_group_id!(params[:group_id]) membership = @current_user.memberships.find_by_group_id!(params[:group_id])
end end

View file

@ -0,0 +1,199 @@
# 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 'logegd in' do
before { login user }
it 'assigns tasks' do
get_with_defaults :index
expect(assigns(:unaccepted_tasks)).not_to be_nil
expect(assigns(:next_tasks)).not_to be_nil
expect(assigns(:unassigned_tasks)).not_to be_nil
expect(response).to render_template('home/index')
end
end
end
describe 'GET profile' do
before { login user }
it 'renders dashboard' do
get_with_defaults :profile
expect(response).to have_http_status(:success)
expect(response).to render_template('home/profile')
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 'renders reference calculator' do
get_with_defaults :reference_calculator
expect(response).to have_http_status(:success)
expect(response).to render_template('home/reference_calculator')
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 'renders profile after update with invalid attributes' do
get_with_defaults :update_profile, params: { user: invalid_attributes }
expect(response).to have_http_status(:success)
expect(response).to render_template('home/profile')
expect(assigns(:current_user).errors.present?).to be true
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 'renders ordergroup' do
get_with_defaults :ordergroup
expect(response).to have_http_status(:success)
expect(response).to render_template('home/ordergroup')
end
describe 'assigns sortings' do
let(:fin_trans1) { create :financial_transaction, user: og_user, ordergroup: og_user.ordergroup, note: 'A', amount: 200, created_on: Time.now }
let(:fin_trans2) { create :financial_transaction, user: og_user, ordergroup: og_user.ordergroup, note: 'B', amount: 100, created_on: Time.now + 2.minutes }
let(:fin_trans3) { create :financial_transaction, user: og_user, ordergroup: og_user.ordergroup, note: 'C', amount: 50, created_on: Time.now + 1.minute }
before do
fin_trans1
fin_trans2
fin_trans3
end
it 'by criteria' do
sortings = [
['date', [fin_trans1, fin_trans3, fin_trans2]],
['note', [fin_trans1, fin_trans2, fin_trans3]],
['amount', [fin_trans3, fin_trans2, fin_trans1]],
['date_reverse', [fin_trans2, fin_trans3, fin_trans1]],
['note_reverse', [fin_trans3, fin_trans2, fin_trans1]],
['amount_reverse', [fin_trans1, fin_trans2, fin_trans3]]
]
sortings.each do |sorting|
get_with_defaults :ordergroup, params: { sort: sorting[0] }
expect(response).to have_http_status(:success)
expect(assigns(:financial_transactions).to_a).to eq(sorting[1])
end
end
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

View file

@ -21,6 +21,10 @@ Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config| RSpec.configure do |config|
# We use capybara with webkit, and need database_cleaner # We use capybara with webkit, and need database_cleaner
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do config.before(:each) do
DatabaseCleaner.strategy = (RSpec.current_example.metadata[:js] ? :truncation : :transaction) DatabaseCleaner.strategy = (RSpec.current_example.metadata[:js] ? :truncation : :transaction)
DatabaseCleaner.start DatabaseCleaner.start
@ -51,8 +55,8 @@ RSpec.configure do |config|
# --seed 1234 # --seed 1234
config.order = "random" config.order = "random"
config.include SpecTestHelper, type: :controller
config.include SessionHelper, type: :feature config.include SessionHelper, type: :feature
# Automatically determine spec from directory structure, see: # Automatically determine spec from directory structure, see:
# https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/directory-structure # https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/directory-structure
config.infer_spec_type_from_file_location! config.infer_spec_type_from_file_location!

View file

@ -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