roqua-core-api 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 854a84f222ad107d7e8201a97f906fcac3f492bb
4
- data.tar.gz: 2624aacd6d166cb8f3b3c4b38518f4500175421a
3
+ metadata.gz: 308a75b5ae9970af2182e36ef88ec844f7cb4254
4
+ data.tar.gz: 30577d7f7ab41d5c3eac4b1b27ac2afbd9c81a7e
5
5
  SHA512:
6
- metadata.gz: c2671c1a0bece7bb7a042398ed6c42edd558e84dcfa6fe4d495157404fbd0262358591b9e0d24d7057944b25fd959222ed8c552feb4636bee5e12771c7f5b390
7
- data.tar.gz: e6d9331cf5c39ce4687ea78a129060e74bd69176650f333945feb2dfc5e325ed69448a2f30dcd6418d81438fc010bd7b524a916a1870292957f260ee6ddee1dd
6
+ metadata.gz: f9a0589c49f1b1ea0e13558b0bfff9cb8259dcb76918bf9e4cb026bafcba234037be125eec9077f3ab401e1944965b9d6d7a0fa4304749aead4f09a49cb1ab7d
7
+ data.tar.gz: b8e9613348b94353fcf4ea278b606b755682b3c9a8de4001b89e52c4e10cb4cbcf979a6f6a4142d8ce6b389024610ded6154f49c8f5ed8e45172058b328a2227
data/ChangeLog.md CHANGED
@@ -1,3 +1,5 @@
1
+ * Added TokenSession model and CreateTokenSession, DestroyTokenSession usecases.
2
+
1
3
  ### 0.1.0
2
4
 
3
5
  * BACKWARD INCOMPATIBLE: CreateDossier now needs a dossier and a person object.
@@ -46,6 +46,11 @@ nl:
46
46
  invalid: is ongeldig
47
47
  invalid_type: is geen geldige %{type}
48
48
  missing: is vereist
49
+ models:
50
+ roqua/core_api/create_token_session:
51
+ attributes:
52
+ base:
53
+ not_found: Username or password incorrect.
49
54
  types:
50
55
  array: array
51
56
  boolean: boolean
@@ -1,8 +1,8 @@
1
1
  begin
2
2
  require 'rails'
3
3
  module I18n
4
- class Railtie < ::Rails::Railtie #:nodoc:
5
- initializer 'rails-i18n' do |app|
4
+ class CoreApiRailtie < ::Rails::Railtie #:nodoc:
5
+ initializer 'rails-i18n' do
6
6
  I18n.load_path << Dir[File.join(File.expand_path(File.dirname(__FILE__) + '/../../config/locales'), '*.yml')]
7
7
  I18n.load_path.flatten!
8
8
  end
@@ -1 +1,2 @@
1
- require 'roqua/core_api'
1
+ require ::File.expand_path('../roqua/core_api', __FILE__)
2
+ require ::File.expand_path('../i18n/core_api_i18n', __FILE__)
@@ -3,27 +3,31 @@ require 'roqua/core_api/version'
3
3
  require 'roqua/core_api/sessions'
4
4
  require 'roqua/core_api/models'
5
5
  require 'roqua/core_api/base'
6
- require 'roqua/core_api/dossiers'
7
- require 'roqua/core_api/dossier_groups'
8
- require 'roqua/core_api/list_dossier_group_rights'
9
- require 'roqua/core_api/delete_dossier_group_right'
10
- require 'roqua/core_api/create_professional'
11
- require 'roqua/core_api/create_dossier'
12
- require 'roqua/core_api/create_dossier_group'
13
- require 'roqua/core_api/send_email_to'
14
- require 'roqua/core_api/send_invite_email'
15
- require 'roqua/core_api/people'
16
- require 'roqua/core_api/person'
17
- require 'roqua/core_api/update_person'
18
- require 'roqua/core_api/update_dossier'
19
- require 'roqua/core_api/create_dossier_group_export_synchronously'
20
6
 
21
7
  module Roqua
