Skip to content

Commit

Permalink
GL-367: Add Admin UI screen to list the overrides (#705)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
rasikasri committed May 14, 2024
1 parent 0370a60 commit 8523fbe
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions app/models/green_lanes/exempting_certificate_override.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<h2>
Manage exempting certificate overrides
</h2>

<%= link_to 'Add a Exempting Certificate Override', new_green_lanes_exempting_certificate_override_path, class: 'govuk-button' %>
<% if @exempting_certificate_overrides.any? %>
<table>
<thead>
<tr>
<th>ID</th>
<th>Certificate Type Code</th>
<th>Certificate Code</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<% @exempting_certificate_overrides.each do |eco| %>
<tr id="<%= dom_id(eco) %>">
<td><%= eco.id %></td>
<td><%= eco.certificate_type_code %></td>
<td><%= eco.certificate_code %></td>
<td>
<%= 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 ...' } %>
</td>
</tr>
<% end %>
</tbody>
</table>

<%= paginate @exempting_certificate_overrides %>
<% else %>
<div class="govuk-inset-text">
<p>No Exempting Certificate Override</p>
</div>
<% end %>
16 changes: 16 additions & 0 deletions app/views/green_lanes/exempting_certificate_overrides/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%= govuk_breadcrumbs 'Exempting Certificate Override': green_lanes_exempting_certificate_overrides_path %>

<h2>New Exempting Certificate Override</h2>

<%= 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 %>
5 changes: 4 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions spec/models/green_lanes/exempting_certificate_override_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8523fbe

Please sign in to comment.