Module: Shaf::Spec::PayloadUtils
- Included in:
- IntegrationSpec, SerializerSpec
- Defined in:
- lib/shaf/spec/payload_utils.rb
Instance Method Summary collapse
- #assert_attribute(attr, expected) ⇒ Object
- #assert_has_attribute(attr) ⇒ Object
- #assert_has_attributes(*attrs) ⇒ Object
- #assert_has_embedded(*names) ⇒ Object
- #assert_has_link(rel) ⇒ Object
- #assert_has_links(*rels) ⇒ Object
- #assert_header(key, value) ⇒ Object
- #assert_link(rel, expected) ⇒ Object
- #assert_status(code) ⇒ Object
- #attributes ⇒ Object
- #default_field_value(field) ⇒ Object
- #each_embedded(name, &block) ⇒ Object
- #embedded(name = nil, &block) ⇒ Object
- #embedded_resources ⇒ Object
- #fill_form(fields, opts = {}) ⇒ Object
- #last_payload ⇒ Object
- #link_rels ⇒ Object
- #links ⇒ Object
- #refute_has_attribute(attr) ⇒ Object
- #refute_has_attributes(*attrs) ⇒ Object
- #refute_has_embedded(*names) ⇒ Object
- #refute_has_link(rel) ⇒ Object
- #refute_has_links(*rels) ⇒ Object
- #set_payload(payload) ⇒ Object
Instance Method Details
#assert_attribute(attr, expected) ⇒ Object
99 100 101 102 |
# File 'lib/shaf/spec/payload_utils.rb', line 99 def assert_attribute(attr, expected) assert_has_attribute(attr) assert_equal expected, last_payload[attr.to_sym] end |
#assert_has_attribute(attr) ⇒ Object
81 82 83 84 |
# File 'lib/shaf/spec/payload_utils.rb', line 81 def assert_has_attribute(attr) assert last_payload[attr.to_sym], "Response does not contain attribute '#{attr}': #{last_payload}" end |
#assert_has_attributes(*attrs) ⇒ Object
91 92 93 |
# File 'lib/shaf/spec/payload_utils.rb', line 91 def assert_has_attributes(*attrs) attrs.each { |attr| assert_has_attribute(attr) } end |
#assert_has_embedded(*names) ⇒ Object
130 131 132 133 134 135 136 137 |
# File 'lib/shaf/spec/payload_utils.rb', line 130 def (*names) names.each do |name| assert last_payload.key?(:_embedded), "Response does not have any embedded resources: #{last_payload}" assert last_payload[:_embedded][name.to_sym], "Response does not contain embedded resource with name '#{name}': #{last_payload}" end end |
#assert_has_link(rel) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/shaf/spec/payload_utils.rb', line 104 def assert_has_link(rel) assert last_payload.key?(:_links), "Response does not have any links: #{last_payload}" assert last_payload[:_links][rel.to_sym], "Response does not contain link with rel '#{rel}'!\nResponse: #{last_payload}" assert last_payload[:_links][rel.to_sym][:href], "link with rel '#{rel}' in response does not have a href!\nResponse: #{last_payload}" end |
#assert_has_links(*rels) ⇒ Object
117 118 119 |
# File 'lib/shaf/spec/payload_utils.rb', line 117 def assert_has_links(*rels) rels.each { |rel| assert_has_link(rel) } end |
#assert_header(key, value) ⇒ Object
76 77 78 79 |
# File 'lib/shaf/spec/payload_utils.rb', line 76 def assert_header(key, value) assert_equal value, headers[key], "Response was expected have header #{key} = #{value}." end |
#assert_link(rel, expected) ⇒ Object
125 126 127 128 |
# File 'lib/shaf/spec/payload_utils.rb', line 125 def assert_link(rel, expected) assert_has_link(rel) assert_equal expected, last_payload.dig(:_links, rel.to_sym, :href) end |
#assert_status(code) ⇒ Object
71 72 73 74 |
# File 'lib/shaf/spec/payload_utils.rb', line 71 def assert_status(code) assert_equal code, status, "Response status was expected to be #{code}." end |
#attributes ⇒ Object
16 17 18 |
# File 'lib/shaf/spec/payload_utils.rb', line 16 def attributes last_payload.reject { |key,_| [:_links, :_embedded].include? key } end |
#default_field_value(field) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/shaf/spec/payload_utils.rb', line 58 def default_field_value(field) case field[:type] when 'integer' field[:name].size when 'string' "value for #{field[:name]}" when 'boolean' true else 'type not supported' end end |
#each_embedded(name, &block) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/shaf/spec/payload_utils.rb', line 39 def (name, &block) name list = last_payload[:_embedded][name] assert_instance_of Array, list, "Embedded '#{name}' is not an instance of Array. Actual: #{list.class}" list.each_with_index do |resource, i| (resource, block, i) end end |
#embedded(name = nil, &block) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/shaf/spec/payload_utils.rb', line 32 def (name = nil, &block) name unless name.nil? keys = [:_embedded, name&.to_sym].compact return last_payload.dig(*keys) unless block_given? (last_payload.dig(*keys), block) end |
#embedded_resources ⇒ Object
28 29 30 |
# File 'lib/shaf/spec/payload_utils.rb', line 28 def last_payload[:_embedded]&.keys || [] end |
#fill_form(fields, opts = {}) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/shaf/spec/payload_utils.rb', line 51 def fill_form(fields, opts = {}) fields.each_with_object({}) do |field, payload| key = field[:name] payload[key] = opts.fetch(key, default_field_value(field)) end end |
#last_payload ⇒ Object
11 12 13 14 |
# File 'lib/shaf/spec/payload_utils.rb', line 11 def last_payload refute @payload.nil?, "No previous response body" @payload end |
#link_rels ⇒ Object
24 25 26 |
# File 'lib/shaf/spec/payload_utils.rb', line 24 def link_rels links.keys end |
#links ⇒ Object
20 21 22 |
# File 'lib/shaf/spec/payload_utils.rb', line 20 def links last_payload[:_links] || {} end |
#refute_has_attribute(attr) ⇒ Object
86 87 88 89 |
# File 'lib/shaf/spec/payload_utils.rb', line 86 def refute_has_attribute(attr) refute last_payload[attr.to_sym], "Response contains disallowed attribute '#{attr}': #{last_payload}" end |
#refute_has_attributes(*attrs) ⇒ Object
95 96 97 |
# File 'lib/shaf/spec/payload_utils.rb', line 95 def refute_has_attributes(*attrs) attrs.each { |attr| refute_has_attribute(attr) } end |
#refute_has_embedded(*names) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/shaf/spec/payload_utils.rb', line 139 def (*names) names.each do |name| refute last_payload.dig(:_embedded, name.to_sym), "Response contains disallowed embedded resource with name '#{name}': #{last_payload}" end end |
#refute_has_link(rel) ⇒ Object
112 113 114 115 |
# File 'lib/shaf/spec/payload_utils.rb', line 112 def refute_has_link(rel) refute last_payload.dig(:_links, rel.to_sym), "Response contains disallowed link with rel '#{rel}': #{last_payload}" end |
#refute_has_links(*rels) ⇒ Object
121 122 123 |
# File 'lib/shaf/spec/payload_utils.rb', line 121 def refute_has_links(*rels) rels.each { |rel| refute_has_link(rel) } end |
#set_payload(payload) ⇒ Object
6 7 8 9 |
# File 'lib/shaf/spec/payload_utils.rb', line 6 def set_payload(payload) @payload = payload @payload = JSON.parse(payload, symbolize_names: true) if payload.is_a?(String) end |