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.



87
88
89
90
91
92
93
94
95
# File 'lib/haveapi/spec/spec_methods.rb', line 87

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


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/haveapi/spec/spec_methods.rb', line 31

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
    )

    method(action.http_method).call(
      path,
      params && params.to_json,
      { 'content-type' => 'application/json' }
    )

  else
    http_method, path, params = args

    method(http_method).call(
      path,
      params && params.to_json,
      { 'content-type' => 'application/json' }
    )
  end
end

#login(*credentials) ⇒ Object

Login with HTTP basic auth.



21
22
23
# File 'lib/haveapi/spec/spec_methods.rb', line 21

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



77
78
79
80
81
82
83
# File 'lib/haveapi/spec/spec_methods.rb', line 77

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