mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-19 04:49:15 +00:00
Merge branch 'master' into 2-0-stable-nov-8th
This commit is contained in:
@@ -2,6 +2,8 @@ require 'spec_helper'
|
||||
|
||||
module Admin
|
||||
describe ManagerInvitationsController, type: :controller do
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
let!(:enterprise_owner) { create(:user) }
|
||||
let!(:other_enterprise_user) { create(:user) }
|
||||
let!(:existing_user) { create(:user) }
|
||||
@@ -25,7 +27,7 @@ module Admin
|
||||
|
||||
context "signing up a new user" do
|
||||
before do
|
||||
create(:mail_method)
|
||||
setup_email
|
||||
controller.stub spree_current_user: admin
|
||||
end
|
||||
|
||||
@@ -46,7 +48,7 @@ module Admin
|
||||
describe "with enterprise permissions" do
|
||||
context "as user with proper enterprise permissions" do
|
||||
before do
|
||||
create(:mail_method)
|
||||
setup_email
|
||||
controller.stub spree_current_user: enterprise_owner
|
||||
end
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ require 'spec_helper'
|
||||
|
||||
describe Admin::SubscriptionsController, type: :controller do
|
||||
include AuthenticationWorkflow
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
describe 'index' do
|
||||
let!(:user) { create(:user, enterprise_limit: 10) }
|
||||
@@ -626,7 +627,7 @@ describe Admin::SubscriptionsController, type: :controller do
|
||||
|
||||
context "when at least one associate orders is 'canceled'" do
|
||||
before do
|
||||
Spree::Config[:mails_from] = "spree@example.com"
|
||||
setup_email
|
||||
proxy_order.cancel
|
||||
end
|
||||
|
||||
|
||||
158
spec/controllers/api/orders_controller_spec.rb
Normal file
158
spec/controllers/api/orders_controller_spec.rb
Normal file
@@ -0,0 +1,158 @@
|
||||
require 'spec_helper'
|
||||
require 'spree/api/testing_support/helpers'
|
||||
|
||||
module Api
|
||||
describe OrdersController, type: :controller do
|
||||
include AuthenticationWorkflow
|
||||
render_views
|
||||
|
||||
describe '#index' do
|
||||
let!(:distributor) { create(:distributor_enterprise) }
|
||||
let!(:distributor2) { create(:distributor_enterprise) }
|
||||
let!(:supplier) { create(:supplier_enterprise) }
|
||||
let!(:coordinator) { create(:distributor_enterprise) }
|
||||
let!(:order_cycle) { create(:simple_order_cycle, coordinator: coordinator) }
|
||||
let!(:order1) do
|
||||
create(:order, order_cycle: order_cycle, state: 'complete', completed_at: Time.zone.now,
|
||||
distributor: distributor, billing_address: create(:address) )
|
||||
end
|
||||
let!(:order2) do
|
||||
create(:order, order_cycle: order_cycle, state: 'complete', completed_at: Time.zone.now,
|
||||
distributor: distributor2, billing_address: create(:address) )
|
||||
end
|
||||
let!(:order3) do
|
||||
create(:order, order_cycle: order_cycle, state: 'complete', completed_at: Time.zone.now,
|
||||
distributor: distributor, billing_address: create(:address) )
|
||||
end
|
||||
let!(:line_item1) do
|
||||
create(:line_item, order: order1,
|
||||
product: create(:product, supplier: supplier))
|
||||
end
|
||||
let!(:line_item2) do
|
||||
create(:line_item, order: order2,
|
||||
product: create(:product, supplier: supplier))
|
||||
end
|
||||
let!(:line_item3) do
|
||||
create(:line_item, order: order2,
|
||||
product: create(:product, supplier: supplier))
|
||||
end
|
||||
let!(:line_item4) do
|
||||
create(:line_item, order: order3,
|
||||
product: create(:product, supplier: supplier))
|
||||
end
|
||||
let!(:regular_user) { create(:user) }
|
||||
let!(:admin_user) { create(:admin_user) }
|
||||
|
||||
context 'as a regular user' do
|
||||
before do
|
||||
allow(controller).to receive(:spree_current_user) { regular_user }
|
||||
get :index
|
||||
end
|
||||
|
||||
it "returns unauthorized" do
|
||||
assert_unauthorized!
|
||||
end
|
||||
end
|
||||
|
||||
context 'as an admin user' do
|
||||
before do
|
||||
allow(controller).to receive(:spree_current_user) { admin_user }
|
||||
get :index
|
||||
end
|
||||
|
||||
it "retrieves a list of orders with appropriate attributes,
|
||||
including line items with appropriate attributes" do
|
||||
|
||||
returns_orders(json_response)
|
||||
end
|
||||
|
||||
it "formats completed_at to 'yyyy-mm-dd hh:mm'" do
|
||||
completed_dates = json_response['orders'].map{ |order| order['completed_at'] }
|
||||
correct_formats = completed_dates.all?{ |a| a == order1.completed_at.strftime('%B %d, %Y') }
|
||||
|
||||
expect(correct_formats).to be_truthy
|
||||
end
|
||||
|
||||
it "returns distributor object with id key" do
|
||||
distributors = json_response['orders'].map{ |order| order['distributor'] }
|
||||
expect(distributors.all?{ |d| d.key?('id') }).to be_truthy
|
||||
end
|
||||
|
||||
it "returns the order number" do
|
||||
order_numbers = json_response['orders'].map{ |order| order['number'] }
|
||||
expect(order_numbers.all?{ |number| number.match("^R\\d{5,10}$") }).to be_truthy
|
||||
end
|
||||
end
|
||||
|
||||
context 'as an enterprise user' do
|
||||
context 'producer enterprise' do
|
||||
before do
|
||||
allow(controller).to receive(:spree_current_user) { supplier.owner }
|
||||
get :index
|
||||
end
|
||||
|
||||
it "does not display line items for which my enterprise is a supplier" do
|
||||
assert_unauthorized!
|
||||
end
|
||||
end
|
||||
|
||||
context 'coordinator enterprise' do
|
||||
before do
|
||||
allow(controller).to receive(:spree_current_user) { coordinator.owner }
|
||||
get :index
|
||||
end
|
||||
|
||||
it "retrieves a list of orders" do
|
||||
returns_orders(json_response)
|
||||
end
|
||||
end
|
||||
|
||||
context 'hub enterprise' do
|
||||
before do
|
||||
allow(controller).to receive(:spree_current_user) { distributor.owner }
|
||||
get :index
|
||||
end
|
||||
|
||||
it "retrieves a list of orders" do
|
||||
returns_orders(json_response)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'with pagination' do
|
||||
before do
|
||||
allow(controller).to receive(:spree_current_user) { distributor.owner }
|
||||
end
|
||||
|
||||
it 'returns pagination data when query params contain :per_page]' do
|
||||
get :index, per_page: 15, page: 1
|
||||
|
||||
pagination_data = {
|
||||
'results' => 2,
|
||||
'pages' => 1,
|
||||
'page' => 1,
|
||||
'per_page' => 15
|
||||
}
|
||||
|
||||
expect(json_response['pagination']).to eq pagination_data
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def returns_orders(response)
|
||||
keys = response['orders'].first.keys.map(&:to_sym)
|
||||
expect(order_attributes.all?{ |attr| keys.include? attr }).to be_truthy
|
||||
end
|
||||
|
||||
def order_attributes
|
||||
[
|
||||
:id, :number, :full_name, :email, :phone, :completed_at, :display_total,
|
||||
:show_path, :edit_path, :state, :payment_state, :shipment_state,
|
||||
:payments_path, :shipments_path, :ship_path, :ready_to_ship, :created_at,
|
||||
:distributor_name, :special_instructions, :payment_capture_path
|
||||
]
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -27,70 +27,72 @@ describe LineItemsController, type: :controller do
|
||||
end
|
||||
end
|
||||
|
||||
describe "destroying a line item on a completed order" do
|
||||
let(:item) do
|
||||
order = create(:completed_order_with_totals)
|
||||
item = create(:line_item, order: order)
|
||||
while !order.completed? do break unless order.next! end
|
||||
item
|
||||
end
|
||||
|
||||
let(:order) { item.order }
|
||||
let(:order_cycle) { create(:simple_order_cycle, distributors: [distributor], variants: [order.line_item_variants]) }
|
||||
|
||||
before { controller.stub spree_current_user: item.order.user }
|
||||
|
||||
context "without a line item id" do
|
||||
it "fails and raises an error" do
|
||||
delete :destroy
|
||||
expect(response.status).to eq 404
|
||||
describe "destroying a line item" do
|
||||
context "on a completed order" do
|
||||
let(:item) do
|
||||
order = create(:completed_order_with_totals)
|
||||
item = create(:line_item, order: order)
|
||||
while !order.completed? do break unless order.next! end
|
||||
item
|
||||
end
|
||||
end
|
||||
|
||||
context "with a line item id" do
|
||||
let(:params) { { format: :json, id: item } }
|
||||
let(:order) { item.order }
|
||||
let(:order_cycle) { create(:simple_order_cycle, distributors: [distributor], variants: [order.line_item_variants]) }
|
||||
|
||||
context "where the item's order is not associated with the user" do
|
||||
it "denies deletion" do
|
||||
delete :destroy, params
|
||||
expect(response.status).to eq 403
|
||||
before { controller.stub spree_current_user: item.order.user }
|
||||
|
||||
context "without a line item id" do
|
||||
it "fails and raises an error" do
|
||||
delete :destroy
|
||||
expect(response.status).to eq 404
|
||||
end
|
||||
end
|
||||
|
||||
context "where the item's order is associated with the current user" do
|
||||
before { order.update_attributes!(user_id: user.id) }
|
||||
context "with a line item id" do
|
||||
let(:params) { { format: :json, id: item } }
|
||||
|
||||
context "without an order cycle or distributor" do
|
||||
context "where the item's order is not associated with the user" do
|
||||
it "denies deletion" do
|
||||
delete :destroy, params
|
||||
expect(response.status).to eq 403
|
||||
end
|
||||
end
|
||||
|
||||
context "with an order cycle and distributor" do
|
||||
before { order.update_attributes!(order_cycle_id: order_cycle.id, distributor_id: distributor.id) }
|
||||
context "where the item's order is associated with the current user" do
|
||||
before { order.update_attributes!(user_id: user.id) }
|
||||
|
||||
context "where changes are not allowed" do
|
||||
context "without an order cycle or distributor" do
|
||||
it "denies deletion" do
|
||||
delete :destroy, params
|
||||
expect(response.status).to eq 403
|
||||
end
|
||||
end
|
||||
|
||||
context "where changes are allowed" do
|
||||
before { distributor.update_attributes!(allow_order_changes: true) }
|
||||
context "with an order cycle and distributor" do
|
||||
before { order.update_attributes!(order_cycle_id: order_cycle.id, distributor_id: distributor.id) }
|
||||
|
||||
it "deletes the line item" do
|
||||
delete :destroy, params
|
||||
expect(response.status).to eq 204
|
||||
expect { item.reload }.to raise_error ActiveRecord::RecordNotFound
|
||||
context "where changes are not allowed" do
|
||||
it "denies deletion" do
|
||||
delete :destroy, params
|
||||
expect(response.status).to eq 403
|
||||
end
|
||||
end
|
||||
|
||||
context "where changes are allowed" do
|
||||
before { distributor.update_attributes!(allow_order_changes: true) }
|
||||
|
||||
it "deletes the line item" do
|
||||
delete :destroy, params
|
||||
expect(response.status).to eq 204
|
||||
expect { item.reload }.to raise_error ActiveRecord::RecordNotFound
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "where shipping and payment fees apply" do
|
||||
context "on a completed order with shipping and payment fees" do
|
||||
let(:shipping_fee) { 3 }
|
||||
let(:payment_fee) { 5 }
|
||||
let(:order) { create(:completed_order_with_fees, shipping_fee: shipping_fee, payment_fee: payment_fee) }
|
||||
@@ -124,7 +126,7 @@ describe LineItemsController, type: :controller do
|
||||
end
|
||||
end
|
||||
|
||||
context "where enterprise fees apply" do
|
||||
context "on a completed order with enterprise fees" do
|
||||
let(:user) { create(:user) }
|
||||
let(:variant) { create(:variant) }
|
||||
let(:distributor) { create(:distributor_enterprise, allow_order_changes: true) }
|
||||
|
||||
@@ -2,6 +2,7 @@ require 'spec_helper'
|
||||
|
||||
describe Spree::Admin::OrdersController, type: :controller do
|
||||
include AuthenticationWorkflow
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
context "updating an order with line items" do
|
||||
let!(:order) { create(:order) }
|
||||
@@ -30,112 +31,22 @@ describe Spree::Admin::OrdersController, type: :controller do
|
||||
end
|
||||
|
||||
describe "#index" do
|
||||
render_views
|
||||
|
||||
let(:order_attributes) { [:id, :full_name, :email, :phone, :completed_at, :distributor, :order_cycle, :number] }
|
||||
|
||||
def self.make_simple_data!
|
||||
let!(:dist1) { FactoryBot.create(:distributor_enterprise) }
|
||||
let!(:order1) { FactoryBot.create(:order, state: 'complete', completed_at: Time.zone.now, distributor: dist1, billing_address: FactoryBot.create(:address) ) }
|
||||
let!(:order2) { FactoryBot.create(:order, state: 'complete', completed_at: Time.zone.now, distributor: dist1, billing_address: FactoryBot.create(:address) ) }
|
||||
let!(:order3) { FactoryBot.create(:order, state: 'complete', completed_at: Time.zone.now, distributor: dist1, billing_address: FactoryBot.create(:address) ) }
|
||||
let!(:line_item1) { FactoryBot.create(:line_item_with_shipment, order: order1) }
|
||||
let!(:line_item2) { FactoryBot.create(:line_item_with_shipment, order: order2) }
|
||||
let!(:line_item3) { FactoryBot.create(:line_item_with_shipment, order: order2) }
|
||||
let!(:line_item4) { FactoryBot.create(:line_item_with_shipment, order: order3) }
|
||||
let(:line_item_attributes) { [:id, :quantity, :max_quantity, :supplier, :units_product, :units_variant] }
|
||||
end
|
||||
|
||||
context "as a normal user" do
|
||||
before { controller.stub spree_current_user: create_enterprise_user }
|
||||
|
||||
make_simple_data!
|
||||
context "as a regular user" do
|
||||
before { allow(controller).to receive(:spree_current_user) { create_enterprise_user } }
|
||||
|
||||
it "should deny me access to the index action" do
|
||||
spree_get :index, :format => :json
|
||||
spree_get :index
|
||||
expect(response).to redirect_to spree.unauthorized_path
|
||||
end
|
||||
end
|
||||
|
||||
context "as an administrator" do
|
||||
make_simple_data!
|
||||
|
||||
before do
|
||||
controller.stub spree_current_user: quick_login_as_admin
|
||||
spree_get :index, :format => :json
|
||||
end
|
||||
|
||||
it "retrieves a list of orders with appropriate attributes, including line items with appropriate attributes" do
|
||||
keys = json_response['orders'].first.keys.map{ |key| key.to_sym }
|
||||
order_attributes.all?{ |attr| keys.include? attr }.should == true
|
||||
end
|
||||
|
||||
it "sorts orders in descending id order" do
|
||||
ids = json_response['orders'].map{ |order| order['id'] }
|
||||
ids[0].should > ids[1]
|
||||
ids[1].should > ids[2]
|
||||
end
|
||||
|
||||
it "formats completed_at to 'yyyy-mm-dd hh:mm'" do
|
||||
pp json_response
|
||||
json_response['orders'].map{ |order| order['completed_at'] }.all?{ |a| a == order1.completed_at.strftime('%B %d, %Y') }.should == true
|
||||
end
|
||||
|
||||
it "returns distributor object with id key" do
|
||||
json_response['orders'].map{ |order| order['distributor'] }.all?{ |d| d.has_key?('id') }.should == true
|
||||
end
|
||||
|
||||
it "retrieves the order number" do
|
||||
json_response['orders'].map{ |order| order['number'] }.all?{ |number| number.match("^R\\d{5,10}$") }.should == true
|
||||
end
|
||||
end
|
||||
|
||||
context "as an enterprise user" do
|
||||
let(:supplier) { create(:supplier_enterprise) }
|
||||
let(:distributor1) { create(:distributor_enterprise) }
|
||||
let(:distributor2) { create(:distributor_enterprise) }
|
||||
let(:coordinator) { create(:distributor_enterprise) }
|
||||
let(:order_cycle) { create(:simple_order_cycle, coordinator: coordinator) }
|
||||
let!(:order1) { FactoryBot.create(:order, order_cycle: order_cycle, state: 'complete', completed_at: Time.zone.now, distributor: distributor1, billing_address: FactoryBot.create(:address) ) }
|
||||
let!(:line_item1) { FactoryBot.create(:line_item_with_shipment, order: order1, product: FactoryBot.create(:product, supplier: supplier)) }
|
||||
let!(:line_item2) { FactoryBot.create(:line_item_with_shipment, order: order1, product: FactoryBot.create(:product, supplier: supplier)) }
|
||||
let!(:order2) { FactoryBot.create(:order, order_cycle: order_cycle, state: 'complete', completed_at: Time.zone.now, distributor: distributor2, billing_address: FactoryBot.create(:address) ) }
|
||||
let!(:line_item3) { FactoryBot.create(:line_item_with_shipment, order: order2, product: FactoryBot.create(:product, supplier: supplier)) }
|
||||
let!(:order) { create(:order_with_distributor) }
|
||||
|
||||
context "producer enterprise" do
|
||||
before { allow(controller).to receive(:spree_current_user) { order.distributor.owner } }
|
||||
|
||||
before do
|
||||
controller.stub spree_current_user: supplier.owner
|
||||
spree_get :index, :format => :json
|
||||
end
|
||||
|
||||
it "does not display line items for which my enterprise is a supplier" do
|
||||
expect(response).to redirect_to spree.unauthorized_path
|
||||
end
|
||||
end
|
||||
|
||||
context "coordinator enterprise" do
|
||||
before do
|
||||
controller.stub spree_current_user: coordinator.owner
|
||||
spree_get :index, :format => :json
|
||||
end
|
||||
|
||||
it "retrieves a list of orders" do
|
||||
keys = json_response['orders'].first.keys.map{ |key| key.to_sym }
|
||||
order_attributes.all?{ |attr| keys.include? attr }.should == true
|
||||
end
|
||||
end
|
||||
|
||||
context "hub enterprise" do
|
||||
before do
|
||||
controller.stub spree_current_user: distributor1.owner
|
||||
spree_get :index, :format => :json
|
||||
end
|
||||
|
||||
it "retrieves a list of orders" do
|
||||
keys = json_response['orders'].first.keys.map{ |key| key.to_sym }
|
||||
order_attributes.all?{ |attr| keys.include? attr }.should == true
|
||||
end
|
||||
it "should allow access" do
|
||||
expect(response.status).to eq 200
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -148,7 +59,7 @@ describe Spree::Admin::OrdersController, type: :controller do
|
||||
let(:params) { { id: order.number } }
|
||||
|
||||
context "as a normal user" do
|
||||
before { controller.stub spree_current_user: user }
|
||||
before { allow(controller).to receive(:spree_current_user) { user } }
|
||||
|
||||
it "should prevent me from sending order invoices" do
|
||||
spree_get :invoice, params
|
||||
@@ -158,7 +69,8 @@ describe Spree::Admin::OrdersController, type: :controller do
|
||||
|
||||
context "as an enterprise user" do
|
||||
context "which is not a manager of the distributor for an order" do
|
||||
before { controller.stub spree_current_user: user }
|
||||
before { allow(controller).to receive(:spree_current_user) { user } }
|
||||
|
||||
it "should prevent me from sending order invoices" do
|
||||
spree_get :invoice, params
|
||||
expect(response).to redirect_to spree.unauthorized_path
|
||||
@@ -166,7 +78,8 @@ describe Spree::Admin::OrdersController, type: :controller do
|
||||
end
|
||||
|
||||
context "which is a manager of the distributor for an order" do
|
||||
before { controller.stub spree_current_user: distributor.owner }
|
||||
before { allow(controller).to receive(:spree_current_user) { distributor.owner } }
|
||||
|
||||
context "when the distributor's ABN has not been set" do
|
||||
before { distributor.update_attribute(:abn, "") }
|
||||
it "should allow me to send order invoices" do
|
||||
@@ -181,8 +94,8 @@ describe Spree::Admin::OrdersController, type: :controller do
|
||||
context "when the distributor's ABN has been set" do
|
||||
before { distributor.update_attribute(:abn, "123") }
|
||||
before do
|
||||
Spree::Config[:mails_from] = "spree@example.com"
|
||||
ActionMailer::Base.perform_deliveries = true
|
||||
setup_email
|
||||
end
|
||||
|
||||
it "should allow me to send order invoices" do
|
||||
@@ -204,7 +117,7 @@ describe Spree::Admin::OrdersController, type: :controller do
|
||||
let(:params) { { id: order.number } }
|
||||
|
||||
context "as a normal user" do
|
||||
before { controller.stub spree_current_user: user }
|
||||
before { allow(controller).to receive(:spree_current_user) { user } }
|
||||
|
||||
it "should prevent me from sending order invoices" do
|
||||
spree_get :print, params
|
||||
@@ -214,7 +127,7 @@ describe Spree::Admin::OrdersController, type: :controller do
|
||||
|
||||
context "as an enterprise user" do
|
||||
context "which is not a manager of the distributor for an order" do
|
||||
before { controller.stub spree_current_user: user }
|
||||
before { allow(controller).to receive(:spree_current_user) { user } }
|
||||
it "should prevent me from sending order invoices" do
|
||||
spree_get :print, params
|
||||
expect(response).to redirect_to spree.unauthorized_path
|
||||
@@ -222,7 +135,7 @@ describe Spree::Admin::OrdersController, type: :controller do
|
||||
end
|
||||
|
||||
context "which is a manager of the distributor for an order" do
|
||||
before { controller.stub spree_current_user: distributor.owner }
|
||||
before { allow(controller).to receive(:spree_current_user) { distributor.owner } }
|
||||
it "should allow me to send order invoices" do
|
||||
spree_get :print, params
|
||||
expect(response).to render_template :invoice
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Spree::OrdersController, type: :controller do
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
let(:distributor) { double(:distributor) }
|
||||
let(:order) { create(:order) }
|
||||
let(:order_cycle) { create(:simple_order_cycle) }
|
||||
@@ -371,7 +373,7 @@ describe Spree::OrdersController, type: :controller do
|
||||
let(:order) { create(:completed_order_with_totals, user: user) }
|
||||
|
||||
before do
|
||||
Spree::Config[:mails_from] = "spree@example.com"
|
||||
setup_email
|
||||
end
|
||||
|
||||
it "responds with success" do
|
||||
|
||||
@@ -2,6 +2,8 @@ require 'spec_helper'
|
||||
|
||||
describe UserConfirmationsController, type: :controller do
|
||||
include AuthenticationWorkflow
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
let!(:user) { create_enterprise_user }
|
||||
let!(:confirmed_user) { create_enterprise_user(confirmed_at: nil) }
|
||||
let!(:unconfirmed_user) { create_enterprise_user(confirmed_at: nil) }
|
||||
@@ -57,7 +59,7 @@ describe UserConfirmationsController, type: :controller do
|
||||
end
|
||||
|
||||
context "requesting confirmation instructions to be resent" do
|
||||
before { create(:mail_method) }
|
||||
before { setup_email }
|
||||
|
||||
it "redirects the user to login" do
|
||||
spree_post :create, { spree_user: { email: unconfirmed_user.email } }
|
||||
|
||||
@@ -2,6 +2,8 @@ require 'spec_helper'
|
||||
require 'spree/api/testing_support/helpers'
|
||||
|
||||
describe UserPasswordsController, type: :controller do
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
let(:user) { create(:user) }
|
||||
let(:unconfirmed_user) { create(:user, confirmed_at: nil) }
|
||||
|
||||
@@ -32,6 +34,7 @@ describe UserPasswordsController, type: :controller do
|
||||
end
|
||||
|
||||
it "renders Darkswarm" do
|
||||
setup_email
|
||||
clear_jobs
|
||||
|
||||
user.send_reset_password_instructions
|
||||
|
||||
@@ -2,9 +2,10 @@ require 'spec_helper'
|
||||
require 'spree/api/testing_support/helpers'
|
||||
|
||||
describe UserRegistrationsController, type: :controller do
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
before(:all) do
|
||||
create(:mail_method)
|
||||
setup_email
|
||||
end
|
||||
|
||||
before do
|
||||
|
||||
@@ -275,7 +275,7 @@ FactoryBot.define do
|
||||
enterprise_role 'distributor'
|
||||
end
|
||||
|
||||
factory :weight_calculator, :class => OpenFoodNetwork::Calculator::Weight do
|
||||
factory :weight_calculator, :class => Calculator::Weight do
|
||||
after(:build) { |c| c.set_preference(:per_kg, 0.5) }
|
||||
after(:create) { |c| c.set_preference(:per_kg, 0.5); c.save! }
|
||||
end
|
||||
|
||||
@@ -6,6 +6,7 @@ feature %q{
|
||||
}, js: true do
|
||||
include AuthenticationWorkflow
|
||||
include WebHelper
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
|
||||
context "as a site administrator" do
|
||||
@@ -137,7 +138,7 @@ feature %q{
|
||||
end
|
||||
|
||||
it "can invite unregistered users to be managers" do
|
||||
create(:mail_method)
|
||||
setup_email
|
||||
find('a.button.help-modal').click
|
||||
expect(page).to have_css '#invite-manager-modal'
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ feature "Product Import", js: true do
|
||||
|
||||
xit "validates entries and saves them if they are all valid and allows viewing new items in Bulk Products" do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"]
|
||||
csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "6.50", "1", "kg"]
|
||||
end
|
||||
@@ -76,7 +76,7 @@ feature "Product Import", js: true do
|
||||
|
||||
it "displays info about invalid entries but no save button if all items are invalid" do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["Bad Carrots", "Unkown Enterprise", "Mouldy vegetables", "666", "3.20", "", "g"]
|
||||
csv << ["Bad Potatoes", "", "Vegetables", "6", "6", "6", ""]
|
||||
end
|
||||
@@ -100,7 +100,7 @@ feature "Product Import", js: true do
|
||||
|
||||
xit "handles saving of named tax and shipping categories" do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "tax_category", "shipping_category"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "tax_category", "shipping_category"]
|
||||
csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g", tax_category.name, shipping_category.name]
|
||||
end
|
||||
File.write('/tmp/test.csv', csv_data)
|
||||
@@ -129,7 +129,7 @@ feature "Product Import", js: true do
|
||||
|
||||
xit "records a timestamp on import that can be viewed and filtered under Bulk Edit Products" do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"]
|
||||
csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "6.50", "1", "kg"]
|
||||
end
|
||||
@@ -176,7 +176,7 @@ feature "Product Import", js: true do
|
||||
|
||||
xit "can reset product stock to zero for products not present in the CSV" do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["Carrots", "User Enterprise", "Vegetables", "500", "3.20", "500", "g"]
|
||||
end
|
||||
File.write('/tmp/test.csv', csv_data)
|
||||
@@ -203,7 +203,7 @@ feature "Product Import", js: true do
|
||||
|
||||
xit "can save a new product and variant of that product at the same time, add variant to existing product" do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "display_name"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "display_name"]
|
||||
csv << ["Potatoes", "User Enterprise", "Vegetables", "5", "3.50", "500", "g", "Small Bag"]
|
||||
csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "5.50", "2", "kg", "Big Bag"]
|
||||
csv << ["Beans", "User Enterprise", "Vegetables", "7", "2.50", "250", "g", nil]
|
||||
@@ -241,7 +241,7 @@ feature "Product Import", js: true do
|
||||
|
||||
xit "can import items into inventory" do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "producer", "category", "on_hand", "price", "units"]
|
||||
csv << ["name", "distributor", "producer", "category", "on_hand", "price", "units"]
|
||||
csv << ["Beans", "Another Enterprise", "User Enterprise", "Vegetables", "5", "3.20", "500"]
|
||||
csv << ["Sprouts", "Another Enterprise", "User Enterprise", "Vegetables", "6", "6.50", "500"]
|
||||
csv << ["Cabbage", "Another Enterprise", "User Enterprise", "Vegetables", "2001", "1.50", "500"]
|
||||
@@ -338,7 +338,7 @@ feature "Product Import", js: true do
|
||||
|
||||
xit "only allows product import into enterprises the user is permitted to manage" do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["My Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"]
|
||||
csv << ["Your Potatoes", "Another Enterprise", "Vegetables", "6", "6.50", "1", "kg"]
|
||||
end
|
||||
|
||||
@@ -2,10 +2,11 @@ require "spec_helper"
|
||||
|
||||
feature "Managing users" do
|
||||
include AuthenticationWorkflow
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
context "as super-admin" do
|
||||
before do
|
||||
create(:mail_method)
|
||||
setup_email
|
||||
quick_login_as_admin
|
||||
end
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ require 'spec_helper'
|
||||
|
||||
feature "Account Settings", js: true do
|
||||
include AuthenticationWorkflow
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
describe "as a logged in user" do
|
||||
let(:user) do
|
||||
@@ -12,7 +13,7 @@ feature "Account Settings", js: true do
|
||||
end
|
||||
|
||||
before do
|
||||
create(:mail_method)
|
||||
setup_email
|
||||
quick_login_as user
|
||||
visit "/account"
|
||||
click_link I18n.t('spree.users.show.tabs.settings')
|
||||
|
||||
@@ -2,6 +2,7 @@ require 'spec_helper'
|
||||
|
||||
feature "Authentication", js: true, retry: 3 do
|
||||
include UIComponentHelper
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
# Attempt to address intermittent failures in these specs
|
||||
around do |example|
|
||||
@@ -75,7 +76,7 @@ feature "Authentication", js: true, retry: 3 do
|
||||
end
|
||||
|
||||
scenario "Signing up successfully" do
|
||||
create(:mail_method)
|
||||
setup_email
|
||||
fill_in "Email", with: "test@foo.com"
|
||||
fill_in "Choose a password", with: "test12345"
|
||||
fill_in "Confirm password", with: "test12345"
|
||||
|
||||
@@ -2,13 +2,14 @@ require "spec_helper"
|
||||
|
||||
feature "Confirm invitation as manager" do
|
||||
include UIComponentHelper # for be_logged_in_as
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
describe "confirm email and set password" do
|
||||
let(:email) { "test@example.org" }
|
||||
let(:user) { Spree::User.create(email: email, unconfirmed_email: email, password: "secret") }
|
||||
|
||||
before do
|
||||
create(:mail_method)
|
||||
setup_email
|
||||
user.reset_password_token = Devise.friendly_token
|
||||
user.reset_password_sent_at = Time.now.utc
|
||||
user.save!
|
||||
|
||||
@@ -3,6 +3,7 @@ require 'spec_helper'
|
||||
feature 'Multilingual', js: true do
|
||||
include AuthenticationWorkflow
|
||||
include WebHelper
|
||||
include ShopWorkflow
|
||||
|
||||
it 'has two locales available' do
|
||||
expect(Rails.application.config.i18n[:default_locale]).to eq 'en'
|
||||
@@ -16,28 +17,54 @@ feature 'Multilingual', js: true do
|
||||
expect(get_i18n_translation('label_shops')).to eq 'Shops'
|
||||
end
|
||||
|
||||
it 'can switch language by params' do
|
||||
visit root_path
|
||||
expect(get_i18n_locale).to eq 'en'
|
||||
expect(get_i18n_translation('label_shops')).to eq 'Shops'
|
||||
expect(page.driver.browser.cookies['locale']).to be_nil
|
||||
expect(page).to have_content 'Interested in getting on the Open Food Network?'
|
||||
expect(page).to have_content 'SHOPS'
|
||||
context 'can switch language by params' do
|
||||
scenario 'in root path' do
|
||||
visit root_path
|
||||
expect(get_i18n_locale).to eq 'en'
|
||||
expect(get_i18n_translation('label_shops')).to eq 'Shops'
|
||||
expect(page.driver.browser.cookies['locale']).to be_nil
|
||||
expect(page).to have_content 'Interested in getting on the Open Food Network?'
|
||||
expect(page).to have_content 'SHOPS'
|
||||
|
||||
visit root_path(locale: 'es')
|
||||
expect(get_i18n_locale).to eq 'es'
|
||||
expect(get_i18n_translation('label_shops')).to eq 'Tiendas'
|
||||
expect(page.driver.browser.cookies['locale'].value).to eq 'es'
|
||||
expect(page).to have_content '¿Estás interesada en entrar en Open Food Network?'
|
||||
expect(page).to have_content 'TIENDAS'
|
||||
visit root_path(locale: 'es')
|
||||
expect(get_i18n_locale).to eq 'es'
|
||||
expect(get_i18n_translation('label_shops')).to eq 'Tiendas'
|
||||
expect_menu_and_cookie_in_es
|
||||
expect(page).to have_content '¿Estás interesada en entrar en Open Food Network?'
|
||||
|
||||
# it is not in the list of available of available_locales
|
||||
visit root_path(locale: 'it')
|
||||
expect(get_i18n_locale).to eq 'es'
|
||||
expect(get_i18n_translation('label_shops')).to eq 'Tiendas'
|
||||
expect(page.driver.browser.cookies['locale'].value).to eq 'es'
|
||||
expect(page).to have_content '¿Estás interesada en entrar en Open Food Network?'
|
||||
expect(page).to have_content 'TIENDAS'
|
||||
# it is not in the list of available of available_locales
|
||||
visit root_path(locale: 'it')
|
||||
expect(get_i18n_locale).to eq 'es'
|
||||
expect(get_i18n_translation('label_shops')).to eq 'Tiendas'
|
||||
expect_menu_and_cookie_in_es
|
||||
expect(page).to have_content '¿Estás interesada en entrar en Open Food Network?'
|
||||
end
|
||||
|
||||
context 'with a product in the cart' do
|
||||
let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
|
||||
let!(:order_cycle) { create(:simple_order_cycle, distributors: [distributor], variants: [product.variants.first]) }
|
||||
let(:product) { create(:simple_product) }
|
||||
let(:order) { create(:order, order_cycle: order_cycle, distributor: distributor) }
|
||||
|
||||
before do
|
||||
set_order order
|
||||
add_product_to_cart order, product, quantity: 1
|
||||
end
|
||||
|
||||
scenario "in the cart page" do
|
||||
visit spree.cart_path(locale: 'es')
|
||||
|
||||
expect_menu_and_cookie_in_es
|
||||
expect(page).to have_content 'Precio'
|
||||
end
|
||||
|
||||
scenario "in the checkout page" do
|
||||
visit checkout_path(locale: 'es')
|
||||
|
||||
expect_menu_and_cookie_in_es
|
||||
expect(page).to have_content 'Total del carrito'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'with user' do
|
||||
@@ -46,12 +73,12 @@ feature 'Multilingual', js: true do
|
||||
it 'updates user locale from cookie if it is empty' do
|
||||
visit root_path(locale: 'es')
|
||||
|
||||
expect(page.driver.browser.cookies['locale'].value).to eq 'es'
|
||||
expect_menu_and_cookie_in_es
|
||||
expect(user.locale).to be_nil
|
||||
quick_login_as user
|
||||
visit root_path
|
||||
|
||||
expect(page.driver.browser.cookies['locale'].value).to eq 'es'
|
||||
expect_menu_and_cookie_in_es
|
||||
end
|
||||
|
||||
it 'updates user locale and stays in cookie after logout' do
|
||||
@@ -63,9 +90,8 @@ feature 'Multilingual', js: true do
|
||||
|
||||
logout
|
||||
|
||||
expect(page.driver.browser.cookies['locale'].value).to eq 'es'
|
||||
expect_menu_and_cookie_in_es
|
||||
expect(page).to have_content '¿Estás interesada en entrar en Open Food Network?'
|
||||
expect(page).to have_content 'TIENDAS'
|
||||
end
|
||||
end
|
||||
|
||||
@@ -103,9 +129,13 @@ feature 'Multilingual', js: true do
|
||||
find('li a[href="?locale=es"]').click
|
||||
end
|
||||
|
||||
expect(page.driver.browser.cookies['locale'].value).to eq 'es'
|
||||
expect(page).to have_content 'TIENDAS'
|
||||
expect_menu_and_cookie_in_es
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def expect_menu_and_cookie_in_es
|
||||
expect(page.driver.browser.cookies['locale'].value).to eq 'es'
|
||||
expect(page).to have_content 'TIENDAS'
|
||||
end
|
||||
|
||||
@@ -2,6 +2,7 @@ require 'spec_helper'
|
||||
|
||||
feature "Order Management", js: true do
|
||||
include AuthenticationWorkflow
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
describe "viewing a completed order" do
|
||||
let!(:distributor) { create(:distributor_enterprise) }
|
||||
@@ -120,7 +121,7 @@ feature "Order Management", js: true do
|
||||
|
||||
context "when the distributor allows changes to be made to orders" do
|
||||
before do
|
||||
Spree::Config[:mails_from] = "spree@example.com"
|
||||
setup_email
|
||||
end
|
||||
before do
|
||||
order.distributor.update_attributes(allow_order_changes: true)
|
||||
|
||||
@@ -37,7 +37,7 @@ describe "LineItemsCtrl", ->
|
||||
order = { id: 9, order_cycle: { id: 4 }, distributor: { id: 5 }, number: "R123456" }
|
||||
lineItem = { id: 7, quantity: 3, order: { id: 9 }, supplier: { id: 1 } }
|
||||
|
||||
httpBackend.expectGET("/admin/orders.json?q%5Bcompleted_at_gteq%5D=SomeDate&q%5Bcompleted_at_lt%5D=SomeDate&q%5Bcompleted_at_not_null%5D=true&q%5Bstate_not_eq%5D=canceled").respond {orders: [order], pagination: {page: 1, pages: 1, results: 1}}
|
||||
httpBackend.expectGET("/api/orders.json?q%5Bcompleted_at_gteq%5D=SomeDate&q%5Bcompleted_at_lt%5D=SomeDate&q%5Bcompleted_at_not_null%5D=true&q%5Bstate_not_eq%5D=canceled").respond {orders: [order], pagination: {page: 1, pages: 1, results: 1}}
|
||||
httpBackend.expectGET("/admin/bulk_line_items.json?q%5Border%5D%5Bcompleted_at_gteq%5D=SomeDate&q%5Border%5D%5Bcompleted_at_lt%5D=SomeDate&q%5Border%5D%5Bcompleted_at_not_null%5D=true&q%5Border%5D%5Bstate_not_eq%5D=canceled").respond [lineItem]
|
||||
httpBackend.expectGET("/admin/enterprises/visible.json?ams_prefix=basic&q%5Bsells_in%5D%5B%5D=own&q%5Bsells_in%5D%5B%5D=any").respond [distributor]
|
||||
httpBackend.expectGET("/admin/order_cycles.json?ams_prefix=basic&as=distributor&q%5Borders_close_at_gt%5D=SomeDate").respond [orderCycle]
|
||||
|
||||
@@ -19,7 +19,7 @@ describe "Orders service", ->
|
||||
|
||||
beforeEach ->
|
||||
response = { orders: [{ id: 5, name: 'Order 1'}], pagination: {page: 1, pages: 1, results: 1} }
|
||||
$httpBackend.expectGET('/admin/orders.json').respond 200, response
|
||||
$httpBackend.expectGET('/api/orders.json').respond 200, response
|
||||
result = Orders.index()
|
||||
$httpBackend.flush()
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
describe "CountryStates service", ->
|
||||
countryStates = null
|
||||
|
||||
states_in_spain = [{id: 55, name: "CAT", abbr: "CAT"}]
|
||||
states_in_portugal = [{id: 55, name: "ACT", abbr: "ACT"}, {id: 5, name: "BFT", abbr: "BFT"}]
|
||||
availableCountries = [
|
||||
{id: 9, name: "Australia", states: []},
|
||||
{id: 119, name: "Spain", states: states_in_spain},
|
||||
{id: 19, name: "Portugal", states: states_in_portugal}
|
||||
]
|
||||
|
||||
beforeEach ->
|
||||
module('admin.utils')
|
||||
inject (CountryStates) ->
|
||||
countryStates = CountryStates
|
||||
|
||||
describe "statesFor", ->
|
||||
it "returns empty array for nil country id", ->
|
||||
expect(countryStates.statesFor(availableCountries, null)).toEqual []
|
||||
|
||||
it "returns empty array for country id not in availableCountries", ->
|
||||
expect(countryStates.statesFor(availableCountries, 10)).toEqual []
|
||||
|
||||
it "returns empty array for country id in availableCountries but without states", ->
|
||||
expect(countryStates.statesFor(availableCountries, 9)).toEqual []
|
||||
|
||||
it "returns states for country id in availableCountries with states", ->
|
||||
expect(countryStates.statesFor(availableCountries, 119)).toEqual states_in_spain
|
||||
|
||||
it "returns empty array for country id (11) in availableCountries but only as part of other country id (119)", ->
|
||||
expect(countryStates.statesFor(availableCountries, 11)).toEqual []
|
||||
|
||||
it "returns states for country id (19) in availableCountries with states even if other country ids contain the requested id (119)", ->
|
||||
expect(countryStates.statesFor(availableCountries, 19)).toEqual states_in_portugal
|
||||
@@ -1,6 +1,8 @@
|
||||
require 'spec_helper'
|
||||
|
||||
xdescribe SubscriptionConfirmJob do
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
let(:job) { SubscriptionConfirmJob.new }
|
||||
|
||||
describe "finding proxy_orders that are ready to be confirmed" do
|
||||
@@ -114,10 +116,7 @@ xdescribe SubscriptionConfirmJob do
|
||||
while !order.completed? do break unless order.next! end
|
||||
allow(job).to receive(:send_confirm_email).and_call_original
|
||||
job.instance_variable_set(:@order, order)
|
||||
Spree::MailMethod.create!(
|
||||
environment: Rails.env,
|
||||
preferred_mails_from: 'spree@example.com'
|
||||
)
|
||||
setup_email
|
||||
expect(job).to receive(:record_order).with(order)
|
||||
end
|
||||
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe EnterpriseMailer do
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
let!(:enterprise) { create(:enterprise) }
|
||||
let!(:user) { create(:user) }
|
||||
|
||||
before do
|
||||
ActionMailer::Base.deliveries = []
|
||||
setup_email
|
||||
end
|
||||
|
||||
describe "#welcome" do
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Spree::OrderMailer do
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
describe "order confimation" do
|
||||
after do
|
||||
ActionMailer::Base.deliveries.clear
|
||||
end
|
||||
|
||||
before do
|
||||
setup_email
|
||||
ActionMailer::Base.delivery_method = :test
|
||||
ActionMailer::Base.perform_deliveries = true
|
||||
ActionMailer::Base.deliveries = []
|
||||
|
||||
@@ -2,8 +2,10 @@ require 'spec_helper'
|
||||
require 'yaml'
|
||||
|
||||
describe ProducerMailer do
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
before do
|
||||
Spree::Config[:mails_from] = "spree@example.com"
|
||||
setup_email
|
||||
end
|
||||
let!(:zone) { create(:zone_with_member) }
|
||||
let!(:tax_rate) { create(:tax_rate, included_in_price: true, calculator: Spree::Calculator::DefaultTax.new, zone: zone, amount: 0.1) }
|
||||
|
||||
@@ -2,6 +2,9 @@ require 'spec_helper'
|
||||
|
||||
xdescribe SubscriptionMailer do
|
||||
include ActionView::Helpers::SanitizeHelper
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
before { setup_email }
|
||||
|
||||
describe "order placement" do
|
||||
let(:shop) { create(:enterprise) }
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Spree::UserMailer do
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
let(:user) { build(:user) }
|
||||
|
||||
after do
|
||||
@@ -11,6 +13,8 @@ describe Spree::UserMailer do
|
||||
ActionMailer::Base.delivery_method = :test
|
||||
ActionMailer::Base.perform_deliveries = true
|
||||
ActionMailer::Base.deliveries = []
|
||||
|
||||
setup_email
|
||||
end
|
||||
|
||||
it "sends an email when given a user" do
|
||||
|
||||
@@ -1,27 +1,41 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe OpenFoodNetwork::Calculator::Weight do
|
||||
describe Calculator::Weight do
|
||||
it "computes shipping cost for an order by total weight" do
|
||||
variant_1 = double(:variant, :weight => 10)
|
||||
variant_2 = double(:variant, :weight => 20)
|
||||
variant_3 = double(:variant, :weight => nil)
|
||||
variant1 = double(:variant, weight: 10)
|
||||
variant2 = double(:variant, weight: 20)
|
||||
variant3 = double(:variant, weight: nil)
|
||||
|
||||
line_item_1 = double(:line_item, :variant => variant_1, :quantity => 1)
|
||||
line_item_2 = double(:line_item, :variant => variant_2, :quantity => 3)
|
||||
line_item_3 = double(:line_item, :variant => variant_3, :quantity => 5)
|
||||
line_item1 = double(:line_item, variant: variant1, quantity: 1)
|
||||
line_item2 = double(:line_item, variant: variant2, quantity: 3)
|
||||
line_item3 = double(:line_item, variant: variant3, quantity: 5)
|
||||
|
||||
order = double(:order, :line_items => [line_item_1, line_item_2, line_item_3])
|
||||
order = double(:order, line_items: [line_item1, line_item2, line_item3])
|
||||
|
||||
subject.set_preference(:per_kg, 10)
|
||||
subject.compute(order).should == (10*1 + 20*3) * 10
|
||||
expect(subject.compute(order)).to eq((10 * 1 + 20 * 3) * 10)
|
||||
end
|
||||
|
||||
it "computes shipping cost for a line item" do
|
||||
variant = double(:variant, :weight => 10)
|
||||
variant = double(:variant, weight: 10)
|
||||
|
||||
line_item = double(:line_item, :variant => variant, :quantity => 2)
|
||||
line_item = double(:line_item, variant: variant, quantity: 2)
|
||||
|
||||
subject.set_preference(:per_kg, 10)
|
||||
subject.compute(line_item).should == 10*2 * 10
|
||||
expect(subject.compute(line_item)).to eq(10 * 2 * 10)
|
||||
end
|
||||
|
||||
it "computes shipping cost for an object with an order" do
|
||||
variant1 = double(:variant, weight: 10)
|
||||
variant2 = double(:variant, weight: 5)
|
||||
|
||||
line_item1 = double(:line_item, variant: variant1, quantity: 1)
|
||||
line_item2 = double(:line_item, variant: variant2, quantity: 2)
|
||||
|
||||
order = double(:order, line_items: [line_item1, line_item2])
|
||||
object_with_order = double(:object_with_order, order: order)
|
||||
|
||||
subject.set_preference(:per_kg, 10)
|
||||
expect(subject.compute(object_with_order)).to eq((10 * 1 + 5 * 2) * 10)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -140,7 +140,7 @@ describe EnterpriseRelationship do
|
||||
end
|
||||
|
||||
describe "callbacks" do
|
||||
context "applying variant override permissions" do
|
||||
context "updating variant override permissions" do
|
||||
let(:hub) { create(:distributor_enterprise) }
|
||||
let(:producer) { create(:supplier_enterprise) }
|
||||
let(:some_other_producer) { create(:supplier_enterprise) }
|
||||
@@ -152,6 +152,17 @@ describe EnterpriseRelationship do
|
||||
let!(:vo2) { create(:variant_override, hub: hub, variant: create(:variant, product: create(:product, supplier: producer))) }
|
||||
let!(:vo3) { create(:variant_override, hub: hub, variant: create(:variant, product: create(:product, supplier: some_other_producer))) }
|
||||
|
||||
context "revoking variant override permissions" do
|
||||
context "when the enterprise relationship is destroyed" do
|
||||
before { er.destroy }
|
||||
it "should set permission_revoked_at to the current time for all variant overrides of the relationship" do
|
||||
expect(vo1.reload.permission_revoked_at).to_not be_nil
|
||||
expect(vo2.reload.permission_revoked_at).to_not be_nil
|
||||
expect(vo2.reload.permission_revoked_at).to_not be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "and is then removed" do
|
||||
before { er.permissions_list = [:add_to_order_cycles]; er.save! }
|
||||
it "should set permission_revoked_at to the current time for all relevant variant overrides" do
|
||||
|
||||
@@ -147,6 +147,70 @@ describe Enterprise do
|
||||
it "sets the enterprise contact to the owner by default" do
|
||||
enterprise.contact.should eq enterprise.owner
|
||||
end
|
||||
|
||||
context "prevent an wrong instagram link pattern" do
|
||||
it "expects to be invalid the instagram attribute @my-user" do
|
||||
e = build(:enterprise, instagram: '@my-user')
|
||||
expect(e).to_not be_valid
|
||||
end
|
||||
|
||||
it "expects to be invalid the instagram attribute https://facebook.com/user" do
|
||||
e = build(:enterprise, instagram: 'https://facebook.com/user')
|
||||
expect(e).to_not be_valid
|
||||
end
|
||||
|
||||
it "expects to be invalid the instagram attribute tagram.com/user" do
|
||||
e = build(:enterprise, instagram: 'tagram.com/user')
|
||||
expect(e).to_not be_valid
|
||||
end
|
||||
|
||||
it "expects to be invalid the instagram attribute https://instagram.com/user/preferences" do
|
||||
e = build(:enterprise, instagram: 'https://instagram.com/user/preferences')
|
||||
expect(e).to_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
context "accepted pattern" do
|
||||
it "expects to be valid empty instagram attribute" do
|
||||
e = build(:enterprise)
|
||||
expect(e).to be_valid
|
||||
end
|
||||
|
||||
it "expects be valid the instagram attribute @user" do
|
||||
e = build(:enterprise, instagram: '@user')
|
||||
expect(e).to be_valid
|
||||
end
|
||||
|
||||
it "expects be valid the instagram attribute @my_www5.example" do
|
||||
e = build(:enterprise, instagram: '@my_www5.example')
|
||||
expect(e).to be_valid
|
||||
end
|
||||
|
||||
it "expects be valid the instagram attribute http://instagram.com/user" do
|
||||
e = build(:enterprise, instagram: 'http://instagram.com/user')
|
||||
expect(e).to be_valid
|
||||
end
|
||||
|
||||
it "expects be valid the instagram attribute https://instagram.com/user/" do
|
||||
e = build(:enterprise, instagram: 'https://instagram.com/user/')
|
||||
expect(e).to be_valid
|
||||
end
|
||||
|
||||
it "expects be valid the instagram attribute https://www.instagram.com/user" do
|
||||
e = build(:enterprise, instagram: 'https://www.instagram.com/user')
|
||||
expect(e).to be_valid
|
||||
end
|
||||
|
||||
it "expects be valid the instagram attribute instagram.com/user" do
|
||||
e = build(:enterprise, instagram: 'instagram.com/user')
|
||||
expect(e).to be_valid
|
||||
end
|
||||
|
||||
it "renders the expected pattern" do
|
||||
e = build(:enterprise, instagram: 'instagram.com/user')
|
||||
expect(e.instagram).to eq('@user')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "preferred_shopfront_taxon_order" do
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe OrderCycle do
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
it "should be valid when built from factory" do
|
||||
build(:simple_order_cycle).should be_valid
|
||||
end
|
||||
@@ -528,7 +530,7 @@ describe OrderCycle do
|
||||
let!(:order5) { create(:completed_order_with_totals, distributor: shop, user: user, order_cycle: oc) }
|
||||
|
||||
before do
|
||||
Spree::Config[:mails_from] = "spree@example.com"
|
||||
setup_email
|
||||
end
|
||||
before { order5.cancel }
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ describe ProductImport::Settings do
|
||||
|
||||
context 'when there are settings' do
|
||||
let(:entry) do
|
||||
instance_double(ProductImport::SpreadsheetEntry, supplier_id: 1)
|
||||
instance_double(ProductImport::SpreadsheetEntry, enterprise_id: 1)
|
||||
end
|
||||
let(:import_settings) { { settings: {} } }
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
describe "importing products from a spreadsheet" do
|
||||
before do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "variant_unit_name", "on_demand"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "variant_unit_name", "on_demand"]
|
||||
csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g", "", ""]
|
||||
csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "6.50", "2", "kg", "", ""]
|
||||
csv << ["Pea Soup", "User Enterprise", "Vegetables", "8", "5.50", "750", "ml", "", "0"]
|
||||
@@ -135,7 +135,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
describe "when uploading a spreadsheet with some invalid entries" do
|
||||
before do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["Good Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"]
|
||||
csv << ["Bad Potatoes", "", "Vegetables", "6", "6.50", "1", ""]
|
||||
end
|
||||
@@ -176,7 +176,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
describe "when enterprises are not valid" do
|
||||
before do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["Product 1", "Non-existent Enterprise", "Vegetables", "5", "5.50", "500", "g"]
|
||||
csv << ["Product 2", "Non-Producer", "Vegetables", "5", "5.50", "500", "g"]
|
||||
end
|
||||
@@ -191,15 +191,15 @@ xdescribe ProductImport::ProductImporter do
|
||||
@importer.validate_entries
|
||||
entries = JSON.parse(@importer.entries_json)
|
||||
|
||||
expect(entries['2']['errors']['supplier']).to include "not found in database"
|
||||
expect(entries['3']['errors']['supplier']).to include "not enabled as a producer"
|
||||
expect(entries['2']['errors']['producer']).to include "not found in database"
|
||||
expect(entries['3']['errors']['producer']).to include "not enabled as a producer"
|
||||
end
|
||||
end
|
||||
|
||||
describe "adding new variants to existing products and updating exiting products" do
|
||||
before do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "display_name"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "display_name"]
|
||||
csv << ["Hypothetical Cake", "Another Enterprise", "Cake", "5", "5.50", "500", "g", "Preexisting Banana"]
|
||||
csv << ["Hypothetical Cake", "Another Enterprise", "Cake", "6", "3.50", "500", "g", "Emergent Coffee"]
|
||||
end
|
||||
@@ -245,7 +245,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
describe "adding new product and sub-variant at the same time" do
|
||||
before do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "display_name"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "display_name"]
|
||||
csv << ["Potatoes", "User Enterprise", "Vegetables", "5", "3.50", "500", "g", "Small Bag"]
|
||||
csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "5.50", "2", "kg", "Big Bag"]
|
||||
end
|
||||
@@ -289,7 +289,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
describe "updating various fields" do
|
||||
before do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "on_demand", "sku"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "on_demand", "sku"]
|
||||
csv << ["Beetroot", "And Another Enterprise", "Vegetables", "5", "3.50", "500", "g", "0", nil]
|
||||
csv << ["Tomato", "And Another Enterprise", "Vegetables", "6", "5.50", "500", "g", "1", "TOMS"]
|
||||
end
|
||||
@@ -331,7 +331,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
describe "updating non-updatable fields on existing products" do
|
||||
before do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["Beetroot", "And Another Enterprise", "Meat", "5", "3.50", "500", "g"]
|
||||
csv << ["Tomato", "And Another Enterprise", "Vegetables", "6", "5.50", "500", "Kg"]
|
||||
end
|
||||
@@ -358,7 +358,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
describe "when more than one product of the same name already exists with multiple variants each" do
|
||||
before do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "description", "on_hand", "price", "units", "unit_type", "display_name"]
|
||||
csv << ["name", "producer", "category", "description", "on_hand", "price", "units", "unit_type", "display_name"]
|
||||
csv << ["Oats", "User Enterprise", "Cereal", "", "50", "3.50", "500", "g", "Rolled Oats"] # Update
|
||||
csv << ["Oats", "User Enterprise", "Cereal", "", "80", "3.75", "500", "g", "Flaked Oats"] # Update
|
||||
csv << ["Oats", "User Enterprise", "Cereal", "", "60", "5.50", "500", "g", "Magic Oats"] # Add
|
||||
@@ -398,7 +398,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
describe "when importer processes create and update across multiple stages" do
|
||||
before do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "display_name"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "display_name"]
|
||||
csv << ["Bag of Oats", "User Enterprise", "Cereal", "60", "5.50", "500", "g", "Magic Oats"] # Add
|
||||
csv << ["Bag of Oats", "User Enterprise", "Cereal", "70", "8.50", "500", "g", "French Oats"] # Add
|
||||
csv << ["Bag of Oats", "User Enterprise", "Cereal", "80", "9.50", "500", "g", "Organic Oats"] # Add
|
||||
@@ -467,7 +467,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
describe "importing items into inventory" do
|
||||
before do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "producer", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["name", "distributor", "producer", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["Beans", "Another Enterprise", "User Enterprise", "5", "3.20", "500", "g"]
|
||||
csv << ["Sprouts", "Another Enterprise", "User Enterprise", "6", "6.50", "500", "g"]
|
||||
csv << ["Cabbage", "Another Enterprise", "User Enterprise", "2001", "1.50", "500", "g"]
|
||||
@@ -517,7 +517,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
|
||||
it "only allows product import into enterprises the user is permitted to manage" do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["My Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"]
|
||||
csv << ["Your Potatoes", "Another Enterprise", "Vegetables", "6", "6.50", "1", "kg"]
|
||||
end
|
||||
@@ -545,7 +545,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
|
||||
it "allows creating inventories for producers that a user's hub has permission for" do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "producer", "supplier", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["name", "producer", "distributor", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["Beans", "User Enterprise", "Another Enterprise", "777", "3.20", "500", "g"]
|
||||
end
|
||||
File.write('/tmp/test-m.csv', csv_data)
|
||||
@@ -572,7 +572,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
|
||||
it "does not allow creating inventories for producers that a user's hubs don't have permission for" do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["name", "producer", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["Beans", "User Enterprise", "5", "3.20", "500", "g"]
|
||||
csv << ["Sprouts", "User Enterprise", "6", "6.50", "500", "g"]
|
||||
end
|
||||
@@ -601,7 +601,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
|
||||
it "can reset all products for an enterprise that are not present in the uploaded file to zero stock" do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g"]
|
||||
csv << ["Beans", "User Enterprise", "Vegetables", "6", "6.50", "500", "g"]
|
||||
end
|
||||
@@ -640,7 +640,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
|
||||
it "can reset all inventory items for an enterprise that are not present in the uploaded file to zero stock" do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "producer", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["name", "distributor", "producer", "on_hand", "price", "units", "unit_type"]
|
||||
csv << ["Beans", "Another Enterprise", "User Enterprise", "6", "3.20", "500", "g"]
|
||||
csv << ["Sprouts", "Another Enterprise", "User Enterprise", "7", "6.50", "500", "g"]
|
||||
end
|
||||
@@ -681,7 +681,7 @@ xdescribe ProductImport::ProductImporter do
|
||||
|
||||
it "can overwrite fields with selected defaults when importing to product list" do
|
||||
csv_data = CSV.generate do |csv|
|
||||
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "tax_category_id", "available_on"]
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", "tax_category_id", "available_on"]
|
||||
csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g", tax_category.id, ""]
|
||||
csv << ["Potatoes", "User Enterprise", "Vegetables", "6", "6.50", "1", "kg", "", ""]
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Spree::Order do
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
describe "setting variant attributes" do
|
||||
it "sets attributes on line items for variants" do
|
||||
d = create(:distributor_enterprise)
|
||||
@@ -493,7 +495,7 @@ describe Spree::Order do
|
||||
describe "scopes" do
|
||||
describe "not_state" do
|
||||
before do
|
||||
Spree::Config[:mails_from] = "spree@example.com"
|
||||
setup_email
|
||||
end
|
||||
|
||||
it "finds only orders not in specified state" do
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Spree.user_class do
|
||||
include OpenFoodNetwork::EmailHelper
|
||||
|
||||
describe "associations" do
|
||||
it { should have_many(:owned_enterprises) }
|
||||
|
||||
@@ -72,7 +74,7 @@ describe Spree.user_class do
|
||||
|
||||
context "#create" do
|
||||
it "should send a confirmation email" do
|
||||
create(:mail_method)
|
||||
setup_email
|
||||
|
||||
expect do
|
||||
create(:user, email: 'new_user@example.com', confirmation_sent_at: nil, confirmed_at: nil)
|
||||
@@ -100,7 +102,7 @@ describe Spree.user_class do
|
||||
|
||||
context "confirming email" do
|
||||
it "should send a welcome email" do
|
||||
create(:mail_method)
|
||||
setup_email
|
||||
|
||||
expect do
|
||||
create(:user, confirmed_at: nil).confirm!
|
||||
|
||||
35
spec/services/search_orders_spec.rb
Normal file
35
spec/services/search_orders_spec.rb
Normal file
@@ -0,0 +1,35 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe SearchOrders do
|
||||
let!(:distributor) { create(:distributor_enterprise) }
|
||||
let!(:order1) { create(:order, distributor: distributor) }
|
||||
let!(:order2) { create(:order, distributor: distributor) }
|
||||
let!(:order3) { create(:order, distributor: distributor) }
|
||||
|
||||
let(:enterprise_user) { distributor.owner }
|
||||
|
||||
describe '#orders' do
|
||||
let(:params) { {} }
|
||||
let(:service) { SearchOrders.new(params, enterprise_user) }
|
||||
|
||||
it 'returns orders' do
|
||||
expect(service.orders.count).to eq 3
|
||||
end
|
||||
end
|
||||
|
||||
describe '#pagination_data' do
|
||||
let(:params) { { per_page: 15, page: 1 } }
|
||||
let(:service) { SearchOrders.new(params, enterprise_user) }
|
||||
|
||||
it 'returns pagination data' do
|
||||
pagination_data = {
|
||||
results: 3,
|
||||
pages: 1,
|
||||
page: 1,
|
||||
per_page: 15
|
||||
}
|
||||
|
||||
expect(service.pagination_data).to eq pagination_data
|
||||
end
|
||||
end
|
||||
end
|
||||
11
spec/support/email_helper.rb
Normal file
11
spec/support/email_helper.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
module OpenFoodNetwork
|
||||
module EmailHelper
|
||||
# Some specs trigger actions that send emails, for example creating an order.
|
||||
# But sending emails doesn't work out-of-the-box. This code sets it up.
|
||||
# It's here in a single place to allow an easy upgrade to Spree 2 which
|
||||
# needs a different implementation of this method.
|
||||
def setup_email
|
||||
create(:mail_method)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user