diff --git a/app/controllers/conviction_details_forms_controller.rb b/app/controllers/conviction_details_forms_controller.rb index 87f793590..fc806f61c 100644 --- a/app/controllers/conviction_details_forms_controller.rb +++ b/app/controllers/conviction_details_forms_controller.rb @@ -1,4 +1,4 @@ -class ConvictionDetailsFormsController < FormsController +class ConvictionDetailsFormsController < PersonFormsController def new super(ConvictionDetailsForm, "conviction_details_form") end @@ -6,4 +6,8 @@ def new def create super(ConvictionDetailsForm, "conviction_details_form") end + + def delete_person + super(ConvictionDetailsForm, "conviction_details_form") + end end diff --git a/app/controllers/key_people_forms_controller.rb b/app/controllers/key_people_forms_controller.rb index bece08659..38b6010e9 100644 --- a/app/controllers/key_people_forms_controller.rb +++ b/app/controllers/key_people_forms_controller.rb @@ -1,37 +1,13 @@ -class KeyPeopleFormsController < FormsController +class KeyPeopleFormsController < PersonFormsController def new super(KeyPeopleForm, "key_people_form") end def create - if params[:commit] == I18n.t("key_people_forms.new.add_person_link") - submit_and_add_another - else - super(KeyPeopleForm, "key_people_form") - end - end - - def submit_and_add_another - return unless set_up_form(KeyPeopleForm, "key_people_form", params["key_people_form"][:reg_identifier]) - - respond_to do |format| - if @key_people_form.submit(params["key_people_form"]) - format.html { redirect_to_correct_form } - else - format.html { render :new } - end - end + super(KeyPeopleForm, "key_people_form") end def delete_person - return unless set_up_form(KeyPeopleForm, "key_people_form", params[:reg_identifier]) - - respond_to do |format| - # Check if there are any matches first, to avoid a Mongoid error - people_with_id = @transient_registration.keyPeople.where(id: params[:id]) - people_with_id.first.delete if people_with_id.any? - - format.html { redirect_to_correct_form } - end + super(KeyPeopleForm, "key_people_form") end end diff --git a/app/controllers/person_forms_controller.rb b/app/controllers/person_forms_controller.rb new file mode 100644 index 000000000..69765b5db --- /dev/null +++ b/app/controllers/person_forms_controller.rb @@ -0,0 +1,35 @@ +class PersonFormsController < FormsController + def create(form_class, form) + if params[:commit] == I18n.t("#{form}s.new.add_person_link") + submit_and_add_another(form_class, form) + else + super(form_class, form) + end + end + + def submit_and_add_another(form_class, form) + return unless set_up_form(form_class, form, params[form][:reg_identifier]) + + form_instance_variable = instance_variable_get("@#{form}") + + respond_to do |format| + if form_instance_variable.submit(params[form]) + format.html { redirect_to_correct_form } + else + format.html { render :new } + end + end + end + + def delete_person(form_class, form) + return unless set_up_form(form_class, form, params[:reg_identifier]) + + respond_to do |format| + # Check if there are any matches first, to avoid a Mongoid error + people_with_id = @transient_registration.keyPeople.where(id: params[:id]) + people_with_id.first.delete if people_with_id.any? + + format.html { redirect_to_correct_form } + end + end +end diff --git a/app/forms/conviction_details_form.rb b/app/forms/conviction_details_form.rb index 3580acba8..96b0e1f2c 100644 --- a/app/forms/conviction_details_form.rb +++ b/app/forms/conviction_details_form.rb @@ -1,17 +1,38 @@ -class ConvictionDetailsForm < BaseForm - # TODO: Define accessible attributes, eg attr_accessor :field +class ConvictionDetailsForm < PersonForm + attr_accessor :position - def initialize(transient_registration) - super - # TODO: Define params to get from transient_registration, eg self.field = @transient_registration.field + def position? + true end - def submit(params) - # Assign the params for validation and pass them to the BaseForm method for updating - # TODO: Define allowed params, eg self.field = params[:field] - # TODO: Include attributes to update in the attributes hash, eg { field: field } - attributes = {} + def maximum_people_in_type + nil + end + + def minimum_people_in_type + 1 + end + + private + + def person_type + "relevant" + end + + # Adding the new person directly to @transient_registration.keyPeople immediately updates the object, + # regardless of validation. So instead we copy all existing people into a new array and modify that. + def list_of_people_to_keep + people = [] + + @transient_registration.keyPeople.each do |person| + # We need to copy the person before adding to the array to avoid a 'conflicting modifications' Mongo error (10151) + people << person.clone + end + + people + end - super(attributes, params[:reg_identifier]) + def age_cutoff_date + (Date.today - 16.years) + 1.day end end diff --git a/app/forms/key_people_form.rb b/app/forms/key_people_form.rb index dcbed24bd..01b756394 100644 --- a/app/forms/key_people_form.rb +++ b/app/forms/key_people_form.rb @@ -1,6 +1,5 @@ -class KeyPeopleForm < BaseForm +class KeyPeopleForm < PersonForm attr_accessor :business_type - attr_accessor :first_name, :last_name, :dob_day, :dob_month, :dob_year, :key_person, :date_of_birth def initialize(transient_registration) super @@ -8,107 +7,53 @@ def initialize(transient_registration) self.business_type = @transient_registration.business_type # If there's only one key person, we can pre-fill the fields so users can easily edit them - prefill_form if can_only_have_one_key_person? && @transient_registration.keyPeople.present? + prefill_form if can_only_have_one_person_in_type? && @transient_registration.key_people.present? end - def submit(params) - # Assign the params for validation and pass them to the BaseForm method for updating - self.first_name = params[:first_name] - self.last_name = params[:last_name] - process_date_fields(params) - - self.key_person = add_key_person - self.date_of_birth = key_person.date_of_birth - - attributes = if fields_have_content? - { keyPeople: all_key_people } - else - {} - end - - super(attributes, params[:reg_identifier]) - end - - validates_with KeyPeopleValidator - validate :old_enough? - - def maximum_key_people + def maximum_people_in_type return unless business_type.present? key_people_limits[business_type.to_sym][:maximum] end - def minimum_key_people + def minimum_people_in_type # Business type should always be set, but use 1 as the default, just in case return 1 unless business_type.present? key_people_limits[business_type.to_sym][:minimum] end - def number_of_existing_key_people - @transient_registration.keyPeople.count - end - - def can_only_have_one_key_person? - return false unless maximum_key_people.present? - maximum_key_people == 1 - end - - def enough_key_people? - return false if number_of_existing_key_people < minimum_key_people - true - end - - def fields_have_content? - fields = [first_name, last_name, dob_day, dob_month, dob_year] - fields.each do |field| - return true if field.present? && field.to_s.length.positive? - end - false - end - private - def prefill_form - self.first_name = @transient_registration.keyPeople.first.first_name - self.last_name = @transient_registration.keyPeople.first.last_name - self.dob_day = @transient_registration.keyPeople.first.dob_day - self.dob_month = @transient_registration.keyPeople.first.dob_month - self.dob_year = @transient_registration.keyPeople.first.dob_year - end - - def process_date_fields(params) - self.dob_day = format_date_field_value(params[:dob_day]) - self.dob_month = format_date_field_value(params[:dob_month]) - self.dob_year = format_date_field_value(params[:dob_year]) - end - - # If we can make the date fields positive integers, use those integers - # Otherwise, return nil - def format_date_field_value(value) - # If this isn't a valid integer, .to_i returns 0 - integer_value = value.to_i - return integer_value if integer_value.positive? + def person_type + "key" end - def add_key_person - KeyPerson.new(first_name: first_name, - last_name: last_name, - dob_day: dob_day, - dob_month: dob_month, - dob_year: dob_year, - person_type: "key") + def prefill_form + self.first_name = @transient_registration.key_people.first.first_name + self.last_name = @transient_registration.key_people.first.last_name + self.dob_day = @transient_registration.key_people.first.dob_day + self.dob_month = @transient_registration.key_people.first.dob_month + self.dob_year = @transient_registration.key_people.first.dob_year end - def all_key_people - # If there's only one key person allowed, just replace existing data - return [key_person] if can_only_have_one_key_person? - - existing_key_people = [] - # Adding the new key person directly to @transient_registration.keyPeople immediately updates the object, - # regardless of validation. So instead we copy the existing key people into a new array and modify that. - @transient_registration.keyPeople.each do |person| - existing_key_people << person + # Adding the new key person directly to @transient_registration.keyPeople immediately updates the object, + # regardless of validation. So instead we copy all existing people into a new array and modify that. + def list_of_people_to_keep + people = [] + + # If there's only one key person allowed, we want to discard any existing key people, but keep people with + # relevant convictions. Otherwise, we copy all the keyPeople, regardless of type. + existing_people = if can_only_have_one_person_in_type? + @transient_registration.relevant_people + else + @transient_registration.keyPeople + end + + existing_people.each do |person| + # We need to copy the person before adding to the array to avoid a 'conflicting modifications' Mongo error (10151) + people << person.clone end - existing_key_people << key_person + + people end def key_people_limits @@ -122,9 +67,7 @@ def key_people_limits } end - def old_enough? - return false unless date_of_birth.present? - + def age_cutoff_date age_limits = { limitedCompany: 16.years, limitedLiabilityPartnership: 17.years, @@ -133,12 +76,11 @@ def old_enough? partnership: 17.years, soleTrader: 17.years } - age_cutoff_date = (Date.today - age_limits[business_type.to_sym]) + 1.day - return true if date_of_birth < age_cutoff_date + (Date.today - age_limits[business_type.to_sym]) + 1.day + end - error_message = "age_limit_#{business_type}".to_sym - errors.add(:date_of_birth, error_message) - false + def age_limit_error_message + "age_limit_#{business_type}".to_sym end end diff --git a/app/forms/person_form.rb b/app/forms/person_form.rb new file mode 100644 index 000000000..adcbaa760 --- /dev/null +++ b/app/forms/person_form.rb @@ -0,0 +1,130 @@ +class PersonForm < BaseForm + attr_accessor :first_name, :last_name, :dob_day, :dob_month, :dob_year, :date_of_birth + attr_accessor :new_person + + def initialize(transient_registration) + super + end + + def submit(params) + # Assign the params for validation and pass them to the BaseForm method for updating + self.first_name = params[:first_name] + self.last_name = params[:last_name] + self.position = params[:position] if position? + process_date_fields(params) + + self.new_person = set_up_new_person + self.date_of_birth = new_person.date_of_birth + + attributes = if fields_have_content? + { keyPeople: all_people } + else + {} + end + + super(attributes, params[:reg_identifier]) + end + + validates_with PersonValidator + validate :old_enough? + + # Used to switch on usage of the :position attribute for validation and form-filling + def position? + false + end + + def number_of_existing_people_in_type + @transient_registration.send("#{person_type}_people".to_sym).count + end + + def enough_people_in_type? + return false if number_of_existing_people_in_type < minimum_people_in_type + true + end + + def can_only_have_one_person_in_type? + return false unless maximum_people_in_type.present? + maximum_people_in_type == 1 + end + + def fields_have_content? + fields = [first_name, last_name, dob_day, dob_month, dob_year] + fields << position if position? + fields.each do |field| + return true if field.present? && field.to_s.length.positive? + end + false + end + + # Methods which are called in this class but defined in subclasses + # We should throw descriptive errors in case an additional subclass of PersonForm is ever added + + def maximum_people_in_type + implemented_in_subclass + end + + def minimum_people_in_type + implemented_in_subclass + end + + private + + def set_up_new_person + KeyPerson.new(first_name: first_name, + last_name: last_name, + dob_day: dob_day, + dob_month: dob_month, + dob_year: dob_year, + person_type: person_type) + end + + def all_people + list_of_people_to_keep << new_person + end + + def process_date_fields(params) + self.dob_day = format_date_field_value(params[:dob_day]) + self.dob_month = format_date_field_value(params[:dob_month]) + self.dob_year = format_date_field_value(params[:dob_year]) + end + + # If we can make the date fields positive integers, use those integers + # Otherwise, return nil + def format_date_field_value(value) + # If this isn't a valid integer, .to_i returns 0 + integer_value = value.to_i + return integer_value if integer_value.positive? + end + + def old_enough? + return false unless date_of_birth.present? + + return true if date_of_birth < age_cutoff_date + + errors.add(:date_of_birth, age_limit_error_message) + false + end + + def age_limit_error_message + :age_limit + end + + # Methods which are called in this class but defined in subclasses + # We should throw descriptive errors in case an additional subclass of PersonForm is ever added + + def person_type + implemented_in_subclass + end + + def list_of_people_to_keep + implemented_in_subclass + end + + def age_cutoff_date + implemented_in_subclass + end + + def implemented_in_subclass + raise NotImplementedError, "This #{self.class} cannot respond to:" + end +end diff --git a/app/models/concerns/can_have_registration_attributes.rb b/app/models/concerns/can_have_registration_attributes.rb index bf7a45768..f59b3cff3 100644 --- a/app/models/concerns/can_have_registration_attributes.rb +++ b/app/models/concerns/can_have_registration_attributes.rb @@ -52,5 +52,15 @@ def registered_address def overseas? location == "overseas" end + + def key_people + return [] unless keyPeople.present? + keyPeople.where(person_type: "key") + end + + def relevant_people + return [] unless keyPeople.present? + keyPeople.where(person_type: "relevant") + end end end diff --git a/app/validators/key_people_validator.rb b/app/validators/person_validator.rb similarity index 65% rename from app/validators/key_people_validator.rb rename to app/validators/person_validator.rb index b89e7cddf..c2d67704d 100644 --- a/app/validators/key_people_validator.rb +++ b/app/validators/person_validator.rb @@ -1,9 +1,9 @@ -class KeyPeopleValidator < ActiveModel::Validator +class PersonValidator < ActiveModel::Validator def validate(record) if record.fields_have_content? validate_individual_fields(record) else - validate_number_of_key_people(record) + validate_number_of_people_in_type(record) end end @@ -12,12 +12,13 @@ def validate(record) def validate_individual_fields(record) validate_first_name(record) validate_last_name(record) + validate_position(record) if record.position? DateOfBirthValidator.new.validate(record) end - def validate_number_of_key_people(record) - return if record.enough_key_people? - record.errors.add(:base, :not_enough_key_people, count: record.minimum_key_people) + def validate_number_of_people_in_type(record) + return if record.enough_people_in_type? + record.errors.add(:base, :not_enough_people_in_type, count: record.minimum_people_in_type) end def validate_first_name(record) @@ -30,6 +31,11 @@ def validate_last_name(record) field_is_not_too_long?(record, :last_name, 35) end + def validate_position(record) + return unless field_is_present?(record, :position) + field_is_not_too_long?(record, :position, 35) + end + def field_is_present?(record, field) return true if record.send(field).present? record.errors.add(field, :blank) diff --git a/app/views/conviction_details_forms/new.html.erb b/app/views/conviction_details_forms/new.html.erb index 3bc008097..c829d2ac6 100644 --- a/app/views/conviction_details_forms/new.html.erb +++ b/app/views/conviction_details_forms/new.html.erb @@ -1,24 +1,57 @@ <%= render("shared/back", back_path: back_conviction_details_forms_path(@conviction_details_form.reg_identifier)) %> -<% if @conviction_details_form.errors.any? %> - -

