Convert CatalogItems controller spec to request spec

This commit is contained in:
Maikel Linke
2023-05-10 16:01:59 +10:00
committed by Konrad
parent 207a15e55c
commit dde4ea9334
3 changed files with 98 additions and 141 deletions

View File

@@ -1,141 +0,0 @@
# frozen_string_literal: true
require DfcProvider::Engine.root.join("spec/spec_helper")
describe DfcProvider::CatalogItemsController, 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 '.index' do
context 'with authorization token' do
before { authorise user.email }
context 'with an authenticated user' do
context 'with an enterprise' do
context 'given with an id' do
context 'related to the user' do
before { api_get :index, enterprise_id: 'default' }
it 'is successful' do
expect(response).to have_http_status :success
end
it 'renders the required content' do
expect(response.body)
.to include(variant.name)
expect(response.body)
.to include(variant.sku)
expect(response.body)
.to include("offers/#{variant.id}")
end
end
context 'not related to the user' do
let(:enterprise) { create(:enterprise) }
it 'returns not_found head' do
api_get :index, enterprise_id: enterprise.id
expect(response).to have_http_status :not_found
end
end
end
context 'as default' do
before { api_get :index, enterprise_id: 'default' }
it 'is successful' do
expect(response.status).to eq 200
end
it 'renders the required content' do
expect(response.body)
.to include(variant.name)
expect(response.body)
.to include(variant.sku)
expect(response.body)
.to include("offers/#{variant.id}")
end
end
end
context 'without a recorded enterprise' do
let(:enterprise) { create(:enterprise) }
it 'is not found' do
api_get :index, enterprise_id: 'default'
expect(response).to have_http_status :not_found
end
end
end
context 'without an authenticated user' do
before { authorise "other@user.net" }
it 'returns unauthorized head' do
authorise "other@user.net"
api_get :index, enterprise_id: 'default'
expect(response.response_code).to eq(401)
end
end
end
context 'without an authorization token' do
it 'returns unauthorized head' do
api_get :index, enterprise_id: enterprise.id
expect(response).to have_http_status :unauthorized
end
end
context "when logged in as app user" do
it "is successful" do
sign_in user
api_get :index, enterprise_id: enterprise.id
expect(response).to have_http_status :success
end
end
end
describe '.show' do
context 'with authorization token' do
before { authorise user.email }
context 'with an authenticated user' do
context 'with an enterprise' do
context 'given with an id' do
before do
api_get :show, enterprise_id: enterprise.id, id: variant.id
end
it 'is successful' do
expect(response).to have_http_status :success
end
it 'renders the required content' do
expect(response.body).to include('dfc-b:CatalogItem')
expect(response.body).to include("offers/#{variant.id}")
end
end
context 'with a variant not linked to the enterprise' do
before do
api_get :show,
enterprise_id: enterprise.id,
id: create(:simple_product).variants.first.id
end
it 'is not found' do
expect(response).to have_http_status :not_found
end
end
end
end
end
end
end

View File

@@ -0,0 +1,97 @@
# frozen_string_literal: true
require DfcProvider::Engine.root.join("spec/spec_helper")
describe "CatalogItems", 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 :index do
it "returns not_found without enterprise" do
items_path = enterprise_catalog_items_path(enterprise_id: "default")
get items_path, headers: auth_header(user.uid)
expect(response).to have_http_status :not_found
end
context "with existing variant" do
before { variant }
it "lists catalog items with offers of default enterprise" do
items_path = enterprise_catalog_items_path(enterprise_id: "default")
get items_path, headers: auth_header(user.uid)
expect(response).to have_http_status :ok
expect(response.body).to include variant.name
expect(response.body).to include variant.sku
expect(response.body).to include "offers/#{variant.id}"
end
it "lists catalog items with offers of requested enterprise" do
items_path = enterprise_catalog_items_path(enterprise_id: enterprise.id)
get items_path, headers: auth_header(user.uid)
expect(response).to have_http_status :ok
expect(response.body).to include variant.name
expect(response.body).to include variant.sku
expect(response.body).to include "offers/#{variant.id}"
end
it "returns not_found for unrelated enterprises" do
other_enterprise = create(:enterprise)
items_path = enterprise_catalog_items_path(enterprise_id: other_enterprise.id)
get items_path, headers: auth_header(user.uid)
expect(response).to have_http_status :not_found
end
it "returns unauthorized for unauthenticated users" do
items_path = enterprise_catalog_items_path(enterprise_id: "default")
get items_path, headers: {}
expect(response).to have_http_status :unauthorized
end
it "recognises app user sessions as logins" do
items_path = enterprise_catalog_items_path(enterprise_id: "default")
login_as user
get items_path, headers: {}
expect(response).to have_http_status :ok
end
end
end
describe :show do
it "returns a catalog item with offer" do
item_path = enterprise_catalog_item_path(
variant,
enterprise_id: enterprise.id
)
get item_path, headers: auth_header(user.uid)
expect(response).to have_http_status :ok
expect(response.body).to include "dfc-b:CatalogItem"
expect(response.body).to include "offers/#{variant.id}"
end
it "returns not_found for unrelated variant" do
item_path = enterprise_catalog_item_path(
create(:variant),
enterprise_id: enterprise.id
)
get item_path, headers: auth_header(user.uid)
expect(response).to have_http_status :not_found
end
end
end

View File

@@ -7,6 +7,7 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f }
RSpec.configure do |config|
config.include AuthorizationHelper, type: :request
config.include DfcProvider::Engine.routes.url_helpers, type: :request
config.include Warden::Test::Helpers, type: :request
config.around(:each) do |example|
# The DFC Connector fetches the context when loaded.