22
8
  module CoreApi
9
+ extend ActiveSupport::Autoload
23
10
  # Raised when a request failed due to an expired/non-existant session.
24
11
  class NoSession < StandardError; end
25
12
  class Unauthorized < StandardError; end
26
13
 
14
+ autoload :CreateDossier
15
+ autoload :CreateDossierGroup
16
+ autoload :CreateDossierGroupExportSynchronously
17
+ autoload :CreateProfessional
18
+ autoload :CreateTokenSession
19
+ autoload :DestroyTokenSession
20
+ autoload :Dossiers
21
+ autoload :DossierGroups
22
+ autoload :DeleteDossierGroupRight
23
+ autoload :ListDossierGroupRights
24
+ autoload :People
25
+ autoload :Person
26
+ autoload :SendEmailTo
27
+ autoload :SendInviteEmail
28
+ autoload :UpdatePerson
29
+ autoload :UpdateDossier
30
+
27
31
  def self.load_fabricators
28
32
  gem_root = Gem::Specification.find_by_name("roqua-core-api").gem_dir
29
33
  Fabrication::Config.path_prefixes << gem_root unless Fabrication::Config.path_prefixes.include? gem_root
@@ -15,6 +15,14 @@ module Roqua
15
15
  end
16
16
  end
17
17
  end
18
+
19
+ def errors_to_usecase(response)
20
+ (response['errors'] || []).each do |attribute, resp_errors|
21
+ resp_errors.each do |error|
22
+ errors.add(attribute.to_sym, error.to_sym)
23
+ end
24
+ end
25
+ end
18
26
  end
19
27
  end
20
28
  end
@@ -0,0 +1,20 @@
1
+ module Roqua
2
+ module CoreApi
3
+ # @api private
4
+ class CreateTokenSession < Base
5
+ string :organization_id
6
+ string :username
7
+ string :password
8
+
9
+ def execute
10
+ response = CoreApi.basic_auth_session.post \
11
+ "/organizations/#{organization_id}/token_sessions",
12
+ username: username, password: password
13
+ if response.code == 422
14
+ errors_to_usecase response
15
+ end
16
+ response['token_session']
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ module Roqua
2
+ module CoreApi
3
+ # @api private
4
+ class DestroyTokenSession < Base
5
+ model :session, class: Sessions::TokenSession
6
+ string :organization_id
7
+
8
+ def execute
9
+ session.delete "/organizations/#{organization_id}/token_sessions"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,6 +1,7 @@
1
1
  require 'httparty'
2
2
  require 'roqua/core_api/sessions/auth_session'
3
3
  require 'roqua/core_api/sessions/oauth_session'
4
+ require 'roqua/core_api/sessions/token_session'
4
5
  require 'roqua/core_api/sessions/basic_auth_session'
5
6
 
6
7
  module Roqua
@@ -9,6 +10,10 @@ module Roqua
9
10
  Sessions::OAuthSession.new(*arguments, &block)
10
11
  end
11
12
 
13
+ def self.token_session(*arguments, &block)
14
+ Sessions::TokenSession.new(*arguments, &block)
15
+ end
16
+
12
17
  def self.basic_auth_session(*arguments, &block)
13
18
  Sessions::BasicAuthSession.new(*arguments, &block)
14
19
  end
@@ -59,7 +59,7 @@ module Roqua
59
59
  when 401
60
60
  access_denied(response)
61
61
  else
62
- fail response.parsed_response || 'error'
62
+ fail(response.parsed_response.to_s || 'error')
63
63
  end
64
64
  end
65
65
 
