defra_ruby_validators 0.1.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -18
  3. data/config/locales/defra_ruby/validators/companies_house_number_validator/en.yml +10 -0
  4. data/lib/defra_ruby/validators.rb +15 -0
  5. data/lib/defra_ruby/validators/base_validator.rb +21 -0
  6. data/lib/defra_ruby/validators/companies_house_number_validator.rb +54 -0
  7. data/lib/defra_ruby/validators/companies_house_service.rb +36 -0
  8. data/lib/defra_ruby/validators/configuration.rb +35 -0
  9. data/lib/defra_ruby/validators/engine.rb +22 -0
  10. data/lib/defra_ruby/validators/version.rb +7 -0
  11. data/lib/defra_ruby_validators.rb +27 -9
  12. data/spec/cassettes/company_no_inactive.yml +14 -14
  13. data/spec/cassettes/company_no_not_found.yml +15 -15
  14. data/spec/cassettes/company_no_valid.yml +14 -14
  15. data/spec/defra_ruby/validators/companies_house_number_validator_spec.rb +88 -0
  16. data/spec/defra_ruby/validators/companies_house_service_spec.rb +58 -0
  17. data/spec/defra_ruby/validators/configuration_spec.rb +49 -0
  18. data/spec/defra_ruby/validators_spec.rb +12 -0
  19. data/spec/examples.txt +36 -27
  20. data/spec/spec_helper.rb +69 -19
  21. data/spec/support/defra_ruby_validators.rb +13 -2
  22. data/spec/support/dotenv.rb +4 -0
  23. data/spec/support/helpers/translator.rb +14 -0
  24. data/spec/support/pry.rb +7 -0
  25. data/spec/support/shared_examples/validators/invalid_record.rb +18 -0
  26. data/spec/support/shared_examples/validators/valid_record.rb +12 -0
  27. data/spec/support/shared_examples/validators/validator.rb +8 -0
  28. data/spec/support/simplecov.rb +17 -0
  29. metadata +37 -19
  30. data/config/locales/defra_ruby_validators/companies_house_number/en.yml +0 -14
  31. data/lib/defra_ruby_validators/companies_house_number_validator.rb +0 -56
  32. data/lib/defra_ruby_validators/companies_house_service.rb +0 -34
  33. data/lib/defra_ruby_validators/engine.rb +0 -20
  34. data/lib/defra_ruby_validators/validators.rb +0 -33
  35. data/lib/defra_ruby_validators/version.rb +0 -5
  36. data/spec/defra_ruby_validators/companies_house_number_validator_spec.rb +0 -125
  37. data/spec/defra_ruby_validators/companies_house_service_spec.rb +0 -55
  38. data/spec/defra_ruby_validators/version_spec.rb +0 -7
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ module Test
6
+ CompaniesHouseNumberValidatable = Struct.new(:company_no) do
7
+ include ActiveModel::Validations
8
+
9
+ validates :company_no, "defra_ruby/validators/companies_house_number": true
10
+ end
11
+ end
12
+
13
+ module DefraRuby
14
+ module Validators
15
+ RSpec.describe CompaniesHouseNumberValidator do
16
+
17
+ valid_numbers = %w[10997904 09764739 SC534714 IP00141R]
18
+ invalid_format_number = "foobar42"
19
+ unknown_number = "99999999"
20
+ inactive_number = "07281919"
21
+
22
+ it_behaves_like "a validator"
23
+
24
+ describe "#validate_each" do
25
+ context "when given a valid company number" do
26
+ before do
27
+ allow_any_instance_of(CompaniesHouseService).to receive(:status).and_return(:active)
28
+ end
29
+
30
+ valid_numbers.each do |company_no|
31
+ context "like #{company_no}" do
32
+ it_behaves_like "a valid record", Test::CompaniesHouseNumberValidatable.new(company_no)
33
+ end
34
+ end
35
+ end
36
+
37
+ context "when given an invalid company number" do
38
+ context "because it is blank" do
39
+ validatable = Test::CompaniesHouseNumberValidatable.new
40
+ error_message = Helpers::Translator.error_message(CompaniesHouseNumberValidator, :company_no, :blank)
41
+
42
+ it_behaves_like "an invalid record", validatable, :company_no, error_message
43
+ end
44
+
45
+ context "because the format is wrong" do
46
+ validatable = Test::CompaniesHouseNumberValidatable.new(invalid_format_number)
47
+ error_message = Helpers::Translator.error_message(CompaniesHouseNumberValidator, :company_no, :invalid)
48
+
49
+ it_behaves_like "an invalid record", validatable, :company_no, error_message
50
+ end
51
+
52
+ context "because it's not found on companies house" do
53
+ before do
54
+ allow_any_instance_of(CompaniesHouseService).to receive(:status).and_return(:not_found)
55
+ end
56
+
57
+ validatable = Test::CompaniesHouseNumberValidatable.new(unknown_number)
58
+ error_message = Helpers::Translator.error_message(CompaniesHouseNumberValidator, :company_no, :not_found)
59
+
60
+ it_behaves_like "an invalid record", validatable, :company_no, error_message
61
+ end
62
+
63
+ context "because it's not 'active' on companies house" do
64
+ before do
65
+ allow_any_instance_of(CompaniesHouseService).to receive(:status).and_return(:inactive)
66
+ end
67
+
68
+ validatable = Test::CompaniesHouseNumberValidatable.new(inactive_number)
69
+ error_message = Helpers::Translator.error_message(CompaniesHouseNumberValidator, :company_no, :inactive)
70
+
71
+ it_behaves_like "an invalid record", validatable, :company_no, error_message
72
+ end
73
+ end
74
+
75
+ context "when there is an error connecting with companies house" do
76
+ before do
77
+ allow_any_instance_of(CompaniesHouseService).to receive(:status).and_raise(StandardError)
78
+ end
79
+
80
+ validatable = Test::CompaniesHouseNumberValidatable.new(valid_numbers.sample)
81
+ error_message = Helpers::Translator.error_message(CompaniesHouseNumberValidator, :company_no, :error)
82
+
83
+ it_behaves_like "an invalid record", validatable, :company_no, error_message
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe DefraRuby::Validators::CompaniesHouseService do
4
+ let(:subject) { described_class.new("09360070") }
5
+ let(:host) { "https://api.companieshouse.gov.uk/" }
6
+
7
+ describe "#status" do
8
+ context "when the company_no is for an active company" do
9
+ before(:each) { VCR.insert_cassette("company_no_valid") }
10
+ after(:each) { VCR.eject_cassette }
11
+
12
+ it "returns :active" do
13
+ expect(subject.status).to eq(:active)
14
+ end
15
+ end
16
+
17
+ context "when the company_no is not found" do
18
+ let(:subject) { described_class.new("99999999") }
19
+ before(:each) { VCR.insert_cassette("company_no_not_found") }
20
+ after(:each) { VCR.eject_cassette }
21
+
22
+ it "returns :not_found" do
23
+ expect(subject.status).to eq(:not_found)
24
+ end
25
+ end
26
+
27
+ context "when the company_no is inactive" do
28
+ let(:subject) { described_class.new("07281919") }
29
+ before(:each) { VCR.insert_cassette("company_no_inactive") }
30
+ after(:each) { VCR.eject_cassette }
31
+
32
+ it "returns :inactive" do
33
+ expect(subject.status).to eq(:inactive)
34
+ end
35
+ end
36
+
37
+ context "when there is a problem with the Companies House API" do
38
+ before(:each) { VCR.turn_off! }
39
+ after(:each) { VCR.turn_on! }
40
+
41
+ context "and the request times out" do
42
+ before(:each) { stub_request(:any, /.*#{host}.*/).to_timeout }
43
+
44
+ it "raises an exception" do
45
+ expect { subject.status }.to raise_error(StandardError)
46
+ end
47
+ end
48
+
49
+ context "and request returns an error" do
50
+ before(:each) { stub_request(:any, /.*#{host}.*/).to_raise(SocketError) }
51
+
52
+ it "raises an exception" do
53
+ expect { subject.status }.to raise_error(StandardError)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe DefraRuby::Validators::Configuration do
6
+ describe "ATTRIBUTES" do
7
+ it "represents the expected config settings and only those settings" do
8
+ expected_attributes = %i[
9
+ companies_house_host
10
+ companies_house_api_key
11
+ ]
12
+
13
+ expect(described_class::ATTRIBUTES).to match_array(expected_attributes)
14
+ end
15
+ end
16
+
17
+ it "sets the appropriate default config settings" do
18
+ fresh_config = described_class.new
19
+
20
+ expect(fresh_config.companies_house_host).to eq("https://api.companieshouse.gov.uk/company/")
21
+ expect(fresh_config.companies_house_api_key).to eq(nil)
22
+ end
23
+
24
+ describe "#ensure_valid" do
25
+ before(:each) do
26
+ described_class::ATTRIBUTES.each do |attribute|
27
+ subject.public_send("#{attribute}=", "foo")
28
+ end
29
+ end
30
+
31
+ context "when all of the attributes are present" do
32
+ it "does not raise an error" do
33
+ expect { subject.ensure_valid }.to_not raise_error
34
+ expect(subject.ensure_valid).to eq(true)
35
+ end
36
+ end
37
+
38
+ context "when at least one of the attributes is missing" do
39
+ before(:each) do
40
+ subject.companies_house_api_key = nil
41
+ end
42
+
43
+ it "raises an error" do
44
+ message = "The following DefraRuby::Validators configuration attributes are missing: [:companies_house_api_key]"
45
+ expect { subject.ensure_valid }.to raise_error(message)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe DefraRuby::Validators do
6
+ describe "VERSION" do
7
+ it "is a version string in the correct format" do
8
+ expect(DefraRuby::Validators::VERSION).to be_a(String)
9
+ expect(DefraRuby::Validators::VERSION).to match(/\d+\.\d+\.\d+/)
10
+ end
11
+ end
12
+ end
data/spec/examples.txt CHANGED
@@ -1,27 +1,36 @@
1
- example_id | status | run_time |
2
- ------------------------------------------------------------------------------ | ------ | --------------- |
3
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:1:1:1] | passed | 0.01413 seconds |
4
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:1:2:1] | passed | 0.0008 seconds |
5
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:1:3:1] | passed | 0.00053 seconds |
6
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:1:4:1] | passed | 0.00051 seconds |
7
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:2:1:1] | passed | 0.00043 seconds |
8
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:2:1:2] | passed | 0.00134 seconds |
9
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:2:1:3] | passed | 0.00047 seconds |
10
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:2:2:1] | passed | 0.00035 seconds |
11
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:2:2:2] | passed | 0.00025 seconds |
12
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:2:2:3] | passed | 0.00024 seconds |
13
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:2:3:1] | passed | 0.00077 seconds |
14
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:2:3:2] | passed | 0.00077 seconds |
15
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:2:3:3] | passed | 0.00076 seconds |
16
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:2:4:1] | passed | 0.00064 seconds |
17
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:2:4:2] | passed | 0.00056 seconds |
18
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:2:4:3] | passed | 0.00058 seconds |
19
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:3:1] | passed | 0.00064 seconds |
20
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:3:2] | passed | 0.00057 seconds |
21
- ./spec/defra_ruby_validators/companies_house_number_validator_spec.rb[1:3:3] | passed | 0.00066 seconds |
22
- ./spec/defra_ruby_validators/companies_house_service_spec.rb[1:1:1] | passed | 0.04998 seconds |
23
- ./spec/defra_ruby_validators/companies_house_service_spec.rb[1:2:1] | passed | 0.02093 seconds |
24
- ./spec/defra_ruby_validators/companies_house_service_spec.rb[1:3:1] | passed | 0.02306 seconds |
25
- ./spec/defra_ruby_validators/companies_house_service_spec.rb[1:4:1] | passed | 0.02332 seconds |
26
- ./spec/defra_ruby_validators/companies_house_service_spec.rb[1:5:1] | passed | 0.01492 seconds |
27
- ./spec/defra_ruby_validators/version_spec.rb[1:1] | passed | 0.00312 seconds |
1
+ example_id | status | run_time |
2
+ ---------------------------------------------------------------------------------- | ------ | --------------- |
3
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:1:1] | passed | 0.00014 seconds |
4
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:1:1:1] | passed | 0.00062 seconds |
5
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:1:1:2] | passed | 0.00048 seconds |
6
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:2:1:1] | passed | 0.00081 seconds |
7
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:2:1:2] | passed | 0.0005 seconds |
8
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:3:1:1] | passed | 0.00047 seconds |
9
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:3:1:2] | passed | 0.00047 seconds |
10
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:4:1:1] | passed | 0.00044 seconds |
11
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:1:4:1:2] | passed | 0.00052 seconds |
12
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:1:1:1] | passed | 0.00172 seconds |
13
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:1:1:2] | passed | 0.00018 seconds |
14
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:1:1:3] | passed | 0.00764 seconds |
15
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:2:1:1] | passed | 0.00055 seconds |
16
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:2:1:2] | passed | 0.00028 seconds |
17
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:2:1:3] | passed | 0.0002 seconds |
18
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:3:1:1] | passed | 0.00049 seconds |
19
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:3:1:2] | passed | 0.00915 seconds |
20
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:3:1:3] | passed | 0.00051 seconds |
21
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:4:1:1] | passed | 0.00049 seconds |
22
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:4:1:2] | passed | 0.00043 seconds |
23
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:2:4:1:3] | passed | 0.00046 seconds |
24
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:3:1:1] | passed | 0.00055 seconds |
25
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:3:1:2] | passed | 0.00049 seconds |
26
+ ./spec/defra_ruby/validators/companies_house_number_validator_spec.rb[1:2:3:1:3] | passed | 0.00061 seconds |
27
+ ./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:1:1] | passed | 0.02711 seconds |
28
+ ./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:2:1] | passed | 0.01832 seconds |
29
+ ./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:3:1] | passed | 0.01756 seconds |
30
+ ./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:4:1:1] | passed | 0.03546 seconds |
31
+ ./spec/defra_ruby/validators/companies_house_service_spec.rb[1:1:4:2:1] | passed | 0.01611 seconds |
32
+ ./spec/defra_ruby/validators/configuration_spec.rb[1:1:1] | passed | 0.00407 seconds |
33
+ ./spec/defra_ruby/validators/configuration_spec.rb[1:2] | passed | 0.0001 seconds |
34
+ ./spec/defra_ruby/validators/configuration_spec.rb[1:3:1:1] | passed | 0.00021 seconds |
35
+ ./spec/defra_ruby/validators/configuration_spec.rb[1:3:2:1] | passed | 0.00021 seconds |
36
+ ./spec/defra_ruby/validators_spec.rb[1:1:1] | passed | 0.00494 seconds |
data/spec/spec_helper.rb CHANGED
@@ -2,32 +2,82 @@
2
2
 
