gds-api-adapters 23.0.0 → 23.1.0

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
  SHA1:
3
- metadata.gz: 5ea6ab5e0f8cd1e6568d3588d21b9918c7818a65
4
- data.tar.gz: 57953905ccbcd5453f4acc5548248a02b57e1fa2
3
+ metadata.gz: bd7b1a091da260080fb3bed10086d31c84e1708d
4
+ data.tar.gz: 1d82d8b9d6d16adae35fe3aa6dd602330a237f5c
5
5
  SHA512:
6
- metadata.gz: fed21cbb734110e486fe6e390a4e8960b4f018d886643a76cd1d75c91ca076a87fd052103c5c982afc287b869c83336d60da7e6a60bcd66ca7f43f3ba6c6db32
7
- data.tar.gz: e2f4fdf14f51fe5ece8d45ffd822b4c4f1fcbc393a3d601bf224a89a6c94bacea7ccf941f20b0e9b0ed8db0ddc045e255da3a0930e3740233c0f3e4947fca885
6
+ metadata.gz: 72c4bab7cb84d20997f1c1739c55939f113590a7d357721127d68df77fcc99568a37068edbebf4c56f381fd2e2018d4e2d1276b5a530fb30713ca923e27184af
7
+ data.tar.gz: 05b9bc994f6f69880135f722e67930673a2ad47735cff4d4aa1d1409f0732d94bc84acb089ced7049eadb78bd130dcb500dfd3d13126e5bf0e0f73a0caceb18c
@@ -43,37 +43,72 @@ module GdsApi
43
43
  stub_request(:put, %r{\A#{PUBLISHING_API_ENDPOINT}/publish-intent})
44
44
  end
45
45
 
46
- def assert_publishing_api_put_item(base_path, attributes = {}, times = 1)
46
+ def assert_publishing_api_put_item(base_path, attributes_or_matcher = {}, times = 1)
47
47
  url = PUBLISHING_API_ENDPOINT + "/content" + base_path
48
- assert_publishing_api_put(url, attributes, times)
48
+ assert_publishing_api_put(url, attributes_or_matcher, times)
49
49
  end
50
50
 
51
- def assert_publishing_api_put_draft_item(base_path, attributes = {}, times = 1)
51
+ def assert_publishing_api_put_draft_item(base_path, attributes_or_matcher = {}, times = 1)
52
52
  url = PUBLISHING_API_ENDPOINT + "/draft-content" + base_path
53
- assert_publishing_api_put(url, attributes, times)
53
+ assert_publishing_api_put(url, attributes_or_matcher, times)
54
54
  end
55
55
 
56
- def assert_publishing_api_put_intent(base_path, attributes = {}, times = 1)
56
+ def assert_publishing_api_put_intent(base_path, attributes_or_matcher = {}, times = 1)
57
57
  url = PUBLISHING_API_ENDPOINT + "/publish-intent" + base_path
58
- assert_publishing_api_put(url, attributes, times)
58
+ assert_publishing_api_put(url, attributes_or_matcher, times)
59
59
  end
60
60
 
61
- def assert_publishing_api_put(url, attributes = {}, times = 1)
62
- if attributes.empty?
63
- assert_requested(:put, url, times: times)
61
+ def assert_publishing_api_put(url, attributes_or_matcher = {}, times = 1)
62
+ if attributes_or_matcher.is_a?(Hash)
63
+ matcher = attributes_or_matcher.empty? ? nil : request_json_matching(attributes_or_matcher)
64
64
  else
65
- assert_requested(:put, url, times: times) do |req|
66
- data = JSON.parse(req.body)
67
- attributes.to_a.all? do |key, value|
68
- data[key.to_s] == value
69
- end
70
- end
65
+ matcher = attributes_or_matcher
66
+ end
67
+
68
+ if matcher
69
+ assert_requested(:put, url, times: times, &matcher)
70
+ else
71
+ assert_requested(:put, url, times: times)
72
+ end
73
+ end
74
+
75
+ def request_json_matching(required_attributes)
76
+ ->(request) do
77
+ data = JSON.parse(request.body)
78
+ required_attributes.to_a.all? { |key, value| data[key.to_s] == value }
79
+ end
80
+ end
81
+
82
+ def request_json_including(required_attributes)
83
+ ->(request) do
84
+ data = JSON.parse(request.body)
85
+ values_match_recursively(required_attributes, data)
71
86
  end
72
87
  end
73
88
 
74
89
  def publishing_api_isnt_available
75
90
  stub_request(:any, /#{PUBLISHING_API_ENDPOINT}\/.*/).to_return(:status => 503)
76
91
  end
92
+
93
+ private
94
+ def values_match_recursively(expected_value, actual_value)
95
+ case expected_value
96
+ when Hash
97
+ return false unless actual_value.is_a?(Hash)
98
+ expected_value.all? do |expected_sub_key, expected_sub_value|
99
+ actual_value.has_key?(expected_sub_key.to_s) &&
100
+ values_match_recursively(expected_sub_value, actual_value[expected_sub_key.to_s])
101
+ end
102
+ when Array
103
+ return false unless actual_value.is_a?(Array)
104
+ return false unless actual_value.size == expected_value.size
105
+ expected_value.each.with_index.all? do |expected_sub_value, i|
106
+ values_match_recursively(expected_sub_value, actual_value[i])
107
+ end
108
+ else
109
+ expected_value == actual_value
110
+ end
111
+ end
77
112
  end
78
113
  end
79
114
  end
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '23.0.0'
2
+ VERSION = '23.1.0'
3
3
  end
@@ -0,0 +1,158 @@
1
+ require 'test_helper'
2
+ require 'gds_api/publishing_api'
3
+ require 'gds_api/test_helpers/publishing_api'
4
+
5
+ describe GdsApi::TestHelpers::PublishingApi do
6
+ include GdsApi::TestHelpers::PublishingApi
7
+ let(:base_api_url) { Plek.current.find("publishing-api") }
8
+ let(:publishing_api) { GdsApi::PublishingApi.new(base_api_url) }
9
+
10
+ describe "#assert_publishing_api_put_item" do
11
+ before { stub_default_publishing_api_put }
12
+ let(:base_path) { "/example" }
13
+
14
+ it "matches a put request with any empty attributes by default" do
15
+ publishing_api.put_content_item(base_path, {})
16
+ assert_publishing_api_put_item(base_path)
17
+ end
18
+
19
+ it "matches a put request with any arbitrary attributes by default" do
20
+ random_attributes = Hash[10.times.map {|n| [Random.rand * 1_000_000_000_000, Random.rand * 1_000_000_000_000]}]
21
+ publishing_api.put_content_item(base_path, random_attributes)
22
+ assert_publishing_api_put_item(base_path)
23
+ end
24
+
25
+ it "if attributes are specified, matches a request with at least those attributes" do
26
+ publishing_api.put_content_item(base_path, {"required_attribute" => 1, "extra_attrbibute" => 1})
27
+ assert_publishing_api_put_item(base_path, {"required_attribute" => 1})
28
+ end
29
+
30
+ it "matches using a custom request matcher" do
31
+ publishing_api.put_content_item(base_path, {})
32
+ matcher_was_called = false
33
+ matcher = ->(request) { matcher_was_called = true; true }
34
+ assert_publishing_api_put_item(base_path, matcher)
35
+ assert matcher_was_called, "matcher should have been called"
36
+ end
37
+ end
38
+
39
+ describe '#request_json_matching predicate' do
40
+ describe "nested required attribute" do
41
+ let(:matcher) { request_json_matching({"a" => {"b" => 1}}) }
42
+
43
+ it "matches a body with exact same nested hash strucure" do
44
+ assert matcher.call(stub("request", body: '{"a": {"b": 1}}'))
45
+ end
46
+
47
+ it "matches a body with exact same nested hash strucure and an extra attribute at the top level" do
48
+ assert matcher.call(stub("request", body: '{"a": {"b": 1}, "c": 3}'))
49
+ end
50
+
51
+ it "does not match a body where the inner hash has the required attribute and an extra one" do
52
+ refute matcher.call(stub("request", body: '{"a": {"b": 1, "c": 2}}'))
53
+ end
54
+
55
+ it "does not match a body where the inner hash has the required attribute with the wrong value" do
56
+ refute matcher.call(stub("request", body: '{"a": {"b": 0}}'))
57
+ end
58
+
59
+ it "does not match a body where the inner hash lacks the required attribute" do
60
+ refute matcher.call(stub("request", body: '{"a": {"c": 1}}'))
61
+ end
62
+ end
63
+
64
+ describe "hash to match uses symbol keys" do
65
+ let(:matcher) { request_json_matching({a: 1}) }
66
+
67
+ it "matches a json body" do
68
+ assert matcher.call(stub("request", body: '{"a": 1}'))
69
+ end
70
+ end
71
+ end
72
+
73
+ describe '#request_json_including predicate' do
74
+ describe "no required attributes" do
75
+ let(:matcher) { request_json_including({}) }
76
+
77
+ it "matches an empty body" do
78
+ assert matcher.call(stub("request", body: "{}"))
79
+ end
80
+
81
+ it "matches a body with some attributes" do
82
+ assert matcher.call(stub("request", body: '{"a": 1}'))
83
+ end
84
+ end
85
+
86
+ describe "one required attribute" do
87
+ let(:matcher) { request_json_including({"a" => 1}) }
88
+
89
+ it "does not match an empty body" do
90
+ refute matcher.call(stub("request", body: "{}"))
91
+ end
92
+
93
+ it "does not match a body with the required attribute if the value is different" do
94
+ refute matcher.call(stub("request", body: '{"a": 2}'))
95
+ end
96
+
97
+ it "matches a body with the required attribute and value" do
98
+ assert matcher.call(stub("request", body: '{"a": 1}'))
99
+ end
100
+
101
+ it "matches a body with the required attribute and value and extra attributes" do
102
+ assert matcher.call(stub("request", body: '{"a": 1, "b": 2}'))
103
+ end
104
+ end
105
+
106
+ describe "nested required attribute" do
107
+ let(:matcher) { request_json_including({"a" => {"b" => 1}}) }
108
+
109
+ it "matches a body with exact same nested hash strucure" do
110
+ assert matcher.call(stub("request", body: '{"a": {"b": 1}}'))
111
+ end
112
+
113
+ it "matches a body where the inner hash has the required attribute and an extra one" do
114
+ assert matcher.call(stub("request", body: '{"a": {"b": 1, "c": 2}}'))
115
+ end
116
+
117
+ it "does not match a body where the inner hash has the required attribute with the wrong value" do
118
+ refute matcher.call(stub("request", body: '{"a": {"b": 0}}'))
119
+ end
120
+
121
+ it "does not match a body where the inner hash lacks the required attribute" do
122
+ refute matcher.call(stub("request", body: '{"a": {"c": 1}}'))
123
+ end
124
+ end
125
+
126
+ describe "hash to match uses symbol keys" do
127
+ let(:matcher) { request_json_including({a: {b: 1}}) }
128
+
129
+ it "matches a json body" do
130
+ assert matcher.call(stub("request", body: '{"a": {"b": 1}}'))
131
+ end
132
+ end
133
+
134
+ describe "nested arrays" do
135
+ let(:matcher) { request_json_including({"a" => [1]}) }
136
+
137
+ it "matches a body with exact same inner array" do
138
+ assert matcher.call(stub("request", body: '{"a": [1]}'))
139
+ end
140
+
141
+ it "does not match a body with an array with extra elements" do
142
+ refute matcher.call(stub("request", body: '{"a": [1, 2]}'))
143
+ end
144
+ end
145
+
146
+ describe "hashes in nested arrays" do
147
+ let(:matcher) { request_json_including({"a" => [{"b" => 1}, 2]}) }
148
+
149
+ it "matches a body with exact same inner array" do
150
+ assert matcher.call(stub("request", body: '{"a": [{"b": 1}, 2]}'))
151
+ end
152
+
153
+ it "matches a body with an inner hash with extra elements" do
154
+ assert matcher.call(stub("request", body: '{"a": [{"b": 1, "c": 3}, 2]}'))
155
+ end
156
+ end
157
+ end
158
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 23.0.0
4
+ version: 23.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Stewart
@@ -381,6 +381,7 @@ files:
381
381
  - test/support_test.rb
382
382
  - test/test_helper.rb
383
383
  - test/test_helpers/panopticon_test.rb
384
+ - test/test_helpers/publishing_api_test.rb
384
385
  - test/whitehall_admin_api_test.rb
385
386
  - test/worldwide_api_test.rb
386
387
  homepage: http://github.com/alphagov/gds-api-adapters
@@ -418,6 +419,7 @@ test_files:
418
419
  - test/support_test.rb
419
420
  - test/content_register_test.rb
420
421
  - test/fact_cave_test.rb
422
+ - test/test_helpers/publishing_api_test.rb
421
423
  - test/test_helpers/panopticon_test.rb
422
424
  - test/licence_application_api_test.rb
423
425
  - test/gov_uk_delivery_test.rb