defra_ruby_validators 2.4.1 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +9 -2
  3. data/lib/defra_ruby/validators/companies_house_number_validator.rb +9 -5
  4. data/lib/defra_ruby/validators/companies_house_service.rb +26 -12
  5. data/lib/defra_ruby/validators/version.rb +1 -1
  6. metadata +22 -174
  7. data/spec/defra_ruby/validators/business_type_validator_spec.rb +0 -56
  8. data/spec/defra_ruby/validators/companies_house_number_validator_spec.rb +0 -108
  9. data/spec/defra_ruby/validators/companies_house_service_spec.rb +0 -70
  10. data/spec/defra_ruby/validators/configuration_spec.rb +0 -49
  11. data/spec/defra_ruby/validators/email_validator_spec.rb +0 -46
  12. data/spec/defra_ruby/validators/grid_reference_validator_spec.rb +0 -57
  13. data/spec/defra_ruby/validators/location_validator_spec.rb +0 -56
  14. data/spec/defra_ruby/validators/past_date_validator_spec.rb +0 -35
  15. data/spec/defra_ruby/validators/phone_number_validator_spec.rb +0 -59
  16. data/spec/defra_ruby/validators/position_validator_spec.rb +0 -47
  17. data/spec/defra_ruby/validators/token_validator_spec.rb +0 -45
  18. data/spec/defra_ruby/validators/true_false_validator_spec.rb +0 -37
  19. data/spec/defra_ruby/validators_spec.rb +0 -12
  20. data/spec/spec_helper.rb +0 -87
  21. data/spec/support/defra_ruby_validators.rb +0 -16
  22. data/spec/support/helpers/text_generator.rb +0 -17
  23. data/spec/support/helpers/translator.rb +0 -15
  24. data/spec/support/i18n.rb +0 -8
  25. data/spec/support/pry.rb +0 -7
  26. data/spec/support/shared_examples/validators/characters_validator.rb +0 -29
  27. data/spec/support/shared_examples/validators/invalid_record.rb +0 -29
  28. data/spec/support/shared_examples/validators/length_validator.rb +0 -29
  29. data/spec/support/shared_examples/validators/presence_validator.rb +0 -29
  30. data/spec/support/shared_examples/validators/selection_validator.rb +0 -40
  31. data/spec/support/shared_examples/validators/valid_record.rb +0 -12
  32. data/spec/support/shared_examples/validators/validator.rb +0 -8
  33. data/spec/support/simplecov.rb +0 -17
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 21bdd325a2591dc6b3240957791d511239cadf35
4
- data.tar.gz: dbf3657df6f3c814a3f2081a4c576f3319c5c713
2
+ SHA256:
3
+ metadata.gz: 3a8cefa4eee1eb9fcc363935f64760eb88aafd35d29ac09cfd3663105aafa3eb
4
+ data.tar.gz: 0dbc80e1d0c36fc4452257e6ade5377d83f04b860f9ec0375975526aca43f961
5
5
  SHA512:
6
- metadata.gz: 22c834aeb4feacd105d153c38a0db5ba1ea3951e8f4e78fb8bb8f48fb1a797ba64973b98565ea4c51904ca623a40934f1e9d6f57b7bb7319197762480cf17938
7
- data.tar.gz: 3553f860583d638a6fcd86038240c507dd3fc4f89ad16e1fd87ec68bbf5386a27d36f46ecb7536717f2f060804ae5f8d9fdaa1ef784cbc05194df1beedf63b44
6
+ metadata.gz: 0417b991688a935460cbd8e96d28dd8d1496838d7aa5d56420f9a0e5ac701d652b278657a1a733c905e1f3b8ae6880e659b58071e90673af351224c9a3445ed2
7
+ data.tar.gz: 88fb21c5eb3ca1cc365ce4b52aeb8171540ec1cc5776b32cab5122fccb978215f46ea92ff13ceedde13fedd4b700d8f757a74db50f08e4e0e45e049c6ab2dfde
data/README.md CHANGED
@@ -1,9 +1,8 @@
1
1
  # Defra Ruby Validators
2
2
 
