challah 1.1.1 → 1.2.0.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -1
  3. data/README.md +3 -3
  4. data/app/models/authorization.rb +2 -2
  5. data/app/models/user.rb +2 -2
  6. data/lib/challah.rb +60 -53
  7. data/lib/challah/active_record_extensions.rb +23 -0
  8. data/lib/challah/{authorization.rb → concerns/authorizeable.rb} +14 -19
  9. data/lib/challah/concerns/user/attributeable.rb +73 -0
  10. data/lib/challah/{user/authentication.rb → concerns/user/authenticateable.rb} +2 -2
  11. data/lib/challah/concerns/user/authorizable.rb +32 -0
  12. data/lib/challah/concerns/user/findable.rb +45 -0
  13. data/lib/challah/{user/password.rb → concerns/user/passwordable.rb} +2 -2
  14. data/lib/challah/{user/providers.rb → concerns/user/provideable.rb} +46 -6
  15. data/lib/challah/concerns/user/validateable.rb +21 -0
  16. data/lib/challah/concerns/userable.rb +20 -0
  17. data/lib/challah/engine.rb +1 -2
  18. data/lib/challah/test.rb +52 -32
  19. data/lib/challah/version.rb +1 -1
  20. metadata +14 -76
  21. data/lib/challah/user.rb +0 -128
  22. data/lib/challah/user/attributes.rb +0 -28
  23. data/lib/challah/user/finders.rb +0 -31
  24. data/lib/challah/user/reflector.rb +0 -15
  25. data/test/controllers/controller_test.rb +0 -34
  26. data/test/controllers/restrictions_controller_test.rb +0 -95
  27. data/test/controllers/sessions_controller_test.rb +0 -42
  28. data/test/dummy/README.rdoc +0 -28
  29. data/test/dummy/Rakefile +0 -6
  30. data/test/dummy/app/assets/javascripts/application.js +0 -13
  31. data/test/dummy/app/assets/stylesheets/application.css +0 -13
  32. data/test/dummy/app/controllers/application_controller.rb +0 -5
  33. data/test/dummy/app/controllers/restrictions_controller.rb +0 -25
  34. data/test/dummy/app/helpers/application_helper.rb +0 -2
  35. data/test/dummy/app/models/user.rb +0 -15
  36. data/test/dummy/app/models/widget.rb +0 -3
  37. data/test/dummy/app/views/bakery/templates/layouts/sample.erb +0 -1
  38. data/test/dummy/app/views/bakery/templates/partials/sample.html.haml +0 -1
  39. data/test/dummy/app/views/bakery/templates/themes/sample.haml +0 -1
  40. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  41. data/test/dummy/config.ru +0 -4
  42. data/test/dummy/config/application.rb +0 -24
  43. data/test/dummy/config/boot.rb +0 -5
  44. data/test/dummy/config/database.yml +0 -30
  45. data/test/dummy/config/environment.rb +0 -5
  46. data/test/dummy/config/environments/development.rb +0 -29
  47. data/test/dummy/config/environments/production.rb +0 -80
  48. data/test/dummy/config/environments/test.rb +0 -36
  49. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  50. data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  51. data/test/dummy/config/initializers/i18n.rb +0 -1
  52. data/test/dummy/config/initializers/inflections.rb +0 -16
  53. data/test/dummy/config/initializers/mime_types.rb +0 -5
  54. data/test/dummy/config/initializers/secret_token.rb +0 -12
  55. data/test/dummy/config/initializers/session_store.rb +0 -3
  56. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  57. data/test/dummy/config/locales/en.yml +0 -23
  58. data/test/dummy/config/routes.rb +0 -5
  59. data/test/dummy/db/migrate/20140114212939_create_widgets.rb +0 -32
  60. data/test/dummy/db/migrate/20140307205735_create_users.challah_engine.rb +0 -34
  61. data/test/dummy/db/migrate/20140307205736_create_authorizations.challah_engine.rb +0 -21
  62. data/test/dummy/db/schema.rb +0 -81
  63. data/test/dummy/db/test.sqlite3 +0 -0
  64. data/test/dummy/log/test.log +0 -11547
  65. data/test/dummy/public/404.html +0 -58
  66. data/test/dummy/public/422.html +0 -58
  67. data/test/dummy/public/500.html +0 -57
  68. data/test/dummy/public/favicon.ico +0 -0
  69. data/test/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  70. data/test/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  71. data/test/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  72. data/test/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  73. data/test/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  74. data/test/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  75. data/test/factories.rb +0 -8
  76. data/test/models/authorization_test.rb +0 -20
  77. data/test/models/user_test.rb +0 -345
  78. data/test/services/audit_test.rb +0 -107
  79. data/test/services/cookie_store_test.rb +0 -97
  80. data/test/services/encrypter_test.rb +0 -73
  81. data/test/services/plugins_test.rb +0 -65
  82. data/test/services/random_test.rb +0 -22
  83. data/test/services/routes_test.rb +0 -11
  84. data/test/services/session_test.rb +0 -197
  85. data/test/services/signup_test.rb +0 -122
  86. data/test/services/simple_cookie_store_test.rb +0 -122
  87. data/test/support/stubs.rb +0 -88
  88. data/test/test_helper.rb +0 -47
