module Shaf::Spec::PayloadUtils
Public Instance Methods
assert_attribute(attr, expected)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/shaf/spec/payload_utils.rb, line 130 def assert_has_embedded(*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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/shaf/spec/payload_utils.rb, line 16 def attributes last_payload.reject { |key,_| [:_links, :_embedded].include? key } end
default_field_value(field)
click to toggle source
# 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)
click to toggle source
# File lib/shaf/spec/payload_utils.rb, line 39 def each_embedded(name, &block) assert_has_embedded 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| exec_embed_block(resource, block, i) end end
embedded(name = nil, &block)
click to toggle source
# File lib/shaf/spec/payload_utils.rb, line 32 def embedded(name = nil, &block) assert_has_embedded name unless name.nil? keys = [:_embedded, name&.to_sym].compact return last_payload.dig(*keys) unless block_given? exec_embed_block(last_payload.dig(*keys), block) end
embedded_resources()
click to toggle source
# File lib/shaf/spec/payload_utils.rb, line 28 def embedded_resources last_payload[:_embedded]&.keys || [] end
fill_form(fields, opts = {})
click to toggle source
# 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()
click to toggle source
# File lib/shaf/spec/payload_utils.rb, line 11 def last_payload refute @payload.nil?, "No previous response body" @payload end
link_rels()
click to toggle source
# File lib/shaf/spec/payload_utils.rb, line 24 def link_rels links.keys end
links()
click to toggle source
# File lib/shaf/spec/payload_utils.rb, line 20 def links last_payload[:_links] || {} end
refute_has_attribute(attr)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/shaf/spec/payload_utils.rb, line 139 def refute_has_embedded(*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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
exec_embed_block(payload, block, *args)
click to toggle source
# File lib/shaf/spec/payload_utils.rb, line 148 def exec_embed_block(payload, block, *args) prev_payload = last_payload set_payload(payload) instance_exec(*args, &block) set_payload(prev_payload) end