From d010a74e36bba47e45a7532ea3896199543e27da Mon Sep 17 00:00:00 2001 From: viehlieb Date: Thu, 6 Oct 2022 12:42:58 +0200 Subject: [PATCH] article_controller wip and balancing_controller spec'd --- .../finance/balancing_controller.rb | 4 +- spec/controllers/articles_controller_spec.rb | 37 +++ .../finance/balancing_controller_spec.rb | 212 ++++++++++++++++++ 3 files changed, 251 insertions(+), 2 deletions(-) create mode 100644 spec/controllers/articles_controller_spec.rb create mode 100644 spec/controllers/finance/balancing_controller_spec.rb diff --git a/app/controllers/finance/balancing_controller.rb b/app/controllers/finance/balancing_controller.rb index 09c109f8..2dca6f71 100644 --- a/app/controllers/finance/balancing_controller.rb +++ b/app/controllers/finance/balancing_controller.rb @@ -22,7 +22,7 @@ class Finance::BalancingController < Finance::BaseController when 'order_number_reverse' then @articles.order('articles.order_number DESC') else - @articles + @articles # TODO: We will never get here end render layout: false if request.xhr? @@ -105,6 +105,6 @@ class Finance::BalancingController < Finance::BaseController end redirect_to finance_order_index_url, notice: t('finance.balancing.close_all_direct_with_invoice.notice', count: count) rescue => error - redirect_to finance_order_index_url, alert: t('errors.general_msg', msg: error.message) + redirect_to finance_order_index_url, alert: t('errors.general_msg', msg: error.message) #TODO: this can't be reached end end diff --git a/spec/controllers/articles_controller_spec.rb b/spec/controllers/articles_controller_spec.rb new file mode 100644 index 00000000..79d8f0ac --- /dev/null +++ b/spec/controllers/articles_controller_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe ArticlesController, type: :controller do + let(:user) { create :user, :role_article_meta } + + before { login user } + + describe 'GET index' do + let(:article_categoryA) { create :article_category, name: "AAAA" } + let(:article_categoryB) { create :article_category, name: "BBBB" } + let(:articleA) { create :article, name: 'AAAA', note: "AAAA", unit: '250 g', article_category: article_categoryA, availability: false } + let(:articleB) { create :article, name: 'BBBB', note: "BBBB", unit: '500 g', article_category: article_categoryB, availability: true } + let(:supplier) { create :supplier, articles: [articleA, articleB] } + + it 'assigns sorting on articles' do + sortings = [ + ['name', [articleA, articleB]], + ['name_reverse', [articleB, articleA]], + ['unit', [articleA, articleB]], + ['unit_reverse', [articleB, articleA]], + ['article_category', [articleA, articleB]], + ['article_category_reverse', [articleB, articleA]], + ['note', [articleA, articleB]], + ['note_reverse', [articleB, articleA]], + ['availability', [articleA, articleB]], + ['availability_reverse', [articleB, articleA]] + ] + sortings.each do |sorting| + get :index, params: { foodcoop: FoodsoftConfig[:default_scope], supplier_id: supplier.id, sort: sorting[0] } + expect(response).to have_http_status(:success) + expect(assigns(:articles).to_a).to eq(sorting[1]) + end + end + end +end diff --git a/spec/controllers/finance/balancing_controller_spec.rb b/spec/controllers/finance/balancing_controller_spec.rb new file mode 100644 index 00000000..dbeb262d --- /dev/null +++ b/spec/controllers/finance/balancing_controller_spec.rb @@ -0,0 +1,212 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Finance::BalancingController, type: :controller do + let(:user) { create :user, :role_finance, :role_orders, groups: [create(:ordergroup)] } + + before { login user } + + describe 'GET index' do + let(:order) { create :order } + + it 'renders index page' do + get :index, params: { foodcoop: FoodsoftConfig[:default_scope] } + expect(response).to have_http_status(:success) + expect(response).to render_template('finance/balancing/index') + end + end + + describe 'new balancing' do + let(:supplier) { create :supplier } + let(:article1) { create :article, name: "AAAA", supplier: supplier, unit_quantity: 1 } + let(:article2) { create :article, name: "AAAB", supplier: supplier, unit_quantity: 1 } + + let(:order) { create :order, supplier: supplier, article_ids: [article1.id, article2.id] } + + let(:go1) { create :group_order, order: order } + let(:go2) { create :group_order, order: order } + let(:oa1) { order.order_articles.find_by_article_id(article1.id) } + let(:oa2) { order.order_articles.find_by_article_id(article2.id) } + let(:oa3) { order2.order_articles.find_by_article_id(article2.id) } + let(:goa1) { create :group_order_article, group_order: go1, order_article: oa1 } + let(:goa2) { create :group_order_article, group_order: go1, order_article: oa2 } + + before do + goa1.update_quantities(3, 0) + goa2.update_quantities(1, 0) + oa1.update_results! + oa2.update_results! + end + + it 'renders new order page' do + get :new, params: { foodcoop: FoodsoftConfig[:default_scope], order_id: order.id } + expect(response).to have_http_status(:success) + expect(response).to render_template('finance/balancing/new') + end + + it 'assigns sorting on articles' do + sortings = [ + ['name', [oa1, oa2]], + ['name_reverse', [oa2, oa1]], + ['order_number', [oa1, oa2]], + ['order_number_reverse', [oa1, oa2]] # just one order + ] + sortings.each do |sorting| + get :new, params: { foodcoop: FoodsoftConfig[:default_scope], order_id: order.id, sort: sorting[0] } + expect(response).to have_http_status(:success) + expect(assigns(:articles).to_a).to eq(sorting[1]) + end + end + end + + describe 'update summary' do + let(:order) { create(:order) } + + it 'shows the summary view' do + get :update_summary, params: { foodcoop: FoodsoftConfig[:default_scope], id: order.id }, xhr: true + expect(response).to have_http_status(:success) + expect(response).to render_template('finance/balancing/update_summary') + end + end + + describe 'new_on_order' do + let(:order) { create(:order) } + let(:order_article) { order.order_articles.first } + + # TODO: how to check for js.erb calls? + it 'calls article update' do + get :new_on_order_article_update, params: { foodcoop: FoodsoftConfig[:default_scope], id: order.id, order_article_id: order_article.id }, xhr: true + expect(response).not_to render_template(layout: "application") + expect(response).to render_template('finance/balancing/new_on_order_article_update') + end + + it 'calls article create' do + get :new_on_order_article_create, params: { foodcoop: FoodsoftConfig[:default_scope], id: order.id, order_article_id: order_article.id }, xhr: true + expect(response).not_to render_template(layout: "application") + expect(response).to render_template('finance/balancing/new_on_order_article_create') + end + end + + describe 'edit_note' do + let(:order) { create(:order) } + + it 'updates order note' do + get :edit_note, params: { foodcoop: FoodsoftConfig[:default_scope], id: order.id, order: { note: "Hello" } }, xhr: true + expect(response).to have_http_status(:success) + expect(response).to render_template('finance/balancing/edit_note') + end + end + + describe 'update_note' do + let(:order) { create(:order) } + + it 'updates order note' do + get :update_note, params: { foodcoop: FoodsoftConfig[:default_scope], id: order.id, order: { note: "Hello" } }, xhr: true + expect(response).to have_http_status(:success) + end + + it 'redirects to edit note on failed update' do + get :update_note, params: { foodcoop: FoodsoftConfig[:default_scope], id: order.id, order: { article_ids: nil } }, xhr: true + expect(response).to have_http_status(:success) + expect(response).to render_template('finance/balancing/edit_note') + end + end + + describe 'transport' do + let(:order) { create(:order) } + + it 'calls the edit transport view' do + get :edit_transport, params: { foodcoop: FoodsoftConfig[:default_scope], id: order.id }, xhr: true + expect(response).to have_http_status(:success) + expect(response).to render_template('finance/balancing/edit_transport') + end + + it 'does redirect if order valid' do + get :update_transport, params: { foodcoop: FoodsoftConfig[:default_scope], id: order.id, order: { ends: Time.now } }, xhr: true + expect(response).to have_http_status(:redirect) + expect(assigns(:order).errors.count).to eq(0) + expect(response).to redirect_to(new_finance_order_path(order_id: order.id)) + end + + it 'does redirect if order invalid' do + get :update_transport, params: { foodcoop: FoodsoftConfig[:default_scope], id: order.id, order: { starts: Time.now + 2, ends: Time.now } }, xhr: true + expect(assigns(:order).errors.count).to eq(1) + expect(response).to have_http_status(:redirect) + expect(response).to redirect_to(new_finance_order_path(order_id: order.id)) + end + end + + describe 'confirm' do + let(:order) { create(:order) } + + it 'renders the confirm template' do + get :confirm, params: { foodcoop: FoodsoftConfig[:default_scope], id: order.id }, xhr: true + expect(response).to have_http_status(:success) + expect(response).to render_template('finance/balancing/confirm') + end + end + + describe 'close and update account balances' do + let(:order) { create(:order) } + let(:order1) { create(:order, ends: Time.now) } + let(:fft) { create(:financial_transaction_type) } + + it 'does not close order if ends not set' do + get :close, params: { foodcoop: FoodsoftConfig[:default_scope], id: order.id, type: fft.id } + expect(assigns(:order).closed?).to be_falsey + expect(response).to have_http_status(:redirect) + expect(response).to redirect_to(new_finance_order_url(order_id: order.id)) + end + + it 'closes order' do + get :close, params: { foodcoop: FoodsoftConfig[:default_scope], id: order1.id, type: fft.id } + expect(assigns(:order).closed?).to be_truthy + expect(response).to have_http_status(:redirect) + expect(response).to redirect_to(finance_order_index_url) + end + end + + describe 'close direct' do + let(:order) { create(:order) } + + it 'does not close order if already closed' do + order.close_direct!(user) + get :close_direct, params: { foodcoop: FoodsoftConfig[:default_scope], id: order.id } + expect(assigns(:order).closed?).to be_truthy + end + + it 'closes order directly' do + get :close_direct, params: { foodcoop: FoodsoftConfig[:default_scope], id: order.id } + expect(assigns(:order).closed?).to be_truthy + end + end + + describe 'close all direct' do + let(:invoice) { create(:invoice) } + let(:invoice1) { create(:invoice) } + let(:order) { create(:order, state: 'finished', ends: Time.now + 2.hours, invoice: invoice) } + let(:order1) { create(:order, state: 'finished', ends: Time.now + 2.hours) } + + before do + order + order1 + end + + it 'does close orders' do + get :close_all_direct_with_invoice, params: { foodcoop: FoodsoftConfig[:default_scope] } + order.reload + expect(order.closed?).to be_truthy + expect(response).to have_http_status(:redirect) + expect(response).to redirect_to(finance_order_index_url) + end + + it 'does not close orders when invoice not set' do + get :close_all_direct_with_invoice, params: { foodcoop: FoodsoftConfig[:default_scope] } + order1.reload + expect(order1.closed?).to be_falsey + expect(response).to have_http_status(:redirect) + expect(response).to redirect_to(finance_order_index_url) + end + end +end