Merge branch 'master' into 2-0-stable-nov-8th

This commit is contained in:
luisramos0
2018-11-08 11:18:54 +00:00
101 changed files with 4970 additions and 760 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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 }

View File

@@ -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: {} } }

View File

@@ -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

View File

@@ -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

View File

@@ -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!