devise-edge 1.2.rc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. data/CHANGELOG.rdoc +500 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +335 -0
  4. data/app/controllers/devise/confirmations_controller.rb +33 -0
  5. data/app/controllers/devise/oauth_callbacks_controller.rb +4 -0
  6. data/app/controllers/devise/passwords_controller.rb +41 -0
  7. data/app/controllers/devise/registrations_controller.rb +75 -0
  8. data/app/controllers/devise/sessions_controller.rb +23 -0
  9. data/app/controllers/devise/unlocks_controller.rb +34 -0
  10. data/app/helpers/devise_helper.rb +17 -0
  11. data/app/mailers/devise/mailer.rb +88 -0
  12. data/app/views/devise/confirmations/new.html.erb +12 -0
  13. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  14. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  15. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  16. data/app/views/devise/passwords/edit.html.erb +16 -0
  17. data/app/views/devise/passwords/new.html.erb +12 -0
  18. data/app/views/devise/registrations/edit.html.erb +25 -0
  19. data/app/views/devise/registrations/new.html.erb +18 -0
  20. data/app/views/devise/sessions/new.html.erb +17 -0
  21. data/app/views/devise/shared/_links.erb +25 -0
  22. data/app/views/devise/unlocks/new.html.erb +12 -0
  23. data/config/locales/en.yml +42 -0
  24. data/lib/devise.rb +371 -0
  25. data/lib/devise/controllers/helpers.rb +261 -0
  26. data/lib/devise/controllers/internal_helpers.rb +113 -0
  27. data/lib/devise/controllers/scoped_views.rb +33 -0
  28. data/lib/devise/controllers/url_helpers.rb +39 -0
  29. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  30. data/lib/devise/encryptors/base.rb +20 -0
  31. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  32. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  33. data/lib/devise/encryptors/sha1.rb +25 -0
  34. data/lib/devise/encryptors/sha512.rb +25 -0
  35. data/lib/devise/failure_app.rb +126 -0
  36. data/lib/devise/hooks/activatable.rb +11 -0
  37. data/lib/devise/hooks/forgetable.rb +12 -0
  38. data/lib/devise/hooks/rememberable.rb +45 -0
  39. data/lib/devise/hooks/timeoutable.rb +22 -0
  40. data/lib/devise/hooks/trackable.rb +9 -0
  41. data/lib/devise/mapping.rb +105 -0
  42. data/lib/devise/models.rb +66 -0
  43. data/lib/devise/models/authenticatable.rb +143 -0
  44. data/lib/devise/models/confirmable.rb +160 -0
  45. data/lib/devise/models/database_authenticatable.rb +94 -0
  46. data/lib/devise/models/encryptable.rb +65 -0
  47. data/lib/devise/models/lockable.rb +168 -0
  48. data/lib/devise/models/oauthable.rb +49 -0
  49. data/lib/devise/models/recoverable.rb +83 -0
  50. data/lib/devise/models/registerable.rb +21 -0
  51. data/lib/devise/models/rememberable.rb +122 -0
  52. data/lib/devise/models/timeoutable.rb +33 -0
  53. data/lib/devise/models/token_authenticatable.rb +72 -0
  54. data/lib/devise/models/trackable.rb +30 -0
  55. data/lib/devise/models/validatable.rb +60 -0
  56. data/lib/devise/modules.rb +30 -0
  57. data/lib/devise/oauth.rb +41 -0
  58. data/lib/devise/oauth/config.rb +33 -0
  59. data/lib/devise/oauth/helpers.rb +18 -0
  60. data/lib/devise/oauth/internal_helpers.rb +182 -0
  61. data/lib/devise/oauth/test_helpers.rb +29 -0
  62. data/lib/devise/oauth/url_helpers.rb +35 -0
  63. data/lib/devise/orm/active_record.rb +36 -0
  64. data/lib/devise/orm/mongo_mapper.rb +46 -0
  65. data/lib/devise/orm/mongoid.rb +29 -0
  66. data/lib/devise/path_checker.rb +18 -0
  67. data/lib/devise/rails.rb +67 -0
  68. data/lib/devise/rails/routes.rb +260 -0
  69. data/lib/devise/rails/warden_compat.rb +42 -0
  70. data/lib/devise/schema.rb +96 -0
  71. data/lib/devise/strategies/authenticatable.rb +150 -0
  72. data/lib/devise/strategies/base.rb +15 -0
  73. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  74. data/lib/devise/strategies/rememberable.rb +51 -0
  75. data/lib/devise/strategies/token_authenticatable.rb +53 -0
  76. data/lib/devise/test_helpers.rb +100 -0
  77. data/lib/devise/version.rb +3 -0
  78. data/lib/generators/active_record/devise_generator.rb +28 -0
  79. data/lib/generators/active_record/templates/migration.rb +30 -0
  80. data/lib/generators/devise/devise_generator.rb +17 -0
  81. data/lib/generators/devise/install_generator.rb +24 -0
  82. data/lib/generators/devise/orm_helpers.rb +24 -0
  83. data/lib/generators/devise/views_generator.rb +63 -0
  84. data/lib/generators/mongoid/devise_generator.rb +17 -0
  85. data/lib/generators/templates/README +25 -0
  86. data/lib/generators/templates/devise.rb +168 -0
  87. data/test/controllers/helpers_test.rb +220 -0
  88. data/test/controllers/internal_helpers_test.rb +56 -0
  89. data/test/controllers/url_helpers_test.rb +59 -0
  90. data/test/devise_test.rb +65 -0
  91. data/test/encryptors_test.rb +30 -0
  92. data/test/failure_app_test.rb +148 -0
  93. data/test/integration/authenticatable_test.rb +424 -0
  94. data/test/integration/confirmable_test.rb +104 -0
  95. data/test/integration/database_authenticatable_test.rb +38 -0
  96. data/test/integration/http_authenticatable_test.rb +64 -0
  97. data/test/integration/lockable_test.rb +109 -0
  98. data/test/integration/oauthable_test.rb +258 -0
  99. data/test/integration/recoverable_test.rb +141 -0
  100. data/test/integration/registerable_test.rb +179 -0
  101. data/test/integration/rememberable_test.rb +179 -0
  102. data/test/integration/timeoutable_test.rb +80 -0
  103. data/test/integration/token_authenticatable_test.rb +99 -0
  104. data/test/integration/trackable_test.rb +64 -0
  105. data/test/mailers/confirmation_instructions_test.rb +84 -0
  106. data/test/mailers/reset_password_instructions_test.rb +72 -0
  107. data/test/mailers/unlock_instructions_test.rb +66 -0
  108. data/test/mapping_test.rb +95 -0
  109. data/test/models/confirmable_test.rb +221 -0
  110. data/test/models/database_authenticatable_test.rb +82 -0
  111. data/test/models/encryptable_test.rb +65 -0
  112. data/test/models/lockable_test.rb +204 -0
  113. data/test/models/oauthable_test.rb +21 -0
  114. data/test/models/recoverable_test.rb +155 -0
  115. data/test/models/rememberable_test.rb +271 -0
  116. data/test/models/timeoutable_test.rb +28 -0
  117. data/test/models/token_authenticatable_test.rb +37 -0
  118. data/test/models/trackable_test.rb +5 -0
  119. data/test/models/validatable_test.rb +99 -0
  120. data/test/models_test.rb +77 -0
  121. data/test/oauth/config_test.rb +44 -0
  122. data/test/oauth/url_helpers_test.rb +47 -0
  123. data/test/orm/active_record.rb +9 -0
  124. data/test/orm/mongoid.rb +10 -0
  125. data/test/rails_app/app/active_record/admin.rb +6 -0
  126. data/test/rails_app/app/active_record/shim.rb +2 -0
  127. data/test/rails_app/app/active_record/user.rb +8 -0
  128. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  129. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  130. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  131. data/test/rails_app/app/controllers/home_controller.rb +12 -0
  132. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  133. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  134. data/test/rails_app/app/controllers/users_controller.rb +18 -0
  135. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  136. data/test/rails_app/app/mongoid/admin.rb +9 -0
  137. data/test/rails_app/app/mongoid/shim.rb +24 -0
  138. data/test/rails_app/app/mongoid/user.rb +10 -0
  139. data/test/rails_app/config/application.rb +35 -0
  140. data/test/rails_app/config/boot.rb +13 -0
  141. data/test/rails_app/config/environment.rb +5 -0
  142. data/test/rails_app/config/environments/development.rb +19 -0
  143. data/test/rails_app/config/environments/production.rb +33 -0
  144. data/test/rails_app/config/environments/test.rb +33 -0
  145. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  146. data/test/rails_app/config/initializers/devise.rb +172 -0
  147. data/test/rails_app/config/initializers/inflections.rb +2 -0
  148. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  149. data/test/rails_app/config/routes.rb +54 -0
  150. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +31 -0
  151. data/test/rails_app/db/schema.rb +52 -0
  152. data/test/rails_app/lib/shared_admin.rb +9 -0
  153. data/test/rails_app/lib/shared_user.rb +48 -0
  154. data/test/routes_test.rb +189 -0
  155. data/test/support/assertions.rb +24 -0
  156. data/test/support/helpers.rb +60 -0
  157. data/test/support/integration.rb +88 -0
  158. data/test/support/webrat/integrations/rails.rb +24 -0
  159. data/test/test_helper.rb +23 -0
  160. data/test/test_helpers_test.rb +101 -0
  161. metadata +335 -0
