Files
openfoodnetwork/spec/controllers/spree/users_controller_spec.rb
Pau Perez cc2e46890e Stop using deprecated req. helpers in users specs
This removes the following two deprecation warnings that we are getting
by millions (the two for each controller action test):

```
DEPRECATION WARNING: You are trying to generate the URL for a named route called "main_app" but no such route was found. In the future, this will result in an `ActionController::UrlGenerationError` exception. (called from process_action_with_route at /usr/
src/app/spec/support/controller_requests_helper.rb:49)
DEPRECATION WARNING: Passing the `use_route` option in functional tests are deprecated. Support for this option in the `process` method (and the related `get`, `head`, `post`, `patch`, `put` and `delete` helpers) will be removed in the next version without
 replacement. Functional tests are essentially unit tests for controllers and they should not require knowledge to how the application's routes are configured. Instead, you should explicitly pass the appropiate params to the `process` method. Previously th
e engines guide also contained an incorrect example that recommended using this option to test an engine's controllers within the dummy application. That recommendation was incorrect and has since been corrected. Instead, you should override the `@routes`
variable in the test case with `Foo::Engine.routes`. See the updated engines guide for details. (called from process_action_with_route at /usr/src/app/spec/support/controller_requests_helper.rb:49)
```

It slows down our test suite and clutters the output a lot. As per my
investigation, this is something that arose in
https://github.com/rails/rails/pull/17453 and addressed in
https://github.com/rails/rails/pull/17725. TL;DR: Engines need to define
their routes in controller tests as shown in
https://github.com/discourse/discourse/pull/3011.

This, however, revealed a much complex reality in our case. We're still
using a `Spree::Core::Engine` with its own routes at
`Spree::Core::Engine.routes`. So we can't skip defining `routes { }` for
each of its controllers unless we merge this engine into our app, but
that's going to require more effort. What could that entail in
https://github.com/openfoodfoundation/openfoodnetwork/compare/master...coopdevs:move-users-to-app-routes.

To make it even worse, note that we override spree's core routes from
our own, resulting in a controller whose actions are being served from
routes defined in either `config/routes.rb` or `config/spree/routes.rb`
🙈.
2021-01-22 11:42:17 +01:00

78 lines
2.7 KiB
Ruby

# frozen_string_literal: true
require 'spec_helper'
describe Spree::UsersController, type: :controller do
routes { Spree::Core::Engine.routes }
include AuthenticationHelper
describe "show" do
let!(:u1) { create(:user) }
let!(:u2) { create(:user) }
let!(:distributor1) { create(:distributor_enterprise) }
let!(:distributor2) { create(:distributor_enterprise) }
let!(:d1o1) { create(:completed_order_with_totals, distributor: distributor1, user_id: u1.id) }
let!(:d1o2) { create(:completed_order_with_totals, distributor: distributor1, user_id: u1.id) }
let!(:d1_order_for_u2) { create(:completed_order_with_totals, distributor: distributor1, user_id: u2.id) }
let!(:d1o3) { create(:order, state: 'cart', distributor: distributor1, user_id: u1.id) }
let!(:d2o1) { create(:completed_order_with_totals, distributor: distributor2, user_id: u2.id) }
let(:orders) { assigns(:orders) }
let(:shops) { Enterprise.where(id: orders.pluck(:distributor_id)) }
before do
allow(controller).to receive(:spree_current_user) { u1 }
end
it "returns orders placed by the user at normal shops" do
get :show
expect(orders).to include d1o1, d1o2
expect(orders).to_not include d1_order_for_u2, d1o3, d2o1
expect(shops).to include distributor1
# Doesn't return orders for irrelevant distributors" do
expect(orders).not_to include d2o1
expect(shops).not_to include distributor2
# Doesn't return other users' orders" do
expect(orders).not_to include d1_order_for_u2
# Doesn't return uncompleted orders" do
expect(orders).not_to include d1o3
end
end
describe "registered_email" do
routes { Openfoodnetwork::Application.routes }
let!(:user) { create(:user) }
it "returns true if email corresponds to a registered user" do
post :registered_email, email: user.email
expect(json_response['registered']).to eq true
end
it "returns false if email does not correspond to a registered user" do
post :registered_email, email: 'nonregistereduser@example.com'
expect(json_response['registered']).to eq false
end
end
context '#load_object' do
it 'should redirect to signup path if user is not found' do
allow(controller).to receive_messages(spree_current_user: nil)
put :update, user: { email: 'foobar@example.com' }
expect(response).to redirect_to('/login')
end
end
context '#create' do
it 'should create a new user' do
post :create, user: { email: 'foobar@example.com', password: 'foobar123', password_confirmation: 'foobar123' }
expect(assigns[:user].new_record?).to be_falsey
end
end
end