Convert SuppliedProducts controller spec to request spec

This commit is contained in:
Maikel Linke
2023-05-10 15:19:36 +10:00
committed by Konrad
parent 742468efd2
commit 207a15e55c
2 changed files with 58 additions and 80 deletions

View File

@@ -1,80 +0,0 @@
# frozen_string_literal: true
require DfcProvider::Engine.root.join("spec/spec_helper")
describe DfcProvider::SuppliedProductsController, type: :controller do
include AuthorizationHelper
render_views
let!(:user) { create(:oidc_user) }
let!(:enterprise) { create(:distributor_enterprise, owner: user) }
let!(:product) { create(:simple_product, supplier: enterprise ) }
let!(:variant) { product.variants.first }
describe '.show' do
context 'with authorization token' do
before do
request.headers['Authorization'] = 'Bearer 123456.abcdef.123456'
end
context 'with an authenticated user' do
before do
allow_any_instance_of(AuthorizationControl)
.to receive(:user)
.and_return(user)
end
context 'with an enterprise' do
context 'given with an id' do
before do
api_get :show, enterprise_id: 'default', id: variant.id
end
it 'is successful' do
expect(response).to be_successful
end
it 'renders the required content' do
expect(response.body).to include(variant.name)
end
end
context 'given with a wrong id' do
before { api_get :show, enterprise_id: 'default', id: 999 }
it 'is not found' do
expect(response).to be_not_found
end
end
end
end
end
end
describe "#update" do
routes { DfcProvider::Engine.routes }
it "requires authorisation" do
api_put :update, enterprise_id: "default", id: "0"
expect(response).to have_http_status :unauthorized
end
describe "with authorisation" do
before { authorise user.email }
it "updates the variant's name" do
params = { enterprise_id: enterprise.id, id: variant.id }
request_body = File.read(File.join(__dir__, "../../support/patch_product.json"))
expect {
put(:update, params: params, body: request_body)
expect(response).to have_http_status :success
variant.reload
}.to change {
variant.name
}
end
end
end
end

View File

@@ -0,0 +1,58 @@
# frozen_string_literal: true
require DfcProvider::Engine.root.join("spec/spec_helper")
describe "SuppliedProducts", type: :request do
let!(:user) { create(:oidc_user) }
let!(:enterprise) { create(:distributor_enterprise, owner: user) }
let!(:product) { create(:simple_product, supplier: enterprise ) }
let!(:variant) { product.variants.first }
describe :show do
it "returns variants" do
get enterprise_supplied_product_path(
variant.id, enterprise_id: enterprise.id
), headers: auth_header(user.uid)
expect(response).to have_http_status :ok
expect(response.body).to include variant.name
end
it "doesn't find unrelated variants" do
other_variant = create(:variant)
get enterprise_supplied_product_path(
other_variant.id, enterprise_id: enterprise.id
), headers: auth_header(user.uid)
expect(response).to have_http_status :not_found
end
end
describe :update do
it "requires authorisation" do
put enterprise_supplied_product_path(
variant.id, enterprise_id: enterprise.id
), headers: {}
expect(response).to have_http_status :unauthorized
end
it "updates a variant's name" do
params = { enterprise_id: enterprise.id, id: variant.id }
request_body = DfcProvider::Engine.root.join("spec/support/patch_product.json").read
expect {
put(
enterprise_supplied_product_path(params),
params: request_body,
headers: auth_header(user.uid)
)
expect(response).to have_http_status :success
variant.reload
}.to change {
variant.name
}
end
end
end