Merge branch 'master' of github.com:openfoodfoundation/openfoodnetwork into bulk-product-edit

This commit is contained in:
Maikel Linke
2015-05-21 18:03:11 +10:00
28 changed files with 334 additions and 247 deletions

View File

@@ -62,7 +62,7 @@ angular.module("ofn.admin").controller "AdminOrderMgmtCtrl", [
$scope.fetchOrders = ->
$scope.loading = true
dataFetcher("/api/orders/managed?template=bulk_index;page=1;per_page=500;q[state_not_eq]=canceled;q[completed_at_not_null]=true;q[completed_at_gt]=#{$scope.startDate};q[completed_at_lt]=#{$scope.endDate}").then (data) ->
dataFetcher("/admin/orders/managed?template=bulk_index;page=1;per_page=500;q[state_not_eq]=canceled;q[completed_at_not_null]=true;q[completed_at_gt]=#{$scope.startDate};q[completed_at_lt]=#{$scope.endDate}").then (data) ->
$scope.resetOrders data
$scope.loading = false

View File

@@ -7,7 +7,7 @@ Spree::Admin::OrdersController.class_eval do
# We need to add expections for collection actions other than :index here
# because spree_auth_devise causes load_order to be called, which results
# in an auth failure as the @order object is nil for collection actions
before_filter :check_authorization, :except => :bulk_management
before_filter :check_authorization, except: [:bulk_management, :managed]
# After updating an order, the fees should be updated as well
# Currently, adding or deleting line items does not trigger updating the
@@ -17,7 +17,7 @@ Spree::Admin::OrdersController.class_eval do
after_filter :update_distribution_charge, :only => :update
respond_override :index => { :html =>
{ :success => lambda {
{ :success => lambda {
# Filter orders to only show those distributed by current user (or all for admin user)
@orders = @search.result.includes([:user, :shipments, :payments]).
distributed_by_user(spree_current_user).
@@ -37,4 +37,10 @@ Spree::Admin::OrdersController.class_eval do
def update_distribution_charge
@order.update_distribution_charge!
end
def managed
permissions = OpenFoodNetwork::Permissions.new(spree_current_user)
@orders = permissions.editable_orders.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
render json: @orders, each_serializer: Api::Admin::OrderSerializer
end
end

View File

@@ -4,12 +4,4 @@ Spree::Api::OrdersController.class_eval do
# because Spree's API controller causes authorize_read! to be called, which
# results in an ActiveRecord::NotFound Exception as the order object is not
# defined for collection actions
before_filter :authorize_read!, :except => [:managed]
def managed
authorize! :admin, Spree::Order
authorize! :read, Spree::Order
@orders = Spree::Order.ransack(params[:q]).result.distributed_by_user(current_api_user).page(params[:page]).per(params[:per_page])
respond_with(@orders, default_template: :index)
end
end

View File

@@ -11,7 +11,7 @@ Spree::Api::ProductsController.class_eval do
# TODO: This should be named 'managed'. Is the action above used? Maybe we should remove it.
def bulk_products
@products = OpenFoodNetwork::Permissions.new(current_api_user).managed_products.
@products = OpenFoodNetwork::Permissions.new(current_api_user).editable_products.
merge(product_scope).
order('created_at DESC').
ransack(params[:q]).result.

View File

@@ -70,7 +70,8 @@ class OrderCycle < ActiveRecord::Base
# Order cycles where I managed an enterprise at either end of an outgoing exchange
# ie. coordinator or distibutor
joins(:exchanges).merge(Exchange.outgoing).
where('exchanges.receiver_id IN (?) OR exchanges.sender_id IN (?)', enterprises, enterprises)
where('exchanges.receiver_id IN (?) OR exchanges.sender_id IN (?)', enterprises, enterprises).
select('DISTINCT order_cycles.*')
}
scope :involving_managed_producers_of, lambda { |user|
@@ -79,7 +80,8 @@ class OrderCycle < ActiveRecord::Base
# Order cycles where I managed an enterprise at either end of an incoming exchange
# ie. coordinator or producer
joins(:exchanges).merge(Exchange.incoming).
where('exchanges.receiver_id IN (?) OR exchanges.sender_id IN (?)', enterprises, enterprises)
where('exchanges.receiver_id IN (?) OR exchanges.sender_id IN (?)', enterprises, enterprises).
select('DISTINCT order_cycles.*')
}
def self.first_opening_for(distributor)

View File

@@ -142,7 +142,7 @@ class AbilityDecorator
# during the order creation process from the admin backend
order.distributor.nil? || user.enterprises.include?(order.distributor)
end
can [:admin, :bulk_management], Spree::Order if user.admin? || user.enterprises.any?(&:is_distributor)
can [:admin, :bulk_management, :managed], Spree::Order if user.admin? || user.enterprises.any?(&:is_distributor)
can [:admin, :create], Spree::LineItem
can [:destroy], Spree::LineItem do |item|
user.admin? || user.enterprises.include?(order.distributor) || user == order.order_cycle.manager
@@ -154,7 +154,6 @@ class AbilityDecorator
can [:admin, :index, :read, :create, :edit, :update, :fire], Spree::ReturnAuthorization
can [:destroy], Spree::Adjustment do |adjustment|
# Sharing code with destroying a line item. This should be unified and probably applied for other actions as well.
binding.pry
if user.admin?
true
elsif adjustment.adjustable.instance_of? Spree::Order

View File

@@ -0,0 +1,14 @@
class Api::Admin::BasicOrderCycleSerializer < ActiveModel::Serializer
attributes :id, :name, :first_order, :last_order
has_many :suppliers, serializer: Api::Admin::IdNameSerializer
has_many :distributors, serializer: Api::Admin::IdNameSerializer
def first_order
object.orders_open_at.strftime("%F")
end
def last_order
(object.orders_close_at + 1.day).strftime("%F")
end
end

View File

@@ -0,0 +1,15 @@
class Api::Admin::LineItemSerializer < ActiveModel::Serializer
attributes :id, :quantity, :max_quantity, :supplier, :units_product, :units_variant
def supplier
Api::Admin::IdNameSerializer.new(object.product.supplier).serializable_hash
end
def units_product
Api::Admin::UnitsProductSerializer.new(object.product).serializable_hash
end
def units_variant
Api::Admin::UnitsVariantSerializer.new(object.variant).serializable_hash
end
end

View File

@@ -0,0 +1,31 @@
class Api::Admin::OrderSerializer < ActiveModel::Serializer
attributes :id, :number, :full_name, :email, :phone, :completed_at, :line_items
has_one :distributor, serializer: Api::Admin::IdNameSerializer
has_one :order_cycle, serializer: Api::Admin::BasicOrderCycleSerializer
def full_name
object.billing_address.nil? ? "" : ( object.billing_address.full_name || "" )
end
def email
object.email || ""
end
def phone
object.billing_address.nil? ? "a" : ( object.billing_address.phone || "" )
end
def completed_at
object.completed_at.blank? ? "" : object.completed_at.strftime("%F %T")
end
def line_items
# we used to have a scope here, but we are at the point where a user which can edit an order
# should be able to edit all of the line_items as well, making the scope redundant
ActiveModel::ArraySerializer.new(
object.line_items.order('id ASC'),
{each_serializer: Api::Admin::LineItemSerializer}
)
end
end

View File

@@ -0,0 +1,3 @@
class Api::Admin::UnitsProductSerializer < ActiveModel::Serializer
attributes :id, :name, :group_buy_unit_size, :variant_unit
end

View File

@@ -0,0 +1,8 @@
class Api::Admin::UnitsVariantSerializer < ActiveModel::Serializer
attributes :id, :full_name, :unit_value
def full_name
full_name = object.full_name
object.product.name + (full_name.empty? ? "" : ": #{full_name}")
end
end

View File

@@ -43,7 +43,7 @@
Shared Resource?
%div{ :class => "eight columns" }
%h6{ :class => "eight columns alpha", 'ng-show' => 'sharedResource', style: 'text-align: center;' } {{ selectedUnitsProduct.name + ": ALL" }}
%h6{ :class => "eight columns alpha", 'ng-hide' => 'sharedResource', style: 'text-align: center;' } {{ selectedUnitsVariant.unit_text }}
%h6{ :class => "eight columns alpha", 'ng-hide' => 'sharedResource', style: 'text-align: center;' } {{ selectedUnitsVariant.full_name }}
%div{ :class => "four columns omega" }
%h6{ :class => "four columns alpha", :style => 'text-align: right;' }
%a{ :href => '#', 'ng-click' => 'selectedUnitsVariant = {};selectedUnitsProduct = {};sharedResource=false;' } Clear
@@ -122,7 +122,7 @@
%th.hub{ 'ng-show' => 'columns.hub.visible' }
%a{ :href => '', 'ng-click' => "predicate = 'order.distributor.name'; reverse = !reverse" } Hub
%th.variant{ 'ng-show' => 'columns.variant.visible' }
%a{ :href => '', 'ng-click' => "predicate = 'units_variant.unit_text'; reverse = !reverse" } Product: Unit
%a{ :href => '', 'ng-click' => "predicate = 'units_variant.full_name'; reverse = !reverse" } Product: Unit
%th.quantity{ 'ng-show' => 'columns.quantity.visible' } Quantity
%th.max{ 'ng-show' => 'columns.max.visible' } Max
%th.actions
@@ -141,7 +141,7 @@
%td.order_cycle{ 'ng-show' => 'columns.order_cycle.visible' } {{ line_item.order.order_cycle.name }}
%td.hub{ 'ng-show' => 'columns.hub.visible' } {{ line_item.order.distributor.name }}
%td.variant{ 'ng-show' => 'columns.variant.visible' }
%a{ :href => '#', 'ng-click' => "setSelectedUnitsVariant(line_item.units_product,line_item.units_variant)" } {{ line_item.units_variant.unit_text }}
%a{ :href => '#', 'ng-click' => "setSelectedUnitsVariant(line_item.units_product,line_item.units_variant)" } {{ line_item.units_variant.full_name }}
%td.quantity{ 'ng-show' => 'columns.quantity.visible' }
%input{ :type => 'number', :name => 'quantity', 'ng-model' => "line_item.quantity", 'ofn-line-item-upd-attr' => "quantity" }
%td.max{ 'ng-show' => 'columns.max.visible' } {{ line_item.max_quantity }}
@@ -149,4 +149,4 @@
%a{ :class => "edit-order icon-edit no-text", 'ofn-confirm-link-path' => "/admin/orders/{{line_item.order.number}}/edit" }
%td.actions
%a{ 'ng-click' => "deleteLineItem(line_item)", :class => "delete-line-item icon-trash no-text" }
%input{ :type => "button", 'value' => 'Update', 'ng-click' => 'pendingChanges.submitAll()' }
%input{ :type => "button", 'value' => 'Update', 'ng-click' => 'pendingChanges.submitAll()' }

View File

@@ -1,5 +0,0 @@
object @line_item
attributes :id, :quantity, :max_quantity
node( :supplier ) { |li| partial 'api/enterprises/bulk_show', :object => li.product.supplier }
node( :units_product ) { |li| partial 'spree/api/products/units_show', :object => li.product }
node( :units_variant ) { |li| partial 'spree/api/variants/units_show', :object => li.variant }

View File

@@ -1,2 +0,0 @@
collection @orders.order('id ASC')
extends "spree/api/orders/bulk_show"

View File

@@ -1,14 +0,0 @@
object @order
attributes :id, :number
node( :full_name ) { |order| order.billing_address.nil? ? "" : ( order.billing_address.full_name || "" ) }
node( :email ) { |order| order.email || "" }
node( :phone ) { |order| order.billing_address.nil? ? "a" : ( order.billing_address.phone || "" ) }
node( :completed_at ) { |order| order.completed_at.blank? ? "" : order.completed_at.strftime("%F %T") }
node( :distributor ) { |order| partial 'api/enterprises/bulk_show', :object => order.distributor }
node( :order_cycle ) { |order| partial 'api/order_cycles/bulk_show', :object => order.order_cycle }
node( :line_items ) do |order|
order.line_items.managed_by(@current_api_user).order('id ASC').map do |line_item|
partial 'spree/api/line_items/bulk_show', :object => line_item
end
end

View File

@@ -1,2 +0,0 @@
object @product
attributes :id, :name, :group_buy_unit_size, :variant_unit

View File

@@ -1,9 +0,0 @@
object @variant
attributes :id
node( :unit_text ) do |v|
options_text = v.options_text
v.product.name + (options_text.empty? ? "" : ": #{options_text}")
end
node( :unit_value ) { |v| v.unit_value }

View File

@@ -169,6 +169,10 @@ Spree::Core::Engine.routes.prepend do
post :bulk_update, :on => :collection, :as => :bulk_update
end
resources :orders do
get :managed, on: :collection
end
end
resources :orders do

View File

@@ -106,10 +106,20 @@ module OpenFoodNetwork
Spree::LineItem.where(order_id: editable_orders)
end
def managed_products
def editable_products
managed_enterprise_products_ids = managed_enterprise_products.pluck :id
permitted_enterprise_products_ids = related_enterprise_products.pluck :id
Spree::Product.where('id IN (?)', managed_enterprise_products_ids + permitted_enterprise_products_ids)
permitted_enterprise_products_ids = products_supplied_by(
related_enterprises_granting(:manage_products).pluck(:id)
).pluck :id
Spree::Product.where('spree_products.id IN (?)', managed_enterprise_products_ids | permitted_enterprise_products_ids)
end
def visible_products
managed_enterprise_products_ids = managed_enterprise_products.pluck :id
permitted_enterprise_products_ids = products_supplied_by(
related_enterprises_granting(:manage_products).pluck(:id) | related_enterprises_granting(:add_to_order_cycle).pluck(:id)
).pluck :id
Spree::Product.where('spree_products.id IN (?)', managed_enterprise_products_ids | permitted_enterprise_products_ids)
end
def managed_product_enterprises
@@ -181,8 +191,8 @@ module OpenFoodNetwork
Spree::Product.managed_by(@user)
end
def related_enterprise_products
Spree::Product.where('supplier_id IN (?)', related_enterprises_granting(:manage_products))
def products_supplied_by(suppliers)
Spree::Product.where('supplier_id IN (?)', suppliers)
end
end
end

View File

@@ -9,7 +9,7 @@ module OpenFoodNetwork
def header
[
"Supplier",
"Supplier",
"Producer Suburb",
"Product",
"Product Properties",
@@ -36,6 +36,16 @@ module OpenFoodNetwork
end
end
def permissions
return @permissions unless @permissions.nil?
@permissions = OpenFoodNetwork::Permissions.new(@user)
end
def visible_products
return @visible_products unless @visible_products.nil?
@visible_products = permissions.visible_products
end
def variants
filter(child_variants) + filter(master_variants)
end
@@ -43,7 +53,7 @@ module OpenFoodNetwork
def child_variants
Spree::Variant.where(:is_master => false)
.joins(:product)
.merge(Spree::Product.managed_by(@user))
.merge(visible_products)
.order("spree_products.name")
end
@@ -53,7 +63,7 @@ module OpenFoodNetwork
.where("(select spree_variants.id from spree_variants as other_spree_variants
WHERE other_spree_variants.product_id = spree_variants.product_id
AND other_spree_variants.is_master = 'f' LIMIT 1) IS NULL")
.merge(Spree::Product.managed_by(@user))
.merge(visible_products)
.order("spree_products.name")
end

View File

@@ -1,9 +1,10 @@
require 'spec_helper'
describe Spree::Admin::OrdersController do
let!(:order) { create(:order) }
include AuthenticationWorkflow
context "updating an order with line items" do
let!(:order) { create(:order) }
let(:line_item) { create(:line_item) }
before { login_as_admin }
@@ -27,4 +28,140 @@ describe Spree::Admin::OrdersController do
}
end
end
describe "managed" do
render_views
let(:order_attributes) { [:id, :full_name, :email, :phone, :completed_at, :line_items, :distributor, :order_cycle, :number] }
def self.make_simple_data!
let!(:dist1) { FactoryGirl.create(:distributor_enterprise) }
let!(:order1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: dist1, billing_address: FactoryGirl.create(:address) ) }
let!(:order2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: dist1, billing_address: FactoryGirl.create(:address) ) }
let!(:order3) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: dist1, billing_address: FactoryGirl.create(:address) ) }
let!(:line_item1) { FactoryGirl.create(:line_item, order: order1) }
let!(:line_item2) { FactoryGirl.create(:line_item, order: order2) }
let!(:line_item3) { FactoryGirl.create(:line_item, order: order2) }
let!(:line_item4) { FactoryGirl.create(:line_item, 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!
it "should deny me access to managed orders" do
spree_get :managed, { :template => 'bulk_index', :format => :json }
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 :managed, { :template => 'bulk_index', :format => :json }
end
it "retrieves a list of orders with appropriate attributes, including line items with appropriate attributes" do
keys = json_response.first.keys.map{ |key| key.to_sym }
order_attributes.all?{ |attr| keys.include? attr }.should == true
end
it "retrieves a list of line items with appropriate attributes" do
li_keys = json_response.first['line_items'].first.keys.map{ |key| key.to_sym }
line_item_attributes.all?{ |attr| li_keys.include? attr }.should == true
end
it "sorts orders in ascending id order" do
ids = json_response.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
json_response.map{ |order| order['completed_at'] }.all?{ |a| a.match("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$") }.should == true
end
it "returns an array for line_items" do
json_response.map{ |order| order['line_items'] }.all?{ |a| a.is_a? Array }.should == true
end
it "returns quantity and max quantity at integers" do
json_response.map{ |order| order['line_items'] }.flatten.map{ |li| li['quantity'] }.all?{ |q| q.is_a? Fixnum }.should == true
json_response.map{ |order| order['line_items'] }.flatten.map{ |li| li['max_quantity'] }.all?{ |mq| mq.nil? || mq.is_a?( Fixnum ) }.should == true
end
it "returns supplier object with id and name keys" do
json_response.map{ |order| order['line_items'] }.flatten.map{ |li| li['supplier'] }.all?{ |s| s.has_key?('id') && s.has_key?('name') }.should == true
end
it "returns distributor object with id and name keys" do
json_response.map{ |order| order['distributor'] }.all?{ |d| d.has_key?('id') && d.has_key?('name') }.should == true
end
it "retrieves the order number" do
json_response.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) { FactoryGirl.create(:order, order_cycle: order_cycle, state: 'complete', completed_at: Time.now, distributor: distributor1, billing_address: FactoryGirl.create(:address) ) }
let!(:line_item1) { FactoryGirl.create(:line_item, order: order1, product: FactoryGirl.create(:product, supplier: supplier)) }
let!(:line_item2) { FactoryGirl.create(:line_item, order: order1, product: FactoryGirl.create(:product, supplier: supplier)) }
let!(:order2) { FactoryGirl.create(:order, order_cycle: order_cycle, state: 'complete', completed_at: Time.now, distributor: distributor2, billing_address: FactoryGirl.create(:address) ) }
let!(:line_item3) { FactoryGirl.create(:line_item, order: order2, product: FactoryGirl.create(:product, supplier: supplier)) }
context "producer enterprise" do
before do
controller.stub spree_current_user: supplier.owner
spree_get :managed, { :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 :managed, { :format => :json }
end
it "retrieves a list of orders" do
keys = json_response.first.keys.map{ |key| key.to_sym }
order_attributes.all?{ |attr| keys.include? attr }.should == true
end
it "only displays line items from orders for which my enterprise is the order_cycle coorinator" do
json_response.map{ |order| order['line_items'] }.flatten.map{ |line_item| line_item["id"] }.sort.should == [line_item1.id, line_item2.id, line_item3.id].sort
end
end
context "hub enterprise" do
before do
controller.stub spree_current_user: distributor1.owner
spree_get :managed, { :format => :json }
end
it "retrieves a list of orders" do
keys = json_response.first.keys.map{ |key| key.to_sym }
order_attributes.all?{ |attr| keys.include? attr }.should == true
end
it "only displays line items from orders for which my enterprise is a distributor" do
json_response.map{ |order| order['line_items'] }.flatten.map{ |line_item| line_item["id"] }.sort.should == [line_item1.id, line_item2.id].sort
end
end
end
end
end

View File

@@ -3,127 +3,6 @@ require 'spree/api/testing_support/helpers'
module Spree
describe Spree::Api::OrdersController do
include Spree::Api::TestingSupport::Helpers
render_views
before do
stub_authentication!
Spree.user_class.stub :find_by_spree_api_key => current_api_user
end
let(:order_attributes) { [:id, :full_name, :email, :phone, :completed_at, :line_items, :distributor, :order_cycle, :number] }
def self.make_simple_data!
let!(:dist1) { FactoryGirl.create(:distributor_enterprise) }
let!(:order1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: dist1, billing_address: FactoryGirl.create(:address) ) }
let!(:order2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: dist1, billing_address: FactoryGirl.create(:address) ) }
let!(:order3) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: dist1, billing_address: FactoryGirl.create(:address) ) }
let!(:line_item1) { FactoryGirl.create(:line_item, order: order1) }
let!(:line_item2) { FactoryGirl.create(:line_item, order: order2) }
let!(:line_item3) { FactoryGirl.create(:line_item, order: order2) }
let!(:line_item4) { FactoryGirl.create(:line_item, order: order3) }
let(:line_item_attributes) { [:id, :quantity, :max_quantity, :supplier, :units_product, :units_variant] }
end
context "as a normal user" do
sign_in_as_user!
make_simple_data!
it "should deny me access to managed orders" do
spree_get :managed, { :template => 'bulk_index', :format => :json }
assert_unauthorized!
end
end
context "as an administrator" do
sign_in_as_admin!
make_simple_data!
before :each do
spree_get :managed, { :template => 'bulk_index', :format => :json }
end
it "retrieves a list of orders with appropriate attributes, including line items with appropriate attributes" do
keys = json_response.first.keys.map{ |key| key.to_sym }
order_attributes.all?{ |attr| keys.include? attr }.should == true
end
it "retrieves a list of line items with appropriate attributes" do
li_keys = json_response.first['line_items'].first.keys.map{ |key| key.to_sym }
line_item_attributes.all?{ |attr| li_keys.include? attr }.should == true
end
it "sorts orders in ascending id order" do
ids = json_response.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
json_response.map{ |order| order['completed_at'] }.all?{ |a| a.match("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$") }.should == true
end
it "returns an array for line_items" do
json_response.map{ |order| order['line_items'] }.all?{ |a| a.is_a? Array }.should == true
end
it "returns quantity and max quantity at integers" do
json_response.map{ |order| order['line_items'] }.flatten.map{ |li| li['quantity'] }.all?{ |q| q.is_a? Fixnum }.should == true
json_response.map{ |order| order['line_items'] }.flatten.map{ |li| li['max_quantity'] }.all?{ |mq| mq.nil? || mq.is_a?( Fixnum ) }.should == true
end
it "returns supplier object with id and name keys" do
json_response.map{ |order| order['line_items'] }.flatten.map{ |li| li['supplier'] }.all?{ |s| s.has_key?('id') && s.has_key?('name') }.should == true
end
it "returns distributor object with id and name keys" do
json_response.map{ |order| order['distributor'] }.all?{ |d| d.has_key?('id') && d.has_key?('name') }.should == true
end
it "retrieves the order number" do
json_response.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!(:order1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: distributor1, billing_address: FactoryGirl.create(:address) ) }
let!(:line_item1) { FactoryGirl.create(:line_item, order: order1, product: FactoryGirl.create(:product, supplier: supplier)) }
let!(:line_item2) { FactoryGirl.create(:line_item, order: order1, product: FactoryGirl.create(:product, supplier: supplier)) }
let!(:order2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: distributor2, billing_address: FactoryGirl.create(:address) ) }
let!(:line_item3) { FactoryGirl.create(:line_item, order: order2, product: FactoryGirl.create(:product, supplier: supplier)) }
context "producer enterprise" do
sign_in_as_enterprise_user! [:supplier]
before :each do
spree_get :managed, { :template => 'bulk_index', :format => :json }
end
it "does not display line item for which my enteprise is a supplier" do
response.status.should == 401
end
end
context "hub enterprise" do
sign_in_as_enterprise_user! [:distributor1]
before :each do
spree_get :managed, { :template => 'bulk_index', :format => :json }
end
it "retrieves a list of orders" do
keys = json_response.first.keys.map{ |key| key.to_sym }
order_attributes.all?{ |attr| keys.include? attr }.should == true
end
it "only displays line items from orders for which my enterprise is a distributor" do
json_response.map{ |order| order['line_items'] }.flatten.map{ |line_item| line_item["id"] }.should == [line_item1.id, line_item2.id]
end
end
end
end
end

