From d5a24990107abcd2812d1b73bd70abf820fbce98 Mon Sep 17 00:00:00 2001 From: Leonardo Diez Date: Mon, 13 Nov 2017 12:07:18 +0100 Subject: [PATCH] BIC format validation. --- Gemfile.lock | 10 +++++----- README.md | 6 +++++- lib/iban_bic/models/bic.rb | 7 +++++++ lib/iban_bic/version.rb | 2 +- spec/iban_bic/core_spec.rb | 4 ++-- spec/models/bic_spec.rb | 32 ++++++++++++++++++++++++++++++++ spec/test_app/Gemfile.lock | 25 ++++++++++++------------- 7 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 spec/models/bic_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 9187b1e..cf7f069 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - iban_bic (1.2.0) + iban_bic (1.3.0) rails (~> 5.1) regexp-examples (~> 1.3) @@ -82,7 +82,7 @@ GEM mail (2.7.0) mini_mime (>= 0.1.1) method_source (0.9.0) - mini_mime (0.1.4) + mini_mime (1.0.0) mini_portile2 (2.3.0) minitest (5.10.3) nio4r (2.1.0) @@ -121,7 +121,7 @@ GEM rainbow (2.2.2) rake rake (12.2.1) - regexp-examples (1.4.0) + regexp-examples (1.4.1) rspec-core (3.7.0) rspec-support (~> 3.7.0) rspec-expectations (3.7.0) @@ -173,7 +173,7 @@ GEM equalizer (~> 0.0, >= 0.0.9) websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.2) + websocket-extensions (0.1.3) PLATFORMS ruby @@ -189,4 +189,4 @@ DEPENDENCIES virtus (~> 1.0) BUNDLED WITH - 1.15.1 + 1.16.0 diff --git a/README.md b/README.md index 399bf95..d54fbb0 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,10 @@ $ bundle exec rails generate iban_bic:install --with-static-data 4. Customize initializer if needed, adding validations for new countries, or overriding YAML files. ## Changelog +#### 1.3.0 + +* Added BIC format validation in BIC model. + #### 1.2.0 * Added `like_pattern_from_parts` method. Not a very performant version. @@ -137,7 +141,7 @@ $ bundle exec rails generate iban_bic:install --with-static-data #### 1.0.1 -* Added presence validations in BIC model +* Added presence validations in BIC model. #### 1.0.0 diff --git a/lib/iban_bic/models/bic.rb b/lib/iban_bic/models/bic.rb index cc4464f..4b09439 100644 --- a/lib/iban_bic/models/bic.rb +++ b/lib/iban_bic/models/bic.rb @@ -5,6 +5,13 @@ class Bic < ActiveRecord::Base validates :country, :bank_code, :bic, presence: true + validate do + bic.upcase! + unless /[A-Z]{4}#{country}[0-9A-Z]{2,5}/.match? bic + errors.add(:bic, :invalid_format) + end + end + after_commit do IbanBic.clear_cache end diff --git a/lib/iban_bic/version.rb b/lib/iban_bic/version.rb index 4d487f8..56c69b7 100644 --- a/lib/iban_bic/version.rb +++ b/lib/iban_bic/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module IbanBic - VERSION = "1.2.0" + VERSION = "1.3.0" end diff --git a/spec/iban_bic/core_spec.rb b/spec/iban_bic/core_spec.rb index bc03891..4793c2d 100644 --- a/spec/iban_bic/core_spec.rb +++ b/spec/iban_bic/core_spec.rb @@ -37,11 +37,11 @@ context "when using database entries" do before do new_bic = Bic.find_or_initialize_by(country: "ES", bank_code: "0003") - new_bic.bic = "DIFFERENT" + new_bic.bic = "ABCDESMM" new_bic.save! IbanBic.clear_cache end - it { is_expected.to eq("DIFFERENT") } + it { is_expected.to eq("ABCDESMM") } end context "when using static bics" do diff --git a/spec/models/bic_spec.rb b/spec/models/bic_spec.rb new file mode 100644 index 0000000..7f45c36 --- /dev/null +++ b/spec/models/bic_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe Bic, :db do + subject(:bic) { Bic.new country: country, bank_code: bank_code, bic: bic_code } + let(:country) { "ES" } + let(:bank_code) { "1234" } + let(:bic_code) { "ABCDESMM123" } + + it { is_expected.to be_valid } + + context "when bic is downcase" do + let(:bic_code) { "abcdesmm123" } + it { is_expected.to be_valid } + end + + context "when bic doesn't include branch code" do + let(:bic_code) { "ABCDESMM" } + it { is_expected.to be_valid } + end + + context "when country inside bic doesn't match country" do + let(:country) { "PT" } + it { is_expected.to be_invalid } + end + + context "when bic format is invalid" do + let(:bic_code) { "fsd234asd" } + it { is_expected.to be_invalid } + end +end diff --git a/spec/test_app/Gemfile.lock b/spec/test_app/Gemfile.lock index b6fd130..df46779 100644 --- a/spec/test_app/Gemfile.lock +++ b/spec/test_app/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../.. specs: - iban_bic (1.0.0) + iban_bic (1.2.0) rails (~> 5.1) regexp-examples (~> 1.3) @@ -50,19 +50,18 @@ GEM byebug (9.1.0) concurrent-ruby (1.0.5) crass (1.0.2) - erubi (1.6.1) - globalid (0.4.0) + erubi (1.7.0) + globalid (0.4.1) activesupport (>= 4.2.0) - i18n (0.8.6) + i18n (0.9.1) + concurrent-ruby (~> 1.0) loofah (2.1.1) crass (~> 1.0.2) nokogiri (>= 1.5.9) - mail (2.6.6) - mime-types (>= 1.16, < 4) + mail (2.7.0) + mini_mime (>= 0.1.1) method_source (0.9.0) - mime-types (3.1) - mime-types-data (~> 3.2015) - mime-types-data (3.2016.0521) + mini_mime (0.1.4) mini_portile2 (2.3.0) minitest (5.10.3) nio4r (2.1.0) @@ -94,8 +93,8 @@ GEM method_source rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - rake (12.1.0) - regexp-examples (1.3.2) + rake (12.2.1) + regexp-examples (1.4.0) sprockets (3.7.1) concurrent-ruby (~> 1.0) rack (> 1, < 3) @@ -106,7 +105,7 @@ GEM sqlite3 (1.3.13) thor (0.20.0) thread_safe (0.3.6) - tzinfo (1.2.3) + tzinfo (1.2.4) thread_safe (~> 0.1) websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) @@ -124,4 +123,4 @@ RUBY VERSION ruby 2.4.1p111 BUNDLED WITH - 1.15.1 + 1.16.0