devise_token_auth 0.2.0 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise_token_auth might be problematic. Click here for more details.

Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -2
  3. data/Rakefile +9 -4
  4. data/app/controllers/devise_token_auth/application_controller.rb +5 -7
  5. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +2 -2
  6. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +18 -10
  7. data/app/controllers/devise_token_auth/confirmations_controller.rb +2 -2
  8. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +17 -23
  9. data/app/controllers/devise_token_auth/passwords_controller.rb +23 -27
  10. data/app/controllers/devise_token_auth/registrations_controller.rb +17 -15
  11. data/app/controllers/devise_token_auth/sessions_controller.rb +8 -12
  12. data/app/controllers/devise_token_auth/token_validations_controller.rb +3 -3
  13. data/app/controllers/devise_token_auth/unlocks_controller.rb +10 -12
  14. data/app/models/devise_token_auth/concerns/user.rb +27 -36
  15. data/config/initializers/devise.rb +1 -1
  16. data/lib/devise_token_auth.rb +6 -6
  17. data/lib/devise_token_auth/controllers/helpers.rb +14 -4
  18. data/lib/devise_token_auth/engine.rb +11 -13
  19. data/lib/devise_token_auth/errors.rb +1 -1
  20. data/lib/devise_token_auth/rails/routes.rb +29 -29
  21. data/lib/devise_token_auth/url.rb +6 -4
  22. data/lib/devise_token_auth/version.rb +1 -1
  23. data/lib/generators/devise_token_auth/install_generator.rb +52 -39
  24. data/lib/generators/devise_token_auth/install_views_generator.rb +5 -5
  25. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +1 -1
  26. data/lib/generators/devise_token_auth/templates/{user.rb → user.rb.erb} +0 -0
  27. data/test/controllers/custom/custom_confirmations_controller_test.rb +3 -1
  28. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +2 -0
  29. data/test/controllers/custom/custom_passwords_controller_test.rb +4 -2
  30. data/test/controllers/custom/custom_registrations_controller_test.rb +6 -8
  31. data/test/controllers/custom/custom_sessions_controller_test.rb +5 -5
  32. data/test/controllers/custom/custom_token_validations_controller_test.rb +3 -3
  33. data/test/controllers/demo_group_controller_test.rb +2 -6
  34. data/test/controllers/demo_mang_controller_test.rb +2 -5
  35. data/test/controllers/demo_user_controller_test.rb +8 -17
  36. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +2 -2
  37. data/test/controllers/devise_token_auth/passwords_controller_test.rb +13 -9
  38. data/test/controllers/devise_token_auth/registrations_controller_test.rb +8 -8
  39. data/test/controllers/devise_token_auth/sessions_controller_test.rb +22 -33
  40. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +2 -6
  41. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +1 -1
  42. data/test/controllers/overrides/confirmations_controller_test.rb +3 -1
  43. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +3 -1
  44. data/test/controllers/overrides/passwords_controller_test.rb +25 -29
  45. data/test/controllers/overrides/registrations_controller_test.rb +4 -2
  46. data/test/controllers/overrides/sessions_controller_test.rb +4 -4
  47. data/test/controllers/overrides/token_validations_controller_test.rb +3 -3
  48. data/test/dummy/app/controllers/custom/confirmations_controller.rb +0 -2
  49. data/test/dummy/app/controllers/custom/passwords_controller.rb +1 -4
  50. data/test/dummy/app/controllers/custom/registrations_controller.rb +1 -2
  51. data/test/dummy/app/controllers/custom/sessions_controller.rb +1 -3
  52. data/test/dummy/app/controllers/custom/token_validations_controller.rb +1 -3
  53. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +3 -3
  54. data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +3 -3
  55. data/test/dummy/app/controllers/overrides/passwords_controller.rb +5 -5
  56. data/test/dummy/app/controllers/overrides/registrations_controller.rb +2 -2
  57. data/test/dummy/app/controllers/overrides/sessions_controller.rb +8 -10
  58. data/test/dummy/app/controllers/overrides/token_validations_controller.rb +3 -5
  59. data/test/dummy/app/helpers/application_helper.rb +1027 -1036
  60. data/test/dummy/app/models/scoped_user.rb +2 -2
  61. data/test/dummy/app/models/unregisterable_user.rb +2 -2
  62. data/test/dummy/app/models/user.rb +1 -1
  63. data/test/dummy/config.ru +5 -3
  64. data/test/dummy/config/application.rb +2 -2
  65. data/test/dummy/config/boot.rb +2 -2
  66. data/test/dummy/config/environment.rb +1 -1
  67. data/test/dummy/config/environments/development.rb +3 -3
  68. data/test/dummy/config/environments/test.rb +1 -1
  69. data/test/dummy/config/initializers/omniauth.rb +2 -2
  70. data/test/dummy/config/routes.rb +10 -29
  71. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +8 -8
  72. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +7 -7
  73. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +4 -4
  74. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +7 -7
  75. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +7 -7
  76. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +7 -7
  77. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +6 -6
  78. data/test/dummy/db/schema.rb +2 -60
  79. data/test/factories/users.rb +40 -0
  80. data/test/lib/devise_token_auth/url_test.rb +6 -6
  81. data/test/lib/generators/devise_token_auth/install_generator_test.rb +22 -14
  82. data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +30 -22
  83. data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +1 -1
  84. data/test/models/user_test.rb +15 -46
  85. data/test/support/controllers/routes.rb +43 -0
  86. data/test/test_helper.rb +12 -12
  87. metadata +8 -14
  88. data/test/dummy/app/models/evil_user.rb +0 -5
  89. data/test/dummy/app/models/nice_user.rb +0 -9
  90. data/test/dummy/db/migrate/20140928231203_devise_token_auth_create_evil_users.rb +0 -66
  91. data/test/dummy/db/migrate/20150409095712_devise_token_auth_create_nice_users.rb +0 -63
  92. data/test/integration/navigation_test.rb +0 -12
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file is auto-generated from the current state of the database. Instead
2
4
  # of editing this file, please use the migrations feature of Active Record to