View File

@@ -13,7 +13,6 @@ module Spree
let!(:product3) { FactoryGirl.create(:product, supplier: supplier) }
let(:product_other_supplier) { FactoryGirl.create(:product, supplier: supplier2) }
let(:attributes) { [:id, :name, :supplier, :price, :on_hand, :available_on, :permalink_live] }
let(:unit_attributes) { [:id, :name, :group_buy_unit_size, :variant_unit] }
before do
stub_authentication!
@@ -72,12 +71,6 @@ module Spree
attributes.all?{ |attr| keys.include? attr }.should == true
end
it "retrieves a list of products with attributes relating to units" do
spree_get :show, { :id => product1.id, :template => "units_show", :format => :json }
keys = json_response.keys.map{ |key| key.to_sym }
unit_attributes.all?{ |attr| keys.include? attr }.should == true
end
it "sorts products in ascending id order" do
spree_get :index, { :template => 'bulk_index', :format => :json }
ids = json_response.map{ |product| product['id'] }

View File

@@ -9,7 +9,6 @@ module Spree
let!(:variant2) { FactoryGirl.create(:variant) }
let!(:variant3) { FactoryGirl.create(:variant) }
let(:attributes) { [:id, :options_text, :price, :on_hand, :unit_value, :unit_description, :on_demand, :display_as, :display_name] }
let(:unit_attributes) { [:id, :unit_text, :unit_value] }
before do
stub_authentication!
@@ -25,12 +24,6 @@ module Spree
attributes.all?{ |attr| keys.include? attr }.should == true
end
it "retrieves a list of variants with attributes relating to units" do
spree_get :show, { :id => variant1.id, :template => "units_show", :format => :json }
keys = json_response.keys.map{ |key| key.to_sym }
unit_attributes.all?{ |attr| keys.include? attr }.should == true
end
it "is denied access when trying to delete a variant" do
product = create(:product)
variant = product.master

