Skip to content

Commit

Permalink
Add two more tests and error message if no orders are found
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Altmann committed Nov 23, 2015
1 parent ed99f9b commit 3e02d52
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
8 changes: 7 additions & 1 deletion app/controllers/line_item_groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ def download
authorize LineItemGroup
set_time_range
export_orders = ExportOrders.new(current_user, @time_range)
send_data(export_orders.csv_data, filename: export_orders.csv_filename, type: 'text/csv')
csv_data = export_orders.csv_data
if csv_data.empty?
flash[:error] = 'Im vom Dir angegebenen Zeitraum liegen keine Bestellungen.'
redirect_to user_path(current_user)
else
send_data(csv_data, filename: export_orders.csv_filename, type: 'text/csv')
end
rescue ArgumentError
flash[:error] = 'Date is incorrect'
redirect_to user_path(current_user)
Expand Down
15 changes: 14 additions & 1 deletion test/controllers/line_item_groups_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
describe "GET 'download'" do
it 'should return 200 if user is logged in' do
sign_in seller
csv_data = stub(empty?: false)
export_orders = mock()
export_orders.expects(:csv_data).at_least_once
export_orders.expects(:csv_data).once.returns(csv_data)
export_orders.expects(:csv_filename).once
ExportOrders.stubs(:new).returns(export_orders)

Expand All @@ -38,6 +39,18 @@
assert_response :success
end

it 'should return an error if no orders can be found' do
sign_in seller
csv_data = stub(empty?: true)
export_orders = stub(csv_data: csv_data)
ExportOrders.stubs(:new).returns(export_orders)

get :download, export_orders_from: '2015-09-22', export_orders_till: '2015-11-22'

assert_redirected_to user_path(seller)
flash[:error].must_equal 'Im vom Dir angegebenen Zeitraum liegen keine Bestellungen.'
end

it 'should return an error if the start or end date is incorrect' do
sign_in seller
get :download, export_orders_from: '2015-09-22', export_orders_till: nil
Expand Down
8 changes: 7 additions & 1 deletion test/features/export_orders_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
include Warden::Test::Helpers

feature 'Export orders' do
let(:user) { FactoryGirl.create :private_user }
let(:bt) { FactoryGirl.create :business_transaction }
let(:user) { bt.seller }

scenario 'User visits his profile and sees the export orders form' do
login_as user
Expand All @@ -20,4 +21,9 @@
page.status_code.must_equal 200
page.response_headers['Content-Type'].must_equal 'text/csv'
end

scenario 'Guest visits profile and does not see the export orders form' do
visit user_path(user)
page.wont_have_selector('#export_orders_from')
end
end

0 comments on commit 3e02d52

Please sign in to comment.