defra_ruby_mocks 4.1.0 → 4.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: daca7a21c8e025f343ee8e118ce47db24bc156f01646d747b3eb69f779f8f47e
4
- data.tar.gz: 7d4d39f87f9caae3bf194ff94eb5e952d841deb2f97928177e83b739158166a5
3
+ metadata.gz: 463dba0a1c6925c35c0265dfa65a55a6e1a8e7ec33c4e5dc793fa1e51f1b3c01
4
+ data.tar.gz: 101fea584ee1cbbf481ebe591f1d1e280903a8be16834e3616697ee80be402b8
5
5
  SHA512:
6
- metadata.gz: b95c89f6d29270099b4d96c5b841de3cbe2f97648a44006bfa13f442e35ea8b191b2c436db0426854efefdc9c001b89686eb3db004ab16acb6854b2036b05f7a
7
- data.tar.gz: de085b31d9767d688660ee45b6530cd139d9cb3d4a2a313c5aa170494d415dc88c308f794a7c60b2891a0da6d47c84ce838318455399fc37f6fbcef783f833f0
6
+ metadata.gz: eb946492539db155e729244008599522dfad177db1536696a8f4d1e7090e9f4a9f03df0f1622add82bb968ab07bba25056b1524f4a19799c2402cd59edd61362
7
+ data.tar.gz: 70de2d5dfe16524ccc00c3d491a1dc933dce584b434877ab6b8d96c02d31729ff51dc5f9005b87b0301af6769cddc1d0ef22c253b3b2ed8779042f1007702d65
data/README.md CHANGED
@@ -178,7 +178,7 @@ In order to use the govpay mock you'll need to provide additional configuration
178
178
  require "defra_ruby_mocks"
179
179
 
180
180
  DefraRubyMocks.configure do |config|
181
- config.govpay_domain = File.join(ENV["WCRS_GOVPAY_DOMAIN"] || "http://localhost:3002", "/fo/mocks/govpay/v1")
181
+ config.govpay_domain = File.join(ENV["WCRS_GOVPAY_MOCK_DOMAIN"] || "http://localhost:3002", "/fo/mocks/govpay/v1")
182
182
  end
