aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXinhui2017-08-22 14:30:19 +0200
committerXinhui2017-08-22 14:52:49 +0200
commite44b43ba2d6aeab84a2aead6e01aa9cd71c2d6e5 (patch)
treeaf22e3c373f8b3c9f37bfaeea937146e43abfb16
parent9a281154ea20a6450bb53880f1dd77d5139075f1 (diff)
downloadchouette-core-e44b43ba2d6aeab84a2aead6e01aa9cd71c2d6e5.tar.bz2
API - Rspec basic auth api controller
-rw-r--r--spec/controllers/api/v1/workbenches_controller_spec.rb18
-rw-r--r--spec/support/api_key.rb4
-rw-r--r--spec/support/shared_context.rb8
3 files changed, 30 insertions, 0 deletions
diff --git a/spec/controllers/api/v1/workbenches_controller_spec.rb b/spec/controllers/api/v1/workbenches_controller_spec.rb
index 187d8ffeb..dc05c3926 100644
--- a/spec/controllers/api/v1/workbenches_controller_spec.rb
+++ b/spec/controllers/api/v1/workbenches_controller_spec.rb
@@ -1,5 +1,23 @@
require 'rails_helper'
RSpec.describe Api::V1::WorkbenchesController, type: :controller do
+ context 'unauthenticated' do
+ describe 'GET #index' do
+ it 'should not be successful' do
+ get :index
+ expect(response).not_to be_success
+ end
+ end
+ end
+ context 'authenticated' do
+ include_context 'iboo authenticated api user'
+
+ describe 'GET #index' do
+ it 'should be successful' do
+ get :index
+ expect(response).to be_success
+ end
+ end
+ end
end
diff --git a/spec/support/api_key.rb b/spec/support/api_key.rb
index 5aaa31217..cc08cd7f1 100644
--- a/spec/support/api_key.rb
+++ b/spec/support/api_key.rb
@@ -7,18 +7,22 @@ module ApiKeyHelper
def get_api_key
Api::V1::ApiKey.first_or_create(referential: referential, organisation: organisation)
end
+
def config_formatted_request_with_authorization( format)
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials( get_api_key.token)
request.accept = format
end
+
def config_formatted_request_with_dummy_authorization( format)
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials( "dummy")
request.accept = format
end
+
def config_formatted_request_without_authorization( format)
request.env['HTTP_AUTHORIZATION'] = nil
request.accept = format
end
+
def json_xml_format?
request.accept == "application/json" || request.accept == "application/xml"
end
diff --git a/spec/support/shared_context.rb b/spec/support/shared_context.rb
new file mode 100644
index 000000000..c08ee6163
--- /dev/null
+++ b/spec/support/shared_context.rb
@@ -0,0 +1,8 @@
+shared_context 'iboo authenticated api user' do
+ let(:api_key) { create(:api_key) }
+ let(:user) { create(:user, organisation: api_key.organisation ) }
+
+ before do
+ request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user.username, api_key.token)
+ end
+end