3
5
  # incrementally modify your database, and then regenerate this schema definition.
@@ -12,37 +14,6 @@
12
14
 
13
15
  ActiveRecord::Schema.define(version: 20160629184441) do
14
16
 
15
- create_table "evil_users", force: :cascade do |t|
16
- t.string "email"
17
- t.string "encrypted_password", default: "", null: false
18
- t.string "reset_password_token"
19
- t.datetime "reset_password_sent_at"
20
- t.boolean "allow_password_change", default: false
21
- t.datetime "remember_created_at"
22
- t.integer "sign_in_count", default: 0, null: false
23
- t.datetime "current_sign_in_at"
24
- t.datetime "last_sign_in_at"
25
- t.string "current_sign_in_ip"
26
- t.string "last_sign_in_ip"
27
- t.string "confirmation_token"
28
- t.datetime "confirmed_at"
29
- t.datetime "confirmation_sent_at"
30
- t.string "unconfirmed_email"
31
- t.string "name"
32
- t.string "nickname"
33
- t.string "image"
34
- t.string "provider"
35
- t.string "uid", default: "", null: false
36
- t.text "tokens"
37
- t.string "favorite_color"
38
- t.datetime "created_at"
39
- t.datetime "updated_at"
40
- t.index ["confirmation_token"], name: "index_evil_users_on_confirmation_token", unique: true
41
- t.index ["email"], name: "index_evil_users_on_email"
42
- t.index ["reset_password_token"], name: "index_evil_users_on_reset_password_token", unique: true
43
- t.index ["uid", "provider"], name: "index_evil_users_on_uid_and_provider", unique: true
44
- end
45
-
46
17
  create_table "lockable_users", force: :cascade do |t|
47
18
  t.string "provider", null: false
48
19
  t.string "uid", default: "", null: false
@@ -94,35 +65,6 @@ ActiveRecord::Schema.define(version: 20160629184441) do
94
65
  t.index ["uid", "provider"], name: "index_mangs_on_uid_and_provider", unique: true
95
66
  end
96
67
 