<%= t(".error_heading") %>

- - <% @conviction_details_form.errors.full_messages.each do |message| %> -
  • <%= message %>
  • - <% end %> - -<% else %> - +
    +
    <%= form_for(@conviction_details_form) do |f| %> <%= render("shared/errors", object: @conviction_details_form) %>

    <%= t(".heading") %>

    + <%= render("shared/person_name", form: @conviction_details_form, f: f) %> + + <% if @conviction_details_form.errors[:position].any? %> +
    + <% else %> +
    + <% end %> +
    + <% if @conviction_details_form.errors[:position].any? %> + <%= @conviction_details_form.errors[:position].join(", ") %> + <% end %> + + <%= f.label :position, t(".position"), class: "form-label" %> + <%= f.text_field :position, value: @conviction_details_form.position, class: "form-control" %> +
    +
    + + <%= render("shared/date_of_birth", form: @conviction_details_form, f: f) %> + <%= f.hidden_field :reg_identifier, value: @conviction_details_form.reg_identifier %> + +
    + <%= f.submit t(".add_person_link"), class: "button-link" %> +
    <%= f.submit t(".next_button"), class: "button" %>
    <% end %> -<% end %> + <% if @conviction_details_form.number_of_existing_people_in_type > 0 %> +
    +
    +

    <%= t(".list_of_people") %>

    + <% @transient_registration.relevant_people.each do |person| %> +
      +
    • + <%= person.first_name %> <%= person.last_name %> + <%= button_to(t(".delete_person_link"), + delete_person_conviction_details_forms_path(person.id), + class: "button-link", + method: :delete, + params: { reg_identifier: @conviction_details_form.reg_identifier }) %> +
    • +
    + <% end %> +
    + <% end %> +
    diff --git a/app/views/key_people_forms/new.html.erb b/app/views/key_people_forms/new.html.erb index 27c5445e5..8edcdbc8f 100644 --- a/app/views/key_people_forms/new.html.erb +++ b/app/views/key_people_forms/new.html.erb @@ -1,6 +1,6 @@ <%= render("shared/back", back_path: back_key_people_forms_path(@key_people_form.reg_identifier)) %> -<% if @key_people_form.can_only_have_one_key_person? %> +<% if @key_people_form.can_only_have_one_person_in_type? %>
    <% else %>
    @@ -12,80 +12,21 @@

    <%= t(".heading_#{@key_people_form.business_type}") %>

    <%= t(".description_1_#{@key_people_form.business_type}") %>

    - <% unless @key_people_form.can_only_have_one_key_person? %> + <% unless @key_people_form.can_only_have_one_person_in_type? %>

    <%= t(".description_2") %>

    <% end %> - <% if @key_people_form.errors[:first_name].any? %> -
    - <% else %> -
    - <% end %> -
    - <% if @key_people_form.errors[:first_name].any? %> - <%= @key_people_form.errors[:first_name].join(", ") %> - <% end %> - - <%= f.label :first_name, t(".first_name"), class: "form-label" %> - <%= f.text_field :first_name, value: @key_people_form.first_name, class: "form-control" %> -
    -
    - - <% if @key_people_form.errors[:last_name].any? %> -
    - <% else %> -
    - <% end %> -
    - <% if @key_people_form.errors[:last_name].any? %> - <%= @key_people_form.errors[:last_name].join(", ") %> - <% end %> - - <%= f.label :last_name, t(".last_name"), class: "form-label" %> - <%= f.text_field :last_name, value: @key_people_form.last_name, class: "form-control" %> -
    -
    - - <% if @key_people_form.errors[:date_of_birth].any? %> -
    - <% else %> -
    - <% end %> -
    - <% if @key_people_form.errors[:date_of_birth].any? %> - <%= @key_people_form.errors[:date_of_birth].join(". ") %> - <% end %> - - - <%= t(".date_of_birth") %> - <%= t(".date_of_birth_hint") %> - - -
    - <%= f.label :dob_day, t(".dob_day"), class: "form-label" %> - <%= f.text_field :dob_day, value: @key_people_form.dob_day, class: "form-control" %> -
    - -
    - <%= f.label :dob_month, t(".dob_month"), class: "form-label" %> - <%= f.text_field :dob_month, value: @key_people_form.dob_month, class: "form-control" %> -
    - -
    - <%= f.label :dob_year, t(".dob_year"), class: "form-label" %> - <%= f.text_field :dob_year, value: @key_people_form.dob_year, class: "form-control" %> -
    + <%= render("shared/person_name", form: @key_people_form, f: f) %> -
    -
    + <%= render("shared/date_of_birth", form: @key_people_form, f: f) %> <%= f.hidden_field :reg_identifier, value: @key_people_form.reg_identifier %> - <% if @key_people_form.can_only_have_one_key_person? %> + <% if @key_people_form.can_only_have_one_person_in_type? %>
    <%= f.submit t(".next_button"), class: "button" %>
    - <% elsif @key_people_form.minimum_key_people > (@key_people_form.number_of_existing_key_people + 1) %> + <% elsif @key_people_form.minimum_people_in_type > (@key_people_form.number_of_existing_people_in_type + 1) %>
    <%= f.submit t(".add_person_link"), class: "button" %>
    @@ -99,11 +40,11 @@ <% end %> <% end %> - <% unless @key_people_form.can_only_have_one_key_person? || @key_people_form.number_of_existing_key_people < 1 %> + <% unless @key_people_form.can_only_have_one_person_in_type? || @key_people_form.number_of_existing_people_in_type < 1 %>

    <%= t(".list_of_people") %>

    - <% @transient_registration.keyPeople.each do |person| %> + <% @transient_registration.key_people.each do |person| %>
    • <%= person.first_name %> <%= person.last_name %> diff --git a/app/views/shared/_date_of_birth.html.erb b/app/views/shared/_date_of_birth.html.erb new file mode 100644 index 000000000..372d3de0a --- /dev/null +++ b/app/views/shared/_date_of_birth.html.erb @@ -0,0 +1,32 @@ +<% if form.errors[:date_of_birth].any? %> +
      +<% else %> +
      +<% end %> +
      + <% if form.errors[:date_of_birth].any? %> + <%= form.errors[:date_of_birth].join(". ") %> + <% end %> + + + <%= t(".date_of_birth") %> + <%= t(".date_of_birth_hint") %> + + +
      + <%= f.label :dob_day, t(".dob_day"), class: "form-label" %> + <%= f.text_field :dob_day, value: form.dob_day, class: "form-control" %> +
      + +
      + <%= f.label :dob_month, t(".dob_month"), class: "form-label" %> + <%= f.text_field :dob_month, value: form.dob_month, class: "form-control" %> +
      + +
      + <%= f.label :dob_year, t(".dob_year"), class: "form-label" %> + <%= f.text_field :dob_year, value: form.dob_year, class: "form-control" %> +
      + +
      +
      diff --git a/app/views/shared/_person_name.html.erb b/app/views/shared/_person_name.html.erb new file mode 100644 index 000000000..e98bab22e --- /dev/null +++ b/app/views/shared/_person_name.html.erb @@ -0,0 +1,29 @@ +<% if form.errors[:first_name].any? %> +
      +<% else %> +
      +<% end %> +
      + <% if form.errors[:first_name].any? %> + <%= form.errors[:first_name].join(", ") %> + <% end %> + + <%= f.label :first_name, t(".first_name"), class: "form-label" %> + <%= f.text_field :first_name, value: form.first_name, class: "form-control" %> +
      +
      + +<% if form.errors[:last_name].any? %> +
      +<% else %> +
      +<% end %> +
      + <% if form.errors[:last_name].any? %> + <%= form.errors[:last_name].join(", ") %> + <% end %> + + <%= f.label :last_name, t(".last_name"), class: "form-label" %> + <%= f.text_field :last_name, value: form.last_name, class: "form-control" %> +
      +
      diff --git a/config/locales/en.yml b/config/locales/en.yml index 469f9d381..4d5dc1b01 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -12,6 +12,12 @@ en: feedback_link: feedback feedback_url: https://www.gov.uk/done/waste-carrier-or-broker-registration shared: + date_of_birth: + date_of_birth: Date of birth + date_of_birth_hint: For example, 26 5 1976 + dob_year: Year + dob_month: Month + dob_day: Day errors: heading: A problem to fix footer: @@ -19,6 +25,9 @@ en: os_terms_footer: text: © Crown copyright and database rights 2018 Ordnance Survey 100024198. Use of this addressing data is subject to the link_text: address data terms and conditions (opens new tab) + person_name: + first_name: First name + last_name: Last name # Custom error pages invalid_reg_identifier_heading: The registration number you entered is not valid diff --git a/config/locales/forms/conviction_details_forms/en.yml b/config/locales/forms/conviction_details_forms/en.yml index d1d14e60b..aadb09a8b 100644 --- a/config/locales/forms/conviction_details_forms/en.yml +++ b/config/locales/forms/conviction_details_forms/en.yml @@ -2,13 +2,40 @@ en: conviction_details_forms: new: heading: Details of the person with a conviction - error_heading: Something is wrong + position: Job title + add_person_link: Add another person + next_button: Continue + list_of_people: You have added the following people + delete_person_link: Delete next_button: Continue activemodel: errors: models: conviction_details_form: + not_enough_people_in_type: + one: "You must add the details of at least one person" attributes: + first_name: + blank: "You must enter a first name" + too_long: "The first name must be no longer than 35 characters" + last_name: + blank: "You must enter a last name" + too_long: "The last name must be no longer than 35 characters" + position: + blank: "You must enter a position" + too_long: "The position must be no longer than 35 characters" + date_of_birth: + age_limit: "You must be 16 or older to use this service" + not_a_date: "You must enter a valid date" + day_blank: "You must enter a day" + day_integer: "The day must be a number" + day_range: "The day must be between 1 and 31" + month_blank: "You must enter a month" + month_integer: "The month must be a number" + month_range: "The month must be between 1 and 12" + year_blank: "You must enter a year" + year_integer: "The year must be a number" + year_range: "The year must be between 1900 and now" reg_identifier: invalid_format: "The registration ID is not in a valid format" no_registration: "There is no registration matching this ID" diff --git a/config/locales/forms/key_people_forms/en.yml b/config/locales/forms/key_people_forms/en.yml index f55f4a550..a06583402 100644 --- a/config/locales/forms/key_people_forms/en.yml +++ b/config/locales/forms/key_people_forms/en.yml @@ -14,13 +14,6 @@ en: description_1_partnership: Provide the name and date of birth for each partner of this business. description_1_soleTrader: Provide the name and date of birth for the owner of this business. description_2: Add their details one person at a time. If you need to add more people, select 'add another person'. - first_name: First name - last_name: Last name - date_of_birth: Date of birth - date_of_birth_hint: For example, 26 5 1976 - dob_year: Year - dob_month: Month - dob_day: Day add_person_link: Add another person next_button: Continue list_of_people: You have added the following people @@ -30,7 +23,7 @@ en: errors: models: key_people_form: - not_enough_key_people: + not_enough_people_in_type: one: "You must add the details of at least one person" other: "You must add the details of at least %{count} people" attributes: diff --git a/config/routes.rb b/config/routes.rb index ea2c8ae1d..4ea06541b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -216,6 +216,11 @@ to: "conviction_details_forms#go_back", as: "back", on: :collection + + delete "delete_person/:id", + to: "conviction_details_forms#delete_person", + as: "delete_person", + on: :collection end resources :contact_name_forms, diff --git a/spec/cassettes/company_postcode_form_modified_postcode.yml b/spec/cassettes/company_postcode_form_modified_postcode.yml index 8455c3ed2..8a938cbad 100644 --- a/spec/cassettes/company_postcode_form_modified_postcode.yml +++ b/spec/cassettes/company_postcode_form_modified_postcode.yml @@ -21,7 +21,7 @@ http_interactions: message: OK headers: Date: - - Mon, 12 Mar 2018 14:22:52 GMT + - Mon, 26 Mar 2018 15:09:22 GMT Content-Type: - application/json Content-Encoding: @@ -35,5 +35,5 @@ http_interactions: string: !binary |- H4sIAAAAAAAAAN2Y0W6bMBSGX8XimkoQgoHdkcRtkChEQFpFVS+cxG3QCI4IbKumvftMIS4QNu3CROkuIvEfw7H9n08Hk6ef0p6m8VeSSV8kTTHNsSrJUnHI0qZO4pQcpS9PEgqn9gKB0HZ9j8UfnGnkB44NwihAKJKeZSmn38tHJ4ETRr7L7jnQY76hW1IGQxVAe86CBB/zOH0t59CtkW6wUEqzfFfFVGMEjTK2oUWaZ28sxMSWHEi6JWnu0g1O4rwbjnYZLV53Lzgj1Qje7uM0PuYZzuNvxM4Ibi0sKdPYRb6jGUu2PGxxTmbsVz2c0Tec3OM46Q4ccJbHOGGiaYYMlp4TAV0GUWB7oX8P5v4yRDLoWCSDegHsgrtxLNaTIk62bPce3pfzVMnY0LodbyVnw+XiT5uub+nWRJZo9oqZD8wFmtY3derIZyn263cS6rr5Ly/xhkzoj+YAM5xZsGeO18nKGC3WCZn1VeiX3ERspENo6h+Icc0RW8wdd+FHoTi8NHiGl6FoV4/XyYgaLetGVUTSVeYbDrBGFS8Il6IqOmzAddIcrnA5ebRX4tAaa+edy9KvHq3KhhosUyRV5nBI8dpdDijV0g3V+ACKaw5UNEdgZjvuCkycCIGpfYvE0QWtc7p08+rp6vGkRk0ViZo6HGr9Vb3oW9KEjYMY15y7dwe0m7Hc3bKoI1kPesbo6tHjtgjkjKUbjrRLYzXWtBZWlW5jBYeCatwDlQk/B1RQJFLwPwFKVUzNGjfejyfdBOoBBaETrYB/C8q++ojCqLxG3p1rezOBLavnLGZ+ipb1d4cEgTcccv9S48s2OctsN7l3zZnkm5oFKzB1ke2x1Qvscuefm9C0rp7DXlfqc5shsvkZw5H4p8pelj6l/f9GpRv0BXeOB+59D63Awp0K/FzQz8GD198Au4bUzI1EMjcakrmzeg6K2/NvwFnliLMVAAA= http_version: - recorded_at: Mon, 12 Mar 2018 14:27:50 GMT + recorded_at: Mon, 26 Mar 2018 15:09:24 GMT recorded_with: VCR 4.0.0 diff --git a/spec/cassettes/company_postcode_form_no_matches_postcode.yml b/spec/cassettes/company_postcode_form_no_matches_postcode.yml index 3d2bae0d6..b2b9376b6 100644 --- a/spec/cassettes/company_postcode_form_no_matches_postcode.yml +++ b/spec/cassettes/company_postcode_form_no_matches_postcode.yml @@ -21,7 +21,7 @@ http_interactions: message: Bad Request headers: Date: - - Mon, 12 Mar 2018 14:22:53 GMT + - Mon, 26 Mar 2018 15:09:24 GMT Content-Type: - application/json Transfer-Encoding: @@ -30,5 +30,5 @@ http_interactions: encoding: UTF-8 string: '{"error":{"message":"Parameters are not valid","statuscode":400}}' http_version: - recorded_at: Mon, 12 Mar 2018 14:27:51 GMT + recorded_at: Mon, 26 Mar 2018 15:09:25 GMT recorded_with: VCR 4.0.0 diff --git a/spec/cassettes/company_postcode_form_valid_postcode.yml b/spec/cassettes/company_postcode_form_valid_postcode.yml index bf309cbdf..803caf92f 100644 --- a/spec/cassettes/company_postcode_form_valid_postcode.yml +++ b/spec/cassettes/company_postcode_form_valid_postcode.yml @@ -21,7 +21,7 @@ http_interactions: message: OK headers: Date: - - Mon, 12 Mar 2018 14:22:52 GMT + - Mon, 26 Mar 2018 15:09:20 GMT Content-Type: - application/json Content-Encoding: @@ -35,5 +35,5 @@ http_interactions: string: !binary |- H4sIAAAAAAAAAOWUUW+bMBDHv4rlZx6gaULWN1NQE42aypBNXdUHJziJNbCRMduqad+9x8JSIN1LlU2T9oDA/7PvfP/7iYfvuNRKfhYGX2HPdd95/sydXbrYwU1l1IlYSCVqfPWAF4TdphFFNyxZ3UEgjAiN2D1iCQnxo4Ot/tqeDtgyzZIYNlS6thudi1ZMPTQlCxAFr61UO9Am07k3aSsobez+oHn+xWzug7bRjbLmCSRY5KISKhfKxnrDC2nHcrY3utntt9yIQ4TnpVSytoZb+UUQI/jgYkWbhjR2rw0kW1U5tyKE53DY6Cde3HJZjAMVN1byAhYDKxyUsWUSJikKCH3voL4vDuqqwsfRgrpZB40scmiZ8rJLvh4q/ZQQba/6q8Vux8B+B2uz49AyNKxVt2M8sGOJplz/nH43o2S7lRsR6G/9AJgL7Zbg7ssdc92sCxG+No0fTh+ryaXrebMXoo7rI0yUZCtGYhTRm5jQ8Dw4XbjTE5x8d/7P4zQyw0GLhC0/JRTeqzQ6E1GDnG9F6nRsfxkqfwSV34cqW7DlhwixiEYfSRBHKbqLr//vP9WrlpyJqLdC9Lsx/VGUHp8B5v82jvgGAAA= http_version: - recorded_at: Mon, 12 Mar 2018 14:27:50 GMT + recorded_at: Mon, 26 Mar 2018 15:09:22 GMT recorded_with: VCR 4.0.0 diff --git a/spec/cassettes/registration_number_form_changed_company_no.yml b/spec/cassettes/registration_number_form_changed_company_no.yml index 85585ab39..cb364d057 100644 --- a/spec/cassettes/registration_number_form_changed_company_no.yml +++ b/spec/cassettes/registration_number_form_changed_company_no.yml @@ -23,7 +23,7 @@ http_interactions: message: OK headers: Date: - - Tue, 13 Mar 2018 10:16:50 GMT + - Wed, 28 Mar 2018 06:58:26 GMT Content-Type: - application/json Content-Length: @@ -51,7 +51,7 @@ http_interactions: X-Ratelimit-Remain: - '595' X-Ratelimit-Reset: - - '1520936486' + - '1522220579' X-Ratelimit-Window: - 5m Server: @@ -62,5 +62,5 @@ http_interactions: House Woodland Road","postal_code":"CT18 8DL","address_line_2":"Lyminge"},"accounts":{"last_accounts":{"period_end_on":"2017-02-28","period_start_on":"2016-03-01","made_up_to":"2017-02-28"},"accounting_reference_date":{"day":"28","month":"02"},"overdue":false,"next_due":"2018-11-30","next_made_up_to":"2018-02-28","next_accounts":{"overdue":false,"due_on":"2018-11-30","period_end_on":"2018-02-28","period_start_on":"2017-03-01"}},"company_name":"JIM GARRAHY''S FUDGE KITCHEN LIMITED","date_of_creation":"1983-03-24","sic_codes":["10822"],"undeliverable_registered_office_address":false,"last_full_members_list_date":"2015-11-20","type":"ltd","has_been_liquidated":false,"company_number":"01709418","jurisdiction":"england-wales","has_insolvency_history":false,"etag":"079ea458cdac90941922af7f7742f1d7cdf14cd1","has_charges":true,"company_status":"active","confirmation_statement":{"next_due":"2018-12-04","next_made_up_to":"2018-11-20","overdue":false,"last_made_up_to":"2017-11-20"},"links":{"self":"/company/01709418","filing_history":"/company/01709418/filing-history","officers":"/company/01709418/officers","charges":"/company/01709418/charges","persons_with_significant_control_statements":"/company/01709418/persons-with-significant-control-statements"},"registered_office_is_in_dispute":false,"can_file":true}' http_version: - recorded_at: Tue, 13 Mar 2018 10:16:47 GMT + recorded_at: Wed, 28 Mar 2018 06:58:26 GMT recorded_with: VCR 4.0.0 diff --git a/spec/cassettes/registration_number_form_inactive_company_no.yml b/spec/cassettes/registration_number_form_inactive_company_no.yml index 20c40eb0a..8be2c004e 100644 --- a/spec/cassettes/registration_number_form_inactive_company_no.yml +++ b/spec/cassettes/registration_number_form_inactive_company_no.yml @@ -23,7 +23,7 @@ http_interactions: message: OK headers: Date: - - Tue, 13 Mar 2018 10:16:27 GMT + - Wed, 28 Mar 2018 06:58:00 GMT Content-Type: - application/json Content-Length: @@ -51,7 +51,7 @@ http_interactions: X-Ratelimit-Remain: - '596' X-Ratelimit-Reset: - - '1520936486' + - '1522220579' X-Ratelimit-Window: - 5m Server: @@ -63,5 +63,5 @@ http_interactions: Elizabeth House","address_line_2":"54-58 High Street"},"undeliverable_registered_office_address":false,"company_name":"DIRECT SKIPS UK LTD","annual_return":{"last_made_up_to":"2014-06-11"},"jurisdiction":"england-wales","etag":"1cdef5bc2a020b3e9003b086033b64b78bcb28f6","company_status":"dissolved","has_insolvency_history":false,"has_charges":false,"links":{"self":"/company/07281919","filing_history":"/company/07281919/filing-history","officers":"/company/07281919/officers"},"date_of_cessation":"2016-01-05","can_file":false}' http_version: - recorded_at: Tue, 13 Mar 2018 10:16:23 GMT + recorded_at: Wed, 28 Mar 2018 06:58:00 GMT recorded_with: VCR 4.0.0 diff --git a/spec/cassettes/registration_number_form_not_found_company_no.yml b/spec/cassettes/registration_number_form_not_found_company_no.yml index d480c48ec..cf45c193f 100644 --- a/spec/cassettes/registration_number_form_not_found_company_no.yml +++ b/spec/cassettes/registration_number_form_not_found_company_no.yml @@ -23,7 +23,7 @@ http_interactions: message: Not Found headers: Date: - - Tue, 13 Mar 2018 10:16:27 GMT + - Wed, 28 Mar 2018 06:58:00 GMT Content-Type: - application/json Content-Length: @@ -51,14 +51,14 @@ http_interactions: X-Ratelimit-Remain: - '597' X-Ratelimit-Reset: - - '1520936486' + - '1522220579' X-Ratelimit-Window: - 5m Server: - CompaniesHouse body: encoding: UTF-8 - string: '{"errors":[{"error":"company-profile-not-found","type":"ch:service"}]}' + string: '{"errors":[{"type":"ch:service","error":"company-profile-not-found"}]}' http_version: - recorded_at: Tue, 13 Mar 2018 10:16:23 GMT + recorded_at: Wed, 28 Mar 2018 06:58:00 GMT recorded_with: VCR 4.0.0 diff --git a/spec/cassettes/registration_number_form_short_company_no.yml b/spec/cassettes/registration_number_form_short_company_no.yml index 89c9d08e5..800fc6445 100644 --- a/spec/cassettes/registration_number_form_short_company_no.yml +++ b/spec/cassettes/registration_number_form_short_company_no.yml @@ -23,7 +23,7 @@ http_interactions: message: OK headers: Date: - - Tue, 13 Mar 2018 10:16:26 GMT + - Wed, 28 Mar 2018 06:57:59 GMT Content-Type: - application/json Content-Length: @@ -51,7 +51,7 @@ http_interactions: X-Ratelimit-Remain: - '598' X-Ratelimit-Reset: - - '1520936486' + - '1522220579' X-Ratelimit-Window: - 5m Server: @@ -64,5 +64,5 @@ http_interactions: Wycombe","region":"Bucks"},"jurisdiction":"england-wales","type":"ltd","undeliverable_registered_office_address":false,"sic_codes":["38110","38120","38210","38220"],"accounts":{"next_due":"2018-12-29","accounting_reference_date":{"day":"29","month":"03"},"next_accounts":{"due_on":"2018-12-29","period_start_on":"2017-03-25","overdue":false,"period_end_on":"2018-03-29"},"last_accounts":{"period_end_on":"2017-03-24","type":"full","made_up_to":"2017-03-24","period_start_on":"2016-03-26"},"next_made_up_to":"2018-03-29","overdue":false},"date_of_creation":"1969-01-16","has_insolvency_history":false,"etag":"71e17f2aaec9b698b7ff73a1cbf996dc9c88b933","has_charges":true,"company_status":"active","previous_company_names":[{"name":"BIFFA LIMITED","effective_from":"1969-01-16","ceased_on":"1986-04-22"}],"confirmation_statement":{"next_due":"2019-01-18","next_made_up_to":"2019-01-04","overdue":false,"last_made_up_to":"2018-01-04"},"links":{"self":"/company/00946107","filing_history":"/company/00946107/filing-history","officers":"/company/00946107/officers","charges":"/company/00946107/charges"},"registered_office_is_in_dispute":false,"can_file":true}' http_version: - recorded_at: Tue, 13 Mar 2018 10:16:23 GMT + recorded_at: Wed, 28 Mar 2018 06:57:59 GMT recorded_with: VCR 4.0.0 diff --git a/spec/cassettes/registration_number_form_valid_company_no.yml b/spec/cassettes/registration_number_form_valid_company_no.yml index a3ddc2b8a..4960fab2a 100644 --- a/spec/cassettes/registration_number_form_valid_company_no.yml +++ b/spec/cassettes/registration_number_form_valid_company_no.yml @@ -23,7 +23,7 @@ http_interactions: message: OK headers: Date: - - Tue, 13 Mar 2018 10:16:26 GMT + - Wed, 28 Mar 2018 06:57:59 GMT Content-Type: - application/json Content-Length: @@ -51,7 +51,7 @@ http_interactions: X-Ratelimit-Remain: - '599' X-Ratelimit-Reset: - - '1520936486' + - '1522220579' X-Ratelimit-Window: - 5m Server: @@ -61,5 +61,5 @@ http_interactions: string: '{"type":"ltd","company_name":"0800 WASTE LTD.","has_insolvency_history":false,"accounts":{"next_made_up_to":"2017-12-31","overdue":false,"next_accounts":{"period_end_on":"2017-12-31","due_on":"2018-09-30","period_start_on":"2017-01-01","overdue":false},"last_accounts":{"made_up_to":"2016-12-31","period_start_on":"2016-01-01","period_end_on":"2016-12-31"},"accounting_reference_date":{"day":"31","month":"12"},"next_due":"2018-09-30"},"undeliverable_registered_office_address":false,"etag":"d1aac62a36df9712e7ef94c383439a6fa001fa84","company_number":"09360070","registered_office_address":{"address_line_1":"21 Haslam Avenue","postal_code":"SM3 9ND","region":"Surrey","locality":"Sutton"},"jurisdiction":"england-wales","date_of_creation":"2014-12-18","company_status":"active","has_charges":false,"sic_codes":["38110"],"last_full_members_list_date":"2015-12-18","confirmation_statement":{"last_made_up_to":"2017-12-15","overdue":false,"next_made_up_to":"2018-12-15","next_due":"2018-12-29"},"links":{"self":"/company/09360070","filing_history":"/company/09360070/filing-history","officers":"/company/09360070/officers"},"registered_office_is_in_dispute":false,"can_file":true}' http_version: - recorded_at: Tue, 13 Mar 2018 10:16:23 GMT + recorded_at: Wed, 28 Mar 2018 06:57:59 GMT recorded_with: VCR 4.0.0 diff --git a/spec/factories/forms/conviction_details_form.rb b/spec/factories/forms/conviction_details_form.rb index c65e81307..1c0a4ec9b 100644 --- a/spec/factories/forms/conviction_details_form.rb +++ b/spec/factories/forms/conviction_details_form.rb @@ -1,6 +1,14 @@ FactoryBot.define do factory :conviction_details_form do trait :has_required_data do + first_name "Foo" + last_name "Bar" + position "Baz" + dob_year 2000 + dob_month 1 + dob_day 1 + date_of_birth Date.new(2000, 1, 1) + initialize_with { new(create(:transient_registration, :has_required_data, workflow_state: "conviction_details_form")) } end end diff --git a/spec/factories/forms/key_people_form.rb b/spec/factories/forms/key_people_form.rb index 2a267b914..b4ca3ab0f 100644 --- a/spec/factories/forms/key_people_form.rb +++ b/spec/factories/forms/key_people_form.rb @@ -6,7 +6,7 @@ dob_year 2000 dob_month 1 dob_day 1 - date_of_birth Date.new(2002, 1, 1) + date_of_birth Date.new(2000, 1, 1) initialize_with { new(create(:transient_registration, :has_required_data, workflow_state: "key_people_form")) } end diff --git a/spec/factories/key_person.rb b/spec/factories/key_person.rb index f754c59a8..936dcf202 100644 --- a/spec/factories/key_person.rb +++ b/spec/factories/key_person.rb @@ -7,7 +7,14 @@ dob_day 1 dob_month 1 dob_year 2000 - person_type "Relevant" + end + + trait :key do + person_type "key" + end + + trait :relevant do + person_type "relevant" end end end diff --git a/spec/forms/conviction_details_forms_spec.rb b/spec/forms/conviction_details_forms_spec.rb index 1ef94a6b2..f112a2478 100644 --- a/spec/forms/conviction_details_forms_spec.rb +++ b/spec/forms/conviction_details_forms_spec.rb @@ -2,40 +2,75 @@ RSpec.describe ConvictionDetailsForm, type: :model do describe "#submit" do + let(:conviction_details_form) { build(:conviction_details_form, :has_required_data) } + context "when the form is valid" do - let(:conviction_details_form) { build(:conviction_details_form, :has_required_data) } - let(:valid_params) { { reg_identifier: conviction_details_form.reg_identifier } } + let(:valid_params) do + { reg_identifier: conviction_details_form.reg_identifier, + first_name: conviction_details_form.first_name, + last_name: conviction_details_form.last_name, + position: conviction_details_form.position, + dob_year: conviction_details_form.dob_year, + dob_month: conviction_details_form.dob_month, + dob_day: conviction_details_form.dob_day } + end it "should submit" do expect(conviction_details_form.submit(valid_params)).to eq(true) end + + it "should set a person_type of 'relevant'" do + conviction_details_form.submit(valid_params) + expect(conviction_details_form.new_person.person_type).to eq("relevant") + end end context "when the form is not valid" do - let(:conviction_details_form) { build(:conviction_details_form, :has_required_data) } let(:invalid_params) { { reg_identifier: "foo" } } it "should not submit" do expect(conviction_details_form.submit(invalid_params)).to eq(false) end end - end - describe "#reg_identifier" do - context "when a valid transient registration exists" do - let(:transient_registration) do - create(:transient_registration, - :has_required_data, - workflow_state: "conviction_details_form") + context "when the form is blank" do + let(:blank_params) do + { reg_identifier: conviction_details_form.reg_identifier, + first_name: "", + last_name: "", + position: "", + dob_year: "", + dob_month: "", + dob_day: "" } end - # Don't use FactoryBot for this as we need to make sure it initializes with a specific object - let(:conviction_details_form) { ConvictionDetailsForm.new(transient_registration) } - context "when a reg_identifier meets the requirements" do + context "when the transient registration already has enough people with convictions" do + before(:each) do + conviction_details_form.transient_registration.update_attributes(keyPeople: [build(:key_person, :has_required_data, :relevant)]) + end + + it "should submit" do + expect(conviction_details_form.submit(blank_params)).to eq(true) + end + end + + context "when the transient registration does not have enough people with convictions" do before(:each) do - conviction_details_form.reg_identifier = transient_registration.reg_identifier + conviction_details_form.transient_registration.update_attributes(keyPeople: [build(:key_person, :has_required_data, :key)]) + end + + it "should not submit" do + expect(conviction_details_form.submit(blank_params)).to eq(false) end + end + end + end + context "when a valid transient registration exists" do + let(:conviction_details_form) { build(:conviction_details_form, :has_required_data) } + + describe "#reg_identifier" do + context "when a reg_identifier meets the requirements" do it "is valid" do expect(conviction_details_form).to be_valid end @@ -51,6 +86,244 @@ end end end + + describe "#first_name" do + context "when a first_name meets the requirements" do + it "is valid" do + expect(conviction_details_form).to be_valid + end + end + + context "when a first_name is blank" do + before(:each) do + conviction_details_form.first_name = "" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + + context "when a first_name is too long" do + before(:each) do + conviction_details_form.first_name = "gsm2lgu3q7cg5pcs02ftc1wtpx4lt5ghmyaclhe9qg9li7ibs5ldi3w3n1pt24pbfo0666bq" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + end + + describe "#last_name" do + context "when a last_name meets the requirements" do + it "is valid" do + expect(conviction_details_form).to be_valid + end + end + + context "when a last_name is blank" do + before(:each) do + conviction_details_form.last_name = "" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + + context "when a last_name is too long" do + before(:each) do + conviction_details_form.last_name = "gsm2lgu3q7cg5pcs02ftc1wtpx4lt5ghmyaclhe9qg9li7ibs5ldi3w3n1pt24pbfo0666bq" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + end + + describe "#position" do + context "when a position meets the requirements" do + it "is valid" do + expect(conviction_details_form).to be_valid + end + end + + context "when a position is blank" do + before(:each) do + conviction_details_form.position = "" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + + context "when a position is too long" do + before(:each) do + conviction_details_form.position = "gsm2lgu3q7cg5pcs02ftc1wtpx4lt5ghmyaclhe9qg9li7ibs5ldi3w3n1pt24pbfo0666bq" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + end + + describe "#dob_day" do + context "when a dob_day meets the requirements" do + it "is valid" do + expect(conviction_details_form).to be_valid + end + end + + context "when a dob_day is blank" do + before(:each) do + conviction_details_form.dob_day = "" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + + context "when a dob_day is not an integer" do + before(:each) do + conviction_details_form.dob_day = "1.5" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + + context "when a dob_day is not in the correct range" do + before(:each) do + conviction_details_form.dob_day = "42" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + end + + describe "#dob_month" do + context "when a dob_month meets the requirements" do + it "is valid" do + expect(conviction_details_form).to be_valid + end + end + + context "when a dob_month is blank" do + before(:each) do + conviction_details_form.dob_month = "" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + + context "when a dob_month is not an integer" do + before(:each) do + conviction_details_form.dob_month = "9.75" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + + context "when a dob_month is not in the correct range" do + before(:each) do + conviction_details_form.dob_month = "13" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + end + + describe "#dob_year" do + context "when a dob_year meets the requirements" do + it "is valid" do + expect(conviction_details_form).to be_valid + end + end + + context "when a dob_year is blank" do + before(:each) do + conviction_details_form.dob_year = "" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + + context "when a dob_year is not an integer" do + before(:each) do + conviction_details_form.dob_year = "3.14" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + + context "when a dob_year is not in the correct range" do + before(:each) do + conviction_details_form.dob_year = (Date.today + 1.year).year.to_i + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + end + + describe "#date_of_birth" do + context "when a date_of_birth meets the requirements" do + it "is valid" do + expect(conviction_details_form).to be_valid + end + end + + context "when all the date of birth fields are empty" do + before(:each) do + conviction_details_form.dob_day = "" + conviction_details_form.dob_month = "" + conviction_details_form.dob_year = "" + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + + context "when a date of birth is not a valid date" do + before(:each) do + conviction_details_form.date_of_birth = nil + end + + it "is not valid" do + expect(conviction_details_form).to_not be_valid + end + end + + context "when the date of birth is below the age limit" do + before(:each) do + conviction_details_form.date_of_birth = Date.today + end + + it "should not be valid" do + expect(conviction_details_form).to_not be_valid + end + end + end end describe "#transient_registration" do diff --git a/spec/forms/key_people_forms_spec.rb b/spec/forms/key_people_forms_spec.rb index 061f58c9d..f3a1e26c5 100644 --- a/spec/forms/key_people_forms_spec.rb +++ b/spec/forms/key_people_forms_spec.rb @@ -17,6 +17,11 @@ it "should submit" do expect(key_people_form.submit(valid_params)).to eq(true) end + + it "should set a person_type of 'key'" do + key_people_form.submit(valid_params) + expect(key_people_form.new_person.person_type).to eq("key") + end end context "when the form is not valid" do @@ -39,7 +44,7 @@ context "when the transient registration already has enough key people" do before(:each) do - key_people_form.transient_registration.update_attributes(keyPeople: [build(:key_person, :has_required_data)]) + key_people_form.transient_registration.update_attributes(keyPeople: [build(:key_person, :has_required_data, :key)]) key_people_form.business_type = "overseas" end @@ -50,7 +55,7 @@ context "when the transient registration does not have enough key people" do before(:each) do - key_people_form.transient_registration.update_attributes(keyPeople: [build(:key_person, :has_required_data)]) + key_people_form.transient_registration.update_attributes(keyPeople: [build(:key_person, :has_required_data, :key)]) key_people_form.business_type = "partnership" end @@ -67,7 +72,7 @@ create(:transient_registration, :has_required_data, business_type: "soleTrader", - keyPeople: [build(:key_person, :has_required_data)]) + keyPeople: [build(:key_person, :has_required_data, :key)]) end let(:key_people_form) { KeyPeopleForm.new(transient_registration) } diff --git a/spec/requests/conviction_details_forms_spec.rb b/spec/requests/conviction_details_forms_spec.rb index a3fdada39..750f8698b 100644 --- a/spec/requests/conviction_details_forms_spec.rb +++ b/spec/requests/conviction_details_forms_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" RSpec.describe "ConvictionDetailsForms", type: :request do - describe "GET new_conviction_details_path" do + describe "GET new_conviction_details_form_path" do context "when a valid user is signed in" do let(:user) { create(:user) } before(:each) do @@ -56,12 +56,25 @@ context "when valid params are submitted" do let(:valid_params) { { - reg_identifier: transient_registration[:reg_identifier] + reg_identifier: transient_registration[:reg_identifier], + first_name: "Foo", + last_name: "Bar", + position: "Baz", + dob_day: "1", + dob_month: "1", + dob_year: "2000" } } + it "increases the total number of people" do + total_people_count = transient_registration.keyPeople.count + post conviction_details_forms_path, conviction_details_form: valid_params + expect(transient_registration.reload.keyPeople.count).to eq(total_people_count + 1) + end + it "updates the transient registration" do - # TODO: Add test once data is submitted through the form + post conviction_details_forms_path, conviction_details_form: valid_params + expect(transient_registration.reload.keyPeople.last.first_name).to eq(valid_params[:first_name]) end it "returns a 302 response" do @@ -73,12 +86,62 @@ post conviction_details_forms_path, conviction_details_form: valid_params expect(response).to redirect_to(new_contact_name_form_path(transient_registration[:reg_identifier])) end + + context "when there is already a relevant conviction person" do + let(:relevant_conviction_person) { build(:key_person, :has_required_data, :relevant) } + + before(:each) do + transient_registration.update_attributes(keyPeople: [relevant_conviction_person]) + end + + it "increases the total number of people" do + total_people_count = transient_registration.keyPeople.count + post conviction_details_forms_path, conviction_details_form: valid_params + expect(transient_registration.reload.keyPeople.count).to eq(total_people_count + 1) + end + + it "does not replace the existing relevant conviction person" do + post conviction_details_forms_path, conviction_details_form: valid_params + expect(transient_registration.reload.keyPeople.first.first_name).to eq(relevant_conviction_person.first_name) + end + end + + context "when there is already a key person" do + let(:key_person) { build(:key_person, :has_required_data, :key) } + + before(:each) do + transient_registration.update_attributes(keyPeople: [key_person]) + end + + it "increases the total number of people" do + total_people_count = transient_registration.keyPeople.count + post conviction_details_forms_path, conviction_details_form: valid_params + expect(transient_registration.reload.keyPeople.count).to eq(total_people_count + 1) + end + + it "does not replace the existing key person" do + post conviction_details_forms_path, conviction_details_form: valid_params + expect(transient_registration.reload.keyPeople.first.first_name).to eq(key_person.first_name) + end + end + + context "when the submit params say to add another" do + it "redirects to the conviction_details form" do + post conviction_details_forms_path, conviction_details_form: valid_params, commit: I18n.t("conviction_details_forms.new.add_person_link") + expect(response).to redirect_to(new_conviction_details_form_path(transient_registration[:reg_identifier])) + end + end end context "when invalid params are submitted" do let(:invalid_params) { { - reg_identifier: "foo" + reg_identifier: "foo", + first_name: "", + last_name: "", + dob_day: "31", + dob_month: "02", + dob_year: "2000" } } @@ -87,9 +150,50 @@ expect(response).to have_http_status(302) end - it "does not update the transient registration" do + it "does not increase the total number of people" do + total_people_count = transient_registration.keyPeople.count post conviction_details_forms_path, conviction_details_form: invalid_params - expect(transient_registration.reload[:reg_identifier]).to_not eq(invalid_params[:reg_identifier]) + expect(transient_registration.reload.keyPeople.count).to eq(total_people_count) + end + + context "when there is already a key person" do + let(:existing_key_person) { build(:key_person, :has_required_data, :key) } + + before(:each) do + transient_registration.update_attributes(keyPeople: [existing_key_person]) + end + + it "does not replace the existing key person" do + post conviction_details_forms_path, conviction_details_form: invalid_params + expect(transient_registration.reload.keyPeople.first.first_name).to eq(existing_key_person.first_name) + end + end + + context "when the submit params say to add another" do + it "returns a 302 response" do + post conviction_details_forms_path, conviction_details_form: invalid_params, commit: I18n.t("conviction_details_forms.new.add_person_link") + expect(response).to have_http_status(302) + end + end + end + + context "when blank params are submitted" do + let(:blank_params) { + { + reg_identifier: "foo", + first_name: "", + last_name: "", + position: "", + dob_day: "", + dob_month: "", + dob_year: "" + } + } + + it "does not increase the total number of people" do + total_people_count = transient_registration.keyPeople.count + post conviction_details_forms_path, conviction_details_form: blank_params + expect(transient_registration.reload.keyPeople.count).to eq(total_people_count) end end end @@ -104,12 +208,19 @@ let(:valid_params) { { - reg_identifier: transient_registration[:reg_identifier] + reg_identifier: transient_registration[:reg_identifier], + first_name: "Foo", + last_name: "Bar", + position: "Baz", + dob_day: "1", + dob_month: "1", + dob_year: "2000" } } it "does not update the transient registration" do - # TODO: Add test once data is submitted through the form + post conviction_details_forms_path, conviction_details_form: valid_params + expect(transient_registration.reload.keyPeople).to_not exist end it "returns a 302 response" do @@ -175,4 +286,59 @@ end end end + + describe "DELETE delete_person_conviction_details_forms_path" do + context "when a valid user is signed in" do + let(:user) { create(:user) } + before(:each) do + sign_in(user) + end + + context "when a valid transient registration exists" do + let(:transient_registration) do + create(:transient_registration, + :has_required_data, + account_email: user.email, + workflow_state: "conviction_details_form") + end + + context "when the registration has key people" do + let(:key_person_a) { build(:key_person, :has_required_data, :key) } + let(:key_person_b) { build(:key_person, :has_required_data, :key) } + + before(:each) do + transient_registration.update_attributes(keyPeople: [key_person_a, key_person_b]) + end + + context "when the delete person action is triggered" do + it "returns a 302 response" do + delete delete_person_conviction_details_forms_path(key_person_a[:id]), reg_identifier: transient_registration.reg_identifier + expect(response).to have_http_status(302) + end + + it "redirects to the key people form" do + delete delete_person_conviction_details_forms_path(key_person_a[:id]), reg_identifier: transient_registration.reg_identifier + expect(response).to redirect_to(new_conviction_details_form_path(transient_registration[:reg_identifier])) + end + + it "reduces the total number of people" do + total_people_count = transient_registration.keyPeople.count + delete delete_person_conviction_details_forms_path(key_person_a[:id]), reg_identifier: transient_registration.reg_identifier + expect(transient_registration.reload.keyPeople.count).to eq(total_people_count - 1) + end + + it "removes the key person" do + delete delete_person_conviction_details_forms_path(key_person_a[:id]), reg_identifier: transient_registration.reg_identifier + expect(transient_registration.reload.keyPeople.where(id: key_person_a[:id]).count).to eq(0) + end + + it "does not modify the other key people" do + delete delete_person_conviction_details_forms_path(key_person_a[:id]), reg_identifier: transient_registration.reg_identifier + expect(transient_registration.reload.keyPeople.where(id: key_person_b[:id]).count).to eq(1) + end + end + end + end + end + end end diff --git a/spec/requests/key_people_forms_spec.rb b/spec/requests/key_people_forms_spec.rb index 60d2740ef..ec1eeee0e 100644 --- a/spec/requests/key_people_forms_spec.rb +++ b/spec/requests/key_people_forms_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" RSpec.describe "KeyPeopleForms", type: :request do - describe "GET new_key_people_path" do + describe "GET new_key_people_form_path" do context "when a valid user is signed in" do let(:user) { create(:user) } before(:each) do @@ -87,7 +87,7 @@ end context "when there is already a key person" do - let(:existing_key_person) { build(:key_person) } + let(:existing_key_person) { build(:key_person, :has_required_data, :key) } before(:each) do transient_registration.update_attributes(keyPeople: [existing_key_person]) @@ -118,6 +118,49 @@ end end + context "when there is a relevant conviction person" do + let(:relevant_conviction_person) { build(:key_person, :has_required_data, :relevant) } + + before(:each) do + transient_registration.update_attributes(keyPeople: [relevant_conviction_person]) + end + + context "when there can be multiple key people" do + it "increases the number of key people" do + key_people_count = transient_registration.keyPeople.count + post key_people_forms_path, key_people_form: valid_params + expect(transient_registration.reload.keyPeople.count).to eq(key_people_count + 1) + end + + it "does not replace the relevant conviction person" do + post key_people_forms_path, key_people_form: valid_params + expect(transient_registration.reload.keyPeople.first.first_name).to eq(relevant_conviction_person.first_name) + end + end + + context "when there can only be one key person" do + before(:each) do + transient_registration.update_attributes(business_type: "soleTrader") + end + + it "increases the number of key people" do + key_people_count = transient_registration.keyPeople.count + post key_people_forms_path, key_people_form: valid_params + expect(transient_registration.reload.keyPeople.count).to eq(key_people_count + 1) + end + + it "adds the new key person" do + post key_people_forms_path, key_people_form: valid_params + expect(transient_registration.reload.keyPeople.last.first_name).to eq(valid_params[:first_name]) + end + + it "does not replace the relevant conviction person" do + post key_people_forms_path, key_people_form: valid_params + expect(transient_registration.reload.keyPeople.first.first_name).to eq(relevant_conviction_person.first_name) + end + end + end + context "when the submit params say to add another" do it "redirects to the key_people form" do post key_people_forms_path, key_people_form: valid_params, commit: I18n.t("key_people_forms.new.add_person_link") @@ -150,7 +193,7 @@ end context "when there is already a key person" do - let(:existing_key_person) { build(:key_person) } + let(:existing_key_person) { build(:key_person, :has_required_data, :key) } before(:each) do transient_registration.update_attributes(keyPeople: [existing_key_person]) @@ -307,8 +350,8 @@ end context "when the registration has key people" do - let(:key_person_a) { build(:key_person, :has_required_data) } - let(:key_person_b) { build(:key_person, :has_required_data) } + let(:key_person_a) { build(:key_person, :has_required_data, :key) } + let(:key_person_b) { build(:key_person, :has_required_data, :key) } before(:each) do transient_registration.update_attributes(keyPeople: [key_person_a, key_person_b])