Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copy cards main forms #576

Merged
merged 3 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
18 changes: 18 additions & 0 deletions app/forms/waste_carriers_engine/copy_cards_form.rb
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
9 changes: 9 additions & 0 deletions app/forms/waste_carriers_engine/copy_cards_payment_form.rb
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.
27 changes: 27 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,33 @@
path: "renew",
path_names: { new: "/:reg_identifier" }

# Order copy cards flow
resources :copy_cards_forms,
only: %i[new create],
path: "order-copy-cards",
path_names: { new: "/:reg_identifier" }

resources :copy_cards_payment_forms,
only: %i[new create],
path: "order-copy-cards-payment",
path_names: { new: "/:reg_identifier" } do
get "back/:reg_identifier",
to: "copy_cards_payment_forms#go_back",
as: "back",
on: :collection
end

resources :copy_cards_bank_transfer_forms,
only: %i[new create],
path: "order-copy-cards-bank-transfer",
path_names: { new: "/:reg_identifier" } do
get "back/:reg_identifier",
to: "copy_cards_bank_transfer_forms#go_back",
as: "back",
on: :collection
end
# End of order copy cards flow

resources :location_forms,
only: %i[new create],
path: "location",
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ class Ability
def initialize(user)
can :read, WasteCarriersEngine::Registration, account_email: user.email
can :manage, WasteCarriersEngine::RenewingRegistration, account_email: user.email
can :order_copy_cards, WasteCarriersEngine::Registration
end
end
182 changes: 182 additions & 0 deletions spec/requests/waste_carriers_engine/copy_cards_forms_spec.rb
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
Loading