-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #710 from trade-tariff/GL-382-exemption-list-ui
GL-382: Exemption list UI
- Loading branch information
Showing
13 changed files
with
283 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module XiOnly | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
def check_service | ||
if TradeTariffAdmin::ServiceChooser.uk? | ||
raise ActionController::RoutingError, 'Invalid service' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module GreenLanes | ||
class ExemptionsController < AuthenticatedController | ||
include XiOnly | ||
|
||
before_action :disable_service_switching! | ||
before_action :check_service | ||
def index | ||
@exemptions = GreenLanes::Exemption.all.fetch | ||
end | ||
|
||
def new | ||
@exemption = GreenLanes::Exemption.new | ||
end | ||
|
||
def create | ||
@exemption = GreenLanes::Exemption.new(ex_params) | ||
|
||
if @exemption.valid? && @exemption.save | ||
redirect_to green_lanes_exemptions_path, notice: 'Exemption created' | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def edit | ||
@exemption = GreenLanes::Exemption.find(params[:id]) | ||
end | ||
|
||
def update | ||
@exemption = GreenLanes::Exemption.find(params[:id]) | ||
@exemption.attributes = ex_params | ||
|
||
if @exemption.valid? && @exemption.save | ||
redirect_to green_lanes_exemptions_path, notice: 'Exemption updated' | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
def destroy | ||
@exemption = GreenLanes::Exemption.find(params[:id]) | ||
@exemption.destroy | ||
|
||
redirect_to green_lanes_exemptions_path, notice: 'Exemption removed' | ||
end | ||
|
||
private | ||
|
||
def ex_params | ||
params.require(:exemption).permit( | ||
:code, | ||
:description, | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module GreenLanes | ||
class Exemption | ||
include Her::JsonApi::Model | ||
use_api Her::XI_API | ||
|
||
attributes :code, | ||
:description, | ||
:created_at, | ||
:updated_at | ||
|
||
collection_path '/admin/green_lanes/exemptions' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<%= govuk_form_for exemption, as: :exemption do |f| %> | ||
|
||
<%= f.govuk_text_field :code, | ||
label: { text: 'Code' }, | ||
width: 'one-half' %> | ||
|
||
<%= f.govuk_text_field :description, | ||
label: { text: 'Description' }, | ||
width: 'one-half' %> | ||
|
||
<%= submit_and_back_buttons f, green_lanes_exemptions_path %> | ||
<% end %> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<%= govuk_breadcrumbs 'Exemptions': green_lanes_exemptions_path %> | ||
|
||
<h2>Exemption</h2> | ||
|
||
<%= render 'form', exemption: @exemption %> | ||
|
||
<hr class="govuk-section-break govuk-section-break--m govuk-section-break--visible"> | ||
|
||
<h3> | ||
Remove Exemption | ||
</h3> | ||
|
||
<p> | ||
Remove this Exemption | ||
</p> | ||
|
||
<%= link_to 'Remove', | ||
green_lanes_exemption_path(@exemption), | ||
method: :delete, | ||
class: 'govuk-button govuk-button--warning', | ||
data: { confirm: "Are you sure?", disable: 'Working ...' } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<h2> | ||
Manage Green Lanes Exemptions | ||
</h2> | ||
|
||
<%= link_to 'Add a Exemption', new_green_lanes_exemption_path, class: 'govuk-button' %> | ||
|
||
<% if @exemptions.any? %> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Code</th> | ||
<th>Description</th> | ||
<th>Action</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% @exemptions.each do |ex| %> | ||
<tr id="<%= dom_id(ex) %>"> | ||
<td><%= ex.code %></td> | ||
<td><%= ex.description %></td> | ||
<td> | ||
<%= link_to 'Edit', | ||
edit_green_lanes_exemption_path(ex) %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<% else %> | ||
<div class="govuk-inset-text"> | ||
<p>No any Green Lanes Exception</p> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= govuk_breadcrumbs 'Exemption': green_lanes_exemptions_path %> | ||
|
||
<h2>New Exemption</h2> | ||
|
||
<%= render 'form', exemption: @exemption %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FactoryBot.define do | ||
factory :exemption, class: 'GreenLanes::Exemption' do | ||
sequence(:id) { |n| n } | ||
code { 'P' } | ||
description { 'pseudo exemptions' } | ||
created_at { 2.days.ago.to_date } | ||
updated_at { nil } | ||
end | ||
end |
113 changes: 113 additions & 0 deletions
113
spec/requests/green_lanes/exemptions_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
RSpec.describe GreenLanes::ExemptionsController do | ||
subject(:rendered_page) { create_user && make_request && response } | ||
|
||
let(:exemption) { build :exemption } | ||
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/exemptions', backend: 'xi').and_return \ | ||
jsonapi_response :exemptions, attributes_for_list(:exemption, 3) | ||
end | ||
|
||
let(:make_request) { get green_lanes_exemptions_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_exemption_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/exemptions', :post).to_return create_response | ||
end | ||
|
||
let :make_request do | ||
post green_lanes_exemptions_path, | ||
params: { exemption: ex_params } | ||
end | ||
|
||
context 'with valid item' do | ||
let(:ex_params) { exemption.attributes.without(:id) } | ||
let(:create_response) { webmock_response(:created, exemption.attributes) } | ||
|
||
it { is_expected.to redirect_to green_lanes_exemptions_path } | ||
end | ||
|
||
context 'with invalid item' do | ||
let(:ex_params) { exemption.attributes.without(:id, :code) } | ||
let(:create_response) { webmock_response(:error, 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 'GET #edit' do | ||
before do | ||
stub_api_request("/admin/green_lanes/exemptions/#{exemption.id}") | ||
.and_return jsonapi_response(:exemption, exemption.attributes) | ||
end | ||
|
||
let(:make_request) { get edit_green_lanes_exemption_path(exemption) } | ||
|
||
it { is_expected.to have_http_status :success } | ||
it { is_expected.not_to include 'div.current-service' } | ||
end | ||
|
||
describe 'PATCH #update' do | ||
before do | ||
stub_api_request("/admin/green_lanes/exemptions/#{exemption.id}") | ||
.and_return jsonapi_response(:exemption, exemption.attributes) | ||
|
||
stub_api_request("/admin/green_lanes/exemptions/#{exemption.id}", :patch) | ||
.and_return patch_response | ||
end | ||
|
||
let :make_request do | ||
patch green_lanes_exemption_path(exemption), | ||
params: { exemption: exemption.attributes.merge(description: new_desc) } | ||
end | ||
|
||
context 'with valid change' do | ||
let(:new_desc) { 'New description' } | ||
let(:patch_response) { webmock_response :updated, "/admin/green_lanes/exemptions/#{exemption.id}" } | ||
|
||
it { is_expected.to redirect_to green_lanes_exemptions_path } | ||
end | ||
|
||
context 'with invalid change' do | ||
let(:new_desc) { nil } | ||
let(:patch_response) { webmock_response :error, description: "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/exemptions/#{exemption.id}") | ||
.and_return jsonapi_response(:exemption, exemption.attributes) | ||
|
||
stub_api_request("/admin/green_lanes/exemptions/#{exemption.id}", :delete) | ||
.and_return webmock_response :no_content | ||
end | ||
|
||
let(:make_request) { delete green_lanes_exemption_path(exemption) } | ||
|
||
it { is_expected.to redirect_to green_lanes_exemptions_path } | ||
end | ||
end |