3
3
  require "bundler/setup"
4
4
 
5
- # Test coverage reporting
6
- require "simplecov"
7
- SimpleCov.start
8
-
9
- # Support debugging in the tests
10
- require "byebug"
11
- # Load env vars from a text file
12
- require "dotenv/load"
13
- # Need to require our actual code files
14
- require "defra_ruby_validators/validators"
15
-
16
- # The following line is provided for convenience purposes. It has the downside
17
- # of increasing the boot-up time by auto-requiring all files in the support
18
- # directory. However in a small gem like this the increase should be neglible
19
- Dir[File.join(__dir__, "support", "**", "*.rb")].each { |f| require f }
5
+ # Require and run our simplecov initializer as the very first thing we do.
6
+ # This is as per its docs https://github.com/colszowka/simplecov#getting-started
7
+ require "./spec/support/simplecov"
8
+
9
+ # Requires supporting ruby files with custom matchers and macros, etc, in
10
+ # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
11
+ # run as spec files by default. This means that files in spec/support that end
12
+ # in _spec.rb will both be required and run as specs, causing the specs to be
13
+ # run twice. It is recommended that you do not name files matching this glob to
14
+ # end with _spec.rb. You can configure this pattern with the --pattern
15
+ # option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
16
+ #
17
+ # We make an exception for simplecov because that will already have been
18
+ # required and run at the very top of spec_helper.rb
19
+ support_files = Dir["./spec/support/**/*.rb"].reject { |file| file == "./spec/support/simplecov.rb" }
20
+ support_files.each { |f| require f }
20
21
 