183
183
  ```
184
184
 
@@ -24,8 +24,7 @@ module DefraRubyMocks
24
24
  def next_url
25
25
  response_url = retrieve_return_url
26
26
  Rails.logger.warn "Govpay mock calling response URL #{response_url}"
27
- redirect_to response_url
28
- # RestClient::Request.execute(method: :GET, url: response_url)
27
+ redirect_to response_url, allow_other_host: true
29
28
  rescue RestClient::ExceptionWithResponse => e
30
29
  Rails.logger.warn "Govpay mock: RestClient received response: #{e}"
31
30
  rescue StandardError => e
@@ -60,18 +59,23 @@ module DefraRubyMocks
60
59
 
61
60
  private
62
61
 
63
- # We need to persist the return_url between the initial payment creation request and the execution of next_url
64
- def return_url_filepath
65
- "#{Dir.tmpdir}/last_return_url"
62
+ def s3_bucket_name
63
+ @s3_bucket_name = ENV.fetch("GOVPAY_MOCKS_BUCKET", "defra-ruby-mocks-s3bkt001")
66
64
  end
67
65
 
66
+ def return_url_file_name
67
+ @return_url_file_name = "return_url_file"
68
+ end
69
+
70
+ # We need to persist the return_url between the initial payment creation request and the execution of next_url.
71
+ # We can't use tmp for multi-server environments so we load the temp file to AWS S3.
68
72
  def store_return_url(return_url)
69
73
  Rails.logger.warn ":::::: storing return_url #{return_url}"
70
- File.write(return_url_filepath, return_url)
74
+ AwsBucketService.write(s3_bucket_name, return_url_file_name, return_url)
71
75
  end
72
76
 
73
77
  def retrieve_return_url
74
- File.read(return_url_filepath)
78
+ AwsBucketService.read(s3_bucket_name, return_url_file_name)
75
79
  end
76
80
 
77
81
  def valid_create_params
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "defra_ruby/aws"
4
+
5
+ module DefraRubyMocks
6
+ class AwsBucketService
7
+
8
+ attr_accessor :bucket_name, :file_name
9
+
10
+ def self.write(bucket_name, file_name, content)
11
+ new.write(bucket_name, file_name, content)
12
+ end
13
+
14
+ def self.read(bucket_name, file_name)
15
+ new.read(bucket_name, file_name)
16
+ end
17
+
18
+ def write(bucket_name, file_name, content)
19
+ @bucket_name = bucket_name
20
+ @file_name = file_name
21
+
22
+ write_temp_file(content)
23
+
24
+ load_temp_file_to_s3
25
+
26
+ true
27
+ end
28
+
29
+ def read(bucket_name, file_name)
30
+ Rails.logger.warn ":::::: reading from S3"
31
+
32
+ s3 = Aws::S3::Client.new
33
+ s3.get_object(bucket: bucket_name, key: file_name)
34
+ rescue Aws::S3::NoSuchBucket => e
35
+ raise StandardError, e
36
+ end
37
+
38
+ private
39
+
40
+ def temp_filepath
41
+ "#{Dir.tmpdir}/#{file_name}"
42
+ end
43
+
44
+ def write_temp_file(content)
45
+ Rails.logger.warn ":::::: creating temp file for #{content}"
46
+ File.write(temp_filepath, content)
47
+ end
48
+
49
+ def load_temp_file_to_s3
50
+ Rails.logger.warn ":::::: loading temp file to S3"
51
+
52
+ result = nil
53
+
54
+ 3.times do
55
+ result = bucket.load(File.new(temp_filepath, "r"))
56
+
57
+ break if result.successful?
58
+ end
59
+
60
+ raise(result&.error) unless result&.successful?
61
+ end
62
+
63
+ def bucket
64
+ @bucket ||= DefraRuby::Aws.get_bucket(bucket_name)
65
+ end
66
+ end
67
+ end
@@ -3,6 +3,8 @@
3
3
  module DefraRubyMocks
4
4
  class GovpayRefundDetailsService < BaseService
5
5
 
6
+ DEFAULT_LAST_REFUND_REQUEST_TIME = 1.day.ago.freeze
7
+
6
8
  def run(payment_id:, refund_id:) # rubocop:disable Lint/UnusedMethodArgument
7
9
  {
8
10
  amount: 2000,
@@ -19,24 +21,37 @@ module DefraRubyMocks
19
21
 
20
22
  # "submitted" for up to GOVPAY_REFUND_SUBMITTED_SUCCESS_LAG seconds after the last refund request, then "success"
21
23
  def status
22
- timestamp = File.read("#{Dir.tmpdir}/govpay_request_refund_service_last_run_time")
23
- last_refund_time = timestamp ? Time.parse(timestamp) : 1.day.ago
24
+ last_refund_time = refund_request_timestamp
24
25
  submitted_success_lag = ENV.fetch("GOVPAY_REFUND_SUBMITTED_SUCCESS_LAG", 0).to_i
25
26
  cutoff_time = (last_refund_time + submitted_success_lag.seconds).to_time
26
27
  return "success" if submitted_success_lag.zero?
27
28
 
28
29
  Time.zone.now < cutoff_time ? "submitted" : "success"
29
30
  rescue Errno::ENOENT
30
- write_timestamp_file("govpay_request_refund_service_last_run_time")
31
+ write_timestamp_file
31
32
 
32
33
  "success"
33
34
  end
34
35
 
35
- def write_timestamp_file(filename)
36
- filepath = "#{Dir.tmpdir}/#{filename}"
36
+ def write_timestamp_file
37
+ timestamp = Time.zone.now
38
+ Rails.logger.warn ":::::: writing timestamp file: #{timestamp}"
39
+ AwsBucketService.write(s3_bucket_name, timestamp_file_name, timestamp)
40
+ end
41
+
42
+ def s3_bucket_name
43
+ @s3_bucket_name = ENV.fetch("GOVPAY_MOCKS_BUCKET", "defra-ruby-mocks-s3bkt001")
44
+ end
45
+
46
+ def timestamp_file_name
47
+ @govpay_request_refund_service_last_run_time = "govpay_request_refund_service_last_run_time"
48
+ end
37
49
 
38
- # FileUtils.touch seems unreliable in VM so need to write/read the actual time
39
- File.write(filepath, Time.zone.now)
50
+ def refund_request_timestamp
51
+ timestamp = AwsBucketService.read(s3_bucket_name, timestamp_file_name)
52
+ timestamp ? Time.parse(timestamp) : DEFAULT_LAST_REFUND_REQUEST_TIME
53
+ rescue StandardError
54
+ DEFAULT_LAST_REFUND_REQUEST_TIME
40
55
  end
41
56
 
42
57
  end
@@ -19,9 +19,16 @@ module DefraRubyMocks
19
19
 
20
20
  # let the refund details service know how long since the refund was requested
21
21
  def write_timestamp
22
- filepath = "#{Dir.tmpdir}/govpay_request_refund_service_last_run_time"
23
- # FileUtils.touch seems unreliable in VM so need to write/read the actual time
24
- File.write(filepath, Time.zone.now)
22
+ Rails.logger.warn ":::::: storing refund request timestamp"
23
+ AwsBucketService.write(s3_bucket_name, timestamp_file_name, Time.zone.now.to_s)
24
+ end
25
+
26
+ def s3_bucket_name
27
+ @s3_bucket_name = ENV.fetch("GOVPAY_MOCKS_BUCKET", "defra-ruby-mocks-s3bkt001")
28
+ end
29
+
30
+ def timestamp_file_name
31
+ @timestamp_file_name = "govpay_request_refund_service_last_run_time"
25
32
  end
26
33
  end
27
34
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DefraRubyMocks
4
- VERSION = "4.1.0"
4
+ VERSION = "4.2.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: defra_ruby_mocks
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 4.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Defra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-23 00:00:00.000000000 Z
11
+ date: 2024-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -28,30 +28,44 @@ dependencies:
28
28
  name: sprockets
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '4.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '4.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: sprockets-rails
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '3.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: defra_ruby_aws
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
53
67
  - !ruby/object:Gem::Version
54
- version: '0'
68
+ version: '0.5'
55
69
  description: A Rails engine which can be used to mock external services when loaded
56
70
  into an application
57
71
  email:
@@ -66,6 +80,7 @@ files:
66
80
  - app/controllers/defra_ruby_mocks/application_controller.rb
67
81
  - app/controllers/defra_ruby_mocks/company_controller.rb
68
82
  - app/controllers/defra_ruby_mocks/govpay_controller.rb
83
+ - app/services/defra_ruby_mocks/aws_bucket_service.rb
69
84
  - app/services/defra_ruby_mocks/base_service.rb
70
85
  - app/services/defra_ruby_mocks/companies_house_service.rb
71
86
  - app/services/defra_ruby_mocks/govpay_create_payment_service.rb