mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-11 03:40:20 +00:00
Create Api::BaseController to allow use of ActiveModelSerializers
Also add index action to Api::CustomersController
This commit is contained in:
@@ -3,13 +3,32 @@ require 'spec_helper'
|
||||
module Api
|
||||
describe CustomersController, type: :controller do
|
||||
include AuthenticationWorkflow
|
||||
include OpenFoodNetwork::ApiHelper
|
||||
render_views
|
||||
|
||||
let(:user) { create(:user) }
|
||||
let(:customer) { create(:customer, user: user) }
|
||||
let(:params) { { format: :json, id: customer.id, customer: { code: '123' } } }
|
||||
|
||||
describe "index" do
|
||||
let!(:customer1) { create(:customer) }
|
||||
let!(:customer2) { create(:customer) }
|
||||
|
||||
before do
|
||||
user.customers << customer1
|
||||
allow(controller).to receive(:spree_current_user) { user }
|
||||
end
|
||||
|
||||
it "lists customers associated with the current user" do
|
||||
spree_get :index
|
||||
expect(response.status).to eq 200
|
||||
expect(json_response.length).to eq 1
|
||||
expect(json_response.first[:id]).to eq customer1.id
|
||||
end
|
||||
end
|
||||
|
||||
describe "#update" do
|
||||
let(:customer) { create(:customer, user: user) }
|
||||
let(:params) { { format: :json, id: customer.id, customer: { code: '123' } } }
|
||||
|
||||
context "as a user who is not associated with the customer" do
|
||||
before do
|
||||
allow(controller).to receive(:spree_current_user) { create(:user) }
|
||||
@@ -30,7 +49,7 @@ module Api
|
||||
it "returns the id of the updated customer" do
|
||||
spree_post :update, params
|
||||
expect(response.status).to eq 200
|
||||
expect(response.body).to eq customer.id.to_s
|
||||
expect(json_response[:id]).to eq customer.id
|
||||
end
|
||||
end
|
||||
|
||||
@@ -40,7 +59,7 @@ module Api
|
||||
it "returns a 422, with an error message" do
|
||||
spree_post :update, params
|
||||
expect(response.status).to be 422
|
||||
expect(JSON.parse(response.body)['error']).to be
|
||||
expect(json_response[:error]).to be
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user