aboutsummaryrefslogtreecommitdiffstats
path: root/spec/support/api_key.rb
blob: cc08cd7f1c606b18bb154cb2367eea611adcf3c3 (plain)
1
2
3
4
5
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module ApiKeyHelper

  def authorization_token_header(key)
    {'Authorization' => "Token token=#{key}"}
  end

  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

  def self.included(base)
    base.class_eval do
      extend ClassMethods
      alias_method :api_key, :get_api_key
    end
  end

  module ClassMethods
    def assign_api_key
      before(:each) do
        assign :api_key, api_key
      end
    end
  end

end

RSpec.configure do |config|
  config.include ApiKeyHelper
end