warden-github 1.2.0 → 1.3.0

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: 1d16b8a0ce775cffede49bad96b407ea7bb03814
4
- data.tar.gz: ad6b95da408a4163251445886b2638c2061b48f2
3
+ metadata.gz: ea7be457e43d5d73595605d0f6107797007b36f0
4
+ data.tar.gz: 39e3ccc8f12771f33e456a7d2609bf9b810d0ddb
5
5
  SHA512:
6
- metadata.gz: f6cd9e974843071be78465b45b44c3332d3ad9ec44069d91768868d85141f9b94415543ee026c88c097e5c27aca85042dd1838ed414085221a9545b47678fff3
7
- data.tar.gz: a06e7b94ddafc1eafa30cfb5257e17cf7f8c3077b4660def2cd60f08043aa113365963d00db5eed1db976518aff381cc7fc8a26fbdcf8e1718c2d191f96c6c92
6
+ metadata.gz: 223429bf4759f89e7eedccea6724c7a9f50526517302386cf63f11d2fcdfcbebd73a96eaab044ac388867cd3f7bfca9cbf241377aeb98eabd6bf6192e5d2e3c2
7
+ data.tar.gz: 30bc47e8d9762afe87fdf58567005a605a8381cc71bfc9904a04d5d258f27f44a64696dd7db71ded800672b8f1ea3715dcf92c943094a9b3d5e4783dc4cf9dec
data/.travis.yml CHANGED
@@ -1,10 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.8.7
4
- - 1.9.2
5
- - 1.9.3
6
- - jruby-18mode # JRuby in 1.8 mode
7
- - jruby-19mode # JRuby in 1.9 mode
8
- - rbx-18mode
9
- - ree
3
+ - 2.3.0
10
4
  - ruby-head
@@ -3,9 +3,13 @@ module Warden
3
3
  # A hash subclass that acts as a cache for organization and team
4
4
  # membership states. Only membership states that are true are cached. These
5
5
  # are invalidated after a certain time.
6
- class MembershipCache < ::Hash
6
+ class MembershipCache
7
7
  CACHE_TIMEOUT = 60 * 5
8
8
 
9
+ def initialize(data)
10
+ @data = data
11
+ end
12
+
9
13
  # Fetches a membership status by type and id (e.g. 'org', 'my_company')
10
14
  # from cache. If no cached value is present or if the cached value
11
15
  # expired, the block will be invoked and the return value, if true,
@@ -27,10 +31,10 @@ module Warden
27
31
  private
28
32
 
29
33
  def cached_membership_valid?(type, id)
30
- timestamp = fetch(type).fetch(id)
34
+ timestamp = @data.fetch(type).fetch(id)
31
35
 
32
36
  if Time.now.to_i > timestamp + CACHE_TIMEOUT
33
- fetch(type).delete(id)
37
+ @data.fetch(type).delete(id)
34
38
  false
35
39
  else
36
40
  true
@@ -40,7 +44,7 @@ module Warden
40
44
  end
41
45
 
42
46
  def cache_membership(type, id)
43
- hash = self[type] ||= {}
47
+ hash = @data[type] ||= {}
44
48
  hash[id] = Time.now.to_i
45
49
  end
46
50
  end
@@ -1,6 +1,6 @@
1
1
  module Warden
2
2
  module GitHub
3
- class User < Struct.new(:attribs, :token, :browser_session_id)
3
+ class User < Struct.new(:attribs, :token, :browser_session_id, :memberships)
4
4
  ATTRIBUTES = %w[id login name gravatar_id avatar_url email company site_admin].freeze
5
5
 
6
6
  def self.load(access_token, browser_session_id = nil)
@@ -32,7 +32,7 @@ module Warden
32
32
  #
33
33
  # Returns: true if the user is publicized as an org member
34
34
  def organization_public_member?(org_name)
35
- memberships.fetch_membership(:org_pub, org_name) do
35
+ membership_cache.fetch_membership(:org_pub, org_name) do
36
36
  api.organization_public_member?(org_name, login)
37
37
  end
38
38
  end
@@ -46,7 +46,7 @@ module Warden
46
46
  #
47
47
  # Returns: true if the user has access, false otherwise
48
48
  def organization_member?(org_name)
49
- memberships.fetch_membership(:org, org_name) do
49
+ membership_cache.fetch_membership(:org, org_name) do
50
50
  api.organization_member?(org_name, login)
51
51
  end
52
52
  end
@@ -57,7 +57,7 @@ module Warden
57
57
  #
58
58
  # Returns: true if the user has access, false otherwise
59
59
  def team_member?(team_id)
