-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add copy cards main forms From: https://eaflood.atlassian.net/browse/RUBY-767 This add the CopyCardForm and the CopyCardsPaymentForm. Both Bank transfer and Worldpay forms needs mor thinking, so in order to facilitate review this only contains the other forms. View implementation is a TODO.
- Loading branch information
Showing
11 changed files
with
506 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
app/controllers/waste_carriers_engine/base_order_copy_cards_forms_controller.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,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module WasteCarriersEngine | ||
class BaseOrderCopyCardsFormsController < FormsController | ||
def find_or_initialize_transient_registration(reg_identifier) | ||
@transient_registration = OrderCopyCardsRegistration.where(reg_identifier: reg_identifier).first || | ||
OrderCopyCardsRegistration.new(reg_identifier: reg_identifier) | ||
end | ||
|
||
def setup_checks_pass? | ||
transient_registration_is_valid? && user_has_permission? && registation_is_active? && state_is_correct? | ||
end | ||
|
||
# Guards | ||
|
||
def user_has_permission? | ||
return true if can? :order_copy_cards, @transient_registration.registration | ||
|
||
redirect_to page_path("permission") | ||
false | ||
end | ||
|
||
def registation_is_active? | ||
return true if @transient_registration.registration.active? | ||
|
||
redirect_to page_path("invalid") | ||
false | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
app/controllers/waste_carriers_engine/copy_cards_forms_controller.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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module WasteCarriersEngine | ||
class CopyCardsFormsController < BaseOrderCopyCardsFormsController | ||
def new | ||
super(CopyCardsForm, "copy_cards_form") | ||
end | ||
|
||
def create | ||
super(CopyCardsForm, "copy_cards_form") | ||
end | ||
|
||
private | ||
|
||
def transient_registration_attributes | ||
params.fetch(:copy_cards_form).permit(:temp_cards) | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
app/controllers/waste_carriers_engine/copy_cards_payment_forms_controller.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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module WasteCarriersEngine | ||
class CopyCardsPaymentFormsController < BaseOrderCopyCardsFormsController | ||
def new | ||
super(CopyCardsPaymentForm, "copy_cards_payment_form") | ||
end | ||
|
||
def create | ||
super(CopyCardsPaymentForm, "copy_cards_payment_form") | ||
end | ||
|
||
private | ||
|
||
def transient_registration_attributes | ||
params.fetch(:copy_cards_payment_form, {}).permit(:temp_payment_method) | ||
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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module WasteCarriersEngine | ||
class CopyCardsForm < CardsForm | ||
def self.can_navigate_flexibly? | ||
true | ||
end | ||
|
||
validates( | ||
:temp_cards, | ||
numericality: { | ||
only_integer: true, | ||
greater_than_or_equal_to: 1, | ||
less_than_or_equal_to: MAX_TEMP_CARDS | ||
} | ||
) | ||
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module WasteCarriersEngine | ||
class CopyCardsPaymentForm < BaseForm | ||
delegate :temp_payment_method, to: :transient_registration | ||
|
||
validates :temp_payment_method, inclusion: { in: %w[card bank_transfer] } | ||
end | ||
end |
Empty file.
Empty file.
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
182 changes: 182 additions & 0 deletions
182
spec/requests/waste_carriers_engine/copy_cards_forms_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,182 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
module WasteCarriersEngine | ||
RSpec.describe "CopyCardsForms", type: :request do | ||
describe "GET new_copy_cards_form_path" do | ||
context "when a user is signed in" do | ||
let(:user) { create(:user) } | ||
|
||
before(:each) do | ||
sign_in(user) | ||
end | ||
|
||
context "when no matching registration exists" do | ||
it "redirects to the invalid reg_identifier error page" do | ||
get new_copy_cards_form_path("CBDU999999999") | ||
expect(response).to redirect_to(page_path("invalid")) | ||
end | ||
end | ||
|
||
context "when the reg_identifier doesn't match the format" do | ||
it "redirects to the invalid reg_identifier error page" do | ||
get new_copy_cards_form_path("foo") | ||
expect(response).to redirect_to(page_path("invalid")) | ||
end | ||
end | ||
|
||
context "when a matching registration exists" do | ||
context "when the given registration is not active" do | ||
let(:registration) { create(:registration, :has_required_data, :is_pending) } | ||
|
||
it "redirects to the page" do | ||
get new_copy_cards_form_path(registration.reg_identifier) | ||
|
||
expect(response).to redirect_to(page_path("invalid")) | ||
end | ||
end | ||
|
||
context "when the given registration is active" do | ||
let(:registration) { create(:registration, :has_required_data, :is_active) } | ||
|
||
it "renders the appropriate template" do | ||
get new_copy_cards_form_path(registration.reg_identifier) | ||
|
||
expect(response).to render_template("waste_carriers_engine/copy_cards_forms/new") | ||
end | ||
|
||
it "responds to the GET request with a 200 status code" do | ||
get new_copy_cards_form_path(registration.reg_identifier) | ||
|
||
expect(response.code).to eq("200") | ||
end | ||
end | ||
end | ||
end | ||
|
||
context "when a user is not signed in" do | ||
before(:each) do | ||
user = create(:user) | ||
sign_out(user) | ||
end | ||
|
||
it "returns a 302 response" do | ||
get new_copy_cards_form_path("foo") | ||
expect(response).to have_http_status(302) | ||
end | ||
|
||
it "redirects to the sign in page" do | ||
get new_copy_cards_form_path("foo") | ||
expect(response).to redirect_to(new_user_session_path) | ||
end | ||
end | ||
end | ||
|
||
describe "POST copy_cards_forms_path" do | ||
context "when a user is signed in" do | ||
let(:user) { create(:user) } | ||
|
||
before(:each) do | ||
sign_in(user) | ||
end | ||
|
||
context "when no matching registration exists" do | ||
let(:invalid_params) { { reg_identifier: "CBDU99999" } } | ||
|
||
it "redirects to the invalid reg_identifier error page" do | ||
post copy_cards_forms_path, copy_cards_form: invalid_params | ||
expect(response).to redirect_to(page_path("invalid")) | ||
end | ||
|
||
it "does not create a new transient registration" do | ||
original_tr_count = OrderCopyCardsRegistration.count | ||
post copy_cards_forms_path, copy_cards_form: invalid_params | ||
updated_tr_count = OrderCopyCardsRegistration.count | ||
|
||
expect(original_tr_count).to eq(updated_tr_count) | ||
end | ||
end | ||
|
||
context "when the reg_identifier doesn't match the format" do | ||
let(:invalid_params) { { reg_identifier: "foo" } } | ||
|
||
it "redirects to the invalid reg_identifier error page" do | ||
post copy_cards_forms_path, copy_cards_form: invalid_params | ||
expect(response).to redirect_to(page_path("invalid")) | ||
end | ||
|
||
it "does not create a new transient registration" do | ||
original_tr_count = OrderCopyCardsRegistration.count | ||
post copy_cards_forms_path, copy_cards_form: invalid_params | ||
updated_tr_count = OrderCopyCardsRegistration.count | ||
|
||
expect(original_tr_count).to eq(updated_tr_count) | ||
end | ||
end | ||
|
||
context "when a matching registration exists" do | ||
let(:registration) { create(:registration, :has_required_data, :is_active) } | ||
|
||
context "when valid params are submitted" do | ||
let(:valid_params) { { reg_identifier: registration.reg_identifier, temp_cards: 3 } } | ||
|
||
it "creates a transient registration with correct data, returns a 302 response and redirects to the copy cards payment form" do | ||
expected_tr_count = OrderCopyCardsRegistration.count + 1 | ||
|
||
post copy_cards_forms_path, copy_cards_form: valid_params | ||
|
||
transient_registration = OrderCopyCardsRegistration.find_by(reg_identifier: registration.reg_identifier) | ||
|
||
expect(expected_tr_count).to eq(OrderCopyCardsRegistration.count) | ||
expect(transient_registration.temp_cards).to eq(3) | ||
expect(response).to have_http_status(302) | ||
expect(response).to redirect_to(new_copy_cards_payment_form_path(valid_params[:reg_identifier])) | ||
end | ||
end | ||
|
||
context "when invalid params are submitted" do | ||
let(:invalid_params) { { reg_identifier: registration.reg_identifier, temp_cards: 0 } } | ||
|
||
it "returns a 200 response and render the new copy cards form" do | ||
post copy_cards_forms_path, copy_cards_form: invalid_params | ||
|
||
expect(response).to have_http_status(200) | ||
expect(response).to render_template("waste_carriers_engine/copy_cards_forms/new") | ||
end | ||
end | ||
end | ||
end | ||
|
||
context "when a user is not signed in" do | ||
let(:registration) { create(:registration, :has_required_data) } | ||
let(:valid_params) { { reg_identifier: registration[:reg_identifier] } } | ||
|
||
before(:each) do | ||
user = create(:user) | ||
sign_out(user) | ||
end | ||
|
||
it "returns a 302 response" do | ||
post copy_cards_forms_path, renewal_start_form: valid_params | ||
|
||
expect(response).to have_http_status(302) | ||
end | ||
|
||
it "redirects to the sign in page" do | ||
post copy_cards_forms_path, renewal_start_form: valid_params | ||
|
||
expect(response).to redirect_to(new_user_session_path) | ||
end | ||
|
||
it "does not create a new transient registration" do | ||
original_tr_count = OrderCopyCardsRegistration.count | ||
post copy_cards_forms_path, renewal_start_form: valid_params | ||
updated_tr_count = OrderCopyCardsRegistration.count | ||
|
||
expect(original_tr_count).to eq(updated_tr_count) | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.