tokens 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ class User < ActiveRecord::Base
2
+ tokenizable
3
+ end
4
+
5
+ class Post < ActiveRecord::Base
6
+ tokenizable
7
+ end
@@ -1,13 +1,5 @@
1
1
  require "spec_helper"
2
2
 
3
- class User < ActiveRecord::Base
4
- tokenizable
5
- end
6
-
7
- class Post < ActiveRecord::Base
8
- tokenizable
9
- end
10
-
11
3
  describe Tokens do
12
4
  before do
13
5
  User.delete_all
@@ -19,7 +11,69 @@ describe Tokens do
19
11
  @expire = 3.days.from_now
20
12
  end
21
13
 
22
- describe "- token" do
14
+ it "should have tokens association" do
15
+ expect { @user.tokens }.to_not raise_error
16
+ end
17
+
18
+ it "should remove all expired tokens" do
19
+ expect {
20
+ %w(uid activation_code reset_password_code).each do |name|
21
+ @user.add_token(name, :expires_at => 3.days.ago)
22
+ end
23
+ }.to change(Token, :count).by(3)
24
+
25
+ Token.clean.should == 3
26
+ end
27
+
28
+ it "should generate token without saving it" do
29
+ expect {
30
+ User.generate_token(32)
31
+ }.to_not change(Token, :count)
32
+ end
33
+
34
+ it "should generate token with custom size" do
35
+ User.generate_token(8).size.should == 8
36
+ end
37
+
38
+ it "should alias token method" do
39
+ token = @user.add_token(:uid)
40
+ token.hash.should == token.token
41
+ end
42
+
43
+ it "should find user by token" do
44
+ token = @user.add_token(:uid)
45
+ User.find_by_token(:uid, token.hash).should == @user
46
+ end
47
+
48
+ it "should return user by its valid token without expiration time" do
49
+ token = @user.add_token(:uid)
50
+ User.find_by_valid_token(:uid, token.hash).should == @user
51
+ end
52
+
53
+ it "should return user by its valid token with expiration time" do
54
+ token = @user.add_token(:uid, :expires_at => @expire)
55
+ User.find_by_valid_token(:uid, token.hash).should == @user
56
+ end
57
+
58
+ it "should find token using class method with one argument (hash only)" do
59
+ token = @user.add_token(:uid)
60
+ User.find_token(:name => :uid, :token => token.hash).should == token
61
+ end
62
+
63
+ it "should not conflict with other models" do
64
+ user_token = @user.add_token(:uid)
65
+ post_token = @post.add_token(:uid)
66
+
67
+ User.find_token(post_token.to_s).should == nil
68
+ User.find_token(:name => :uid)
69
+ end
70
+
71
+ it "to_s should return hash" do
72
+ token = @user.add_token(:uid)
73
+ token.to_s.should == token.hash
74
+ end
75
+
76
+ describe Token do
23
77
  it "should be created" do
24
78
  expect { @user.add_token(:uid) }.to change(Token, :count)
25
79
  end
@@ -37,6 +91,12 @@ describe Tokens do
37
91
  @user.add_token(:uid, :data => "some value").data.should == "some value"
38
92
  end
39
93
 
94
+ it "should serialize data" do
95
+ token = @user.add_token(:uid, :data => {:name => "John Doe"})
96
+ token.reload
97
+ token.data.should == {:name => "John Doe"}
98
+ end
99
+
40
100
  it "should be created with custom size" do
41
101
  @user.add_token(:uid, :size => 6).hash.size.should == 6
42
102
  end
@@ -99,66 +159,4 @@ describe Tokens do
99
159
  @user.tokens.find_all_by_name("uid").size.should == 1
100
160
  end
101
161
  end
102
-
103
- it "should have tokens association" do
104
- expect { @user.tokens }.to_not raise_error
105
- end
106
-
107
- it "should remove all expired tokens" do
108
- expect {
109
- %w(uid activation_code reset_password_code).each do |name|
110
- @user.add_token(name, :expires_at => 3.days.ago)
111
- end
112
- }.to change(Token, :count).by(3)
113
-
114
- Token.clean.should == 3
115
- end
116
-
117
- it "should generate token without saving it" do
118
- expect {
119
- User.generate_token(32)
120
- }.to_not change(Token, :count)
121
- end
122
-
123
- it "should generate token with custom size" do
124
- User.generate_token(8).size.should == 8
125
- end
126
-
127
- it "should alias token method" do
128
- token = @user.add_token(:uid)
129
- token.hash.should == token.token
130
- end
131
-
132
- it "should find user by token" do
133
- token = @user.add_token(:uid)
134
- User.find_by_token(:uid, token.hash).should == @user
135
- end
136
-
137
- it "should return user by its valid token without expiration time" do
138
- token = @user.add_token(:uid)
139
- User.find_by_valid_token(:uid, token.hash).should == @user
140
- end
141
-
142
- it "should return user by its valid token with expiration time" do
143
- token = @user.add_token(:uid, :expires_at => @expire)
144
- User.find_by_valid_token(:uid, token.hash).should == @user
145
- end
146
-
147
- it "should find token using class method with one argument (hash only)" do
148
- token = @user.add_token(:uid)
149
- User.find_token(:name => :uid, :token => token.hash).should == token
150
- end
151
-
152
- it "should not conflict with other models" do
153
- user_token = @user.add_token(:uid)
154
- post_token = @post.add_token(:uid)
155
-
156
- User.find_token(post_token.to_s).should == nil
157
- User.find_token(:name => :uid)
158
- end
159
-
160
- it "to_s should return hash" do
161
- token = @user.add_token(:uid)
162
- token.to_s.should == token.hash
163
- end
164
162
  end