60
- memberships.fetch_membership(:team, team_id) do
60
+ membership_cache.fetch_membership(:team, team_id) do
61
61
  api.team_member?(team_id, login)
62
62
  end
63
63
  end
@@ -105,8 +105,9 @@ module Warden
105
105
 
106
106
  private
107
107
 
108
- def memberships
109
- attribs['member'] ||= MembershipCache.new
108
+ def membership_cache
109
+ self.memberships ||= {}
110
+ @membership_cache ||= MembershipCache.new(memberships)
110
111
  end
111
112
  end
112
113
  end
@@ -1,5 +1,5 @@
1
1
  module Warden
2
2
  module GitHub
3
- VERSION = "1.2.0"
3
+ VERSION = "1.3.0"
4
4
  end
5
5
  end
@@ -1,19 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Warden::GitHub::MembershipCache do
4
- subject(:cache) do
5
- described_class.new
6
- end
7
-
8
4
  describe '#fetch_membership' do
9
5
  it 'returns false by default' do
6
+ cache = described_class.new({})
10
7
  cache.fetch_membership('foo', 'bar').should be_falsey
11
8
  end
12
9
 
13
10
  context 'when cache valid' do
14
- before do
15
- cache['foo'] = {}
16
- cache['foo']['bar'] = Time.now.to_i - described_class::CACHE_TIMEOUT + 5
11
+ let(:cache) do
12
+ described_class.new('foo' => {
13
+ 'bar' => Time.now.to_i - described_class::CACHE_TIMEOUT + 5
14
+ })
17
15
  end
18
16
 
19
17
  it 'returns true' do
@@ -27,9 +25,10 @@ describe Warden::GitHub::MembershipCache do
27
25
  end
28
26
 
29
27
  context 'when cache expired' do
30
- before do
31
- cache['foo'] = {}
32
- cache['foo']['bar'] = Time.now.to_i - described_class::CACHE_TIMEOUT - 5
28
+ let(:cache) do
29
+ described_class.new('foo' => {
30
+ 'bar' => Time.now.to_i - described_class::CACHE_TIMEOUT - 5
31
+ })
33
32
  end
34
33
 
35
34
  context 'when no block given' do
@@ -38,11 +37,6 @@ describe Warden::GitHub::MembershipCache do
38
37
  end
39
38
  end
40
39
 
41
- it 'deletes the cached value' do
42
- cache.fetch_membership('foo', 'bar')
43
- cache['foo'].should_not have_key('bar')
44
- end
45
-
46
40
  it 'invokes the block' do
47
41
  expect { |b| cache.fetch_membership('foo', 'bar', &b) }.
48
42
  to yield_control
@@ -50,13 +44,40 @@ describe Warden::GitHub::MembershipCache do
50
44
  end
51
45
 
52
46
  it 'caches the value when block returns true' do
47
+ cache = described_class.new({})
53
48
  cache.fetch_membership('foo', 'bar') { true }
54
49
  cache.fetch_membership('foo', 'bar').should be_truthy
55
50
  end
56
51
 
57
52
  it 'does not cache the value when block returns false' do
53
+ cache = described_class.new({})
58
54
  cache.fetch_membership('foo', 'bar') { false }
59
55
  cache.fetch_membership('foo', 'bar').should be_falsey
60
56
  end
61
57
  end
58
+
59
+ it 'uses the hash that is passed to the initializer as storage' do
60
+ time = Time.now.to_i
61
+ hash = {
62
+ 'foo' => {
63
+ 'valid' => time,
64
+ 'timedout' => time - described_class::CACHE_TIMEOUT - 5
65
+ }
66
+ }
67
+ cache = described_class.new(hash)
68
+
69
+ # Verify that existing data in the hash is used:
70
+ expect(cache.fetch_membership('foo', 'valid')).to be_true
71
+ expect(cache.fetch_membership('foo', 'timedout')).to be_false
72
+
73
+ # Add new data to the hash:
74
+ cache.fetch_membership('foo', 'new') { true }
75
+ cache.fetch_membership('foo', 'false') { false }
76
+
77
+ # Verify the final hash state:
78
+ expect(hash).to eq('foo' => {
79
+ 'valid' => time,
80
+ 'new' => time
81
+ })
82
+ end
62
83
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warden-github
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Corey Donohoe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-18 00:00:00.000000000 Z
11
+ date: 2016-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: warden
@@ -250,7 +250,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
250
250
  version: '0'
251
251
  requirements: []
252
252
  rubyforge_project: warden-github
253
- rubygems_version: 2.2.2
253
+ rubygems_version: 2.5.1
254
254
  signing_key:
255
255
  specification_version: 4
256
256
  summary: A warden strategy for easy oauth integration with github