From 8523fbea5d9024e10d94fc08e7378ebfad863472 Mon Sep 17 00:00:00 2001 From: rasikasri <44141067+rasikasri@users.noreply.github.com> Date: Tue, 14 May 2024 10:12:34 +0100 Subject: [PATCH] GL-367: Add Admin UI screen to list the overrides (#705) GL-457: Add admin UI to create new overrides GL-367: Add Admin UI screen to list the overrides GL-458: Add admin UI to remove overrides (#706) --- ...mpting_certificate_overrides_controller.rb | 45 ++++++++++++ .../exempting_certificate_override.rb | 14 ++++ .../index.html.erb | 40 +++++++++++ .../new.html.erb | 16 +++++ app/views/layouts/application.html.erb | 5 +- config/routes.rb | 1 + .../exempting_certificate_override_factory.rb | 9 +++ .../exempting_certificate_override_spec.rb | 41 +++++++++++ ...g_certificate_overrides_controller_spec.rb | 70 +++++++++++++++++++ 9 files changed, 240 insertions(+), 1 deletion(-) create mode 100644 app/controllers/green_lanes/exempting_certificate_overrides_controller.rb create mode 100644 app/models/green_lanes/exempting_certificate_override.rb create mode 100644 app/views/green_lanes/exempting_certificate_overrides/index.html.erb create mode 100644 app/views/green_lanes/exempting_certificate_overrides/new.html.erb create mode 100644 spec/factories/green_lanes/exempting_certificate_override_factory.rb create mode 100644 spec/models/green_lanes/exempting_certificate_override_spec.rb create mode 100644 spec/requests/green_lanes/exempting_certificate_overrides_controller_spec.rb diff --git a/app/controllers/green_lanes/exempting_certificate_overrides_controller.rb b/app/controllers/green_lanes/exempting_certificate_overrides_controller.rb new file mode 100644 index 00000000..65c134e7 --- /dev/null +++ b/app/controllers/green_lanes/exempting_certificate_overrides_controller.rb @@ -0,0 +1,45 @@ +module GreenLanes + class ExemptingCertificateOverridesController < AuthenticatedController + before_action :disable_service_switching! + before_action :check_service + def index + @exempting_certificate_overrides = GreenLanes::ExemptingCertificateOverride.all(page: current_page).fetch + end + + def new + @exempting_certificate_override = GreenLanes::ExemptingCertificateOverride.new + end + + def create + @exempting_certificate_override = GreenLanes::ExemptingCertificateOverride.new(eco_params) + + if @exempting_certificate_override.valid? && @exempting_certificate_override.save + redirect_to green_lanes_exempting_certificate_overrides_path, notice: 'Exempting Certificate Override created' + else + render :new + end + end + + def destroy + @exempting_certificate_override = GreenLanes::ExemptingCertificateOverride.find(params[:id]) + @exempting_certificate_override.destroy + + redirect_to green_lanes_exempting_certificate_overrides_path, notice: 'Exempting Certificate Override removed' + end + + private + + def eco_params + params.require(:exempting_certificate_override).permit( + :certificate_type_code, + :certificate_code, + ) + end + + def check_service + if TradeTariffAdmin::ServiceChooser.uk? + raise ActionController::RoutingError, 'Invalid service' + end + end + end +end diff --git a/app/models/green_lanes/exempting_certificate_override.rb b/app/models/green_lanes/exempting_certificate_override.rb new file mode 100644 index 00000000..a565ced0 --- /dev/null +++ b/app/models/green_lanes/exempting_certificate_override.rb @@ -0,0 +1,14 @@ +module GreenLanes + class ExemptingCertificateOverride + include Her::JsonApi::Model + use_api Her::XI_API + extend HerPaginatable + + attributes :certificate_type_code, + :certificate_code, + :created_at, + :updated_at + + collection_path '/admin/green_lanes/exempting_certificate_overrides' + end +end diff --git a/app/views/green_lanes/exempting_certificate_overrides/index.html.erb b/app/views/green_lanes/exempting_certificate_overrides/index.html.erb new file mode 100644 index 00000000..2e980684 --- /dev/null +++ b/app/views/green_lanes/exempting_certificate_overrides/index.html.erb @@ -0,0 +1,40 @@ +

+ Manage exempting certificate overrides +

+ +<%= link_to 'Add a Exempting Certificate Override', new_green_lanes_exempting_certificate_override_path, class: 'govuk-button' %> + +<% if @exempting_certificate_overrides.any? %> + + + + + + + + + + + <% @exempting_certificate_overrides.each do |eco| %> + + + + + + + <% end %> + +
IDCertificate Type CodeCertificate CodeAction
<%= eco.id %><%= eco.certificate_type_code %><%= eco.certificate_code %> + <%= link_to 'Remove', + green_lanes_exempting_certificate_override_path(eco), + method: :delete, + class: 'govuk-button govuk-button--warning', + data: { confirm: "Are you sure?", disable: 'Working ...' } %> +
+ + <%= paginate @exempting_certificate_overrides %> +<% else %> +
+

No Exempting Certificate Override

+
+<% end %> diff --git a/app/views/green_lanes/exempting_certificate_overrides/new.html.erb b/app/views/green_lanes/exempting_certificate_overrides/new.html.erb new file mode 100644 index 00000000..eaa5bbf3 --- /dev/null +++ b/app/views/green_lanes/exempting_certificate_overrides/new.html.erb @@ -0,0 +1,16 @@ +<%= govuk_breadcrumbs 'Exempting Certificate Override': green_lanes_exempting_certificate_overrides_path %> + +

New Exempting Certificate Override

+ +<%= govuk_form_for @exempting_certificate_override, as: :exempting_certificate_override do |f| %> + + <%= f.govuk_text_field :certificate_type_code, + label: { text: 'Certificate Type Code' }, + width: 'one-half' %> + + <%= f.govuk_text_field :certificate_code, + label: { text: 'Certificate Code' }, + width: 'one-half' %> + + <%= submit_and_back_buttons f, green_lanes_exempting_certificate_overrides_path %> +<% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index ba5bc9e3..830da653 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -48,9 +48,12 @@ href: reports_path, active: active_nav_link?(/\/reports/) %> <% if TradeTariffAdmin::ServiceChooser.xi? %> - <%= header.with_navigation_item text: 'Category assessments', + <%= header.with_navigation_item text: 'Category assessments', href: green_lanes_category_assessments_path, active: active_nav_link?(/\/category_assessments/) %> + <%= header.with_navigation_item text: 'Exempting Certificate Overrides', + href: green_lanes_exempting_certificate_overrides_path, + active: active_nav_link?(/\/exempting_certificate_overrides/) %> <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 8f985db0..a4831b30 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -64,6 +64,7 @@ namespace :green_lanes, path: 'green_lanes' do resources :category_assessments, only: %i[index new create edit update destroy] + resources :exempting_certificate_overrides, only: %i[index new create destroy] end resources :tariff_updates, only: %i[index show] do diff --git a/spec/factories/green_lanes/exempting_certificate_override_factory.rb b/spec/factories/green_lanes/exempting_certificate_override_factory.rb new file mode 100644 index 00000000..da46cc96 --- /dev/null +++ b/spec/factories/green_lanes/exempting_certificate_override_factory.rb @@ -0,0 +1,9 @@ +FactoryBot.define do + factory :exempting_certificate_override, class: 'GreenLanes::ExemptingCertificateOverride' do + sequence(:id) { |n| n } + certificate_type_code { 'Y' } + certificate_code { '435' } + created_at { 2.days.ago.to_date } + updated_at { nil } + end +end diff --git a/spec/models/green_lanes/exempting_certificate_override_spec.rb b/spec/models/green_lanes/exempting_certificate_override_spec.rb new file mode 100644 index 00000000..1a0a0511 --- /dev/null +++ b/spec/models/green_lanes/exempting_certificate_override_spec.rb @@ -0,0 +1,41 @@ +require 'rails_helper' + +RSpec.describe GreenLanes::ExemptingCertificateOverride do + subject(:exempting_certificate_override) { build :exempting_certificate_override } + + it { is_expected.to respond_to :id } + it { is_expected.to respond_to :certificate_type_code } + it { is_expected.to respond_to :certificate_code } + it { is_expected.to respond_to :created_at } + it { is_expected.to respond_to :updated_at } + + it { is_expected.to have_attributes id: exempting_certificate_override.id } + it { is_expected.to have_attributes certificate_type_code: exempting_certificate_override.certificate_type_code } + it { is_expected.to have_attributes certificate_code: exempting_certificate_override.certificate_code } + it { is_expected.to have_attributes created_at: exempting_certificate_override.created_at } + it { is_expected.to have_attributes updated_at: exempting_certificate_override.updated_at } + + describe '#all' do + subject { described_class.all } + + before do + allow(TradeTariffAdmin::ServiceChooser).to \ + receive(:service_choice).and_return service_choice + + stub_api_request('/admin/green_lanes/exempting_certificate_overrides', backend: 'xi').to_return \ + jsonapi_response(:exempting_certificate_override, attributes_for_list(:exempting_certificate_override, 2)) + end + + context 'with UK service' do + let(:service_choice) { 'uk' } + + it { is_expected.to have_attributes length: 2 } + end + + context 'with XI service' do + let(:service_choice) { 'xi' } + + it { is_expected.to have_attributes length: 2 } + end + end +end diff --git a/spec/requests/green_lanes/exempting_certificate_overrides_controller_spec.rb b/spec/requests/green_lanes/exempting_certificate_overrides_controller_spec.rb new file mode 100644 index 00000000..5296ca0d --- /dev/null +++ b/spec/requests/green_lanes/exempting_certificate_overrides_controller_spec.rb @@ -0,0 +1,70 @@ +RSpec.describe GreenLanes::ExemptingCertificateOverridesController do + subject(:rendered_page) { create_user && make_request && response } + + let(:exempting_certificate_override) { build :exempting_certificate_override } + let(:create_user) { create :user, permissions: ['signin', 'HMRC Editor'] } + + before do + allow(TradeTariffAdmin::ServiceChooser).to receive(:service_choice).and_return 'xi' + end + + describe 'GET #index' do + before do + stub_api_request('/admin/green_lanes/exempting_certificate_overrides?page=1', backend: 'xi').and_return \ + jsonapi_response :exempting_certificate_overrides, attributes_for_list(:exempting_certificate_override, 3) + end + + let(:make_request) { get green_lanes_exempting_certificate_overrides_path } + + it { is_expected.to have_http_status :success } + it { is_expected.not_to include 'div.current-service' } + end + + describe 'GET #new' do + let(:make_request) { get new_green_lanes_exempting_certificate_override_path } + + it { is_expected.to have_http_status :ok } + it { is_expected.not_to include 'div.current-service' } + end + + describe 'POST #create' do + before do + stub_api_request('/admin/green_lanes/exempting_certificate_overrides', :post).to_return create_response + end + + let :make_request do + post green_lanes_exempting_certificate_overrides_path, + params: { exempting_certificate_override: eco_params } + end + + context 'with valid item' do + let(:eco_params) { exempting_certificate_override.attributes.without(:id) } + let(:create_response) { webmock_response(:created, exempting_certificate_override.attributes) } + + it { is_expected.to redirect_to green_lanes_exempting_certificate_overrides_path } + end + + context 'with invalid item' do + let(:eco_params) { exempting_certificate_override.attributes.without(:id, :certificate_type_code) } + let(:create_response) { webmock_response(:error, certificate_type_code: "can't be blank'") } + + it { is_expected.to have_http_status :ok } + it { is_expected.to have_attributes body: /can.+t be blank/ } + it { is_expected.not_to include 'div.current-service' } + end + end + + describe 'DELETE #destroy' do + before do + stub_api_request("/admin/green_lanes/exempting_certificate_overrides/#{exempting_certificate_override.id}") + .and_return jsonapi_response(:exempting_certificate_override, exempting_certificate_override.attributes) + + stub_api_request("/admin/green_lanes/exempting_certificate_overrides/#{exempting_certificate_override.id}", :delete) + .and_return webmock_response :no_content + end + + let(:make_request) { delete green_lanes_exempting_certificate_override_path(exempting_certificate_override) } + + it { is_expected.to redirect_to green_lanes_exempting_certificate_overrides_path } + end +end