@@ -0,0 +1,38 @@
1
+ module Roqua
2
+ module CoreApi
3
+ module Sessions
4
+ class TokenSession < AuthSession
5
+ attr_reader :access_token
6
+
7
+ def initialize(access_token:, **additional_arguments)
8
+ @access_token = access_token
9
+ super additional_arguments
10
+ end
11
+
12
+ def logout
13
+ delete 'sessions/destroy'
14
+ end
15
+
16
+ # ping the server to check if session is still valid.
17
+ # Will throw NoSession as usual if not.
18
+ def ping
19
+ get "/ping"
20
+ end
21
+
22
+ private
23
+
24
+ def access_denied(response)
25
+ if response['no_session']
26
+ fail NoSession
27
+ else
28
+ fail Unauthorized
29
+ end
30
+ end
31
+
32
+ def headers
33
+ {"Authorization" => "Session #{access_token}"}
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,5 +1,5 @@
1
1
  module Roqua
2
2
  module CoreApi
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
@@ -1,3 +1,9 @@
1
1
  Fabricator(:roqua_core_api_auth_session, from: Roqua::CoreApi::Sessions::AuthSession) do
2
2
  initialize_with { Roqua::CoreApi::Sessions::AuthSession.new core_site: 'http://core.dev' }
3
3
  end
4
+
5
+ Fabricator(:roqua_core_api_token_session, from: Roqua::CoreApi::Sessions::TokenSession) do
6
+ initialize_with do
7
+ Roqua::CoreApi::Sessions::TokenSession.new access_token: 'some_access_token', core_site: 'http://core.dev'
8
+ end
9
+ end
@@ -42,7 +42,7 @@ module Roqua
42
42
  it 'can be set by datetime(i1) params' do
43
43
  subject.attributes = { 'datetime(1i)' => '1999', 'datetime(2i)' => '12', 'datetime(3i)' => '8',
44
44
  'datetime(4i)' => '22', 'datetime(5i)' => '23', 'datetime(6i)' => '24'}
45
- expect(subject.datetime).to eq DateTime.new(1999, 12, 8, 22, 23, 24)
45
+ expect(subject.datetime).to eq DateTime.new(1999, 12, 8, 22, 23, 24).utc
46
46
  end
47
47
  end
48
48
  end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+ require 'webmock/rspec'
3
+
4
+ describe TokenSession do
5
+ let(:session) { Fabricate :roqua_core_api_token_session }
6
+ let(:response) { double('response', code: 201, parsed_response: 'some_response') }
7
+
8
+ describe '#initialize' do
9
+ it 'sets the access_token instance variable' do
10
+ session = Roqua::CoreApi.token_session access_token: 'some_access_token'
11
+ expect(session.access_token).to eq('some_access_token')
12
+ end
13
+
14
+ it 'allows to override the core_site variable' do
15
+ session = Roqua::CoreApi.token_session access_token: 'some_access_token', core_site: 'some_core_site'
16
+ expect(session.core_site).to eq('some_core_site')
17
+ end
18
+
19
+ it 'defaults the AuthSession core_site CORE_SITE env variable default value' do
20
+ original_env_core_site = ENV['CORE_SITE']
21
+ ENV['CORE_SITE'] = 'some_env_core_site'
22
+ session = Roqua::CoreApi.token_session access_token: 'some_access_token'
23
+ ENV['CORE_SITE'] = original_env_core_site
24
+ expect(session.core_site).to eq('some_env_core_site')
25
+ end
26
+ end
27
+
28
+ describe '#logout' do
29
+ it 'sends a put request to the session#destroy' do
30
+ expect(HTTParty).to receive(:delete).with("http://core.dev/api/v1sessions/destroy.json",
31
+ headers: {"Authorization" => "Session some_access_token"},
32
+ query: {},
33
+ basic_auth: nil,
34
+ timeout: nil).and_return(response)
35
+ session.logout
36
+ end
37
+ end
38
+
39
+ describe 'headers' do
40
+ it 'sets the Authorization header' do
41
+ expect(HTTParty).to receive(:get).with(an_instance_of(String),
42
+ headers: {"Authorization" => "Session some_access_token"},
43
+ query: {},
44
+ basic_auth: nil,
45
+ timeout: nil).and_return(response)
46
+ session.get 'some_path'
47
+ end
48
+ end
49
+
50
+ describe '#access_denied' do
51
+ it 'raises a no_session error when response is 401 with a no_session response' do
52
+ stub_request(:get, 'http://core.dev/api/v1/some_path.json?').to_return(
53
+ status: 401,
54
+ body: '{ "no_session": true }',
55
+ headers: { 'Content-Type' => 'application/json' })
56
+ expect { session.get '/some_path' }.to raise_error(NoSession)
57
+ end
58
+
59
+ it 'raises a unauthorized error when response is 401 without a no_session response' do
60
+ stub_request(:get, 'http://core.dev/api/v1/some_path.json?').to_return(
61
+ status: 401,
62
+ body: '',
63
+ headers: { 'Content-Type' => 'application/json' })
64
+ expect { session.get '/some_path' }.to raise_error(Unauthorized)
65
+ end
66
+ end
67
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roqua-core-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marten Veldthuis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-06 00:00:00.000000000 Z
11
+ date: 2016-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -182,7 +182,7 @@ files:
182
182
  - Rakefile