21
22
  RSpec.configure do |config|
23
+ # rspec-expectations config goes here. You can use an alternate
24
+ # assertion/expectation library such as wrong or the stdlib/minitest
25
+ # assertions if you prefer.
26
+ config.expect_with :rspec do |expectations|
27
+ # This option will default to `true` in RSpec 4. It makes the `description`
28
+ # and `failure_message` of custom matchers include text for helper methods
29
+ # defined using `chain`, e.g.:
30
+ # be_bigger_than(2).and_smaller_than(4).description
31
+ # # => "be bigger than 2 and smaller than 4"
32
+ # ...rather than:
33
+ # # => "be bigger than 2"
34
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
35
+ end
36
+
37
+ # rspec-mocks config goes here. You can use an alternate test double
38
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
39
+ config.mock_with :rspec do |mocks|
40
+ # Prevents you from mocking or stubbing a method that does not exist on
41
+ # a real object. This is generally recommended, and will default to
42
+ # `true` in RSpec 4.
43
+ mocks.verify_partial_doubles = true
44
+ end
45
+
46
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
47
+ # have no way to turn it off -- the option exists only for backwards
48
+ # compatibility in RSpec 3). It causes shared context metadata to be
49
+ # inherited by the metadata hash of host groups and examples, rather than
50
+ # triggering implicit auto-inclusion in groups with matching metadata.
51
+ config.shared_context_metadata_behavior = :apply_to_host_groups
52
+
53
+ # This allows you to limit a spec run to individual examples or groups
54
+ # you care about by tagging them with `:focus` metadata. When nothing
55
+ # is tagged with `:focus`, all examples get run. RSpec also provides
56
+ # aliases for `it`, `describe`, and `context` that include `:focus`
57
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
58
+ config.filter_run_when_matching :focus
59
+
22
60
  # Allows RSpec to persist some state between runs in order to support