97
- create_table "nice_users", force: :cascade do |t|
98
- t.string "provider", null: false
99
- t.string "uid", default: "", null: false
100
- t.string "encrypted_password", default: "", null: false
101
- t.string "reset_password_token"
102
- t.datetime "reset_password_sent_at"
103
- t.boolean "allow_password_change", default: false
104
- t.datetime "remember_created_at"
105
- t.integer "sign_in_count", default: 0, null: false
106
- t.datetime "current_sign_in_at"
107
- t.datetime "last_sign_in_at"
108
- t.string "current_sign_in_ip"
109
- t.string "last_sign_in_ip"
110
- t.string "confirmation_token"
111
- t.datetime "confirmed_at"
112
- t.datetime "confirmation_sent_at"
113
- t.string "unconfirmed_email"
114
- t.string "name"
115
- t.string "nickname"
116
- t.string "image"
117
- t.string "email"
118
- t.text "tokens"
119
- t.datetime "created_at"
120
- t.datetime "updated_at"
121
- t.index ["email"], name: "index_nice_users_on_email"
122
- t.index ["reset_password_token"], name: "index_nice_users_on_reset_password_token", unique: true
123
- t.index ["uid", "provider"], name: "index_nice_users_on_uid_and_provider", unique: true
124
- end
125
-
126
68
  create_table "only_email_users", force: :cascade do |t|
127
69
  t.string "provider", null: false
128
70
  t.string "uid", default: "", null: false
@@ -0,0 +1,40 @@
1
+ FactoryBot.define do
2
+ factory :user do
3
+ email { Faker::Internet.safe_email }
4
+ password { Faker::Internet.password }
5
+ provider { 'email' }
6
+
7
+ transient do
8
+ allow_unconfirmed_period { Time.now.utc - Devise.allow_unconfirmed_access_for }
9
+ end
10
+
11
+ trait :with_nickname do
12
+ nickname { Faker::Internet.username }
13
+ end
14
+
15
+ trait :confirmed do
16
+ after(:create) { |user| user.confirm }
17
+ end
18
+
19
+ # confirmation period is expired
20
+ trait :unconfirmed do
21
+ after(:create) do |user, evaluator|
22
+ user.update_attribute(:confirmation_sent_at, evaluator.allow_unconfirmed_period - 1.day )
23
+ end
24
+ end
25
+
26
+ trait :facebook do
27
+ uid { Faker::Number.number(10) }
28
+ provider { 'facebook' }
29
+ end
30
+
31
+ trait :locked do
32
+ after(:create) { |user| user.lock_access! }
33
+ end
34
+
35
+ factory :lockable_user, class: 'LockableUser'
36
+ factory :mang_user, class: 'Mang'
37
+ factory :only_email_user, class: 'OnlyEmailUser'
38
+ factory :scoped_user, class: 'ScopedUser'
39
+ end
40
+ end
@@ -3,23 +3,23 @@
3
3
  require 'test_helper'
4
4
 
5
5
  class DeviseTokenAuth::UrlTest < ActiveSupport::TestCase
6
- describe "DeviseTokenAuth::Url#generate" do
6
+ describe 'DeviseTokenAuth::Url#generate' do
7
7
  test 'URI fragment should appear at the end of URL' do
8
- params = {client_id: 123}
8
+ params = { client_id: 123 }
9
9
  url = 'http://example.com#fragment'
10
- assert_equal DeviseTokenAuth::Url.send(:generate, url, params), "http://example.com?client_id=123#fragment"
10
+ assert_equal DeviseTokenAuth::Url.send(:generate, url, params), 'http://example.com?client_id=123#fragment'
11
11
  end
12
12
 
13
13
  describe 'with existing query params' do
14
14
  test 'should preserve existing query params' do
15
15
  url = 'http://example.com?a=1'
16
- assert_equal DeviseTokenAuth::Url.send(:generate, url), "http://example.com?a=1"
16
+ assert_equal DeviseTokenAuth::Url.send(:generate, url), 'http://example.com?a=1'
17
17
  end
18
18
 
19
19
  test 'should marge existing query params with new ones' do
20
- params = {client_id: 123}
20
+ params = { client_id: 123 }
21
21
  url = 'http://example.com?a=1'
