Module: HaveAPI::Spec::SpecMethods

Includes:
Rack::Test::Methods
Defined in:
lib/haveapi/spec/spec_methods.rb

Overview

Helper methods for specs.

Instance Method Summary collapse

Instance Method Details

#api_responseHaveAPI::Spec::ApiResponse

Return parsed API response.



111
112
113
114
115
116
117
118
119
# File 'lib/haveapi/spec/spec_methods.rb', line 111

def api_response
  if last_response == @last_response
    @api_response ||= ApiResponse.new(last_response.body)
  else
    @last_response = last_response
    @api_response = ApiResponse.new(last_response.body)

  end
end

#appObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/haveapi/spec/spec_methods.rb', line 6

def app
  return @api.app if @api

  auth = get_opt(:auth_chain)
  default = get_opt(:default_version)

  @api = HaveAPI::Server.new(get_opt(:api_module))
  @api.auth_chain << auth if auth
  @api.use_version(get_opt(:versions) || :all)
  @api.default_version = default if default
  as = get_opt(:action_state)
  @api.action_state = as if as
  asa = get_opt(:action_state_auth)
  @api.action_state_auth = asa if asa
  ves = get_opt(:validation_error_http_status)
  @api.validation_error_http_status = ves if ves
  dl = get_opt(:default_locale)
  @api.default_locale = dl if dl
  al = get_opt(:available_locales)
  @api.available_locales = al if al
  lh = get_opt(:locale_header)
  @api.locale_header = lh if lh
  locale = get_opt(:locale)
  @api.locale(&locale) if locale
  parameter_i18n_scope = get_opt(:parameter_i18n_scope)
  @api.parameter_i18n_scope = parameter_i18n_scope if parameter_i18n_scope
  @api.mount(get_opt(:mount) || '/')
  @api.app
end

#call_api(*args) ⇒ Object

Make API request. This method is a wrapper for Rack::Test::Methods. Input parameters are encoded into JSON and sent with a correct Content-Type. Two modes:

http_method, path, params = {}
[resource], action, params, &block


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/haveapi/spec/spec_methods.rb', line 47

def call_api(*args, &)
  if args[0].is_a?(::Array) || args[1].is_a?(::Symbol)
    r_name, a_name, params = args

    app

    action, path = find_action(
      (params && params[:version]) || @api.default_version,
      r_name, a_name
    )

    json_body = params && params.to_json
    env = { 'CONTENT_TYPE' => 'application/json' }

    if %i[get head options].include?(action.http_method.to_sym)
      method(action.http_method).call(path, params, env)
    else
      env[:input] = json_body if json_body
      method(action.http_method).call(path, nil, env)
    end

  else
    http_method, path, params = args

    json_body = params && params.to_json
    env = { 'CONTENT_TYPE' => 'application/json' }

    if %i[get head options].include?(http_method.to_sym)
      method(http_method).call(path, params, env)
    else
      env[:input] = json_body if json_body
      method(http_method).call(path, nil, env)
    end
  end
end

#login(*credentials) ⇒ Object

Login with HTTP basic auth.



37
38
39
# File 'lib/haveapi/spec/spec_methods.rb', line 37

def (*credentials)
  basic_authorize(*credentials)
end

#mock_action(r_name, a_name, params, version: nil, user: nil) {|self| ... } ⇒ Object

Mock action call. Note that this method does not involve rack request/response in any way. It simply creates an instance of specified action and executes it. Provided block is executed in the context of the action instance after exec() has been called.

If exec() signals error, the block is not called at all, but RuntimeError is raised instead.

Authentication does not take place. Argument user may be used to provide user object. That will signify that the user is authenticated and it will be passed to Action.authorize.

Parameters:

  • r_name (Array, Symbol)

    path to resource in the API

  • a_name (Symbol)

    name of wanted action

  • params (Hash)

    a hash of parameters, must contain correct namespace

  • version (any) (defaults to: nil)

    API version, if not specified, the default version is used

  • user (any) (defaults to: nil)

    object representing authenticated user

Yields:

  • (self)

    the block is executed in the action instance



101
102
103
104
105
106
107
# File 'lib/haveapi/spec/spec_methods.rb', line 101

def mock_action(r_name, a_name, params, version: nil, user: nil, &)
  app
  v = version || @api.default_version
  action, path = find_action(v, r_name, a_name)
  m = MockAction.new(self, @api, action, path, v)
  m.call(params, user:, &)
end