23
61
  # the `--only-failures` and `--next-failure` CLI options. We recommend
24
62
  # you configure your source control system to ignore this file.
25
63
  config.example_status_persistence_file_path = "spec/examples.txt"
26
64
 
27
- # Disable RSpec exposing methods globally on `Module` and `main`
65
+ # Limits the available syntax to the non-monkey patched syntax that is
66
+ # recommended. For more details, see:
67
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
68
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
69
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
28
70
  config.disable_monkey_patching!
29
71
 
30
- config.expect_with :rspec do |c|
31
- c.syntax = :expect
32
- end
72
+ # Run specs in random order to surface order dependencies. If you find an
73
+ # order dependency and want to debug it, you can fix the order by providing
74
+ # the seed, which is printed after each run.
75
+ # --seed 1234
76
+ config.order = :random
77
+
78
+ # Seed global randomization in this process using the `--seed` CLI option.
79
+ # Setting this allows you to use `--seed` to deterministically reproduce
80
+ # test failures related to randomization by passing the same `--seed` value
81
+ # as the one that triggered the failure.
82
+ Kernel.srand config.seed
33
83
  end
@@ -1,5 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- DefraRubyValidators.configure do |configuration|
4
- configuration.companies_house_api_key = ENV["COMPANIES_HOUSE_API_KEY"]
3
+ # Need to require our actual code files. We don't just require everything in
4
+ # lib/defra_ruby because it contains the engine file which has a dependency on
5
+ # rails. We don't have that as a dependency of this project because it is
6
+ # a given this will be used in a rails project. So instead we require the
7
+ # validators file directly to load the content covered by our tests.
8
+ require "defra_ruby/validators"
9
+
10
+ DefraRuby::Validators.configure do |c|
11
+ def raise_missing_env_var(variable)
12
+ raise("Environment variable #{variable} has not been set")
13
+ end
14
+
15
+ c.companies_house_api_key = (ENV["COMPANIES_HOUSE_API_KEY"] || raise_missing_env_var("COMPANIES_HOUSE_API_KEY"))
5
16
  end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Load env vars from a text file
4
+ require "dotenv/load"
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helpers
4
+ module Translator
5
+ def self.error_message(klass, attribute, error)
6
+ class_name = klass_name(klass)
7
+ I18n.t("defra_ruby.validators.#{class_name}.#{attribute}.#{error}")
8
+ end
9
+
10
+ def self.klass_name(klass)
11
+ klass.name.split("::").last
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Support debugging in the tests. Add `binding.pry` wherever you want execution
4
+ # to stop and the debugger to kick in.
5
+ # Details on the debugging commands can be found here
6
+ # https://github.com/deivid-rodriguez/pry-byebug#commands
7
+ require "pry-byebug"
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.shared_examples "an invalid record" do |validatable, property, error_message|
4
+ it "confirms the object is invalid" do
5
+ expect(validatable).to_not be_valid
6
+ end
7
+
8
+ it "adds a single validation error to the record" do
9
+ validatable.valid?
10
+ expect(validatable.errors[property].count).to eq(1)
11
+ end
12
+
13
+ it "adds an appropriate validation error" do
14
+ validatable.valid?
15
+ expect(error_message).to_not include("translation missing:")
16
+ expect(validatable.errors[property]).to eq([error_message])
17
+ end
18
+ end