22
- assert_equal DeviseTokenAuth::Url.send(:generate, url, params), "http://example.com?a=1&client_id=123"
22
+ assert_equal DeviseTokenAuth::Url.send(:generate, url, params), 'http://example.com?a=1&client_id=123'
23
23
  end
24
24
  end
25
25
  end
@@ -41,15 +41,24 @@ module DeviseTokenAuth
41
41
  test 'subsequent runs raise no errors' do
42
42
  run_generator
43
43
  end
44
+
45
+ test 'add primary key type with rails 5 when specified in rails generator' do
46
+ run_generator %w[--primary_key_type=uuid --force]
47
+ if Rails::VERSION::MAJOR >= 5
48
+ assert_migration 'db/migrate/devise_token_auth_create_users.rb', /create_table\(:users, id: :uuid\) do/
49
+ else
50
+ assert_migration 'db/migrate/devise_token_auth_create_users.rb', /create_table\(:users\) do/
51
+ end
52
+ end
44
53
  end
45
54
 
46
55
  describe 'existing user model' do
47
56
  setup :prepare_destination
48
57
 
49
58
  before do
50
- @dir = File.join(destination_root, "app", "models")
59
+ @dir = File.join(destination_root, 'app', 'models')
51
60
 
52
- @fname = File.join(@dir, "user.rb")
61
+ @fname = File.join(@dir, 'user.rb')
53
62
 
54
63
  # make dir if not exists
55
64
  FileUtils.mkdir_p(@dir)
@@ -57,7 +66,7 @@ module DeviseTokenAuth
57
66
  # account for rails version 5
58
67
  active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
59
68
 
60
- @f = File.open(@fname, 'w') {|f|
69
+ @f = File.open(@fname, 'w') do |f|
61
70
  f.write <<-RUBY
62
71
  class User < #{active_record_needle}
63
72
 
@@ -66,7 +75,7 @@ module DeviseTokenAuth
66
75
  end
67
76
  end
68
77
  RUBY
69
- }
78
+ end
70
79
 
71
80
  run_generator
72
81
  end
@@ -86,25 +95,24 @@ module DeviseTokenAuth
86
95
  end
87
96
  end
88
97
 
89
-
90
98
  describe 'routes' do
91
99
  setup :prepare_destination
92
100
 
93
101
  before do
94
- @dir = File.join(destination_root, "config")
102
+ @dir = File.join(destination_root, 'config')
95
103
 
96
- @fname = File.join(@dir, "routes.rb")
104
+ @fname = File.join(@dir, 'routes.rb')
97
105
 
98
106
  # make dir if not exists
99
107
  FileUtils.mkdir_p(@dir)
100
108
 
101
- @f = File.open(@fname, 'w') {|f|
109
+ @f = File.open(@fname, 'w') do |f|
102
110
  f.write <<-RUBY
103
111
  Rails.application.routes.draw do
104
112
  patch '/chong', to: 'bong#index'
105
113
  end
106
114
  RUBY
107
- }
115
+ end
108
116
 
109
117
  run_generator
110
118
  end
@@ -125,7 +133,7 @@ module DeviseTokenAuth
125
133
 
126
134
  describe 'subsequent models' do
127
135
  before do
128
- run_generator %w(Mang mangs)
136
+ run_generator %w[Mang mangs]
129
137
  end
130
138
 
131
139
  test 'migration is created' do
@@ -151,14 +159,14 @@ module DeviseTokenAuth
151
159
  setup :prepare_destination
152
160
 
153
161
  before do
154
- @dir = File.join(destination_root, "app", "controllers")
162
+ @dir = File.join(destination_root, 'app', 'controllers')
155
163
 
156
- @fname = File.join(@dir, "application_controller.rb")
164
+ @fname = File.join(@dir, 'application_controller.rb')
157
165
 
158
166
  # make dir if not exists
159
167
  FileUtils.mkdir_p(@dir)
160
168
 
