Module: Shaf::Spec::PayloadUtils

Included in:
IntegrationSpec, SerializerSpec
Defined in:
lib/shaf/spec/payload_utils.rb

Instance Method Summary collapse

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 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


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


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


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

#attributesObject



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 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) ⇒ Object



32
33
34
35
36
37
# 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_resourcesObject



28
29
30
# File 'lib/shaf/spec/payload_utils.rb', line 28

def embedded_resources
  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_payloadObject



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


24
25
26
# File 'lib/shaf/spec/payload_utils.rb', line 24

def link_rels
  links.keys
end


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 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


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


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