View File

@@ -183,6 +183,10 @@ FactoryGirl.define do
end
end
factory :order_with_distributor, :parent => :order do
distributor { create(:distributor_enterprise) }
end
factory :zone_with_member, :parent => :zone do
default_tax true

View File

@@ -18,9 +18,9 @@ feature %q{
end
context "displaying the list of line items" do
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o3) { FactoryGirl.create(:order, state: 'address', completed_at: nil ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:o2) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:o3) { FactoryGirl.create(:order_with_distributor, state: 'address', completed_at: nil ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1 ) }
let!(:li2) { FactoryGirl.create(:line_item, order: o2 ) }
let!(:li3) { FactoryGirl.create(:line_item, order: o3 ) }
@@ -41,8 +41,8 @@ feature %q{
end
context "displaying individual columns" do
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, bill_address: FactoryGirl.create(:address) ) }
let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, bill_address: nil ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now, bill_address: FactoryGirl.create(:address) ) }
let!(:o2) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now, bill_address: nil ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1 ) }
let!(:li2) { FactoryGirl.create(:line_item, order: o2, product: FactoryGirl.create(:product_with_option_types) ) }
@@ -94,7 +94,7 @@ feature %q{
end
context "tracking changes" do
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1, :quantity => 5 ) }
before :each do
@@ -117,7 +117,7 @@ feature %q{
end
context "submitting data to the server" do
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1, :quantity => 5 ) }
before :each do
@@ -141,7 +141,7 @@ feature %q{
admin_user = quick_login_as_admin
end
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1, :quantity => 5 ) }
context "using column display toggle" do
@@ -171,7 +171,7 @@ feature %q{
context "supplier filter" do
let!(:s1) { create(:supplier_enterprise) }
let!(:s2) { create(:supplier_enterprise) }
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1, product: create(:product, supplier: s1) ) }
let!(:li2) { FactoryGirl.create(:line_item, order: o1, product: create(:product, supplier: s2) ) }
@@ -205,8 +205,8 @@ feature %q{
context "distributor filter" do
let!(:d1) { create(:distributor_enterprise) }
let!(:d2) { create(:distributor_enterprise) }
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: d1 ) }
let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: d2 ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now, distributor: d1 ) }
let!(:o2) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now, distributor: d2 ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1 ) }
let!(:li2) { FactoryGirl.create(:line_item, order: o2 ) }
@@ -241,8 +241,8 @@ feature %q{
let!(:distributor) { create(:distributor_enterprise) }
let!(:oc1) { FactoryGirl.create(:simple_order_cycle, distributors: [distributor]) }
let!(:oc2) { FactoryGirl.create(:simple_order_cycle, distributors: [distributor]) }
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, order_cycle: oc1 ) }
let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, order_cycle: oc2 ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now, order_cycle: oc1 ) }
let!(:o2) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now, order_cycle: oc2 ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1 ) }
let!(:li2) { FactoryGirl.create(:line_item, order: o2 ) }
@@ -284,8 +284,8 @@ feature %q{
let!(:oc2) { FactoryGirl.create(:simple_order_cycle, suppliers: [s2], distributors: [d2] ) }
let!(:p1) { FactoryGirl.create(:product, supplier: s1) }
let!(:p2) { FactoryGirl.create(:product, supplier: s2) }
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: d1, order_cycle: oc1 ) }
let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: d2, order_cycle: oc2 ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now, distributor: d1, order_cycle: oc1 ) }
let!(:o2) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now, distributor: d2, order_cycle: oc2 ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1, product: p1 ) }
let!(:li2) { FactoryGirl.create(:line_item, order: o2, product: p2 ) }
@@ -328,9 +328,9 @@ feature %q{
end
context "using quick search" do
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o3) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:o2) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:o3) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1 ) }
let!(:li2) { FactoryGirl.create(:line_item, order: o2 ) }
let!(:li3) { FactoryGirl.create(:line_item, order: o3 ) }
@@ -355,9 +355,9 @@ feature %q{
end
context "using date restriction controls" do
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: (Date.today - 8).strftime("%F %T") ) }
let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o3) { FactoryGirl.create(:order, state: 'complete', completed_at: (Date.today + 2).strftime("%F %T") ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: (Date.today - 8).strftime("%F %T") ) }
let!(:o2) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:o3) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: (Date.today + 2).strftime("%F %T") ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1, :quantity => 1 ) }
let!(:li2) { FactoryGirl.create(:line_item, order: o2, :quantity => 2 ) }
let!(:li3) { FactoryGirl.create(:line_item, order: o3, :quantity => 3 ) }
@@ -429,8 +429,8 @@ feature %q{
end
context "bulk action controls" do
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:o2) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1 ) }
let!(:li2) { FactoryGirl.create(:line_item, order: o2 ) }
@@ -496,8 +496,8 @@ feature %q{
context "using action buttons" do
context "using edit buttons" do
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:o2) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1 ) }
let!(:li2) { FactoryGirl.create(:line_item, order: o2 ) }
@@ -515,8 +515,8 @@ feature %q{
end
context "using delete buttons" do
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:o2) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1 ) }
let!(:li2) { FactoryGirl.create(:line_item, order: o2 ) }
@@ -539,13 +539,13 @@ feature %q{
end
context "clicking the link on variant name" do
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:o2) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:li1) { FactoryGirl.create(:line_item, order: o1 ) }
let!(:li2) { FactoryGirl.create(:line_item, order: o2 ) }
let!(:p3) { FactoryGirl.create(:product_with_option_types, group_buy: true, group_buy_unit_size: 5000, variant_unit: "weight", variants: [FactoryGirl.create(:variant, unit_value: 1000)] ) }
let!(:v3) { p3.variants.first }
let!(:o3) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now ) }
let!(:o3) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) }
let!(:li3) { FactoryGirl.create(:line_item, order: o3, variant: v3, quantity: 3, max_quantity: 6 ) }
let!(:li4) { FactoryGirl.create(:line_item, order: o2, variant: v3, quantity: 1, max_quantity: 3 ) }
@@ -605,8 +605,8 @@ feature %q{
let(:s1) { create(:supplier_enterprise, name: 'First Supplier') }
let(:d1) { create(:distributor_enterprise, name: 'First Distributor') }
let(:d2) { create(:distributor_enterprise, name: 'Another Distributor') }
let!(:o1) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: d1 ) }
let!(:o2) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now, distributor: d2 ) }
let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now, distributor: d1 ) }
let!(:o2) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now, distributor: d2 ) }
let!(:line_item_distributed) { FactoryGirl.create(:line_item, order: o1, product: create(:product, supplier: s1) ) }
let!(:line_item_not_distributed) { FactoryGirl.create(:line_item, order: o2, product: create(:product, supplier: s1) ) }