161
- @f = File.open(@fname, 'w') {|f|
169
+ @f = File.open(@fname, 'w') do |f|
162
170
  f.write <<-RUBY
163
171
  class ApplicationController < ActionController::Base
164
172
  def whatever
@@ -166,7 +174,7 @@ module DeviseTokenAuth
166
174
  end
167
175
  end
168
176
  RUBY
169
- }
177
+ end
170
178
 
171
179
  run_generator
172
180
  end
@@ -10,15 +10,15 @@ module DeviseTokenAuth
10
10
  destination Rails.root.join('tmp/generators')
11
11
 
12
12
  # The namespaced user model for testing
13
- let(:user_class) { "Azpire::V1::HumanResource::User" }
13
+ let(:user_class) { 'Azpire::V1::HumanResource::User' }
14
14
  let(:namespace_path) { user_class.underscore }
15
- let(:table_name) { user_class.pluralize.underscore.gsub("/","_") }
15
+ let(:table_name) { user_class.pluralize.underscore.gsub('/','_') }
16
16
 
17
17
  describe 'user model with namespace, clean install' do
18
18
  setup :prepare_destination
19
19
 
20
20
  before do
21
- run_generator %W(#{user_class} auth)
21
+ run_generator %W[#{user_class} auth]
22
22
  end
23
23
 
24
24
  test 'user model (with namespace) is created, concern is included' do
@@ -44,7 +44,16 @@ module DeviseTokenAuth
44
44
  end
45
45
 
46
46
  test 'subsequent runs raise no errors' do
47
- run_generator %W(#{user_class} auth)
47
+ run_generator %W[#{user_class} auth]
48
+ end
49
+
50
+ test 'add primary key type with rails 5 when specified in rails generator' do
51
+ run_generator %W[#{user_class} auth --primary_key_type=uuid --force]
52
+ if Rails::VERSION::MAJOR >= 5
53
+ assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb", /create_table\(:#{table_name}, id: :uuid\) do/
54
+ else
55
+ assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb", /create_table\(:#{table_name}\) do/
56
+ end
48
57
  end
49
58
  end
50
59
 
@@ -52,9 +61,9 @@ module DeviseTokenAuth
52
61
  setup :prepare_destination
53
62
 
54
63
  before do
55
- @dir = File.join(destination_root, "app", "models")
64
+ @dir = File.join(destination_root, 'app', 'models')
56
65
 
57
- @fname = File.join(@dir, "user.rb")
66
+ @fname = File.join(@dir, 'user.rb')
58
67
 
59
68
  # make dir if not exists
60
69
  FileUtils.mkdir_p(@dir)
@@ -62,7 +71,7 @@ module DeviseTokenAuth
62
71
  # account for rails version 5
63
72
  active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
64
73
 
65
- @f = File.open(@fname, 'w') {|f|
74
+ @f = File.open(@fname, 'w') do |f|
66
75
  f.write <<-RUBY
67
76
  class User < #{active_record_needle}
68
77
 
@@ -71,7 +80,7 @@ module DeviseTokenAuth
71
80
  end
72
81
  end
73
82
  RUBY
74
- }
83
+ end
75
84
 
76
85
  run_generator
77
86
  end
@@ -91,27 +100,26 @@ module DeviseTokenAuth
91
100
  end
92
101
  end
93
102
 
94
-
95
103
  describe 'routes' do
96
104
  setup :prepare_destination
97
105
 
98
106
  before do
99
- @dir = File.join(destination_root, "config")
107
+ @dir = File.join(destination_root, 'config')
100
108
 
101
- @fname = File.join(@dir, "routes.rb")
109
+ @fname = File.join(@dir, 'routes.rb')
102
110
 
103
111
  # make dir if not exists
104
112
  FileUtils.mkdir_p(@dir)
105
113
 
106
- @f = File.open(@fname, 'w') {|f|
114
+ @f = File.open(@fname, 'w') do |f|
107
115
  f.write <<-RUBY
108
116
  Rails.application.routes.draw do
109
117
  patch '/chong', to: 'bong#index'
110
118
  end
111
119
  RUBY
112
- }
120
+ end
113
121
 
114
- run_generator %W(#{user_class} auth)
122
+ run_generator %W[#{user_class} auth]
115
123
  end
116
124
 
117
125
  test 'route method for user model with namespace is appended to routes file' do
@@ -121,7 +129,7 @@ module DeviseTokenAuth
121
129
  end
122
130
 
123
131
  test 'subsequent runs do not modify file' do
124
- run_generator %W(#{user_class} auth)
132
+ run_generator %W[#{user_class} auth]
125
133
  assert_file 'config/routes.rb' do |routes|
126
134
  matches = routes.scan(/mount_devise_token_auth_for '#{user_class}', at: 'auth'/m).size
127
135
  assert_equal 1, matches
@@ -130,7 +138,7 @@ module DeviseTokenAuth
130
138
 
131
139
  describe 'subsequent models' do
132
140
  before do
133
- run_generator %w(Mang mangs)
141
+ run_generator %w[Mang mangs]
134
142
  end
135
143
 
136
144
  test 'migration is created' do
@@ -156,14 +164,14 @@ module DeviseTokenAuth
156
164
  setup :prepare_destination
157
165
 
158
166
  before do
159
- @dir = File.join(destination_root, "app", "controllers")
167
+ @dir = File.join(destination_root, 'app', 'controllers')
160
168
 
161
- @fname = File.join(@dir, "application_controller.rb")
169
+ @fname = File.join(@dir, 'application_controller.rb')
162
170
 
163
171
  # make dir if not exists
164
172
  FileUtils.mkdir_p(@dir)
165
173
 
166
- @f = File.open(@fname, 'w') {|f|
174
+ @f = File.open(@fname, 'w') do |f|
167
175
  f.write <<-RUBY
168
176
  class ApplicationController < ActionController::Base
169
177
  def whatever
@@ -171,9 +179,9 @@ module DeviseTokenAuth
171
179
  end
172
180
  end
173
181
  RUBY
174
- }
182
+ end
175
183
 
176
- run_generator %W(#{user_class} auth)
184
+ run_generator %W[#{user_class} auth]
177
185
  end
178
186
 
179
187
  test 'controller concern is appended to application controller' do
@@ -183,7 +191,7 @@ module DeviseTokenAuth
183
191
  end
184
192
 
185
193
  test 'subsequent runs do not modify file' do
186
- run_generator %W(#{user_class} auth)
194
+ run_generator %W[#{user_class} auth]
187
195
  assert_file 'app/controllers/application_controller.rb' do |controller|
188
196
  matches = controller.scan(/include DeviseTokenAuth::Concerns::SetUserByToken/m).size
189
197
  assert_equal 1, matches
@@ -16,7 +16,7 @@ module DeviseTokenAuth
16
16
  run_generator
17
17
  end
18
18
 
19
- test "files are copied" do
19
+ test 'files are copied' do
20
20
  assert_file 'app/views/devise/mailer/reset_password_instructions.html.erb'
21
21
  assert_file 'app/views/devise/mailer/confirmation_instructions.html.erb'
22
22
  end
@@ -4,21 +4,16 @@ require 'test_helper'
4
4
 
5
5
  class UserTest < ActiveSupport::TestCase
6
6
  describe User do
7
- before do
8
- @password = Faker::Internet.password(10, 20)
9
- @email = Faker::Internet.email
10
- @success_url = Faker::Internet.url
11
- @resource = User.new()
12
- end
13
-
14
7
  describe 'serialization' do
15
8
  test 'hash should not include sensitive info' do
9
+ @resource = build(:user)
16
10
  refute @resource.as_json[:tokens]
17
11
  end
18
12
  end
19
13
 
20
14
  describe 'creation' do
21
15
  test 'save fails if uid is missing' do
16
+ @resource = User.new
22
17
  @resource.uid = nil
23
18
  @resource.save
24
19
 
@@ -28,40 +23,25 @@ class UserTest < ActiveSupport::TestCase
28
23
 
29
24
  describe 'email registration' do
30
25
  test 'model should not save if email is blank' do
31
- @resource.provider = 'email'
32
- @resource.password = @password
33
- @resource.password_confirmation = @password
26
+ @resource = build(:user, email: nil)
34
27
 
35
28
  refute @resource.save
36
- assert @resource.errors.messages[:email] == [I18n.t("errors.messages.blank")]
29
+ assert @resource.errors.messages[:email] == [I18n.t('errors.messages.blank')]
37
30
  end
38
31
 
39
32
  test 'model should not save if email is not an email' do
40
- @resource.provider = 'email'
41
- @resource.email = '@example.com'
42
- @resource.password = @password
43
- @resource.password_confirmation = @password
33
+ @resource = build(:user, email: '@example.com')
44
34
 
45
35
  refute @resource.save
46
- assert @resource.errors.messages[:email] == [I18n.t("errors.messages.not_email")]
36
+ assert @resource.errors.messages[:email] == [I18n.t('errors.messages.not_email')]
47
37
  end
48
38
  end
49
39
 
50
40
  describe 'email uniqueness' do
51
41
  test 'model should not save if email is taken' do
52
- provider = 'email'
53
-
54
- User.create(
55
- email: @email,
56
- provider: provider,
57
- password: @password,
58
- password_confirmation: @password
59
- )
60
-
61
- @resource.email = @email
62
- @resource.provider = provider
63
- @resource.password = @password
64
- @resource.password_confirmation = @password
42
+ user_attributes = attributes_for(:user)
43
+ create(:user, user_attributes)
44
+ @resource = build(:user, user_attributes)
65
45
 
66
46
  refute @resource.save
67
47
  assert @resource.errors.messages[:email] == [I18n.t('errors.messages.taken')]
@@ -71,10 +51,7 @@ class UserTest < ActiveSupport::TestCase
71
51
 
72
52
  describe 'oauth2 authentication' do
73
53
  test 'model should save even if email is blank' do
74
- @resource.provider = 'facebook'
75
- @resource.uid = 123
76
- @resource.password = @password
77
- @resource.password_confirmation = @password
54
+ @resource = build(:user, :facebook, email: nil)
78
55
 
79
56
  assert @resource.save
80
57
  assert @resource.errors.messages[:email].blank?
@@ -83,9 +60,7 @@ class UserTest < ActiveSupport::TestCase
83
60
 
84
61
  describe 'token expiry' do
85
62
  before do
86
- @resource = users(:confirmed_email_user)
87
- @resource.skip_confirmation!
88
- @resource.save!
63
+ @resource = create(:user, :confirmed)
89
64
 
90
65
  @auth_headers = @resource.create_new_auth_token
91
66
 
@@ -103,9 +78,7 @@ class UserTest < ActiveSupport::TestCase
103
78
 
104
79
  describe 'user specific token lifespan' do
105
80
  before do
106
- @resource = users(:confirmed_email_user)
107
- @resource.skip_confirmation!
108
- @resource.save!
81
+ @resource = create(:user, :confirmed)
109
82
 
110
83
  auth_headers = @resource.create_new_auth_token
111
84
  @token_global = auth_headers['access-token']
@@ -137,9 +110,7 @@ class UserTest < ActiveSupport::TestCase
137
110
 
138
111
  describe 'expired tokens are destroyed on save' do
139
112
  before do
140
- @resource = users(:confirmed_email_user)
141
- @resource.skip_confirmation!
142
- @resource.save!
113
+ @resource = create(:user, :confirmed)
143
114
 
144
115
  @old_auth_headers = @resource.create_new_auth_token
145
116
  @new_auth_headers = @resource.create_new_auth_token
@@ -151,15 +122,13 @@ class UserTest < ActiveSupport::TestCase
151
122
  end
152
123
 
153
124
  test 'current token was not removed' do
154
- assert @resource.tokens[@new_auth_headers["client"]]
125
+ assert @resource.tokens[@new_auth_headers['client']]
155
126
  end
156
127
  end
157
128
 
158
129
  describe 'nil tokens are handled properly' do
159
130
  before do
160
- @resource = users(:confirmed_email_user)
161
- @resource.skip_confirmation!
162
- @resource.save!
131
+ @resource = create(:user, :confirmed)
163
132
  end
164
133
 
165
134
  test 'tokens can be set to nil' do