empresaula-api 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,4 @@
1
+ require_relative '../resource/session'
1
2
  require_relative '../resource/user'
2
3
 
3
4
  module Empresaula
@@ -10,17 +11,21 @@ module Empresaula
10
11
  end
11
12
  end
12
13
 
14
+ def current_session
15
+ @current_session ||= find_session_from_cookies
16
+ end
17
+
13
18
  # Sets the current user
14
19
  #
15
20
  def current_user
16
- @current_user ||= find_from_cookies
21
+ current_session.user if current_session
17
22
  end
18
23
 
19
24
  private
20
25
 
21
- def find_from_cookies
26
+ def find_session_from_cookies
22
27
  if cookies[:empresaula_session].present?
23
- Empresaula::API::Resource::User.find_by_session_id(
28
+ Empresaula::API::Resource::Session.find(
24
29
  cookies[:empresaula_session])
25
30
  end
26
31
  end
@@ -4,6 +4,15 @@ module Empresaula
4
4
  module API
5
5
  module Resource
6
6
  class Base < ActiveResource::Base
7
+ class << self
8
+ def headers
9
+ @headers ||= {}
10
+ if superclass.respond_to?(:headers)
11
+ @headers = superclass.headers.merge(@headers)
12
+ end
13
+ @headers
14
+ end
15
+ end
7
16
  self.format = :json
8
17
  end
9
18
  end
@@ -0,0 +1,10 @@
1
+ require_relative 'base'
2
+
3
+ module Empresaula
4
+ module API
5
+ module Resource
6
+ class Session < Base
7
+ end
8
+ end
9
+ end
10
+ end
@@ -4,9 +4,6 @@ module Empresaula
4
4
  module API
5
5
  module Resource
6
6
  class User < Base
7
- def self.find_by_session_id(session_id)
8
- User.new(get(session_id))
9
- end
10
7
  end
11
8
  end
12
9
  end
@@ -1,3 +1,4 @@
1
+ require 'empresaula-api/config'
1
2
  require 'empresaula-api/resource/user'
2
3
  require 'active_resource'
3
4
  require 'json'
@@ -6,19 +7,41 @@ module Empresaula
6
7
  module API
7
8
  module TestHelpers
8
9
  module User
10
+
9
11
  def mock_current_user(user)
10
12
  cookies = Capybara.current_session.driver.browser.current_session.instance_variable_get(:@rack_mock_session).cookie_jar
11
13
  cookies[:empresaula_session] = 'qwertyuiop123456789'
12
- ActiveResource::HttpMock.respond_to do |mock|
13
- mock.get "/api/users/qwertyuiop123456789.json", {},
14
- user.to_json
15
- end
14
+ add_mock '/api/users/qwertyuiop123456789.json', user
15
+ mock_user user
16
+ @current_user = Empresaula::API::Resource::User.find(user[:id])
17
+ end
18
+
19
+ def current_user
20
+ @current_user
16
21
  end
17
22
 
18
23
  def mock_user(user)
24
+ add_mock "/api/users/#{user[:id]}.json", user
25
+ end
26
+
27
+ def add_mock(url, object, method = 'get')
28
+ @_mocks ||= {}
29
+ @_mocks[url] = {method: method, object: object}
30
+ perform_mocks!
31
+ end
32
+
33
+ def mocks
34
+ @_mocks || {}
35
+ end
36
+
37
+ def perform_mocks!
38
+ ActiveResource::HttpMock.reset!
19
39
  ActiveResource::HttpMock.respond_to do |mock|
20
- mock.get "/api/users/#{user[:id]}.json", {},
21
- user.to_json
40
+ mocks.each do |k, v|
41
+ mock.send(v[:method], k,
42
+ {"X-SecretToken"=>Empresaula::API.config.secret_token},
43
+ v[:object].to_json)
44
+ end
22
45
  end
23
46
  end
24
47
  end
@@ -1,5 +1,5 @@
1
1
  module Empresaula
2
2
  module API
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -15,19 +15,34 @@ module Empresaula::API::Controller
15
15
  end
16
16
  before do
17
17
  subject.cookies[:empresaula_session] = empresaula_session
18
+ Empresaula::API::Resource::Base.site = "http://fakehost/api"
18
19
  end
19
20
  let(:empresaula_session) do
20
- stub(present?: false)
21
+ stub(present?: false, to_s: 'session-secret')
22
+ end
23
+ describe "#current_session" do
24
+ it "returns nil if no cookie set" do
25
+ subject.current_session.must_equal nil
26
+ end
27
+ it "returns an user if a cookie is found" do
28
+ empresaula_session.stubs(present?: true)
29
+ Empresaula::API::Resource::Session.expects(:find).
30
+ with(empresaula_session)
31
+ subject.current_session
32
+ end
21
33
  end
22
34
  describe "#current_user" do
23
35
  it "returns nil if no cookie set" do
24
36
  subject.current_user.must_equal nil
25
37
  end
26
38
  it "returns an user if a cookie is found" do
39
+ session = {session: {user: {name: 'Jonah'}}}
40
+ ActiveResource::HttpMock.respond_to do |mock|
41
+ mock.get '/api/sessions/session-secret.json', {},
42
+ session.to_json
43
+ end
27
44
  empresaula_session.stubs(present?: true)
28
- Empresaula::API::Resource::User.expects(:find_by_session_id).
29
- with(empresaula_session)
30
- subject.current_user
45
+ subject.current_user.name.must_equal "Jonah"
31
46
  end
32
47
  end
33
48
  end
@@ -0,0 +1,31 @@
1
+ require_relative '../../test_helper'
2
+ require 'empresaula-api/resource/session'
3
+
4
+ module Empresaula::API::Resource
5
+ describe Session do
6
+ before do
7
+ Session.site = "http://fakehost/api"
8
+ end
9
+ describe ".find_by_session_id" do
10
+ let(:session) do
11
+ {
12
+ session: {
13
+ user: {
14
+ name: 'James'
15
+ }
16
+ }
17
+ }
18
+ end
19
+ before do
20
+ ActiveResource::HttpMock.respond_to do |mock|
21
+ mock.get '/api/sessions/session_key.json', {},
22
+ session.to_json
23
+ end
24
+ end
25
+ it "builds a new user given a session id" do
26
+ session = Session.find("session_key")
27
+ session.user.name.must_equal "James"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -12,12 +12,12 @@ module Empresaula::API::Resource
12
12
  end
13
13
  before do
14
14
  ActiveResource::HttpMock.respond_to do |mock|
15
- mock.get '/api/users/session_key.json', {},
15
+ mock.get '/api/users/id.json', {},
16
16
  user.to_json
17
17
  end
18
18
  end
19
19
  it "builds a new user given a session id" do
20
- user = User.find_by_session_id("session_key")
20
+ user = User.find("id")
21
21
  user.id.must_equal 1
22
22
  user.name.must_equal "James"
23
23
  end
@@ -11,6 +11,10 @@ module Empresaula::API::TestHelpers
11
11
  describe "#mock_user" do
12
12
  before do
13
13
  Empresaula::API::Resource::User.site = "http://fake/api"
14
+ Empresaula::API::config.secret_token = 'secret'
15
+ end
16
+ after do
17
+ Empresaula::API::config.secret_token = nil
14
18
  end
15
19
  it "mocks a particular user" do
16
20
  subject.mock_user({id: 1, name: "James"})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: empresaula-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-01 00:00:00.000000000 Z
12
+ date: 2011-11-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activeresource
16
- requirement: &70309093422200 !ruby/object:Gem::Requirement
16
+ requirement: &70214754650080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70309093422200
24
+ version_requirements: *70214754650080
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: turn
27
- requirement: &70309093421720 !ruby/object:Gem::Requirement
27
+ requirement: &70214754649500 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70309093421720
35
+ version_requirements: *70214754649500
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: minitest
38
- requirement: &70309093421160 !ruby/object:Gem::Requirement
38
+ requirement: &70214754648880 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70309093421160
46
+ version_requirements: *70214754648880
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: mocha
49
- requirement: &70309093420700 !ruby/object:Gem::Requirement
49
+ requirement: &70214754648440 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70309093420700
57
+ version_requirements: *70214754648440
58
58
  description: Ruby wrapper for interacting with Empresaula's API
59
59
  email:
60
60
  - info@codegram.com
@@ -74,12 +74,14 @@ files:
74
74
  - lib/empresaula-api/controller/authentication.rb
75
75
  - lib/empresaula-api/resource.rb
76
76
  - lib/empresaula-api/resource/base.rb
77
+ - lib/empresaula-api/resource/session.rb
77
78
  - lib/empresaula-api/resource/user.rb
78
79
  - lib/empresaula-api/test_helpers.rb
79
80
  - lib/empresaula-api/test_helpers/user.rb
80
81
  - lib/empresaula-api/version.rb
81
82
  - test/empresaula-api/config_test.rb
82
83
  - test/empresaula-api/controller/authentication_test.rb
84
+ - test/empresaula-api/resource/session_test.rb
83
85
  - test/empresaula-api/resource/user_test.rb
84
86
  - test/empresaula-api/test_helpers/user_test.rb
85
87
  - test/test_helper.rb
@@ -110,6 +112,7 @@ summary: Ruby wrapper for interacting with Empresaula's API
110
112
  test_files:
111
113
  - test/empresaula-api/config_test.rb
112
114
  - test/empresaula-api/controller/authentication_test.rb
115
+ - test/empresaula-api/resource/session_test.rb
113
116
  - test/empresaula-api/resource/user_test.rb
114
117
  - test/empresaula-api/test_helpers/user_test.rb
115
118
  - test/test_helper.rb