@@ -0,0 +1,104 @@
1
+ require 'test_helper'
2
+
3
+ class ConfirmationTest < ActionController::IntegrationTest
4
+
5
+ def visit_user_confirmation_with_token(confirmation_token)
6
+ visit user_confirmation_path(:confirmation_token => confirmation_token)
7
+ end
8
+
9
+ test 'user should be able to request a new confirmation' do
10
+ user = create_user(:confirm => false)
11
+ ActionMailer::Base.deliveries.clear
12
+
13
+ visit new_user_session_path
14
+ click_link "Didn't receive confirmation instructions?"
15
+
16
+ fill_in 'email', :with => user.email
17
+ click_button 'Resend confirmation instructions'
18
+
19
+ assert_current_url '/users/sign_in'
20
+ assert_contain 'You will receive an email with instructions about how to confirm your account in a few minutes'
21
+ assert_equal 1, ActionMailer::Base.deliveries.size
22
+ end
23
+
24
+ test 'user with invalid confirmation token should not be able to confirm an account' do
25
+ visit_user_confirmation_with_token('invalid_confirmation')
26
+ assert_have_selector '#error_explanation'
27
+ assert_contain /Confirmation token(.*)invalid/
28
+ end
29
+
30
+ test 'user with valid confirmation token should be able to confirm an account' do
31
+ user = create_user(:confirm => false)
32
+ assert_not user.confirmed?
33
+ visit_user_confirmation_with_token(user.confirmation_token)
34
+
35
+ assert_contain 'Your account was successfully confirmed.'
36
+ assert_current_url '/'
37
+ assert user.reload.confirmed?
38
+ end
39
+
40
+ test 'already confirmed user should not be able to confirm the account again' do
41
+ user = create_user(:confirm => false)
42
+ user.confirmed_at = Time.now
43
+ user.save
44
+ visit_user_confirmation_with_token(user.confirmation_token)
45
+
46
+ assert_have_selector '#error_explanation'
47
+ assert_contain 'already confirmed'
48
+ end
49
+
50
+ test 'already confirmed user should not be able to confirm the account again neither request confirmation' do
51
+ user = create_user(:confirm => false)
52
+ user.confirmed_at = Time.now
53
+ user.save
54
+
55
+ visit_user_confirmation_with_token(user.confirmation_token)
56
+ assert_contain 'already confirmed'
57
+
58
+ fill_in 'email', :with => user.email
59
+ click_button 'Resend confirmation instructions'
60
+ assert_contain 'already confirmed'
61
+ end
62
+
63
+ test 'sign in user automatically after confirming it\'s email' do
64
+ user = create_user(:confirm => false)
65
+ visit_user_confirmation_with_token(user.confirmation_token)
66
+
67
+ assert warden.authenticated?(:user)
68
+ end
69
+
70
+ test 'increases sign count when signed in through confirmation' do
71
+ user = create_user(:confirm => false)
72
+ visit_user_confirmation_with_token(user.confirmation_token)
73
+
74
+ user.reload
75
+ assert_equal 1, user.sign_in_count
76
+ end
77
+
78
+ test 'not confirmed user with setup to block without confirmation should not be able to sign in' do
79
+ swap Devise, :confirm_within => 0.days do
80
+ sign_in_as_user(:confirm => false)
81
+
82
+ assert_contain 'You have to confirm your account before continuing'
83
+ assert_not warden.authenticated?(:user)
84
+ end
85
+ end
86
+
87
+ test 'not confirmed user but configured with some days to confirm should be able to sign in' do
88
+ swap Devise, :confirm_within => 1.day do
89
+ sign_in_as_user(:confirm => false)
90
+
91
+ assert_response :success
92
+ assert warden.authenticated?(:user)
93
+ end
94
+ end
95
+
96
+ test 'error message is configurable by resource name' do
97
+ store_translations :en, :devise => {
98
+ :failure => { :user => { :unconfirmed => "Not confirmed user" } }
99
+ } do
100
+ sign_in_as_user(:confirm => false)
101
+ assert_contain 'Not confirmed user'
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class DatabaseAuthenticationTest < ActionController::IntegrationTest
4
+ test 'sign in should not authenticate if not using proper authentication keys' do
5
+ swap Devise, :authentication_keys => [:username] do
6
+ sign_in_as_user
7
+ assert_not warden.authenticated?(:user)
8
+ end
9
+ end
10
+
11
+ test 'sign in with invalid email should return to sign in form with error message' do
12
+ sign_in_as_admin do
13
+ fill_in 'email', :with => 'wrongemail@test.com'
14
+ end
15
+
16
+ assert_contain 'Invalid email or password'
17
+ assert_not warden.authenticated?(:admin)
18
+ end
19
+
20
+ test 'sign in with invalid pasword should return to sign in form with error message' do
21
+ sign_in_as_admin do
22
+ fill_in 'password', :with => 'abcdef'
23
+ end
24
+
25
+ assert_contain 'Invalid email or password'
26
+ assert_not warden.authenticated?(:admin)
27
+ end
28
+
29
+ test 'error message is configurable by resource name' do
30
+ store_translations :en, :devise => { :failure => { :admin => { :invalid => "Invalid credentials" } } } do
31
+ sign_in_as_admin do
32
+ fill_in 'password', :with => 'abcdef'
33
+ end
34
+
35
+ assert_contain 'Invalid credentials'
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+
3
+ class HttpAuthenticationTest < ActionController::IntegrationTest
4
+
5
+ test 'sign in should authenticate with http' do
6
+ sign_in_as_new_user_with_http
7
+ assert_response :success
8
+ assert_match '<email>user@test.com</email>', response.body
9
+ assert warden.authenticated?(:user)
10
+ end
11
+
12
+ test 'returns a custom response with www-authenticate header on failures' do
13
+ sign_in_as_new_user_with_http("unknown")
14
+ assert_equal 401, status
15
+ assert_equal 'Basic realm="Application"', headers["WWW-Authenticate"]
16
+ end
17
+
18
+ test 'uses the request format as response content type' do
19
+ sign_in_as_new_user_with_http("unknown")
20
+ assert_equal 401, status
21
+ assert_equal "application/xml; charset=utf-8", headers["Content-Type"]
22
+ assert_match "<error>Invalid email or password.</error>", response.body
23
+ end
24
+
25
+ test 'returns a custom response with www-authenticate and chosen realm' do
26
+ swap Devise, :http_authentication_realm => "MyApp" do
27
+ sign_in_as_new_user_with_http("unknown")
28
+ assert_equal 401, status
29
+ assert_equal 'Basic realm="MyApp"', headers["WWW-Authenticate"]
30
+ end
31
+ end
32
+
33
+ test 'sign in should authenticate with http even with specific authentication keys' do
34
+ swap Devise, :authentication_keys => [:username] do
35
+ sign_in_as_new_user_with_http("usertest")
36
+ assert_response :success
37
+ assert_match '<email>user@test.com</email>', response.body
38
+ assert warden.authenticated?(:user)
39
+ end
40
+ end
41
+
42
+ test 'test request with oauth2 header doesnt get mistaken for basic authentication' do
43
+ swap Devise, :http_authenticatable => true do
44
+ add_oauth2_header
45
+ assert_equal 401, status
46
+ assert_equal 'Basic realm="Application"', headers["WWW-Authenticate"]
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def sign_in_as_new_user_with_http(username="user@test.com", password="123456")
53
+ user = create_user
54
+ get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "Basic #{ActiveSupport::Base64.encode64("#{username}:#{password}")}"
55
+ user
56
+ end
57
+
58
+ # Sign in with oauth2 token. This is just to test that it isn't misinterpreted as basic authentication
59
+ def add_oauth2_header
60
+ user = create_user
61
+ get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "OAuth #{ActiveSupport::Base64.encode64("#{user.email}:123456")}"
62
+ end
63
+
64
+ end
@@ -0,0 +1,109 @@
1
+ require 'test_helper'
2
+
3
+ class LockTest < ActionController::IntegrationTest
4
+
5
+ def visit_user_unlock_with_token(unlock_token)
6
+ visit user_unlock_path(:unlock_token => unlock_token)
7
+ end
8
+
9
+ test 'user should be able to request a new unlock token' do
10
+ user = create_user(:locked => true)
11
+ ActionMailer::Base.deliveries.clear
12
+
13
+ visit new_user_session_path
14
+ click_link "Didn't receive unlock instructions?"
15
+
16
+ fill_in 'email', :with => user.email
17
+ click_button 'Resend unlock instructions'
18
+
19
+ assert_template 'sessions/new'
20
+ assert_contain 'You will receive an email with instructions about how to unlock your account in a few minutes'
21
+ assert_equal 1, ActionMailer::Base.deliveries.size
22
+ end
23
+
24
+ test 'unlocked user should not be able to request a unlock token' do
25
+ user = create_user(:locked => false)
26
+ ActionMailer::Base.deliveries.clear
27
+
28
+ visit new_user_session_path
29
+ click_link "Didn't receive unlock instructions?"
30
+
31
+ fill_in 'email', :with => user.email
32
+ click_button 'Resend unlock instructions'
33
+
34
+ assert_template 'unlocks/new'
35
+ assert_contain 'not locked'
36
+ assert_equal 0, ActionMailer::Base.deliveries.size
37
+ end
38
+
39
+ test 'unlocked pages should not be available if email strategy is disabled' do
40
+ visit "/admins/sign_in"
41
+
42
+ assert_raise Webrat::NotFoundError do
43
+ click_link "Didn't receive unlock instructions?"
44
+ end
45
+
46
+ assert_raise NameError do
47
+ visit new_admin_unlock_path
48
+ end
49
+
50
+ visit "/admins/unlock/new"
51
+ assert_response :not_found
52
+ end
53
+
54
+ test 'user with invalid unlock token should not be able to unlock an account' do
55
+ visit_user_unlock_with_token('invalid_token')
56
+
57
+ assert_response :success
58
+ assert_current_url '/users/unlock?unlock_token=invalid_token'
59
+ assert_have_selector '#error_explanation'
60
+ assert_contain /Unlock token(.*)invalid/
61
+ end
62
+
63
+ test "locked user should be able to unlock account" do
64
+ user = create_user(:locked => true)
65
+ assert user.access_locked?
66
+
67
+ visit_user_unlock_with_token(user.unlock_token)
68
+
69
+ assert_current_url '/'
70
+ assert_contain 'Your account was successfully unlocked.'
71
+
72
+ assert_not user.reload.access_locked?
73
+ end
74
+
75
+ test "sign in user automatically after unlocking it's account" do
76
+ user = create_user(:locked => true)
77
+ visit_user_unlock_with_token(user.unlock_token)
78
+ assert warden.authenticated?(:user)
79
+ end
80
+
81
+ test "user should not be able to sign in when locked" do
82
+ user = sign_in_as_user(:locked => true)
83
+ assert_template 'sessions/new'
84
+ assert_contain 'Your account is locked.'
85
+ assert_not warden.authenticated?(:user)
86
+ end
87
+
88
+ test "user should not send a new e-mail if already locked" do
89
+ user = create_user(:locked => true)
90
+ user.failed_attempts = User.maximum_attempts + 1
91
+ user.save!
92
+
93
+ ActionMailer::Base.deliveries.clear
94
+
95
+ sign_in_as_user(:password => "invalid")
96
+ assert_contain 'Your account is locked.'
97
+ assert ActionMailer::Base.deliveries.empty?
98
+ end
99
+
100
+ test 'error message is configurable by resource name' do
101
+ store_translations :en, :devise => {
102
+ :failure => { :user => { :locked => "You are locked!" } }
103
+ } do
104
+ user = sign_in_as_user(:locked => true)
105
+ assert_contain 'You are locked!'
106
+ end
107
+ end
108
+
109
+ end
@@ -0,0 +1,258 @@
1
+ require 'test_helper'
2
+
3
+ class OAuthableIntegrationTest < ActionController::IntegrationTest
4
+ FACEBOOK_INFO = {
5
+ :username => 'usertest',
6
+ :email => 'user@test.com'
7
+ }
8
+
9
+ ACCESS_TOKEN = {
10
+ :access_token => "plataformatec"
11
+ }
12
+
13
+ setup do
14
+ Devise::Oauth.short_circuit_authorizers!
15
+ end
16
+
17
+ teardown do
18
+ Devise::Oauth.unshort_circuit_authorizers!
19
+ Devise::Oauth.reset_stubs!
20
+ User.singleton_class.remove_possible_method(:find_for_github_oauth)
21
+ end
22
+
23
+ def stub_github!(times=1)
24
+ def User.find_for_github_oauth(*); end
25
+
26
+ Devise::Oauth.stub!(:github) do |b|
27
+ b.post('/login/oauth/access_token') { [200, {}, ACCESS_TOKEN.to_json] }
28
+ end
29
+ end
30
+
31
+ def stub_facebook!(times=1)
32
+ # If times != 1, use invalid data
33
+ data = (times != 1) ? FACEBOOK_INFO.except(:email) : FACEBOOK_INFO
34
+
35
+ Devise::Oauth.stub!(:facebook) do |b|
36
+ b.post('/oauth/access_token') { [200, {}, ACCESS_TOKEN.to_json] }
37
+ times.times {
38
+ b.get('/me?access_token=plataformatec') { [200, {}, data.to_json] }
39
+ }
40
+ end
41
+ end
42
+
43
+ test "[BASIC] setup with persisted user" do
44
+ stub_facebook!
45
+
46
+ assert_difference "User.count", 1 do
47
+ visit "/users/sign_in"
48
+ click_link "Sign in with Facebook"
49
+ end
50
+
51
+ assert_current_url "/"
52
+ assert_contain "Successfully authorized from Facebook account."
53
+
54
+ assert warden.authenticated?(:user)
55
+ assert_not warden.authenticated?(:admin)
56
+ assert "plataformatec", warden.user(:user).facebook_token
57
+ end
58
+
59
+ test "[BASIC] setup with not persisted user and follow up" do
60
+ stub_facebook!(2)
61
+
62
+ assert_no_difference "User.count" do
63
+ visit "/users/sign_in"
64
+ click_link "Sign in with Facebook"
65
+ end
66
+
67
+ assert_contain "1 error prohibited this user from being saved"
68
+ assert_contain "Email can't be blank"
69
+
70
+ assert_not warden.authenticated?(:user)
71
+ assert_not warden.authenticated?(:admin)
72
+
73
+ fill_in "Email", :with => "user.form@test.com"
74
+ click_button "Sign up"
75
+
76
+ assert_current_url "/"
77
+ assert_contain "You have signed up successfully."
78
+ assert_contain "Hello User user.form@test.com"
79
+
80
+ assert warden.authenticated?(:user)
81
+ assert_not warden.authenticated?(:admin)
82
+ assert "plataformatec", warden.user(:user).facebook_token
83
+ end
84
+
85
+ test "[BASIC] setup updating an existing user in database" do
86
+ stub_facebook!
87
+ user = create_user
88
+
89
+ assert_no_difference "User.count" do
90
+ visit "/users/sign_in"
91
+ click_link "Sign in with Facebook"
92
+ end
93
+
94
+ assert_current_url "/"
95
+ assert_contain "Successfully authorized from Facebook account."
96
+
97
+ assert_equal user, warden.user(:user)
98
+ assert_equal "plataformatec", user.reload.facebook_token
99
+ end
100
+
101
+ test "[BASIC] setup updating an existing user in session" do
102
+ stub_facebook!
103
+
104
+ # Create an user and change his e-mail
105
+ user = sign_in_as_user
106
+ user.email = "another@test.com"
107
+ user.save!
108
+
109
+ assert_no_difference "User.count" do
110
+ visit "/"
111
+ click_link "Sign in with Facebook"
112
+ end
113
+
114
+ assert_current_url "/"
115
+ assert_contain "Successfully authorized from Facebook account."
116
+
117
+ assert_equal user, warden.user(:user)
118
+ assert_equal "another@test.com", warden.user(:user).email
119
+ assert_equal "plataformatec", user.reload.facebook_token
120
+ end
121
+
122
+ test "[SESSION CLEANUP] ensures session is cleaned up after sign up" do
123
+ stub_facebook!(2)
124
+
125
+ assert_no_difference "User.count" do
126
+ visit "/users/sign_in"
127
+ click_link "Sign in with Facebook"
128
+ end
129
+
130
+ assert_contain "1 error prohibited this user from being saved"
131
+ fill_in "Email", :with => "user.form@test.com"
132
+ click_button "Sign up"
133
+
134
+ assert_contain "You have signed up successfully."
135
+ visit "/users/sign_out"
136
+
137
+ user = sign_in_as_user
138
+ assert_nil warden.user(:user).facebook_token
139
+ assert_equal user, warden.user(:user)
140
+ end
141
+
142
+ test "[SESSION CLEANUP] ensures session is cleaned up on cancel" do
143
+ stub_facebook!(2)
144
+
145
+ assert_no_difference "User.count" do
146
+ visit "/users/sign_in"
147
+ click_link "Sign in with Facebook"
148
+ end
149
+
150
+ assert_contain "1 error prohibited this user from being saved"
151
+ visit "/users/cancel"
152
+
153
+ user = sign_in_as_user
154
+ assert_nil warden.user(:user).facebook_token
155
+ assert_equal user, warden.user(:user)
156
+ end
157
+
158
+ test "[SESSION CLEANUP] ensures session is cleaned up on sign in" do
159
+ stub_facebook!(2)
160
+
161
+ assert_no_difference "User.count" do
162
+ visit "/users/sign_in"
163
+ click_link "Sign in with Facebook"
164
+ end
165
+
166
+ assert_contain "1 error prohibited this user from being saved"
167
+
168
+ user = sign_in_as_user
169
+ assert_nil warden.user(:user).facebook_token
170
+ assert_equal user, warden.user(:user)
171
+ end
172
+
173
+ test "[I18N] scopes messages based on oauth callback for success" do
174
+ stub_facebook!
175
+
176
+ store_translations :en, :devise => { :oauth_callbacks => {
177
+ :facebook => { :success => "Welcome facebooker" } } } do
178
+ visit "/users/sign_in"
179
+ click_link "Sign in with Facebook"
180
+ assert_contain "Welcome facebooker"
181
+ end
182
+ end
183
+
184
+ test "[I18N] scopes messages based on oauth callback and resource name for success" do
185
+ stub_facebook!
186
+
187
+ store_translations :en, :devise => { :oauth_callbacks => {
188
+ :user => { :facebook => { :success => "Welcome facebooker user" } },
189
+ :facebook => { :success => "Welcome facebooker" } } } do
190
+ visit "/users/sign_in"
191
+ click_link "Sign in with Facebook"
192
+ assert_contain "Welcome facebooker user"
193
+ end
194
+ end
195
+
196
+ test "[FAILURE] shows 404 if no code or error are given as params" do
197
+ assert_raise AbstractController::ActionNotFound do
198
+ visit "/users/oauth/facebook/callback"
199
+ end
200
+ end
201
+
202
+ test "[FAILURE] raises an error if model does not implement a hook" do
203
+ begin
204
+ visit "/users/oauth/github/callback?code=123456"
205
+ raise "Expected visit to raise an error"
206
+ rescue Exception => e
207
+ assert_match "User does not respond to find_for_github_oauth", e.message
208
+ end
209
+ end
210
+
211
+ test "[FAILURE] handles callback error parameter according to the specification" do
212
+ visit "/users/oauth/facebook/callback?error=access_denied"
213
+ assert_current_url "/users/sign_in"
214
+ assert_contain 'Could not authorize you from Facebook because "Access denied".'
215
+ end
216
+
217
+ test "[FAILURE] handles callback error_reason just for Facebook compatibility" do
218
+ visit "/users/oauth/facebook/callback?error_reason=access_denied"
219
+ assert_current_url "/users/sign_in"
220
+ assert_contain 'Could not authorize you from Facebook because "Access denied".'
221
+ end
222
+
223
+ test "[FAILURE][I18N] uses I18n for custom messages" do
224
+ store_translations :en, :devise => { :oauth_callbacks => { :access_denied => "Access denied bro" } } do
225
+ visit "/users/oauth/facebook/callback?error=access_denied"
226
+ assert_current_url "/users/sign_in"
227
+ assert_contain "Access denied bro"
228
+ end
229
+ end
230
+
231
+ test "[FAILURE][I18N] uses I18n with oauth callback scope for custom messages" do
232
+ store_translations :en, :devise => { :oauth_callbacks => {
233
+ :facebook => { :access_denied => "Access denied bro" } } } do
234
+ visit "/users/oauth/facebook/callback?error=access_denied"
235
+ assert_current_url "/users/sign_in"
236
+ assert_contain "Access denied bro"
237
+ end
238
+ end
239
+
240
+ test "[FAILURE][I18N] uses I18n with oauth callback scope and resource name for custom messages" do
241
+ store_translations :en, :devise => { :oauth_callbacks => {
242
+ :user => { :facebook => { :access_denied => "Access denied user" } },
243
+ :facebook => { :access_denied => "Access denied bro" } } } do
244
+ visit "/users/oauth/facebook/callback?error=access_denied"
245
+ assert_current_url "/users/sign_in"
246
+ assert_contain "Access denied user"
247
+ end
248
+ end
249
+
250
+ test "[FAILURE][I18N] trim messages to avoid long symbols lookups" do
251
+ store_translations :en, :devise => { :oauth_callbacks => {
252
+ :facebook => { ("a"*25) => "Access denied bro" } } } do
253
+ visit "/users/oauth/facebook/callback?error=#{"a"*100}"
254
+ assert_current_url "/users/sign_in"
255
+ assert_contain "Access denied bro"
256
+ end
257
+ end
258
+ end