@@ -0,0 +1,22 @@
1
+ class CreateTokens < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :tokens do |t|
4
+ t.integer :tokenizable_id, :null => false
5
+ t.string :tokenizable_type, :name, :null => false
6
+ t.string :token, :limit => 40, :null => false
7
+ t.text :data, :null => true
8
+ t.datetime :expires_at, :null => true
9
+ t.datetime :created_at
10
+ end
11
+
12
+ add_index :tokens, :tokenizable_type
13
+ add_index :tokens, :tokenizable_id
14
+ add_index :tokens, [:tokenizable_type, :tokenizable_id]
15
+ add_index :tokens, :token
16
+ add_index :tokens, :expires_at
17
+ end
18
+
19
+ def self.down
20
+ drop_table :tokens
21
+ end
22
+ end
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{tokens}
8
+ s.version = "0.2.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nando Vieira"]
12
+ s.date = %q{2010-10-06}
13
+ s.email = %q{fnando.vieira@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "README.rdoc"
16
+ ]
17
+ s.files = [
18
+ "Gemfile",
19
+ "Gemfile.lock",
20
+ "README.rdoc",
21
+ "Rakefile",
22
+ "lib/tokens.rb",
23
+ "lib/tokens/active_record.rb",
24
+ "lib/tokens/generator.rb",
25
+ "lib/tokens/railtie.rb",
26
+ "lib/tokens/token.rb",
27
+ "lib/tokens/version.rb",
28
+ "spec/schema.rb",
29
+ "spec/spec_helper.rb",
30
+ "spec/support/config/boot.rb",
31
+ "spec/support/config/database.yml",
32
+ "spec/support/log/test.log",
33
+ "spec/support/models.rb",
34
+ "spec/tokens_spec.rb",
35
+ "templates/tokens.rb",
36
+ "tokens.gemspec"
37
+ ]
38
+ s.homepage = %q{http://github.com/fnando/tokens}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.7}
42
+ s.summary = %q{Generate named tokens on your ActiveRecord models.}
43
+ s.test_files = [
44
+ "spec/schema.rb",
45
+ "spec/spec_helper.rb",
46
+ "spec/support/config/boot.rb",
47
+ "spec/support/models.rb",
48
+ "spec/tokens_spec.rb"
49
+ ]
50
+
51
+ if s.respond_to? :specification_version then
52
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
53
+ s.specification_version = 3
54
+
55
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
56
+ else
57
+ end
58
+ else
59
+ end
60
+ end
61
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nando Vieira
@@ -27,14 +27,25 @@ extensions: []
27
27
  extra_rdoc_files:
28
28
  - README.rdoc
29
29
  files:
30
+ - Gemfile
31
+ - Gemfile.lock
30
32
  - README.rdoc
31
33
  - Rakefile
32
34
  - lib/tokens.rb
35
+ - lib/tokens/active_record.rb
36
+ - lib/tokens/generator.rb
37
+ - lib/tokens/railtie.rb
33
38
  - lib/tokens/token.rb
34
39
  - lib/tokens/version.rb
35
40
  - spec/schema.rb
36
41
  - spec/spec_helper.rb
42
+ - spec/support/config/boot.rb
43
+ - spec/support/config/database.yml
44
+ - spec/support/log/test.log
45
+ - spec/support/models.rb
37
46
  - spec/tokens_spec.rb
47
+ - templates/tokens.rb
48
+ - tokens.gemspec
38
49
  has_rdoc: true
39
50
  homepage: http://github.com/fnando/tokens
40
51
  licenses: []
@@ -70,4 +81,6 @@ summary: Generate named tokens on your ActiveRecord models.
70
81
  test_files:
71
82
  - spec/schema.rb
72
83
  - spec/spec_helper.rb
84
+ - spec/support/config/boot.rb
85
+ - spec/support/models.rb
73
86
  - spec/tokens_spec.rb