3
- [![Build Status](https://travis-ci.com/DEFRA/defra-ruby-validators.svg?branch=main)](https://travis-ci.com/DEFRA/defra-ruby-validators)
3
+ ![Build Status](https://github.com/DEFRA/defra-ruby-validators/workflows/CI/badge.svg?branch=main)
4
4
  [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=DEFRA_defra-ruby-validators&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=DEFRA_defra-ruby-validators)
5
5
  [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=DEFRA_defra-ruby-validators&metric=coverage)](https://sonarcloud.io/dashboard?id=DEFRA_defra-ruby-validators)
6
- [![security](https://hakiri.io/github/DEFRA/defra-ruby-validators/main.svg)](https://hakiri.io/github/DEFRA/defra-ruby-validators/main)
7
6
  [![Gem Version](https://badge.fury.io/rb/defra_ruby_validators.svg)](https://badge.fury.io/rb/defra_ruby_validators)
8
7
  [![Licence](https://img.shields.io/badge/Licence-OGLv3-blue.svg)](http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3)
9
8
 
@@ -74,6 +73,14 @@ Add it to your model or form object using
74
73
  validates :company_no, "defra_ruby/validators/companies_house_number": true
75
74
  ```
76
75
 
76
+ The company number also accepts a `company_type` option. This checks the `type` attribute in the API response. To validate that the company number is for a Limited Company, for example:
77
+
78
+ ```ruby
79
+ validates :company_no, "defra_ruby/validators/companies_house_number": { company_type: "ltd" }
80
+ ```
81
+
82
+ Note: to mock the Companies House service with the `company_type` option, you will need to be running at least v2.2.0 of [defra-ruby-mocks](https://github.com/DEFRA/defra-ruby-mocks#companies-house).
83
+
77
84
  ### Email
78
85
 
79
86
  This validator checks the value is present and in a format that meets [RFC 2822](https://tools.ietf.org/html/rfc2822) and [RFC 3696](https://tools.ietf.org/html/rfc3696) (we use [validates_email_format_of](https://github.com/validates-email-format-of/validates_email_format_of) to check the format).
@@ -17,7 +17,8 @@ module DefraRuby
17
17
  return false unless value_is_present?(record, attribute, value)
18
18
  return false unless format_is_valid?(record, attribute, value)
19
19
 
20
- validate_with_companies_house(record, attribute, value)
20
+ company_type = options[:company_type]
21
+ validate_with_companies_house(record, attribute, value, company_type)
21
22
  end
22
23
 
23
24
  private
@@ -30,25 +31,28 @@ module DefraRuby
30
31
  end
31
32
 
32
33
  def format_is_valid?(record, attribute, value)
33
- return true if value.match?(VALID_COMPANIES_HOUSE_REGISTRATION_NUMBER_REGEX)
34
+ # All-zero values are invalid, despite matching the main regex
35
+ return true if value.match?(VALID_COMPANIES_HOUSE_REGISTRATION_NUMBER_REGEX) && !value.match(/^0+$/)
34
36
 
35
37
  add_validation_error(record, attribute, :invalid_format)
36
38
  false
37
39
  end
38
40
 
39
- def validate_with_companies_house(record, attribute, value)
40
- case CompaniesHouseService.new(value).status
41
+ def validate_with_companies_house(record, attribute, value, company_type)
42
+ case CompaniesHouseService.new(value, company_type).status
41
43
  when :active
42
44
  true
43
45
  when :inactive
44
46
  add_validation_error(record, attribute, :inactive)
45
47
  when :not_found
46
48
  add_validation_error(record, attribute, :not_found)
49
+ else
50
+ # Sonarcloud suggested that not having an `else` is a code smell
51
+ add_validation_error(record, attribute, :error)
47
52
  end
48
53
  rescue StandardError
49
54
  add_validation_error(record, attribute, :error)
50
55
  end
51
-
52
56
  end
53
57
  end
54
58
  end
@@ -5,31 +5,45 @@ require "rest-client"
5
5
  module DefraRuby
6
6
  module Validators
7
7
  class CompaniesHouseService
8
- def initialize(company_no)
8
+ def initialize(company_no, company_type = nil)
9
9
  @company_no = company_no
10
10
  @url = "#{DefraRuby::Validators.configuration.companies_house_host}#{@company_no}"
11
11
  @api_key = DefraRuby::Validators.configuration.companies_house_api_key
12
+ @company_type = company_type
12
13
  end
13
14
 
14
15
  def status
15
- response = RestClient::Request.execute(
16
- method: :get,
17
- url: @url,
18
- user: @api_key,
19
- password: ""
20
- )
16
+ return :not_found unless company_type_is_allowed?(json_response)
21
17
 
22
- json = JSON.parse(response)
23
-
24
- status_is_allowed?(json["company_status"]) ? :active : :inactive
18
+ status_is_allowed?(json_response) ? :active : :inactive
25
19
  rescue RestClient::ResourceNotFound
26
20
  :not_found
27
21
  end
28
22
 
23
+ def json_response
24
+ @_json_response ||=
25
+ JSON.parse(
26
+ RestClient::Request.execute(
27
+ method: :get,
28
+ url: @url,
29
+ user: @api_key,
30
+ password: ""
31
+ )
32
+ )
33
+ end
34
+
29
35
  private
30
36
 
31
- def status_is_allowed?(status)
32
- %w[active voluntary-arrangement].include?(status)
37
+ def status_is_allowed?(json_response)
38
+ %w[active voluntary-arrangement].include?(json_response["company_status"])
39
+ end
40
+
41
+ def company_type_is_allowed?(json_response)
42
+ # if a company_type has not been defined in the validator,
43
+ # we skip this check
44
+ return true if @company_type.blank?
45
+
46
+ @company_type.to_s == json_response["type"]
33
47
  end
34
48
  end
35
49
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module DefraRuby
4
4
  module Validators
5
- VERSION = "2.4.1"
5
+ VERSION = "2.5.1"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: defra_ruby_validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.1
4
+ version: 2.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Defra
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-07 00:00:00.000000000 Z
11
+ date: 2023-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: os_map_ref
28
+ name: i18n
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: phonelib
42
+ name: matrix
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,21 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rest-client
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '2.0'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '2.0'
69
- - !ruby/object:Gem::Dependency
70
- name: validates_email_format_of
56
+ name: os_map_ref
71
57
  requirement: !ruby/object:Gem::Requirement
72
58
  requirements:
73
59
  - - ">="
@@ -81,55 +67,13 @@ dependencies:
81
67
  - !ruby/object:Gem::Version
82
68
  version: '0'
83
69
  - !ruby/object:Gem::Dependency
84
- name: defra_ruby_style
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: dotenv
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
- - !ruby/object:Gem::Dependency
112
- name: github_changelog_generator
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - ">="
123
- - !ruby/object:Gem::Version
124
- version: '0'
125
- - !ruby/object:Gem::Dependency
126
- name: i18n
70
+ name: phonelib
127
71
  requirement: !ruby/object:Gem::Requirement
128
72
  requirements:
129
73
  - - ">="
130
74
  - !ruby/object:Gem::Version
131
75
  version: '0'
132
- type: :development
76
+ type: :runtime
133
77
  prerelease: false
134
78
  version_requirements: !ruby/object:Gem::Requirement
135
79
  requirements:
@@ -137,75 +81,33 @@ dependencies:
137
81
  - !ruby/object:Gem::Version
138
82
  version: '0'
139
83
  - !ruby/object:Gem::Dependency
140
- name: pry-byebug
84
+ name: rest-client
141
85
  requirement: !ruby/object:Gem::Requirement
142
86
  requirements:
143
- - - ">="
87
+ - - "~>"
144
88
  - !ruby/object:Gem::Version
145
- version: '0'
146
- type: :development
89
+ version: '2.0'
90
+ type: :runtime
147
91
  prerelease: false
148
92
  version_requirements: !ruby/object:Gem::Requirement
149
93
  requirements:
150
- - - ">="
94
+ - - "~>"
151
95
  - !ruby/object:Gem::Version
152
- version: '0'
96
+ version: '2.0'
153
97
  - !ruby/object:Gem::Dependency
154
- name: rake
98
+ name: validates_email_format_of
155
99
  requirement: !ruby/object:Gem::Requirement
156
100
  requirements:
157
101
  - - ">="
158
102
  - !ruby/object:Gem::Version
159
103
  version: '0'
160
- type: :development
104
+ type: :runtime
161
105
  prerelease: false
162
106
  version_requirements: !ruby/object:Gem::Requirement
163
107
  requirements:
164
108
  - - ">="
165
109
  - !ruby/object:Gem::Version
166
110
  version: '0'
167
- - !ruby/object:Gem::Dependency
168
- name: rspec
169
- requirement: !ruby/object:Gem::Requirement
170
- requirements:
171
- - - "~>"
172
- - !ruby/object:Gem::Version
173
- version: '3.0'
174
- type: :development
175
- prerelease: false
176
- version_requirements: !ruby/object:Gem::Requirement
177
- requirements:
178
- - - "~>"
179
- - !ruby/object:Gem::Version
180
- version: '3.0'
181
- - !ruby/object:Gem::Dependency
182
- name: simplecov
183
- requirement: !ruby/object:Gem::Requirement
184
- requirements:
185
- - - "~>"
186
- - !ruby/object:Gem::Version
187
- version: 0.17.1
188
- type: :development
189
- prerelease: false
190
- version_requirements: !ruby/object:Gem::Requirement
191
- requirements:
192
- - - "~>"
193
- - !ruby/object:Gem::Version
194
- version: 0.17.1
195
- - !ruby/object:Gem::Dependency
196
- name: webmock
197
- requirement: !ruby/object:Gem::Requirement
198
- requirements:
199
- - - "~>"
200
- - !ruby/object:Gem::Version
201
- version: '3.4'
202
- type: :development
203
- prerelease: false
204
- version_requirements: !ruby/object:Gem::Requirement
205
- requirements:
206
- - - "~>"
207
- - !ruby/object:Gem::Version
208
- version: '3.4'
209
111
  description: Package of validations commonly used in Defra Rails based digital services
210
112
  email:
211
113
  - alan.cruikshanks@environment-agency.gov.uk
@@ -249,39 +151,13 @@ files:
249
151
  - lib/defra_ruby/validators/true_false_validator.rb
250
152
  - lib/defra_ruby/validators/version.rb
251
153
  - lib/defra_ruby_validators.rb
252
- - spec/defra_ruby/validators/business_type_validator_spec.rb
253
- - spec/defra_ruby/validators/companies_house_number_validator_spec.rb
254
- - spec/defra_ruby/validators/companies_house_service_spec.rb
255
- - spec/defra_ruby/validators/configuration_spec.rb
256
- - spec/defra_ruby/validators/email_validator_spec.rb
257
- - spec/defra_ruby/validators/grid_reference_validator_spec.rb
258
- - spec/defra_ruby/validators/location_validator_spec.rb
259
- - spec/defra_ruby/validators/past_date_validator_spec.rb
260
- - spec/defra_ruby/validators/phone_number_validator_spec.rb
261
- - spec/defra_ruby/validators/position_validator_spec.rb
262
- - spec/defra_ruby/validators/token_validator_spec.rb
263
- - spec/defra_ruby/validators/true_false_validator_spec.rb
264
- - spec/defra_ruby/validators_spec.rb
265
- - spec/spec_helper.rb
266
- - spec/support/defra_ruby_validators.rb
267
- - spec/support/helpers/text_generator.rb
268
- - spec/support/helpers/translator.rb
269
- - spec/support/i18n.rb
270
- - spec/support/pry.rb
271
- - spec/support/shared_examples/validators/characters_validator.rb
272
- - spec/support/shared_examples/validators/invalid_record.rb
273
- - spec/support/shared_examples/validators/length_validator.rb
274
- - spec/support/shared_examples/validators/presence_validator.rb
275
- - spec/support/shared_examples/validators/selection_validator.rb
276
- - spec/support/shared_examples/validators/valid_record.rb
277
- - spec/support/shared_examples/validators/validator.rb
278
- - spec/support/simplecov.rb
279
154
  homepage: https://github.com/DEFRA/defra-ruby-validators
280
155
  licenses:
281
156
  - The Open Government Licence (OGL) Version 3
282
157
  metadata:
283
158
  allowed_push_host: https://rubygems.org
284
- post_install_message:
159
+ rubygems_mfa_required: 'true'
160
+ post_install_message:
285
161
  rdoc_options: []
286
162
  require_paths:
287
163
  - lib
@@ -289,43 +165,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
289
165
  requirements:
290
166
  - - ">="
291
167
  - !ruby/object:Gem::Version
292
- version: '0'
168
+ version: '3.1'
293
169
  required_rubygems_version: !ruby/object:Gem::Requirement
294
170
  requirements:
295
171
  - - ">="
296
172
  - !ruby/object:Gem::Version
297
173
  version: '0'
298
174
  requirements: []
299
- rubyforge_project:
300
- rubygems_version: 2.6.13
301
- signing_key:
175
+ rubygems_version: 3.3.7
176
+ signing_key:
302
177
  specification_version: 4
303
178
  summary: Defra ruby on rails validations
304
- test_files:
305
- - spec/spec_helper.rb
306
- - spec/defra_ruby/validators_spec.rb
307
- - spec/defra_ruby/validators/past_date_validator_spec.rb
308
- - spec/defra_ruby/validators/companies_house_service_spec.rb
309
- - spec/defra_ruby/validators/configuration_spec.rb
310
- - spec/defra_ruby/validators/phone_number_validator_spec.rb
311
- - spec/defra_ruby/validators/position_validator_spec.rb
312
- - spec/defra_ruby/validators/grid_reference_validator_spec.rb
313
- - spec/defra_ruby/validators/business_type_validator_spec.rb
314
- - spec/defra_ruby/validators/companies_house_number_validator_spec.rb
315
- - spec/defra_ruby/validators/token_validator_spec.rb
316
- - spec/defra_ruby/validators/true_false_validator_spec.rb
317
- - spec/defra_ruby/validators/location_validator_spec.rb
318
- - spec/defra_ruby/validators/email_validator_spec.rb
319
- - spec/support/simplecov.rb
320
- - spec/support/i18n.rb
321
- - spec/support/pry.rb
322
- - spec/support/defra_ruby_validators.rb
323
- - spec/support/helpers/translator.rb
324
- - spec/support/helpers/text_generator.rb
325
- - spec/support/shared_examples/validators/selection_validator.rb
326
- - spec/support/shared_examples/validators/invalid_record.rb
327
- - spec/support/shared_examples/validators/presence_validator.rb
328
- - spec/support/shared_examples/validators/characters_validator.rb
329
- - spec/support/shared_examples/validators/length_validator.rb
330
- - spec/support/shared_examples/validators/valid_record.rb
331
- - spec/support/shared_examples/validators/validator.rb
179
+ test_files: []
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- module Test
6
- BusinessTypeValidatable = Struct.new(:business_type) do
7
- include ActiveModel::Validations
8
-
9
- validates :business_type, "defra_ruby/validators/business_type": true
10
- end
11
- end
12
-
13
- module DefraRuby
14
- module Validators
15
- RSpec.describe BusinessTypeValidator do
16
-
17
- valid_type = %w[soleTrader limitedCompany partnership limitedLiabilityPartnership localAuthority charity].sample
18
- invalid_type = "coven"
19
-
20
- it_behaves_like("a validator")
21
- it_behaves_like(
22
- "a selection validator",
23
- BusinessTypeValidator,
24
- Test::BusinessTypeValidatable,
25
- :business_type,
26
- valid: valid_type, invalid: invalid_type
27
- )
28
-
29
- context "when the value is overseas" do
30
- validatable = Test::BusinessTypeValidatable.new("overseas")
31
-
32
- context "when overseas_allowed is enabled" do
33
- before do
34
- allow_any_instance_of(DefraRuby::Validators::BaseValidator).to receive(:options).and_return(allow_overseas: true)
35
- end
36
-
37
- it_behaves_like "a valid record", validatable
38
- end
39
-
40
- context "when overseas_allowed is not enabled" do
41
- before do
42
- allow_any_instance_of(DefraRuby::Validators::BaseValidator).to receive(:options).and_return({})
43
- end
44
-
45
- error_message = Helpers::Translator.error_message(BusinessTypeValidator, :inclusion)
46
-
47
- it_behaves_like "an invalid record",
48
- validatable: validatable,
49
- attribute: :business_type,
50
- error: :inclusion,
51
- error_message: error_message
52
- end
53
- end
54
- end
55
- end
56
- end
@@ -1,108 +0,0 @@
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, :blank)
41
-
42
- it_behaves_like "an invalid record",
43
- validatable: validatable,
44
- attribute: :company_no,
45
- error: :blank,
46
- error_message: error_message
47
- end
48
-
49
- context "because the format is wrong" do
50
- validatable = Test::CompaniesHouseNumberValidatable.new(invalid_format_number)
51
- error_message = Helpers::Translator.error_message(CompaniesHouseNumberValidator, :invalid_format)
52
-
53
- it_behaves_like "an invalid record",
54
- validatable: validatable,
55
- attribute: :company_no,
56
- error: :invalid_format,
57
- error_message: error_message
58
- end
59
-
60
- context "because it's not found on companies house" do
61
- before do
62
- allow_any_instance_of(CompaniesHouseService).to receive(:status).and_return(:not_found)
63
- end
64
-
65
- validatable = Test::CompaniesHouseNumberValidatable.new(unknown_number)
66
- error_message = Helpers::Translator.error_message(CompaniesHouseNumberValidator, :not_found)
67
-
68
- it_behaves_like "an invalid record",
69
- validatable: validatable,
70
- attribute: :company_no,
71
- error: :not_found,
72
- error_message: error_message
73
- end
74
-
75
- context "because it's not 'active' on companies house" do
76
- before do
77
- allow_any_instance_of(CompaniesHouseService).to receive(:status).and_return(:inactive)
78
- end
79
-
80
- validatable = Test::CompaniesHouseNumberValidatable.new(inactive_number)
81
- error_message = Helpers::Translator.error_message(CompaniesHouseNumberValidator, :inactive)
82
-
83
- it_behaves_like "an invalid record",
84
- validatable: validatable,
85
- attribute: :company_no,
86
- error: :inactive,
87
- error_message: error_message
88
- end
89
- end
90
-
91
- context "when there is an error connecting with companies house" do
92
- before do
93
- allow_any_instance_of(CompaniesHouseService).to receive(:status).and_raise(StandardError)
94
- end
95
-
96
- validatable = Test::CompaniesHouseNumberValidatable.new(valid_numbers.sample)
97
- error_message = Helpers::Translator.error_message(CompaniesHouseNumberValidator, :error)
98
-
99
- it_behaves_like "an invalid record",
100
- validatable: validatable,
101
- attribute: :company_no,
102
- error: :error,
103
- error_message: error_message
104
- end
105
- end
106
- end
107
- end
108
- end
@@ -1,70 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "webmock/rspec"
4
-
5
- RSpec.describe DefraRuby::Validators::CompaniesHouseService do
6
- let(:subject) { described_class.new("09360070") }
7
- let(:host) { "https://api.companieshouse.gov.uk/" }
8
-
9
- describe "#status" do
10
- context "when the company_no is for an active company" do
11
- before do
12
- expected_body = { "company_status": "active" }
13
-
14
- stub_request(:any, /.*#{host}.*/).to_return(
15
- status: 200,
16
- body: expected_body.to_json
17
- )
18
- end
19
-
20
- it "returns :active" do
21
- expect(subject.status).to eq(:active)
22
- end
23
- end
24
-
25
- context "when the company_no is not found" do
26
- before do
27
- stub_request(:any, /.*#{host}.*/).to_return(
28
- status: 404
29
- )
30
- end
31
-
32
- it "returns :not_found" do
33
- expect(subject.status).to eq(:not_found)
34
- end
35
- end
36
-
37
- context "when the company_no is inactive" do
38
- before do
39
- expected_body = { "company_status": "dissolved" }
40
-
41
- stub_request(:any, /.*#{host}.*/).to_return(
42
- status: 200,
43
- body: expected_body.to_json
44
- )
45
- end
46
-
47
- it "returns :inactive" do
48
- expect(subject.status).to eq(:inactive)
49
- end
50
- end
51
-
52
- context "when there is a problem with the Companies House API" do
53
- context "and the request times out" do
54
- before { stub_request(:any, /.*#{host}.*/).to_timeout }
55
-
56
- it "raises an exception" do
57
- expect { subject.status }.to raise_error(StandardError)
58
- end
59
- end
60
-
61
- context "and request returns an error" do
62
- before { stub_request(:any, /.*#{host}.*/).to_raise(SocketError) }
63
-
64
- it "raises an exception" do
65
- expect { subject.status }.to raise_error(StandardError)
66
- end
67
- end
68
- end
69
- end
70
- end