aboutsummaryrefslogtreecommitdiffstats
path: root/spec/support
diff options
context:
space:
mode:
authorXinhui2017-08-25 17:13:24 +0200
committerXinhui2017-08-25 17:13:24 +0200
commit0777d35ff4460cf07c34e69ee7c10c0270a446bf (patch)
treeb2d850bbce3c8669ee6c636fa93e91c5a6a662bc /spec/support
parent5dda0f5286043823acab68a73d84437a3cbd803f (diff)
parent1d7db2b6c254ac55105c08ee177580036b0377f3 (diff)
downloadchouette-core-0777d35ff4460cf07c34e69ee7c10c0270a446bf.tar.bz2
Merge branch 'master' into staging
Diffstat (limited to 'spec/support')
-rw-r--r--spec/support/api_key.rb10
-rw-r--r--spec/support/checksum_support.rb53
-rw-r--r--spec/support/fixtures_helper.rb18
-rw-r--r--spec/support/json_helper.rb11
-rw-r--r--spec/support/referential.rb1
-rw-r--r--spec/support/shared_context.rb15
-rw-r--r--spec/support/webmock/helpers.rb18
7 files changed, 125 insertions, 1 deletions
diff --git a/spec/support/api_key.rb b/spec/support/api_key.rb
index 9353fac15..cc08cd7f1 100644
--- a/spec/support/api_key.rb
+++ b/spec/support/api_key.rb
@@ -1,20 +1,28 @@
module ApiKeyHelper
+ def authorization_token_header(key)
+ {'Authorization' => "Token token=#{key}"}
+ end
+
def get_api_key
- Api::V1::ApiKey.first_or_create( :referential_id => referential.id, :name => "test")
+ 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/checksum_support.rb b/spec/support/checksum_support.rb
new file mode 100644
index 000000000..14ea3c55e
--- /dev/null
+++ b/spec/support/checksum_support.rb
@@ -0,0 +1,53 @@
+shared_examples 'checksum support' do |factory_name|
+ let(:instance) { create(factory_name) }
+
+ describe '#current_checksum_source' do
+ let(:attributes) { ['code_value', 'label_value'] }
+ let(:seperator) { ChecksumSupport::SEPARATOR }
+ let(:nil_value) { ChecksumSupport::VALUE_FOR_NIL_ATTRIBUTE }
+
+ before do
+ allow_any_instance_of(instance.class).to receive(:checksum_attributes).and_return(attributes)
+ end
+
+ it 'should separate attribute by seperator' do
+ expect(instance.current_checksum_source).to eq("code_value#{seperator}label_value")
+ end
+
+ context 'nil value' do
+ let(:attributes) { ['code_value', nil] }
+
+ it 'should replace nil attributes by default value' do
+ source = "code_value#{seperator}#{nil_value}"
+ expect(instance.current_checksum_source).to eq(source)
+ end
+ end
+
+ context 'empty array' do
+ let(:attributes) { ['code_value', []] }
+
+ it 'should convert to nil' do
+ source = "code_value#{seperator}#{nil_value}"
+ expect(instance.current_checksum_source).to eq(source)
+ end
+ end
+ end
+
+ it 'should save checksum on create' do
+ expect(instance.checksum).to_not be_nil
+ end
+
+ it 'should save checksum_source' do
+ expect(instance.checksum_source).to_not be_nil
+ end
+
+ it 'should trigger set_current_checksum_source on save' do
+ expect(instance).to receive(:set_current_checksum_source)
+ instance.save
+ end
+
+ it 'should trigger update_checksum on save' do
+ expect(instance).to receive(:update_checksum)
+ instance.save
+ end
+end
diff --git a/spec/support/fixtures_helper.rb b/spec/support/fixtures_helper.rb
new file mode 100644
index 000000000..20963261b
--- /dev/null
+++ b/spec/support/fixtures_helper.rb
@@ -0,0 +1,18 @@
+module Support
+ module FixturesHelper
+ def fixtures_path *segments
+ Rails.root.join( fixture_path, *segments )
+ end
+
+ def open_fixture *segments
+ File.open(fixtures_path(*segments))
+ end
+ def read_fixture *segments
+ File.read(fixtures_path(*segments))
+ end
+ end
+end
+
+RSpec.configure do |c|
+ c.include Support::FixturesHelper
+end
diff --git a/spec/support/json_helper.rb b/spec/support/json_helper.rb
new file mode 100644
index 000000000..a383981a0
--- /dev/null
+++ b/spec/support/json_helper.rb
@@ -0,0 +1,11 @@
+module Support
+ module JsonHelper
+ def json_response_body
+ JSON.parse(response.body)
+ end
+ end
+end
+
+RSpec.configure do | config |
+ config.include Support::JsonHelper, type: :request
+end
diff --git a/spec/support/referential.rb b/spec/support/referential.rb
index 57b510f69..c431856b8 100644
--- a/spec/support/referential.rb
+++ b/spec/support/referential.rb
@@ -12,6 +12,7 @@ module ReferentialHelper
base.class_eval do
extend ClassMethods
alias_method :referential, :first_referential
+ alias_method :organisation, :first_organisation
end
end
diff --git a/spec/support/shared_context.rb b/spec/support/shared_context.rb
new file mode 100644
index 000000000..e9b0025a2
--- /dev/null
+++ b/spec/support/shared_context.rb
@@ -0,0 +1,15 @@
+shared_context 'iboo authenticated api user' do
+ let(:api_key) { create(:api_key, organisation: organisation) }
+
+ before do
+ request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(api_key.organisation.code, api_key.token)
+ end
+end
+
+shared_context 'iboo wrong authorisation api user' do
+ let(:api_key) { create(:api_key, organisation: organisation) }
+
+ before do
+ request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials('fake code', api_key.token)
+ end
+end
diff --git a/spec/support/webmock/helpers.rb b/spec/support/webmock/helpers.rb
new file mode 100644
index 000000000..fc6c77850
--- /dev/null
+++ b/spec/support/webmock/helpers.rb
@@ -0,0 +1,18 @@
+module Support
+ module Webmock
+ module Helpers
+ def stub_headers(*args)
+ {headers: make_headers(*args)}
+ end
+
+ def make_headers(headers={}, authorization_token:)
+ headers.merge('Authorization' => "Token token=#{authorization_token.inspect}")
+ end
+ end
+ end
+end
+
+RSpec.configure do | conf |
+ conf.include Support::Webmock::Helpers, type: :model
+ conf.include Support::Webmock::Helpers, type: :worker
+end