Class: RSpec::Authorization::Adapters::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/authorization/adapters/request.rb

Overview

Create a request using wrapper around RSpec example. The request is made possible through wrapping the call around RSpec example, this approach is choosen because we need to get as close as possible to how the request is made inside RSpec.

Once the request is created, it will immediately run and we can evaluate the response, consider the following example.

request = Request.new(ArticlesController, :index, :user)
request.response.status # => 200

Since the request is actually creating a request to the provided controller, it will run everything inside the controller, and no stubbing is performed. Therefore exception inside controller will bubble up to the request and may cause unexpected result. That is with one exception, there is one exception, ActiveRecord::RecordNotFound is rescued and bypassed, therefore it will not affect the request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, action, role) ⇒ Request

Create request object and immediately run the request. Inside initialization a lot of stuff get stubbed, it is to allow request to be run with assumptions, following are the assumptions composed as private method run inside the initializer:

stub_current_user

The request is expected to run as a user with provided role. There is no user created whatsoever, it only creates a double with role_symbols defined and return it inside current_user.

stub_authorization_load_controller_object

The request is expected to ignore declarative_authorization controller's object loading.

stub_authorization_load_object

The request is expected to ignore declarative_authorization object loading. This is different from the above, and is expected to be called if controller object isn't loaded.

stub_callbacks

The request is expected to ignore all callbacks defined inside the controller.

stub_action

The request is expected to ignore all statements run inside the action and configured to render nothing instead.

All of the above assumptions is expected to run only inside this request only, and not to change the behavior of the application outside of this request.

Parameters:

  • klass (Class)

    controller class name to request from

  • action (Symbol)

    action name, currently only support RESTful action

  • role (Symbol)

    role name from config/authorization_rules.rb


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rspec/authorization/adapters/request.rb', line 70

def initialize(klass, action, role)
  @klass, @action, @role = klass, action, role
  @group, @route = ExampleGroup.new(klass), Route.new(action)

  @authorization_stubbed = false

  stub_current_user
  stub_authorization_load_controller_object
  stub_authorization_load_object
  stub_callbacks
  stub_action

  setup_response_retrieval
  group.run_example
end

Instance Attribute Details

#actionSymbol (readonly)

Returns controller action name

Returns:

  • (Symbol)

    controller action name


24
25
26
# File 'lib/rspec/authorization/adapters/request.rb', line 24

def action
  @action
end

#groupExampleGroup (readonly)

Returns example group of the request

Returns:

See Also:


29
30
31
# File 'lib/rspec/authorization/adapters/request.rb', line 29

def group
  @group
end

#klassClass (readonly)

Returns controller class name

Returns:

  • (Class)

    controller class name


22
23
24
# File 'lib/rspec/authorization/adapters/request.rb', line 22

def klass
  @klass
end

#responseActionController::TestResponse (readonly)

Returns response object of the request

Returns:

  • (ActionController::TestResponse)

    response object of the request


34
35
36
# File 'lib/rspec/authorization/adapters/request.rb', line 34

def response
  @response
end

#roleSymbol (readonly)

Returns role name from config/authorization_rules.rb

Returns:

  • (Symbol)

    role name from config/authorization_rules.rb


26
27
28
# File 'lib/rspec/authorization/adapters/request.rb', line 26

def role
  @role
end

#routeRoute (readonly)

Returns route object of the action

Returns:

  • (Route)

    route object of the action

See Also:


32
33
34
# File 'lib/rspec/authorization/adapters/request.rb', line 32

def route
  @route
end