Module: Hephaestus::API::TestHelpers

Includes:
Rack::Test::Methods
Defined in:
lib/hephaestus/support/hephaestus/api.rb

Instance Method Summary collapse

Instance Method Details

#api(method, path, headers: {}, version: Rails.configuration.yetto_api_version, body: {}, parse: true) ⇒ Object



15
16
17
18
19
# File 'lib/hephaestus/support/hephaestus/api.rb', line 15

def api(method, path, headers: {}, version: Rails.configuration.yetto_api_version, body: {}, parse: true)
  prepended_path = prepend_api_path(version, path)

  http_call(method, prepended_path, headers: headers, version: version, body: body, parse: true)
end

#assert_response(expected_status, expected_body = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hephaestus/support/hephaestus/api.rb', line 35

def assert_response(expected_status, expected_body = nil)
  expected_status = case expected_status
  when :ok, :okay
    Hephaestus::HTTP::OK_I
  when :created
    Hephaestus::HTTP::CREATED_I
  when :no_content
    Hephaestus::HTTP::NO_CONTENT_I
  when :redirect
    Hephaestus::HTTP::FOUND_I
  when :bad_request
    Hephaestus::HTTP::BAD_REQUEST_I
  when :unauthorized
    Hephaestus::HTTP::UNAUTHORIZED_I
  when :forbidden
    Hephaestus::HTTP::FORBIDDEN_I
  when :not_found
    Hephaestus::HTTP::NOT_FOUND_I
  when :not_acceptable
    Hephaestus::HTTP::NOT_ACCEPTABLE_I
  when :service_unavailable
    Hephaestus::HTTP::SERVICE_UNAVAILABLE_I
  else
    raise ArgumentError, "Unknown status: #{expected_status}"
  end

  assert_equal(expected_status, last_response.status)
end

#headers(event, body) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/hephaestus/support/hephaestus/api.rb', line 72

def headers(event, body)
  case event
  when :after_create
    {
      Hephaestus::Headers::HEADER_EVENT => "after_create",
      Hephaestus::Headers::HEADER_RECORD_TYPE => "plug_installation",
    }
  when :after_update_plug_installation
    {
      Hephaestus::Headers::HEADER_EVENT => "after_update",
      Hephaestus::Headers::HEADER_RECORD_TYPE => "plug_installation",
    }
  when :after_update_inbox
    {
      Hephaestus::Headers::HEADER_EVENT => "after_update",
      Hephaestus::Headers::HEADER_RECORD_TYPE => "inbox",
    }
  when :after_update_organization
    {
      Hephaestus::Headers::HEADER_EVENT => "after_update",
      Hephaestus::Headers::HEADER_RECORD_TYPE => "organization",
    }
  when :after_destroy_plug_installation
    {
      Hephaestus::Headers::HEADER_EVENT => "after_destroy",
      Hephaestus::Headers::HEADER_RECORD_TYPE => "plug_installation",
    }
  else
    {}
  end.merge(Hephaestus::Headers::HEADER_SIGNATURE => yetto_auth_header(body))
end

#http_call(method, path, headers: {}, version: nil, body: {}, parse: true) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hephaestus/support/hephaestus/api.rb', line 21

def http_call(method, path, headers: {}, version: nil, body: {}, parse: true)
  # explicitly assert headers cannot be nil
  if headers.nil?
    send(method, path, body.to_json)
  else
    headers["HTTP_ACCEPT"] ||= headers.fetch("HTTP_ACCEPT", "application/json")
    headers["CONTENT_TYPE"] ||= headers.fetch("CONTENT_TYPE", "application/json")
    body = body.to_json if headers["CONTENT_TYPE"] == "application/json"
    send(method, path, body, headers)
  end

  JSON.parse(last_response.body) if last_response.body.present? && parse
end

#plug(method, path, headers: {}, version: Rails.configuration.yetto_api_version, body: {}, parse: true) ⇒ Object



9
10
11
12
13
# File 'lib/hephaestus/support/hephaestus/api.rb', line 9

def plug(method, path, headers: {}, version: Rails.configuration.yetto_api_version, body: {}, parse: true)
  prepended_path = prepend_plug_path(version, path)

  http_call(method, prepended_path, headers: headers, version: version, body: body, parse: true)
end

#prepend_api_path(version, path) ⇒ Object



68
69
70
# File 'lib/hephaestus/support/hephaestus/api.rb', line 68

def prepend_api_path(version, path)
  "/api/#{version}#{path}"
end

#prepend_plug_path(version, path) ⇒ Object



64
65
66
# File 'lib/hephaestus/support/hephaestus/api.rb', line 64

def prepend_plug_path(version, path)
  "/#{plug_shortname}/#{version}#{path}"
end