wristband 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/.DS_Store +0 -0
  2. data/Gemfile +14 -0
  3. data/Gemfile.lock +88 -0
  4. data/README.md +113 -0
  5. data/Rakefile +21 -0
  6. data/VERSION +1 -0
  7. data/app/controllers/application_controller.rb +10 -0
  8. data/app/controllers/sessions_controller.rb +34 -0
  9. data/app/controllers/users_controller.rb +34 -0
  10. data/app/mailers/user_mailer.rb +33 -0
  11. data/app/models/session_user.rb +59 -0
  12. data/app/models/user.rb +52 -0
  13. data/app/views/layouts/application.html.erb +14 -0
  14. data/app/views/sessions/new.html.haml +15 -0
  15. data/app/views/user_mailer/email_verification.text.html.rhtml +7 -0
  16. data/app/views/user_mailer/email_verification.text.plain.rhtml +9 -0
  17. data/app/views/user_mailer/forgot_password.text.html.rhtml +10 -0
  18. data/app/views/user_mailer/forgot_password.text.plain.rhtml +10 -0
  19. data/app/views/users/show.html.haml +6 -0
  20. data/config/application.rb +15 -0
  21. data/config/boot.rb +13 -0
  22. data/config/database.yml +16 -0
  23. data/config/environment.rb +5 -0
  24. data/config/environments/development.rb +26 -0
  25. data/config/environments/production.rb +49 -0
  26. data/config/environments/test.rb +35 -0
  27. data/config/initializers/wristband.rb +1 -0
  28. data/config/locales/en.yml +5 -0
  29. data/config/routes.rb +11 -0
  30. data/config.ru +4 -0
  31. data/db/migrate/01_create_users_table.rb +26 -0
  32. data/db/schema.rb +27 -0
  33. data/lib/generators/wristband/.DS_Store +0 -0
  34. data/lib/generators/wristband/wristband_generator.rb +56 -0
  35. data/lib/wristband/application_extensions.rb +67 -0
  36. data/lib/wristband/authority_check.rb +160 -0
  37. data/lib/wristband/support.rb +28 -0
  38. data/lib/wristband/user_extensions.rb +89 -0
  39. data/lib/wristband.rb +86 -0
  40. data/public/robots.txt +5 -0
  41. data/script/rails +6 -0
  42. data/test/dummy/user.rb +13 -0
  43. data/test/test_helper.rb +78 -0
  44. data/test/unit/has_authorities_test.rb +86 -0
  45. data/test/unit/session_user_test.rb +8 -0
  46. data/test/unit/user_test.rb +105 -0
  47. data/wristband.gemspec +107 -0
  48. metadata +209 -0