View File

@@ -43,7 +43,7 @@ describe "AdminOrderMgmtCtrl", ->
describe "fetching orders", ->
beforeEach ->
scope.initialiseVariables()
httpBackend.expectGET("/api/orders/managed?template=bulk_index;page=1;per_page=500;q[state_not_eq]=canceled;q[completed_at_not_null]=true;q[completed_at_gt]=SomeDate;q[completed_at_lt]=SomeDate").respond "list of orders"
httpBackend.expectGET("/admin/orders/managed?template=bulk_index;page=1;per_page=500;q[state_not_eq]=canceled;q[completed_at_not_null]=true;q[completed_at_gt]=SomeDate;q[completed_at_lt]=SomeDate").respond "list of orders"
it "makes a call to dataFetcher, with current start and end date parameters", ->
scope.fetchOrders()

View File

@@ -164,23 +164,53 @@ module OpenFoodNetwork
end
end
describe "finding managed products" do
let!(:p1) { create(:simple_product) }
let!(:p2) { create(:simple_product) }
describe "finding editable products" do
let!(:p1) { create(:simple_product, supplier: create(:supplier_enterprise) ) }
let!(:p2) { create(:simple_product, supplier: create(:supplier_enterprise) ) }
before do
permissions.stub(:managed_enterprise_products) { Spree::Product.where('1=0') }
permissions.stub(:related_enterprise_products) { Spree::Product.where('1=0') }
allow(permissions).to receive(:related_enterprises_granting).with(:manage_products) { Enterprise.where("1=0") }
end
it "returns products produced by managed enterprises" do
permissions.stub(:managed_enterprise_products) { Spree::Product.where(id: p1) }
permissions.managed_products.should == [p1]
permissions.editable_products.should == [p1]
end
it "returns products produced by permitted enterprises" do
permissions.stub(:related_enterprise_products) { Spree::Product.where(id: p2) }
permissions.managed_products.should == [p2]
allow(permissions).to receive(:related_enterprises_granting).
with(:manage_products) { Enterprise.where(id: p2.supplier) }
permissions.editable_products.should == [p2]
end
end
describe "finding visible products" do
let!(:p1) { create(:simple_product, supplier: create(:supplier_enterprise) ) }
let!(:p2) { create(:simple_product, supplier: create(:supplier_enterprise) ) }
let!(:p3) { create(:simple_product, supplier: create(:supplier_enterprise) ) }
before do
permissions.stub(:managed_enterprise_products) { Spree::Product.where("1=0") }
allow(permissions).to receive(:related_enterprises_granting).with(:manage_products) { Enterprise.where("1=0") }
allow(permissions).to receive(:related_enterprises_granting).with(:add_to_order_cycle) { Enterprise.where("1=0") }
end
it "returns products produced by managed enterprises" do
permissions.stub(:managed_enterprise_products) { Spree::Product.where(id: p1) }
permissions.visible_products.should == [p1]
end
it "returns products produced by enterprises that have granted manage products" do
allow(permissions).to receive(:related_enterprises_granting).
with(:manage_products) { Enterprise.where(id: p2.supplier) }
permissions.visible_products.should == [p2]
end
it "returns products produced by enterprises that have granted P-OC" do
allow(permissions).to receive(:related_enterprises_granting).
with(:add_to_order_cycle) { Enterprise.where(id: p3.supplier) }
permissions.visible_products.should == [p3]
end
end
@@ -232,17 +262,6 @@ module OpenFoodNetwork
end
end
describe "finding the supplied products of related enterprises" do
let!(:e) { create(:enterprise) }
let!(:p) { create(:simple_product, supplier: e) }
it "returns supplied products" do
permissions.should_receive(:related_enterprises_granting).with(:manage_products) { [e] }
permissions.send(:related_enterprise_products).should == [p]
end
end
describe "finding orders that are visible in reports" do
let(:distributor) { create(:distributor_enterprise) }
let(:coordinator) { create(:distributor_enterprise) }