mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Display variant override errors
This commit is contained in:
@@ -46,7 +46,21 @@ angular.module("ofn.admin").controller "AdminVariantOverridesCtrl", ($scope, $ti
|
||||
DirtyVariantOverrides.save()
|
||||
.success (data) ->
|
||||
DirtyVariantOverrides.clear()
|
||||
#VariantOverrides.update data.variant_overrides
|
||||
$timeout -> StatusMessage.display 'success', 'Changes saved.'
|
||||
.error (data, status) ->
|
||||
$timeout -> StatusMessage.display 'failure', 'Oh no!'
|
||||
$timeout -> StatusMessage.display 'failure', $scope.updateError(data, status)
|
||||
|
||||
|
||||
$scope.updateError = (data, status) ->
|
||||
if status == 401
|
||||
"I couldn't get authorisation to save those changes, so they remain unsaved."
|
||||
|
||||
else if status == 400 && data.errors?
|
||||
errors = []
|
||||
for field, field_errors of data.errors
|
||||
errors = errors.concat field_errors
|
||||
errors = errors.join ', '
|
||||
"I had some trouble saving: #{errors}"
|
||||
|
||||
else
|
||||
"Oh no! I was unable to save your changes."
|
||||
|
||||
@@ -14,7 +14,7 @@ feature %q{
|
||||
let!(:hub) { create(:distributor_enterprise) }
|
||||
let!(:hub2) { create(:distributor_enterprise) }
|
||||
let!(:producer) { create(:supplier_enterprise) }
|
||||
let!(:er) { create(:enterprise_relationship, parent: producer, child: hub,
|
||||
let!(:er1) { create(:enterprise_relationship, parent: producer, child: hub,
|
||||
permissions_list: [:add_to_order_cycle]) }
|
||||
|
||||
context "as an enterprise user" do
|
||||
@@ -41,7 +41,7 @@ feature %q{
|
||||
let!(:variant) { create(:variant, product: product, unit_value: 1, price: 1.23, on_hand: 12) }
|
||||
let!(:producer2) { create(:supplier_enterprise) }
|
||||
let!(:product2) { create(:simple_product, supplier: producer2) }
|
||||
let!(:er) { create(:enterprise_relationship, parent: producer2, child: hub2,
|
||||
let!(:er2) { create(:enterprise_relationship, parent: producer2, child: hub2,
|
||||
permissions_list: [:add_to_order_cycle]) }
|
||||
|
||||
before do
|
||||
@@ -84,6 +84,34 @@ feature %q{
|
||||
vo.price.should == 777.77
|
||||
vo.count_on_hand.should == 123
|
||||
end
|
||||
|
||||
it "displays an error when unauthorised to access the page" do
|
||||
fill_in "variant-overrides-#{variant.id}-price", with: '777.77'
|
||||
fill_in "variant-overrides-#{variant.id}-count-on-hand", with: '123'
|
||||
page.should have_content "Changes to one override remain unsaved."
|
||||
|
||||
user.enterprises.clear
|
||||
|
||||
expect do
|
||||
click_button 'Save Changes'
|
||||
page.should have_content "I couldn't get authorisation to save those changes, so they remain unsaved."
|
||||
end.to change(VariantOverride, :count).by(0)
|
||||
end
|
||||
|
||||
it "displays an error when unauthorised to update a particular override" do
|
||||
fill_in "variant-overrides-#{variant.id}-price", with: '777.77'
|
||||
fill_in "variant-overrides-#{variant.id}-count-on-hand", with: '123'
|
||||
page.should have_content "Changes to one override remain unsaved."
|
||||
|
||||
EnterpriseRole.where(user_id: user).where('enterprise_id != ?', producer).destroy_all
|
||||
er1.destroy
|
||||
er2.destroy
|
||||
|
||||
expect do
|
||||
click_button 'Save Changes'
|
||||
page.should have_content "I couldn't get authorisation to save those changes, so they remain unsaved."
|
||||
end.to change(VariantOverride, :count).by(0)
|
||||
end
|
||||
end
|
||||
|
||||
context "with overrides" do
|
||||
|
||||
@@ -42,4 +42,16 @@ describe "VariantOverridesCtrl", ->
|
||||
it "does nothing when no selection has been made", ->
|
||||
scope.hub_id = ''
|
||||
scope.selectHub
|
||||
expect(scope.hub).toBeNull
|
||||
expect(scope.hub).toBeNull
|
||||
|
||||
describe "updating", ->
|
||||
describe "error messages", ->
|
||||
it "returns an unauthorised message upon 401", ->
|
||||
expect(scope.updateError({}, 401)).toEqual "I couldn't get authorisation to save those changes, so they remain unsaved."
|
||||
|
||||
it "returns errors when they are provided", ->
|
||||
data = {errors: {base: ["Hub can't be blank", "Variant can't be blank"]}}
|
||||
expect(scope.updateError(data, 400)).toEqual "I had some trouble saving: Hub can't be blank, Variant can't be blank"
|
||||
|
||||
it "returns a generic message otherwise", ->
|
||||
expect(scope.updateError({}, 500)).toEqual "Oh no! I was unable to save your changes."
|
||||
Reference in New Issue
Block a user