@@ -0,0 +1,78 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ require File.expand_path('../../config/environment', __FILE__)
3
+ require 'rails/test_help'
4
+
5
+ class ActiveSupport::TestCase
6
+ self.use_transactional_fixtures = false
7
+ self.use_instantiated_fixtures = false
8
+
9
+ def data_path(name)
10
+ File.expand_path(File.join('test', 'data', name), Rails.root)
11
+ end
12
+
13
+ def assert_created(model)
14
+ assert model
15
+ assert_equal [ ], model.errors.full_messages
16
+ assert model.valid?
17
+ assert !model.new_record?
18
+ end
19
+
20
+ def assert_not_created(model)
21
+ assert model
22
+ assert model.new_record?
23
+ end
24
+
25
+ def assert_errors_on(model, *attrs)
26
+ found_attrs = [ ]
27
+
28
+ model.errors.each do |attr, error|
29
+ found_attrs << attr
30
+ end
31
+
32
+ assert_equal attrs.flatten.collect(&:to_s).sort, found_attrs.uniq.collect(&:to_s).sort
33
+ end
34
+
35
+ def assert_mapping(map, &block)
36
+ result_map = map.inject({ }) do |h, (k,v)|
37
+ h[k] = yield(k)
38
+ h
39
+ end
40
+
41
+ differences = result_map.inject([ ]) do |a, (k,v)|
42
+ if (v != map[k])
43
+ a << k
44
+ end
45
+
46
+ a
47
+ end
48
+
49
+ assert_equal map, result_map, "Difference: #{map.slice(*differences).inspect} vs #{result_map.slice(*differences).inspect}"
50
+ end
51
+
52
+ def login_as(obj)
53
+ case obj
54
+ when String
55
+ user = a User
56
+ user.update_attribute(:role, obj)
57
+ when Symbol
58
+ user = a User
59
+ user.update_attribute(:role, obj.to_s)
60
+ when User
61
+ user = obj
62
+ else
63
+ raise "Please specifuy either a User or a role: #{obj.class.to_s}"
64
+ end
65
+
66
+ session[:user_id] = user.id
67
+
68
+ token = Wristband::Support.encrypt_with_salt(user.id.to_s, Time.now.to_f.to_s)
69
+
70
+ cookies[:login_token] = {
71
+ :value => token,
72
+ :expires => 2.weeks.from_now.utc
73
+ }
74
+
75
+ user
76
+ end
77
+
78
+ end
@@ -0,0 +1,86 @@
1
+ require 'test_helper'
2
+
3
+ class NewUserAuthorityCheck < AuthorityCheck
4
+ before_check :is_admin?
5
+
6
+ def is_admin?
7
+ unless (@user.email.match(/^scott/i))
8
+ fail!("Only scott can be an admin.")
9
+ else
10
+ allow!
11
+ end
12
+ end
13
+
14
+ def wear_shoes?
15
+ unless (@user.email.match(/^s/i))
16
+ fail!("Only people with emails that start with 'S' can wear shoes.")
17
+ end
18
+ end
19
+
20
+ def walk_outside?
21
+ wear_shoes?
22
+ unless (@user.email.match(/^j/i))
23
+ fail!("Only people with emails that start with 'J' or 'S' can walk outside.")
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ class NewUser < ActiveRecord::Base
30
+ def self.columns() @columns ||= []; end
31
+
32
+ def self.column(name, sql_type = nil, default = nil, null = true)
33
+ columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
34
+ end
35
+
36
+ column :email, :string
37
+ column :to_email, :string
38
+ column :article_id, :integer
39
+ column :message, :text
40
+ column :email
41
+ column :email_validation_key
42
+ column :validated_at
43
+ column :password_crypt
44
+ column :password_salt
45
+ column :remember_token
46
+ column :role
47
+
48
+ wristband :has_authorities => true
49
+ end
50
+
51
+
52
+ class HasAuthoritiesTest < ActiveSupport::TestCase
53
+
54
+ def test_has_authority_to_with_fail
55
+ scott = NewUser.new(
56
+ :email => 'scott@example.com',
57
+ :password => 'tester',
58
+ :password_confirmation => 'tester',
59
+ :role => :admin
60
+ )
61
+ jack = NewUser.new(
62
+ :email => 'jack@example.com',
63
+ :password => 'tester',
64
+ :password_confirmation => 'tester',
65
+ :role => :admin
66
+ )
67
+ oleg = NewUser.new(
68
+ :email => 'oleg@example.com',
69
+ :password => 'tester',
70
+ :password_confirmation => 'tester',
71
+ :role => :admin
72
+ )
73
+
74
+
75
+ assert scott.has_authority_to?(:wear_shoes)
76
+ assert !jack.has_authority_to?(:wear_shoes)
77
+ assert_equal jack.has_objections_to?(:wear_shoes), ["Only scott can be an admin.", "Only people with emails that start with 'S' can wear shoes."]
78
+
79
+
80
+ assert scott.has_authority_to?(:walk_outside)
81
+ assert !jack.has_authority_to?(:walk_outside)
82
+ assert_equal jack.has_objections_to?(:walk_outside), ["Only scott can be an admin.", "Only people with emails that start with 'S' can wear shoes."]
83
+ assert !oleg.has_authority_to?(:walk_outside)
84
+ assert_equal oleg.has_objections_to?(:walk_outside), ["Only scott can be an admin.", "Only people with emails that start with 'S' can wear shoes.", "Only people with emails that start with 'J' or 'S' can walk outside."]
85
+ end
86
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class SessionUserTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,105 @@
1
+ require 'test_helper'
2
+
3
+ class UserTest < ActiveSupport::TestCase
4
+
5
+ def test_create_defaults
6
+ user = User.new(
7
+ :email => 'user@example.com',
8
+ :password => 'tester',
9
+ :password_confirmation => 'tester'
10
+ )
11
+ user.role = :admin
12
+ user.save
13
+ assert_created user
14
+ end
15
+
16
+ def test_create_requirements
17
+ user = User.create
18
+
19
+ assert_errors_on user, :email, :password, :role
20
+ assert user.errors[:email].include?("Please enter your email address")
21
+ assert user.errors[:email].include?("The email address you entered is not valid")
22
+ assert user.errors[:email].include?("The email address you entered is to short")
23
+ assert user.errors[:password].include?("Please choose a password")
24
+ assert user.errors[:password].include?("The password you entered is too short (minimum is 4 characters)")
25
+ assert user.errors[:role].include?("can't be blank")
26
+ assert user.errors[:role].include?("is not included in the list")
27
+ end
28
+
29
+ def test_create_dummy
30
+ user = a User
31
+ assert_created user
32
+ end
33
+
34
+ def test_user_instance_methods
35
+ @user = a User
36
+ %w{
37
+ has_authority_to?
38
+ has_objections_to?
39
+ initialize_salt
40
+ initialize_token
41
+ encrypt_password
42
+ password_match?
43
+ password_crypted?
44
+ password_crypt=
45
+ is_admin?
46
+ is_regular_user?
47
+ }.each do |method|
48
+ assert @user.respond_to?(method), "On '#{method}' method"
49
+ end
50
+ end
51
+
52
+ def test_user_class_methods
53
+ @user = a User
54
+ %w{
55
+ authenticate
56
+ execute_authentication_chain
57
+ verify_email!
58
+ wristband
59
+ }.each do |method|
60
+ assert User.respond_to?(method), "On '#{method}' method"
61
+ end
62
+ end
63
+
64
+ def test_user_class_private_methods
65
+ %w{
66
+ random_string
67
+ encrypt_with_salt
68
+ random_salt
69
+ }.each do |method|
70
+ assert User.private_methods.include?(method), "On '#{method}' method"
71
+ end
72
+ end
73
+
74
+ def test_assigned_options
75
+ assert_equal User.wristband[:login_with_fields], [:email]
76
+ assert_equal User.wristband[:before_authentication_chain], []
77
+ assert_equal User.wristband[:after_authentication_chain], []
78
+ assert_equal User.wristband[:password_column], :password_crypt
79
+ assert_equal User.wristband[:roles], [:admin, :regular_user]
80
+ end
81
+
82
+ def test_authentication_by_email
83
+ @user = a User
84
+ assert_equal @user, User.authenticate(@user.email, 'passpass')
85
+ end
86
+
87
+ def test_authentication_fails
88
+ @user = a User
89
+ assert_nil User.authenticate('-bugus-', 'passpass')
90
+ assert_nil User.authenticate(@user.email, '-bugus-')
91
+ end
92
+
93
+ def test_password_match
94
+ @user = a User
95
+ assert @user.password_match?('passpass')
96
+ end
97
+
98
+ def test_user_roles
99
+ @user = a User
100
+ @user.update_attribute(:role, :regular_user)
101
+ assert @user.is_regular_user?
102
+ @user.update_attribute(:role, :admin)
103
+ assert @user.is_admin?
104
+ end
105
+ end
data/wristband.gemspec ADDED
@@ -0,0 +1,107 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{wristband}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jack Neto", "The Working Group Inc"]
12
+ s.date = %q{2011-01-23}
13
+ s.description = %q{Provides a starting point for user authentication}
14
+ s.email = %q{jack@theworkinggroup.ca}
15
+ s.extra_rdoc_files = [
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ ".DS_Store",
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "README.md",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "app/controllers/application_controller.rb",
26
+ "app/controllers/sessions_controller.rb",
27
+ "app/controllers/users_controller.rb",
28
+ "app/mailers/user_mailer.rb",
29
+ "app/models/session_user.rb",
30
+ "app/models/user.rb",
31
+ "app/views/layouts/application.html.erb",
32
+ "app/views/sessions/new.html.haml",
33
+ "app/views/user_mailer/email_verification.text.html.rhtml",
34
+ "app/views/user_mailer/email_verification.text.plain.rhtml",
35
+ "app/views/user_mailer/forgot_password.text.html.rhtml",
36
+ "app/views/user_mailer/forgot_password.text.plain.rhtml",
37
+ "app/views/users/show.html.haml",
38
+ "config.ru",
39
+ "config/application.rb",
40
+ "config/boot.rb",
41
+ "config/database.yml",
42
+ "config/environment.rb",
43
+ "config/environments/development.rb",
44
+ "config/environments/production.rb",
45
+ "config/environments/test.rb",
46
+ "config/initializers/wristband.rb",
47
+ "config/locales/en.yml",
48
+ "config/routes.rb",
49
+ "db/migrate/01_create_users_table.rb",
50
+ "db/schema.rb",
51
+ "lib/generators/wristband/.DS_Store",
52
+ "lib/generators/wristband/wristband_generator.rb",
53
+ "lib/wristband.rb",
54
+ "lib/wristband/application_extensions.rb",
55
+ "lib/wristband/authority_check.rb",
56
+ "lib/wristband/support.rb",
57
+ "lib/wristband/user_extensions.rb",
58
+ "public/robots.txt",
59
+ "script/rails",
60
+ "test/dummy/user.rb",
61
+ "test/test_helper.rb",
62
+ "test/unit/has_authorities_test.rb",
63
+ "test/unit/session_user_test.rb",
64
+ "test/unit/user_test.rb",
65
+ "wristband.gemspec"
66
+ ]
67
+ s.homepage = %q{http://github.com/twg/wristband}
68
+ s.require_paths = ["lib"]
69
+ s.rubygems_version = %q{1.3.7}
70
+ s.summary = %q{An authentication engine}
71
+ s.test_files = [
72
+ "test/dummy/user.rb",
73
+ "test/test_helper.rb",
74
+ "test/unit/has_authorities_test.rb",
75
+ "test/unit/session_user_test.rb",
76
+ "test/unit/user_test.rb"
77
+ ]
78
+
79
+ if s.respond_to? :specification_version then
80
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
81
+ s.specification_version = 3
82
+
83
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
84
+ s.add_runtime_dependency(%q<rails>, ["= 3.0.3"])
85
+ s.add_runtime_dependency(%q<haml>, ["= 3.0.25"])
86
+ s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
87
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
88
+ s.add_runtime_dependency(%q<rails>, [">= 3.0.3"])
89
+ s.add_runtime_dependency(%q<haml>, [">= 3.0.25"])
90
+ else
91
+ s.add_dependency(%q<rails>, ["= 3.0.3"])
92
+ s.add_dependency(%q<haml>, ["= 3.0.25"])
93
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
94
+ s.add_dependency(%q<jeweler>, [">= 0"])
95
+ s.add_dependency(%q<rails>, [">= 3.0.3"])
96
+ s.add_dependency(%q<haml>, [">= 3.0.25"])
97
+ end
98
+ else
99
+ s.add_dependency(%q<rails>, ["= 3.0.3"])
100
+ s.add_dependency(%q<haml>, ["= 3.0.25"])
101
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
102
+ s.add_dependency(%q<jeweler>, [">= 0"])
103
+ s.add_dependency(%q<rails>, [">= 3.0.3"])
104
+ s.add_dependency(%q<haml>, [">= 3.0.25"])
105
+ end
106
+ end
107
+
metadata ADDED
@@ -0,0 +1,209 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wristband
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Jack Neto
14
+ - The Working Group Inc
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-01-23 00:00:00 -05:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - "="
27
+ - !ruby/object:Gem::Version
28
+ hash: 1
29
+ segments:
30
+ - 3
31
+ - 0
32
+ - 3
33
+ version: 3.0.3
34
+ type: :runtime
35
+ name: rails
36
+ prerelease: false
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - "="
43
+ - !ruby/object:Gem::Version
44
+ hash: 53
45
+ segments:
46
+ - 3
47
+ - 0
48
+ - 25
49
+ version: 3.0.25
50
+ type: :runtime
51
+ name: haml
52
+ prerelease: false
53
+ version_requirements: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ type: :development
65
+ name: sqlite3-ruby
66
+ prerelease: false
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ type: :development
79
+ name: jeweler
80
+ prerelease: false
81
+ version_requirements: *id004
82
+ - !ruby/object:Gem::Dependency
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 1
89
+ segments:
90
+ - 3
91
+ - 0
92
+ - 3
93
+ version: 3.0.3
94
+ type: :runtime
95
+ name: rails
96
+ prerelease: false
97
+ version_requirements: *id005
98
+ - !ruby/object:Gem::Dependency
99
+ requirement: &id006 !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 53
105
+ segments:
106
+ - 3
107
+ - 0
108
+ - 25
109
+ version: 3.0.25
110
+ type: :runtime
111
+ name: haml
112
+ prerelease: false
113
+ version_requirements: *id006
114
+ description: Provides a starting point for user authentication
115
+ email: jack@theworkinggroup.ca
116
+ executables: []
117
+
118
+ extensions: []
119
+
120
+ extra_rdoc_files:
121
+ - README.md
122
+ files:
123
+ - .DS_Store
124
+ - Gemfile
125
+ - Gemfile.lock
126
+ - README.md
127
+ - Rakefile
128
+ - VERSION
129
+ - app/controllers/application_controller.rb
130
+ - app/controllers/sessions_controller.rb
131
+ - app/controllers/users_controller.rb
132
+ - app/mailers/user_mailer.rb
133
+ - app/models/session_user.rb
134
+ - app/models/user.rb
135
+ - app/views/layouts/application.html.erb
136
+ - app/views/sessions/new.html.haml
137
+ - app/views/user_mailer/email_verification.text.html.rhtml
138
+ - app/views/user_mailer/email_verification.text.plain.rhtml
139
+ - app/views/user_mailer/forgot_password.text.html.rhtml
140
+ - app/views/user_mailer/forgot_password.text.plain.rhtml
141
+ - app/views/users/show.html.haml
142
+ - config.ru
143
+ - config/application.rb
144
+ - config/boot.rb
145
+ - config/database.yml
146
+ - config/environment.rb
147
+ - config/environments/development.rb
148
+ - config/environments/production.rb
149
+ - config/environments/test.rb
150
+ - config/initializers/wristband.rb
151
+ - config/locales/en.yml
152
+ - config/routes.rb
153
+ - db/migrate/01_create_users_table.rb
154
+ - db/schema.rb
155
+ - lib/generators/wristband/.DS_Store
156
+ - lib/generators/wristband/wristband_generator.rb
157
+ - lib/wristband.rb
158
+ - lib/wristband/application_extensions.rb
159
+ - lib/wristband/authority_check.rb
160
+ - lib/wristband/support.rb
161
+ - lib/wristband/user_extensions.rb
162
+ - public/robots.txt
163
+ - script/rails
164
+ - test/dummy/user.rb
165
+ - test/test_helper.rb
166
+ - test/unit/has_authorities_test.rb
167
+ - test/unit/session_user_test.rb
168
+ - test/unit/user_test.rb
169
+ - wristband.gemspec
170
+ has_rdoc: true
171
+ homepage: http://github.com/twg/wristband
172
+ licenses: []
173
+
174
+ post_install_message:
175
+ rdoc_options: []
176
+
177
+ require_paths:
178
+ - lib
179
+ required_ruby_version: !ruby/object:Gem::Requirement
180
+ none: false
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ hash: 3
185
+ segments:
186
+ - 0
187
+ version: "0"
188
+ required_rubygems_version: !ruby/object:Gem::Requirement
189
+ none: false
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ hash: 3
194
+ segments:
195
+ - 0
196
+ version: "0"
197
+ requirements: []
198
+
199
+ rubyforge_project:
200
+ rubygems_version: 1.3.7
201
+ signing_key:
202
+ specification_version: 3
203
+ summary: An authentication engine
204
+ test_files:
205
+ - test/dummy/user.rb
206
+ - test/test_helper.rb
207
+ - test/unit/has_authorities_test.rb
208
+ - test/unit/session_user_test.rb
209
+ - test/unit/user_test.rb