Class: RSpec::Authorization::Adapters::Request
- Inherits:
-
Object
- Object
- RSpec::Authorization::Adapters::Request
- 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
-
#action ⇒ Symbol
readonly
Controller action name.
-
#group ⇒ ExampleGroup
readonly
Example group of the request.
-
#klass ⇒ Class
readonly
Controller class name.
-
#response ⇒ ActionController::TestResponse
readonly
Response object of the request.
-
#role ⇒ Symbol
readonly
Role name from
config/authorization_rules.rb
. -
#route ⇒ Route
readonly
Route object of the action.
Instance Method Summary collapse
-
#initialize(klass, action, role) ⇒ Request
constructor
Create request object and immediately run the request.
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.
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_callbacks stub_action setup_response_retrieval group.run_example end |
Instance Attribute Details
#action ⇒ Symbol (readonly)
Returns controller action name.
24 25 26 |
# File 'lib/rspec/authorization/adapters/request.rb', line 24 def action @action end |
#group ⇒ ExampleGroup (readonly)
Returns example group of the request.
29 30 31 |
# File 'lib/rspec/authorization/adapters/request.rb', line 29 def group @group end |
#klass ⇒ Class (readonly)
Returns controller class name.
22 23 24 |
# File 'lib/rspec/authorization/adapters/request.rb', line 22 def klass @klass end |
#response ⇒ ActionController::TestResponse (readonly)
Returns response object of the request.
34 35 36 |
# File 'lib/rspec/authorization/adapters/request.rb', line 34 def response @response end |
#role ⇒ Symbol (readonly)
Returns role name from config/authorization_rules.rb
.
26 27 28 |
# File 'lib/rspec/authorization/adapters/request.rb', line 26 def role @role end |
#route ⇒ Route (readonly)
Returns route object of the action.
32 33 34 |
# File 'lib/rspec/authorization/adapters/request.rb', line 32 def route @route end |