183
183
  - config/locales/validation_errors.yml
184
184
  - core_api.gemspec
185
- - lib/i18n/i18n.rb
185
+ - lib/i18n/core_api_i18n.rb
186
186
  - lib/roqua-core-api.rb
187
187
  - lib/roqua/core_api.rb
188
188
  - lib/roqua/core_api/base.rb
@@ -190,7 +190,9 @@ files:
190
190
  - lib/roqua/core_api/create_dossier_group.rb
191
191
  - lib/roqua/core_api/create_dossier_group_export_synchronously.rb
192
192
  - lib/roqua/core_api/create_professional.rb
193
+ - lib/roqua/core_api/create_token_session.rb
193
194
  - lib/roqua/core_api/delete_dossier_group_right.rb
195
+ - lib/roqua/core_api/destroy_token_session.rb
194
196
  - lib/roqua/core_api/dossier_groups.rb
195
197
  - lib/roqua/core_api/dossiers.rb
196
198
  - lib/roqua/core_api/list_dossier_group_rights.rb
@@ -208,6 +210,7 @@ files:
208
210
  - lib/roqua/core_api/sessions/auth_session.rb
209
211
  - lib/roqua/core_api/sessions/basic_auth_session.rb
210
212
  - lib/roqua/core_api/sessions/oauth_session.rb
213
+ - lib/roqua/core_api/sessions/token_session.rb
211
214
  - lib/roqua/core_api/update_dossier.rb
212
215
  - lib/roqua/core_api/update_person.rb
213
216
  - lib/roqua/core_api/version.rb
@@ -230,6 +233,7 @@ files:
230
233
  - spec/lib/roqua/core_api/sessions/auth_session_spec.rb
231
234
  - spec/lib/roqua/core_api/sessions/basic_auth_session_spec.rb
232
235
  - spec/lib/roqua/core_api/sessions/oauth_session_spec.rb
236
+ - spec/lib/roqua/core_api/sessions/token_session_spec.rb
233
237
  - spec/lib/roqua/core_api/update_dossier_spec.rb
234
238
  - spec/spec_helper.rb
235
239
  - spec/support/httpparty_helpers.rb
@@ -253,7 +257,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
253
257
  version: '0'
254
258
  requirements: []
255
259
  rubyforge_project:
256
- rubygems_version: 2.4.3
260
+ rubygems_version: 2.4.8
257
261
  signing_key:
258
262
  specification_version: 4
259
263
  summary: API wrapper gem around Core's API
@@ -275,6 +279,7 @@ test_files:
275
279
  - spec/lib/roqua/core_api/sessions/auth_session_spec.rb
276
280
  - spec/lib/roqua/core_api/sessions/basic_auth_session_spec.rb
277
281
  - spec/lib/roqua/core_api/sessions/oauth_session_spec.rb
282
+ - spec/lib/roqua/core_api/sessions/token_session_spec.rb
278
283
  - spec/lib/roqua/core_api/update_dossier_spec.rb
279
284
  - spec/spec_helper.rb
280
285
  - spec/support/httpparty_helpers.rb