From 5de07c194e85fa74e7d7f2be12e99cb9acad3ccc Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 9 Sep 2017 17:37:13 +0200 Subject: Validate postcode properly. Don't accept invalid postcodes, or postcodes containing invalid chars. --- lib/contact.rb | 27 +++++++++++++++++++-------- spec/contact_spec.rb | 29 +++++++++++++++++++++++++++++ spec/spec_helper.rb | 2 ++ spec/support/contact_factory.rb | 12 ++++++++++++ 4 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 spec/contact_spec.rb create mode 100644 spec/support/contact_factory.rb diff --git a/lib/contact.rb b/lib/contact.rb index ffa33bf..cb9a6e9 100644 --- a/lib/contact.rb +++ b/lib/contact.rb @@ -16,28 +16,39 @@ class Contact "#{@street}, #{@postcode} #{@city}" end + def valid? + validate! + @errors.length == 0 + end + def validate! - errors = [] + @errors = [] if @name.nil? || @name.strip.empty? - errors << "Du må oppgi en kontaktperson" + @errors << "Du må oppgi en kontaktperson" end - if @postcode.nil? || @postcode.strip.empty? - errors << "Du må oppgi et gyldig postnr." + unless valid_postcode + @errors << "Du må oppgi et gyldig postnr." end if @city.nil? || @city.strip.empty? - errors << "Du må oppgi poststed." + @errors << "Du må oppgi poststed." end if @phone.nil? || @phone.strip.empty? - errors << "Du må oppgi et telefonnummer" + @errors << "Du må oppgi et telefonnummer" end if @email.nil? || @email.strip.empty? - errors << "Du må oppgi en epostadresse" + @errors << "Du må oppgi en epostadresse" end - errors + @errors + end + + private + + def valid_postcode + @postcode && @postcode =~ /^\d{4}$/ end end diff --git a/spec/contact_spec.rb b/spec/contact_spec.rb new file mode 100644 index 0000000..980626b --- /dev/null +++ b/spec/contact_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +describe Contact do + let(:params) { create_contact_params } + + it "rejects empty postcodes" do + params.delete('postcode') + @contact = Contact.new(params) + expect(@contact.valid?).to be(false) + end + + it "rejects too short postcodes" do + params['postcode'] = '123' + @contact = Contact.new(params) + expect(@contact.valid?).to be(false) + end + + it "rejects too long postcodes" do + params['postcode'] = '12345' + @contact = Contact.new(params) + expect(@contact.valid?).to be(false) + end + + it "rejects postcodes wiht invalid chars" do + params['postcode'] = '123s' + @contact = Contact.new(params) + expect(@contact.valid?).to be(false) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cd9f3f3..293bd8e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,11 +2,13 @@ ENV['RACK_ENV'] = 'test' require_relative '../registration' require_relative 'support/band_factory' +require_relative 'support/contact_factory' require_relative 'support/submit_form_helper' require 'capybara/rspec' RSpec.configure do |config| config.include BandFactory + config.include ContactFactory config.include SubmitFormHelper config.before(:example) do diff --git a/spec/support/contact_factory.rb b/spec/support/contact_factory.rb new file mode 100644 index 0000000..f05e9a1 --- /dev/null +++ b/spec/support/contact_factory.rb @@ -0,0 +1,12 @@ +module ContactFactory + def create_contact_params(params = {}) + par = { + 'name' => 'Contact Name', + 'street' => "Streetname 666", + 'postcode' => "1234", + 'city' => "Someplace Nice", + 'phone' => '98765432', + 'email' => 'band@example.com', + }.merge(params) + end +end -- cgit v1.2.3