esp_sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +1185 -0
- data/Rakefile +9 -0
- data/esp_sdk.gemspec +33 -0
- data/lib/esp_sdk/api.rb +29 -0
- data/lib/esp_sdk/client.rb +59 -0
- data/lib/esp_sdk/configure.rb +36 -0
- data/lib/esp_sdk/end_points/base.rb +86 -0
- data/lib/esp_sdk/end_points/contact_requests.rb +6 -0
- data/lib/esp_sdk/end_points/custom_signatures.rb +41 -0
- data/lib/esp_sdk/end_points/dashboard.rb +30 -0
- data/lib/esp_sdk/end_points/external_accounts.rb +9 -0
- data/lib/esp_sdk/end_points/organizations.rb +6 -0
- data/lib/esp_sdk/end_points/reports.rb +6 -0
- data/lib/esp_sdk/end_points/services.rb +6 -0
- data/lib/esp_sdk/end_points/signatures.rb +39 -0
- data/lib/esp_sdk/end_points/sub_organizations.rb +6 -0
- data/lib/esp_sdk/end_points/teams.rb +6 -0
- data/lib/esp_sdk/end_points/users.rb +6 -0
- data/lib/esp_sdk/exceptions.rb +7 -0
- data/lib/esp_sdk/extensions/rest_client/request.rb +9 -0
- data/lib/esp_sdk/version.rb +3 -0
- data/lib/esp_sdk.rb +45 -0
- data/test/esp_sdk/api_test.rb +36 -0
- data/test/esp_sdk/client_test.rb +119 -0
- data/test/esp_sdk/configure_test.rb +49 -0
- data/test/esp_sdk/end_points/.keep +0 -0
- data/test/esp_sdk/end_points/base_test.rb +175 -0
- data/test/esp_sdk/end_points/custom_signatures_test.rb +90 -0
- data/test/esp_sdk/end_points/dashboard_test.rb +55 -0
- data/test/esp_sdk/end_points/external_accounts_test.rb +20 -0
- data/test/esp_sdk/end_points/signatures_test.rb +83 -0
- data/test/esp_sdk/exceptions_test.rb +35 -0
- data/test/esp_sdk_test.rb +70 -0
- data/test/test_helper.rb +20 -0
- metadata +264 -0
data/Rakefile
ADDED
data/esp_sdk.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'esp_sdk/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'esp_sdk'
|
8
|
+
spec.version = EspSdk::VERSION
|
9
|
+
spec.authors = ['Evident.io']
|
10
|
+
spec.email = ['support@evident.io']
|
11
|
+
spec.summary = %q{SDK for interacting with the ESP API.}
|
12
|
+
spec.homepage = ''
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.3.2'
|
22
|
+
spec.add_development_dependency 'rubocop', '~> 0.27.1'
|
23
|
+
spec.add_development_dependency 'minitest', '~> 5.4.2'
|
24
|
+
spec.add_development_dependency 'minitest-reporters', '~> 1.0.7'
|
25
|
+
spec.add_development_dependency 'shoulda', '~> 3.5.0'
|
26
|
+
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4.1'
|
27
|
+
spec.add_development_dependency 'mocha', '~> 1.1.0'
|
28
|
+
spec.add_development_dependency 'fakeweb', '~> 1.3'
|
29
|
+
spec.add_development_dependency 'awesome_print', '~> 1.2.0'
|
30
|
+
|
31
|
+
spec.add_runtime_dependency 'activesupport', '~> 4.1.6'
|
32
|
+
spec.add_runtime_dependency 'rest_client', '~> 1.7.3'
|
33
|
+
end
|
data/lib/esp_sdk/api.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module EspSdk
|
2
|
+
class Api
|
3
|
+
attr_reader :end_points, :config
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
fail MissingAttribute, 'Missing required email' if options[:email].blank?
|
7
|
+
fail MissingAttribute, 'Missing required password or token' if options[:password].blank? && options[:token].blank?
|
8
|
+
@config = Configure.new(options)
|
9
|
+
@end_points = []
|
10
|
+
define_methods
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def define_methods
|
16
|
+
end_points = EspSdk::EndPoints.constants - [:Base]
|
17
|
+
|
18
|
+
end_points.each do |end_point|
|
19
|
+
name = end_point.to_s.underscore
|
20
|
+
define_singleton_method name do
|
21
|
+
return instance_variable_get(:"@#{name}") if instance_variable_get(:"@#{name}").present?
|
22
|
+
instance_variable_set(:"@#{name}", EspSdk::EndPoints.const_get(end_point).new(config))
|
23
|
+
end
|
24
|
+
|
25
|
+
@end_points << send(name)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rest_client'
|
2
|
+
require_relative 'extensions/rest_client/request'
|
3
|
+
|
4
|
+
module EspSdk
|
5
|
+
# Client class for our endpoints. Every endpoint gets its own client.
|
6
|
+
class Client
|
7
|
+
attr_reader :config, :errors
|
8
|
+
|
9
|
+
def initialize(config)
|
10
|
+
@config = config
|
11
|
+
end
|
12
|
+
|
13
|
+
def connect(url, type = :get, payload = {})
|
14
|
+
payload = { payload_key => payload } if payload.present?
|
15
|
+
|
16
|
+
begin
|
17
|
+
if type == :get || type == :delete
|
18
|
+
if payload.present?
|
19
|
+
response = RestClient.send(type, url, headers.merge(params: payload))
|
20
|
+
else
|
21
|
+
response = RestClient.send(type, url, headers)
|
22
|
+
end
|
23
|
+
else
|
24
|
+
# The rest of our actions will require a payload
|
25
|
+
fail MissingAttribute, 'Missing required attributes' if payload.blank?
|
26
|
+
response = RestClient.send(type, url, payload, headers)
|
27
|
+
end
|
28
|
+
rescue RestClient::UnprocessableEntity, RestClient::Unauthorized => e
|
29
|
+
response = e.response
|
30
|
+
check_errors(JSON.load(response.body))
|
31
|
+
end
|
32
|
+
|
33
|
+
response
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def headers
|
39
|
+
@headers ||= { 'Authorization' => @config.token, 'Authorization-Email' => @config.email, 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
|
40
|
+
end
|
41
|
+
|
42
|
+
def payload_key
|
43
|
+
@payload_key ||= self.class.to_s.demodulize.singularize.underscore
|
44
|
+
end
|
45
|
+
|
46
|
+
def check_errors(body)
|
47
|
+
@errors = body['errors'] if body.present? && body.is_a?(Hash) && body['errors'].present?
|
48
|
+
return unless @errors.present?
|
49
|
+
|
50
|
+
if @errors.select { |error| error.to_s.include?('Token has expired') }.present?
|
51
|
+
fail TokenExpired, 'Token has expired'
|
52
|
+
elsif (error = @errors.select { |error| error.to_s.include?('Record not found') }[0]).present?
|
53
|
+
fail RecordNotFound, error
|
54
|
+
else
|
55
|
+
fail EspSdk::Exception, "#{@errors.join('. ')}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module EspSdk
|
2
|
+
class Configure
|
3
|
+
attr_accessor :token, :email, :version, :token_expires_at
|
4
|
+
|
5
|
+
def initialize(options)
|
6
|
+
@email = options[:email]
|
7
|
+
@version = options[:version] || 'v1'
|
8
|
+
token_setup(options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def uri
|
12
|
+
return @uri if @uri.present?
|
13
|
+
|
14
|
+
if EspSdk.production?
|
15
|
+
@uri = 'https://api.evident.io/api'
|
16
|
+
elsif EspSdk.release?
|
17
|
+
@uri = 'https://api-rel.evident.io/api'
|
18
|
+
else
|
19
|
+
@uri = 'http://0.0.0.0:3001/api'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def token_setup(options)
|
26
|
+
self.token = options[:password] || options[:token]
|
27
|
+
end_point = options[:password].present? ? 'new' : 'valid'
|
28
|
+
client = Client.new(self)
|
29
|
+
response = client.connect("#{uri}/#{version}/token/#{end_point}")
|
30
|
+
user = JSON.load(response.body)
|
31
|
+
self.token = user['authentication_token']
|
32
|
+
self.token_expires_at = user['token_expires_at'].to_s.to_datetime.utc ||
|
33
|
+
1.hour.from_now.to_datetime.utc
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module EspSdk
|
4
|
+
module EndPoints
|
5
|
+
class Base < EspSdk::Client
|
6
|
+
attr_reader :current_page, :current_record
|
7
|
+
|
8
|
+
def next_page
|
9
|
+
return list if current_page.blank?
|
10
|
+
|
11
|
+
if @page_links['next'].present?
|
12
|
+
response = connect(@page_links['next'], :get)
|
13
|
+
pagination_links(response)
|
14
|
+
@current_page = JSON.load(response.body)
|
15
|
+
else
|
16
|
+
current_page
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def prev_page
|
21
|
+
return list if current_page.blank?
|
22
|
+
|
23
|
+
if @page_links['prev'].present?
|
24
|
+
response = connect(@page_links['prev'], :get)
|
25
|
+
pagination_links(response)
|
26
|
+
@current_page = JSON.load(response.body)
|
27
|
+
else
|
28
|
+
@current_page
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Get a pageable list of records
|
33
|
+
def list
|
34
|
+
response = connect(base_url, :get)
|
35
|
+
pagination_links(response)
|
36
|
+
@current_page = JSON.load(response.body)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Get a single record
|
40
|
+
def show(params = {})
|
41
|
+
validate_id(params)
|
42
|
+
submit(id_url(params.delete(:id)), :get)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Update a single record
|
46
|
+
def update(params = {})
|
47
|
+
validate_id(params)
|
48
|
+
submit(id_url(params.delete(:id)), :patch, params)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Destroy a single record
|
52
|
+
def destroy(params = {})
|
53
|
+
validate_id(params)
|
54
|
+
submit(id_url(params.delete(:id)), :delete)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Create a new record
|
58
|
+
def create(params = {})
|
59
|
+
submit(base_url, :post, params)
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def validate_id(params)
|
65
|
+
fail MissingAttribute, 'Missing required attribute id' if params[:id].blank?
|
66
|
+
end
|
67
|
+
|
68
|
+
def id_url(id)
|
69
|
+
"#{base_url}/#{id}"
|
70
|
+
end
|
71
|
+
|
72
|
+
def base_url
|
73
|
+
"#{config.uri}/#{config.version}/#{self.class.to_s.demodulize.underscore}"
|
74
|
+
end
|
75
|
+
|
76
|
+
def submit(url, type, options = {})
|
77
|
+
response = connect(url, type, options)
|
78
|
+
@current_record = JSON.load(response.body)
|
79
|
+
end
|
80
|
+
|
81
|
+
def pagination_links(response)
|
82
|
+
@page_links = JSON.load(response.headers[:link])
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module EspSdk
|
2
|
+
module EndPoints
|
3
|
+
class CustomSignatures < Base
|
4
|
+
def run(params = {})
|
5
|
+
validate_run_params(valid_run_params, params.keys)
|
6
|
+
submit(run_url, :post, params)
|
7
|
+
end
|
8
|
+
|
9
|
+
def run_raw(params = {})
|
10
|
+
validate_run_params(valid_run_raw_params, params.keys)
|
11
|
+
submit(run_url, :post, params)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def run_url
|
17
|
+
"#{base_url}/run"
|
18
|
+
end
|
19
|
+
|
20
|
+
def valid_run_params
|
21
|
+
[:id, :regions, :external_account_id]
|
22
|
+
end
|
23
|
+
|
24
|
+
def valid_run_raw_params
|
25
|
+
[:signature, :regions, :external_account_id]
|
26
|
+
end
|
27
|
+
|
28
|
+
def validate_run_params(valid_params, keys)
|
29
|
+
# Check that all the valid params are present
|
30
|
+
valid_params.each do |param|
|
31
|
+
fail MissingAttribute, "Missing required attribute #{param}" unless keys.include?(param)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Check for invalid params
|
35
|
+
keys.each do |key|
|
36
|
+
fail UnknownAttribute, "Unknown attribute #{key}" unless valid_params.include?(key)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module EspSdk
|
2
|
+
module EndPoints
|
3
|
+
class Dashboard < Base
|
4
|
+
def timewarp(params = {})
|
5
|
+
validate_timewarp_params(params.keys)
|
6
|
+
submit(timewarp_url, :post, params)
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def timewarp_url
|
12
|
+
"#{base_url}/timewarp"
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate_timewarp_params(keys)
|
16
|
+
valid_timewarp_params = [:time]
|
17
|
+
|
18
|
+
# Check that all the valid params are present
|
19
|
+
valid_timewarp_params.each do |param|
|
20
|
+
fail MissingAttribute, "Missing required attribute #{param}" unless keys.include?(param)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Check for invalid params
|
24
|
+
keys.each do |key|
|
25
|
+
fail UnknownAttribute, "Unknown attribute #{key}" unless valid_timewarp_params.include?(key)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module EspSdk
|
2
|
+
module EndPoints
|
3
|
+
class Signatures < Base
|
4
|
+
def run(params = {})
|
5
|
+
validate_run_params(params)
|
6
|
+
submit(run_url, :post, params)
|
7
|
+
end
|
8
|
+
|
9
|
+
def names
|
10
|
+
submit(name_url, :get)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def run_url
|
16
|
+
"#{base_url}/run"
|
17
|
+
end
|
18
|
+
|
19
|
+
def name_url
|
20
|
+
"#{base_url}/signature_names"
|
21
|
+
end
|
22
|
+
|
23
|
+
def validate_run_params(options)
|
24
|
+
valid_params = [:signature_name, :regions, :external_account_id]
|
25
|
+
keys = options.keys
|
26
|
+
|
27
|
+
# Check that all the valid params are present
|
28
|
+
valid_params.each do |param|
|
29
|
+
fail EspSdk::MissingAttribute, "Missing required attribute #{param}" unless keys.include?(param)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Check for invalid params
|
33
|
+
keys.each do |key|
|
34
|
+
fail EspSdk::UnknownAttribute, "Unknown attribute #{key}" unless valid_params.include?(key)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/esp_sdk.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require_relative 'esp_sdk/version'
|
3
|
+
require_relative 'esp_sdk/configure'
|
4
|
+
require_relative 'esp_sdk/client'
|
5
|
+
require_relative 'esp_sdk/end_points/base'
|
6
|
+
require_relative 'esp_sdk/end_points/reports'
|
7
|
+
require_relative 'esp_sdk/end_points/users'
|
8
|
+
require_relative 'esp_sdk/end_points/external_accounts'
|
9
|
+
require_relative 'esp_sdk/end_points/custom_signatures'
|
10
|
+
require_relative 'esp_sdk/end_points/organizations'
|
11
|
+
require_relative 'esp_sdk/end_points/sub_organizations'
|
12
|
+
require_relative 'esp_sdk/end_points/teams'
|
13
|
+
require_relative 'esp_sdk/end_points/signatures'
|
14
|
+
require_relative 'esp_sdk/end_points/dashboard'
|
15
|
+
require_relative 'esp_sdk/end_points/contact_requests'
|
16
|
+
require_relative 'esp_sdk/end_points/services'
|
17
|
+
require_relative 'esp_sdk/api'
|
18
|
+
require_relative 'esp_sdk/exceptions'
|
19
|
+
|
20
|
+
module EspSdk
|
21
|
+
# Default environment is production
|
22
|
+
def self.env
|
23
|
+
@env ||= (ENV['ESP_ENV'] || ENV['RAILS_ENV'] || :production).to_sym
|
24
|
+
end
|
25
|
+
|
26
|
+
# Production environment query method
|
27
|
+
def self.production?
|
28
|
+
env == :production
|
29
|
+
end
|
30
|
+
|
31
|
+
# Release environment query method
|
32
|
+
def self.release?
|
33
|
+
env == :release
|
34
|
+
end
|
35
|
+
|
36
|
+
# Development environment query method
|
37
|
+
def self.development?
|
38
|
+
env == :development
|
39
|
+
end
|
40
|
+
|
41
|
+
# Test environment query method
|
42
|
+
def self.test?
|
43
|
+
env == :test
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
class ApiTest < ActiveSupport::TestCase
|
4
|
+
context 'Api' do
|
5
|
+
context '#initalize' do
|
6
|
+
should 'raise a MissingAttribute error for a missing email' do
|
7
|
+
e = assert_raises EspSdk::MissingAttribute do
|
8
|
+
EspSdk::Api.new({ })
|
9
|
+
end
|
10
|
+
assert_equal 'Missing required email', e.message
|
11
|
+
|
12
|
+
end
|
13
|
+
should 'raise a MissingAttribute error for a missing token and password' do
|
14
|
+
e = assert_raises EspSdk::MissingAttribute do
|
15
|
+
EspSdk::Api.new(email: 'test@evident.io')
|
16
|
+
end
|
17
|
+
assert_equal 'Missing required password or token', e.message
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
should 'define our endpoint methods and add them to the end_points array' do
|
22
|
+
# Stub the token setup for our configuration object
|
23
|
+
EspSdk::Configure.any_instance.expects(:token_setup).returns(nil).at_least_once
|
24
|
+
api = EspSdk::Api.new(email: 'test@evident.io', password: 'password')
|
25
|
+
end_points = (EspSdk::EndPoints.constants - [:Base]).map(&:to_s).map(&:underscore)
|
26
|
+
methods = api.singleton_methods.map(&:to_s)
|
27
|
+
|
28
|
+
assert_equal end_points.count, api.end_points.count
|
29
|
+
|
30
|
+
methods.each do |method|
|
31
|
+
assert_includes end_points, method
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
class ClientTest < ActiveSupport::TestCase
|
4
|
+
context 'Client' do
|
5
|
+
setup do
|
6
|
+
# Stub the token setup for our configuration object
|
7
|
+
EspSdk::Configure.any_instance.expects(:token_setup).returns(nil).at_least_once
|
8
|
+
@config = EspSdk::Configure.new(email: 'test@evident.io')
|
9
|
+
@config.token = '1234abc'
|
10
|
+
@client = EspSdk::Client.new(@config)
|
11
|
+
end
|
12
|
+
|
13
|
+
context '#initalize' do
|
14
|
+
# Sanity check really.
|
15
|
+
should 'set the config on the client' do
|
16
|
+
assert @client.config.is_a?(EspSdk::Configure)
|
17
|
+
assert_equal 'test@evident.io', @client.config.email
|
18
|
+
assert_equal '1234abc', @client.config.token
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context '#connect' do
|
23
|
+
setup { @url = 'https://esp.evident.io/api/v1/test' }
|
24
|
+
[:get, :delete].each do |type|
|
25
|
+
should "make #{type} request with headers only" do
|
26
|
+
# Make sure the correct RestClient method is called and arguments are passed
|
27
|
+
RestClient.expects(type).with(@url, @client.send(:headers))
|
28
|
+
@client.connect(@url, type)
|
29
|
+
end
|
30
|
+
|
31
|
+
should "make #{type} request with headers and payload" do
|
32
|
+
payload = { id: 1 }
|
33
|
+
# Make sure the correct RestClient method is called and arguments are passed
|
34
|
+
RestClient.expects(type).with(@url, @client.send(:headers).merge(params: { @client.send(:payload_key) => payload }))
|
35
|
+
@client.connect(@url, type, payload)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
[:post, :patch, :put].each do |type|
|
40
|
+
should "raise MissingAttribute exception for an empty payload from request type #{type}" do
|
41
|
+
e = assert_raises EspSdk::MissingAttribute do
|
42
|
+
@client.connect(@url, type)
|
43
|
+
end
|
44
|
+
|
45
|
+
assert_equal 'Missing required attributes', e.message
|
46
|
+
end
|
47
|
+
|
48
|
+
should "make #{type} request with headers and payload" do
|
49
|
+
payload = { id: 1 }
|
50
|
+
# Make sure the correct RestClient method is called and arguments are passed
|
51
|
+
RestClient.expects(type).with(@url, { @client.send(:payload_key) => payload }, @client.send(:headers))
|
52
|
+
@client.connect(@url, type, payload)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context '#headers' do
|
58
|
+
should 'have the correct keys and values' do
|
59
|
+
headers = @client.send(:headers)
|
60
|
+
# Assert the correct keys are present
|
61
|
+
assert headers.is_a?(Hash)
|
62
|
+
assert headers.key?('Authorization')
|
63
|
+
assert headers.key?('Authorization-Email')
|
64
|
+
assert headers.key?('Content-Type')
|
65
|
+
assert headers.key?('Accept')
|
66
|
+
|
67
|
+
# Assert the correct values are present
|
68
|
+
assert_equal @config.token, headers['Authorization']
|
69
|
+
assert_equal @config.email, headers['Authorization-Email']
|
70
|
+
assert_equal 'application/json', headers['Content-Type']
|
71
|
+
assert_equal 'application/json', headers['Accept']
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context '#payload_key' do
|
76
|
+
should 'create the correct payload key' do
|
77
|
+
# Check this through and endpoint class.
|
78
|
+
# Should take the class name and turn it into a valid key the api understands
|
79
|
+
external_account = EspSdk::EndPoints::ExternalAccounts.new(@config)
|
80
|
+
assert_equal 'external_account', external_account.send(:payload_key)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context '#check_errors' do
|
85
|
+
should 'return nil when errors are blank' do
|
86
|
+
assert_nil @client.send(:check_errors, {})
|
87
|
+
end
|
88
|
+
|
89
|
+
should 'return nil when errors is not a hash' do
|
90
|
+
assert_nil @client.send(:check_errors, [1])
|
91
|
+
end
|
92
|
+
|
93
|
+
should 'return nil when errors is missing the errors key' do
|
94
|
+
assert_nil @client.send(:check_errors, { test: 1 })
|
95
|
+
end
|
96
|
+
|
97
|
+
should 'raise TokenExpired exception when a token has expired error is present' do
|
98
|
+
e = assert_raises EspSdk::TokenExpired do
|
99
|
+
@client.send(:check_errors, { 'errors' => ['Token has expired'] })
|
100
|
+
end
|
101
|
+
assert_equal 'Token has expired', e.message
|
102
|
+
end
|
103
|
+
|
104
|
+
should 'raise TokenExpired exception when a record has not been found' do
|
105
|
+
e = assert_raises EspSdk::RecordNotFound do
|
106
|
+
@client.send(:check_errors, { 'errors' => ['Record not found'] })
|
107
|
+
end
|
108
|
+
assert_equal 'Record not found', e.message
|
109
|
+
end
|
110
|
+
|
111
|
+
should 'join the return errors into a EspSdk::Exception' do
|
112
|
+
e = assert_raises EspSdk::Exception do
|
113
|
+
@client.send(:check_errors, { 'errors' => ['ARN is blank', 'External ID is blank'] })
|
114
|
+
end
|
115
|
+
assert_equal 'ARN is blank. External ID is blank', e.message
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|