@@ -1,107 +0,0 @@
1
- require 'test_helper'
2
-
3
- class AuditTest < ActiveSupport::TestCase
4
- include ActiveModel::Lint::Tests
5
-
6
- # Use Widget as a fake model to test auditing.
7
- class Widget
8
- include ActiveModel::Conversion
9
- include ActiveModel::Validations
10
- extend ActiveModel::Naming
11
- extend ActiveModel::Callbacks
12
-
13
- define_model_callbacks :create, :update, :save
14
-
15
- include Challah::Audit
16
-
17
- attr_accessor :name, :created_by, :created_at, :updated_by, :updated_at
18
-
19
- def initialize(attributes = {})
20
- attributes.each do |name, value|
21
- send("#{name}=", value)
22
- end
23
-
24
- @attributes = {}
25
- end
26
-
27
- def persisted?
28
- false
29
- end
30
-
31
- def new_record?
32
- !@saved
33
- end
34
-
35
- def save
36
- # Saving doesn't do anything, just a stub
37
- run_callbacks :save do
38
- @saved = true
39
- end
40
-
41
- true
42
- end
43
-
44
- def column_for_attribute(attr_name)
45
- self.respond_to?("#{attr_name}=") ? attr_name : nil
46
- end
47
-
48
- def write_attribute(attr_name, value)
49
- self.send("#{attr_name}=", value)
50
- end
51
-
52
- # Stub for tests
53
- def changed_attributes
54
- {}
55
- end
56
-
57
- # Stubs
58
- def attribute_changed?(attr_name)
59
- false
60
- end
61
- end
62
-
63
- # Runs the ActiveModel::Lint tests against a new Widget instance.
64
- def setup
65
- @model = Widget.new
66
- end
67
-
68
- context "An audited model" do
69
- should "be able to receive a current user" do
70
- user = create(:user)
71
- user_two = create(:user, :first_name => 'User', :last_name => 'Test 2')
72
-
73
- assert_equal 0, @model.current_user_id
74
- assert_equal true, @model.new_record?
75
-
76
- # For a new record, setting current_user should update both attributes
77
- @model.current_user = user
78
- assert_equal user.id, @model.current_user_id
79
-
80
- @model.save
81
-
82
- assert_equal user.id, @model.created_by
83
- assert_equal user.id, @model.updated_by
84
-
85
- assert_equal false, @model.new_record?
86
-
87
- # For an existing record, setting current_user (or current_user_id) should update only updated_by
88
- @model.current_user_id = user_two.id
89
- assert_equal user_two.id, @model.current_user_id
90
-
91
- @model.save
92
-
93
- assert_equal user.id, @model.created_by
94
- assert_equal user_two.id, @model.updated_by
95
- end
96
-
97
- should "be able to clear audit attributes" do
98
- @model.created_by = 1
99
- @model.updated_by = 2
100
-
101
- @model.send(:initialize_dup, nil)
102
-
103
- assert_equal nil, @model.created_by
104
- assert_equal nil, @model.updated_by
105
- end
106
- end
107
- end
@@ -1,97 +0,0 @@
1
- require 'test_helper'
2
-
3
- class CookieStoreTest < ActiveSupport::TestCase
4
- include Challah
5
-
6
- context "The CookieStore class" do
7
- setup do
8
- @user = create(:user)
9
- @request = MockRequest.new
10
- end
11
-
12
- should "save session in a request cookie store" do
13
- assert_equal [], @request.cookies.keys
14
-
15
- session = Session.new(@request)
16
- session.store = CookieStore.new(session)
17
- session.persist = true
18
- session.user = @user
19
- session.save
20
-
21
- assert_equal %w( challah-s challah-v ), @request.cookies.keys.sort
22
- assert_equal "#{@user.persistence_token}@#{@user.id}", @request.cookies['challah-s'][:value]
23
- assert_equal "test.dev", @request.cookies['challah-s'][:domain]
24
-
25
- assert_equal Encrypter.md5("#{@user.persistence_token}@#{@user.id}", @request.user_agent, @request.remote_ip), @request.cookies['challah-v'][:value]
26
- assert_equal "test.dev", @request.cookies['challah-v'][:domain]
27
- end
28
-
29
- should "be able to inspect the store" do
30
- session = Session.new(@request)
31
- session.store = CookieStore.new(session)
32
- session.persist = true
33
- session.user = @user
34
- session.save
35
-
36
- assert session.store.inspect =~ /<CookieStore:(.*?)>/, 'Does not match'
37
- end
38
-
39
- should "read cookies and detect tampered verification cookies" do
40
- assert_equal [], @request.cookies.keys
41
-
42
- session = Session.new(@request)
43
- session.store = CookieStore.new(session)
44
- session.persist = true
45
- session.user = @user
46
- session.save
47
-
48
- validation_cookie_val = Encrypter.md5("#{@user.persistence_token}@#{@user.id}", @request.user_agent, @request.remote_ip)
49
- session_cookie_val = "#{@user.persistence_token}@#{@user.id}"
50
-
51
- assert_equal session_cookie_val, @request.cookies['challah-s'][:value]
52
- assert_equal session_cookie_val, session.store.send(:session_cookie)[:value]
53
- assert_equal validation_cookie_val, @request.cookies['challah-v'][:value]
54
- assert_equal validation_cookie_val, session.store.send(:validation_cookie)[:value]
55
-
56
- session.store.stubs(:validation_cookie).returns(validation_cookie_val)
57
- session.store.stubs(:session_cookie).returns(session_cookie_val)
58
-
59
- session2 = Session.new(@request)
60
- session2.persist = true
61
- session2.store = session.store
62
- session2.read
63
-
64
- assert_equal true, session2.store.send(:existing?)
65
- assert_equal true, session2.valid?
66
- assert_equal @user.id, session2.user_id
67
-
68
- session.store.stubs(:validation_cookie).returns('bad-value')
69
-
70
- session3 = Session.new(@request)
71
- session3.store = session.store
72
- session3.read
73
-
74
- assert_equal false, session3.store.send(:existing?)
75
- assert_equal false, session3.valid?
76
- end
77
-
78
- should "delete sessions from cookies" do
79
- session = Session.new(@request)
80
- session.store = CookieStore.new(session)
81
- session.user = @user
82
- session.persist = true
83
-
84
- session.save
85
-
86
- assert_equal true, session.valid?
87
- assert_equal @user, session.user
88
- assert_equal %w( challah-s challah-v ), @request.cookies.keys.sort
89
-
90
- session.destroy
91
-
92
- assert_equal false, session.valid?
93
- assert_equal nil, session.user
94
- assert_equal [], @request.cookies.keys.sort
95
- end
96
- end
97
- end
@@ -1,73 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TestEncrypter < ActiveSupport::TestCase
4
- include Challah
5
-
6
- context "The encrypter class" do
7
- should "encrypt a string" do
8
- assert_not_nil Encrypter.encrypt("testing 123")
9
- end
10
-
11
- should "use bcrypt to encrypt a string" do
12
- BCrypt::Password.expects(:create).with('testing 123', :cost => 10)
13
-
14
- Encrypter.encrypt("testing 123")
15
-
16
- BCrypt::Password.unstub(:create)
17
- end
18
-
19
- should "compare two encrypted strings quickly" do
20
- pass = Encrypter.encrypt("test A")
21
-
22
- assert_equal true, Encrypter.compare(pass, "test A")
23
- assert_equal false, Encrypter.compare("test A", "test A")
24
- end
25
- end
26
-
27
- context "An encrypter instance" do
28
- setup do
29
- @enc = Encrypter.new
30
- end
31
-
32
- should "encrypt a string" do
33
- pass = @enc.encrypt('testing 123')
34
- assert_not_nil pass
35
- end
36
-
37
- should "encrypt a string at a provided cost" do
38
- @enc.cost = 5
39
-
40
- pass = @enc.encrypt('testing 456')
41
-
42
- bpass = BCrypt::Password.new(pass)
43
-
44
- assert_equal 5, bpass.cost
45
- end
46
-
47
- should "compare a string" do
48
- pass = @enc.encrypt("test A")
49
-
50
- assert_equal true, @enc.compare(pass, "test A")
51
- assert_equal false, @enc.compare("test A", "test A")
52
- end
53
-
54
- should "md5 hash a batch of strings" do
55
- expected = Digest::MD5.hexdigest("str1|str2|str3")
56
- assert_equal expected, @enc.md5('str1', 'str2', 'str3')
57
- end
58
-
59
- should "md5 hash a batch of strings with a different join" do
60
- @enc.joiner = ' - '
61
- expected = Digest::MD5.hexdigest("str1 - str2 - str3")
62
- assert_equal expected, @enc.md5('str1', 'str2', 'str3')
63
- end
64
-
65
- should "hash some strings a given number of times" do
66
- Digest::SHA512.expects(:hexdigest).times(10)
67
-
68
- @enc.hash('hash me')
69
-
70
- Digest::SHA512.unstub(:hexdigest)
71
- end
72
- end
73
- end
@@ -1,65 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TestPlugins < ActiveSupport::TestCase
4
- include Challah
5
-
6
- class PluginContext
7
- def self.setup_active_record
8
- end
9
-
10
- def self.setup_controllers
11
- end
12
- end
13
-
14
- module UserStuff
15
- def set_me_up
16
- include AndMore
17
- end
18
-
19
- module AndMore
20
- def hey_baller
21
- "whatsup"
22
- end
23
- end
24
- end
25
-
26
- context "The plugin loader" do
27
- should "be able to load a plugin and any dependencies" do
28
- assert_equal false, Challah.plugins.has_key?(:tester)
29
-
30
- Challah.register_plugin :tester do
31
- on_load :active_record do
32
- PluginContext.setup_active_record
33
- end
34
-
35
- on_load :action_controller do
36
- PluginContext.setup_controllers
37
- end
38
- end
39
-
40
- assert_equal true, Challah.plugins.has_key?(:tester)
41
- assert_equal 1, Challah.plugins[:tester].active_record.size
42
- assert_equal 1, Challah.plugins[:tester].action_controller.size
43
-
44
- PluginContext.expects(:setup_active_record).once
45
- PluginContext.expects(:setup_controllers).once
46
-
47
- Challah::Engine.setup_active_record!
48
- Challah::Engine.setup_action_controller!
49
- end
50
-
51
- should "be able to include a module when loading Challah::User" do
52
- assert_equal false, ::User.included_modules.include?(UserStuff::AndMore)
53
-
54
- Challah.register_plugin :user_mods do
55
- extend_user UserStuff, :set_me_up
56
- end
57
-
58
- Challah.include_user_plugins!
59
-
60
- assert_equal true, ::User.included_modules.include?(UserStuff::AndMore)
61
-
62
- assert_equal "whatsup", ::User.new().hey_baller
63
- end
64
- end
65
- end
@@ -1,22 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TestRandom < ActiveSupport::TestCase
4
- include Challah
5
-
6
- should "be able to provide a random string from ActiveSupport" do
7
- result = Random.token(10)
8
-
9
- assert_not_nil result
10
- assert_equal 10, result.size
11
- end
12
-
13
- should "be able to provide a random string without ActiveSupport" do
14
- Challah::Random.stubs(:secure_random?).returns(false)
15
- SecureRandom.expects(:hex).never
16
-
17
- result = Challah::Random.token(10)
18
-
19
- assert_not_nil result
20
- assert_equal 10, result.size
21
- end
22
- end
@@ -1,11 +0,0 @@
1
- require 'test_helper'
2
-
3
- class RoutesTest < ActionDispatch::IntegrationTest
4
- context "The routing engine" do
5
- should "have sign-in and sign-out routes" do
6
- assert_routing({ :method => 'get', :path => '/sign-in' }, { :controller => 'sessions', :action => 'new' })
7
- assert_routing({ :method => 'get', :path => '/sign-out' }, { :controller => 'sessions', :action => 'destroy' })
8
- assert_routing({ :method => 'post', :path => '/sign-in' }, { :controller => 'sessions', :action => 'create' })
9
- end
10
- end
11
- end
@@ -1,197 +0,0 @@
1
- require 'test_helper'
2
-
3
- class SessionTest < ActiveSupport::TestCase
4
- include Challah
5
-
6
- class FakeUserModel
7
-
8
- def id
9
- 999
10
- end
11
-
12
- def active?
13
- true
14
- end
15
-
16
- end
17
-
18
- context "An Challah::Session class" do
19
- should "have an inspected view" do
20
- user = create(:user)
21
- session = Session.create(user)
22
-
23
- assert /#<Session/ =~ session.inspect
24
- end
25
-
26
- should "use the test storage method" do
27
- assert_equal TestSessionStore, Challah.options[:storage_class]
28
- end
29
-
30
- should "create a new session instance from a user or id" do
31
- user = create(:user)
32
-
33
- assert_equal true, user.active?
34
-
35
- session = Session.create(user)
36
- assert_equal true, session.valid?
37
- assert_equal user.id, session.user_id
38
- end
39
-
40
- should "create a blank but invalid session for a non-existant or inactive user" do
41
- session = Session.create(999)
42
- assert_equal false, session.valid?
43
- assert_equal nil, session.user_id
44
- end
45
-
46
- should "persist a session" do
47
- user = create(:user)
48
-
49
- session = Session.create(user)
50
- assert_equal true, session.valid?
51
- assert_equal user, session.user
52
-
53
- session.save
54
-
55
- session_two = Session.find
56
- assert_equal true, session_two.valid?
57
- assert_equal user, session_two.user
58
-
59
- Session.destroy
60
-
61
- session_three = Session.find
62
- assert_equal false, session_three.valid?
63
- assert_equal nil, session_three.user
64
- end
65
-
66
- should "accept a user model" do
67
- user = create(:user)
68
- session = Session.create(user, {}, {}, ::User)
69
- assert_equal ::User, session.user_model
70
-
71
- user = FakeUserModel.new
72
- session = Session.create(user, {}, {}, FakeUserModel)
73
- assert_equal FakeUserModel, session.user_model
74
- end
75
- end
76
-
77
- context "A Challah::Session instance" do
78
- should "receive a request and params object" do
79
- session = Session.new(MockRequest.new, { :username => 'test-user', :password => 'test123' })
80
-
81
- assert_not_nil session.request
82
- assert_not_nil session.params
83
-
84
- assert_equal true, session.username?
85
- end
86
-
87
- should "be able to set a username" do
88
- session = Session.new
89
-
90
- assert_equal false, session.username?
91
-
92
- session.username = 'test-user'
93
-
94
- assert_equal true, session.username?
95
-
96
- assert_equal 'test-user', session.username
97
- end
98
-
99
- should "be able to set attributes" do
100
- session = Session.new
101
-
102
- assert_equal Hash.new, session.params
103
-
104
- session.username = 'test-user'
105
- expected = { :username => 'test-user' }
106
- assert_equal expected, session.params
107
- assert_equal true, session.username?
108
-
109
- session.password = 'abc123'
110
- expected = { :username => 'test-user', :password => 'abc123' }
111
- assert_equal expected, session.params
112
- assert_equal true, session.password?
113
-
114
- session.api_key = '122345873847'
115
- expected = { :username => 'test-user', :password => 'abc123', :api_key => '122345873847' }
116
- assert_equal expected, session.params
117
- assert_equal true, session.api_key?
118
-
119
- session.other = true
120
- expected = { :username => 'test-user', :password => 'abc123', :api_key => '122345873847', :other => true }
121
- assert_equal expected, session.params
122
- assert_equal true, session.other?
123
-
124
- assert_raises NoMethodError do
125
- session.no_key
126
- end
127
- end
128
-
129
- should "validate with a password" do
130
- user = build(:user, :username => 'test-user')
131
- user.password!('abc123')
132
- user.save
133
-
134
- ::User.stubs(:find_for_session).returns(user)
135
-
136
- session = Session.new
137
- session.ip = '127.0.0.1'
138
- session.username = 'test-user'
139
- session.password = 'abc123'
140
-
141
- assert_difference 'user.session_count', 1 do
142
- assert_equal true, session.valid?
143
- end
144
-
145
- assert_equal user, session.user
146
- assert_equal user.id, session.user_id
147
- assert_equal true, session.persist?
148
- assert_equal true, session.save
149
-
150
- ::User.unstub(:find_for_session)
151
- end
152
-
153
- should "validate with an api key" do
154
- Challah.options[:api_key_enabled] = true
155
-
156
- user = create(:user, :api_key => '123456abcdefg')
157
-
158
- ::User.stubs(:find_for_session).returns(user)
159
-
160
- session = Session.new
161
- session.ip = '127.0.0.1'
162
- session.key = '123456abcdefg'
163
-
164
- assert_no_difference 'user.session_count' do
165
- assert_equal true, session.valid?
166
- end
167
-
168
- assert_equal user, session.user
169
- assert_equal user.id, session.user_id
170
- assert_equal false, session.persist?
171
- assert_equal false, session.save
172
-
173
- ::User.unstub(:find_for_session)
174
-
175
- Challah.options[:api_key_enabled] = false
176
- end
177
-
178
- should "reject if password is incorrect" do
179
- user = build(:user, :username => 'test-user')
180
- user.password!('abc123')
181
- user.save
182
-
183
- ::User.stubs(:find_for_session).returns(user)
184
-
185
- session = Session.new
186
- session.username = 'test-user'
187
- session.password = 'bad-pass'
188
-
189
- user.expects(:failed_authentication!).once
190
-
191
- assert_equal false, session.valid?
192
- assert_equal nil, session.user
193
-
194
- ::User.unstub(:find_for_session)
195
- end
196
- end
197
- end