You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
8.8 KiB

7 years ago
7 years ago
7 years ago
  1. require 'rails_helper'
  2. require 'devise_two_factor/spec_helpers'
  3. RSpec.describe User, type: :model do
  4. it_behaves_like 'two_factor_backupable'
  5. describe 'otp_secret' do
  6. it 'is encrypted with OTP_SECRET environment variable' do
  7. user = Fabricate(:user,
  8. encrypted_otp_secret: "Fttsy7QAa0edaDfdfSz094rRLAxc8cJweDQ4BsWH/zozcdVA8o9GLqcKhn2b\nGi/V\n",
  9. encrypted_otp_secret_iv: 'rys3THICkr60BoWC',
  10. encrypted_otp_secret_salt: '_LMkAGvdg7a+sDIKjI3mR2Q==')
  11. expect(user.otp_secret).to eq 'anotpsecretthatshouldbeencrypted'
  12. end
  13. end
  14. describe 'validations' do
  15. it 'is invalid without an account' do
  16. user = Fabricate.build(:user, account: nil)
  17. user.valid?
  18. expect(user).to model_have_error_on_field(:account)
  19. end
  20. it 'is invalid without a valid locale' do
  21. user = Fabricate.build(:user, locale: 'toto')
  22. user.valid?
  23. expect(user).to model_have_error_on_field(:locale)
  24. end
  25. it 'is invalid without a valid email' do
  26. user = Fabricate.build(:user, email: 'john@')
  27. user.valid?
  28. expect(user).to model_have_error_on_field(:email)
  29. end
  30. it 'is valid with an invalid e-mail that has already been saved' do
  31. user = Fabricate.build(:user, email: 'invalid-email')
  32. user.save(validate: false)
  33. expect(user.valid?).to be true
  34. end
  35. it 'cleans out empty string from languages' do
  36. user = Fabricate.build(:user, filtered_languages: [''])
  37. user.valid?
  38. expect(user.filtered_languages).to eq []
  39. end
  40. end
  41. describe 'scopes' do
  42. describe 'recent' do
  43. it 'returns an array of recent users ordered by id' do
  44. user_1 = Fabricate(:user)
  45. user_2 = Fabricate(:user)
  46. expect(User.recent).to eq [user_2, user_1]
  47. end
  48. end
  49. describe 'admins' do
  50. it 'returns an array of users who are admin' do
  51. user_1 = Fabricate(:user, admin: false)
  52. user_2 = Fabricate(:user, admin: true)
  53. expect(User.admins).to match_array([user_2])
  54. end
  55. end
  56. describe 'confirmed' do
  57. it 'returns an array of users who are confirmed' do
  58. user_1 = Fabricate(:user, confirmed_at: nil)
  59. user_2 = Fabricate(:user, confirmed_at: Time.now)
  60. expect(User.confirmed).to match_array([user_2])
  61. end
  62. end
  63. describe 'inactive' do
  64. it 'returns a relation of inactive users' do
  65. specified = Fabricate(:user, current_sign_in_at: 15.days.ago)
  66. Fabricate(:user, current_sign_in_at: 13.days.ago)
  67. expect(User.inactive).to match_array([specified])
  68. end
  69. end
  70. describe 'matches_email' do
  71. it 'returns a relation of users whose email starts with the given string' do
  72. specified = Fabricate(:user, email: 'specified@spec')
  73. Fabricate(:user, email: 'unspecified@spec')
  74. expect(User.matches_email('specified')).to match_array([specified])
  75. end
  76. end
  77. describe 'with_recent_ip_address' do
  78. it 'returns a relation of users who is, or was at last time, online with the given IP address' do
  79. specifieds = [
  80. Fabricate(:user, current_sign_in_ip: '0.0.0.42', last_sign_in_ip: '0.0.0.0'),
  81. Fabricate(:user, current_sign_in_ip: nil, last_sign_in_ip: '0.0.0.42')
  82. ]
  83. Fabricate(:user, current_sign_in_ip: '0.0.0.0', last_sign_in_ip: '0.0.0.0')
  84. expect(User.with_recent_ip_address('0.0.0.42')).to match_array(specifieds)
  85. end
  86. end
  87. end
  88. let(:account) { Fabricate(:account, username: 'alice') }
  89. let(:password) { 'abcd1234' }
  90. describe 'blacklist' do
  91. around(:each) do |example|
  92. old_blacklist = Rails.configuration.x.email_blacklist
  93. Rails.configuration.x.email_domains_blacklist = 'mvrht.com'
  94. example.run
  95. Rails.configuration.x.email_domains_blacklist = old_blacklist
  96. end
  97. it 'should allow a non-blacklisted user to be created' do
  98. user = User.new(email: 'foo@example.com', account: account, password: password)
  99. expect(user.valid?).to be_truthy
  100. end
  101. it 'should not allow a blacklisted user to be created' do
  102. user = User.new(email: 'foo@mvrht.com', account: account, password: password)
  103. expect(user.valid?).to be_falsey
  104. end
  105. it 'should not allow a subdomain blacklisted user to be created' do
  106. user = User.new(email: 'foo@mvrht.com.topdomain.tld', account: account, password: password)
  107. expect(user.valid?).to be_falsey
  108. end
  109. end
  110. describe '#confirmed?' do
  111. it 'returns true when a confirmed_at is set' do
  112. user = Fabricate.build(:user, confirmed_at: Time.now.utc)
  113. expect(user.confirmed?).to be true
  114. end
  115. it 'returns false if a confirmed_at is nil' do
  116. user = Fabricate.build(:user, confirmed_at: nil)
  117. expect(user.confirmed?).to be false
  118. end
  119. end
  120. describe '#disable_two_factor!' do
  121. it 'saves false for otp_required_for_login' do
  122. user = Fabricate.build(:user, otp_required_for_login: true)
  123. user.disable_two_factor!
  124. expect(user.reload.otp_required_for_login).to be false
  125. end
  126. it 'saves cleared otp_backup_codes' do
  127. user = Fabricate.build(:user, otp_backup_codes: %w(dummy dummy))
  128. user.disable_two_factor!
  129. expect(user.reload.otp_backup_codes.empty?).to be true
  130. end
  131. end
  132. describe '#send_confirmation_instructions' do
  133. around do |example|
  134. queue_adapter = ActiveJob::Base.queue_adapter
  135. example.run
  136. ActiveJob::Base.queue_adapter = queue_adapter
  137. end
  138. it 'delivers confirmation instructions later' do
  139. user = Fabricate(:user)
  140. ActiveJob::Base.queue_adapter = :test
  141. expect { user.send_confirmation_instructions }.to have_enqueued_job(ActionMailer::DeliveryJob)
  142. end
  143. end
  144. describe 'settings' do
  145. it 'is instance of Settings::ScopedSettings' do
  146. user = Fabricate(:user)
  147. expect(user.settings).to be_kind_of Settings::ScopedSettings
  148. end
  149. end
  150. describe '#setting_default_privacy' do
  151. it 'returns default privacy setting if user has configured' do
  152. user = Fabricate(:user)
  153. user.settings[:default_privacy] = 'unlisted'
  154. expect(user.setting_default_privacy).to eq 'unlisted'
  155. end
  156. it "returns 'private' if user has not configured default privacy setting and account is locked" do
  157. user = Fabricate(:user, account: Fabricate(:account, locked: true))
  158. expect(user.setting_default_privacy).to eq 'private'
  159. end
  160. it "returns 'public' if user has not configured default privacy setting and account is not locked" do
  161. user = Fabricate(:user, account: Fabricate(:account, locked: false))
  162. expect(user.setting_default_privacy).to eq 'public'
  163. end
  164. end
  165. describe 'whitelist' do
  166. around(:each) do |example|
  167. old_whitelist = Rails.configuration.x.email_whitelist
  168. Rails.configuration.x.email_domains_whitelist = 'mastodon.space'
  169. example.run
  170. Rails.configuration.x.email_domains_whitelist = old_whitelist
  171. end
  172. it 'should not allow a user to be created unless they are whitelisted' do
  173. user = User.new(email: 'foo@example.com', account: account, password: password)
  174. expect(user.valid?).to be_falsey
  175. end
  176. it 'should allow a user to be created if they are whitelisted' do
  177. user = User.new(email: 'foo@mastodon.space', account: account, password: password)
  178. expect(user.valid?).to be_truthy
  179. end
  180. it 'should not allow a user with a whitelisted top domain as subdomain in their email address to be created' do
  181. user = User.new(email: 'foo@mastodon.space.userdomain.com', account: account, password: password)
  182. expect(user.valid?).to be_falsey
  183. end
  184. context do
  185. around do |example|
  186. old_blacklist = Rails.configuration.x.email_blacklist
  187. example.run
  188. Rails.configuration.x.email_domains_blacklist = old_blacklist
  189. end
  190. it 'should not allow a user to be created with a specific blacklisted subdomain even if the top domain is whitelisted' do
  191. Rails.configuration.x.email_domains_blacklist = 'blacklisted.mastodon.space'
  192. user = User.new(email: 'foo@blacklisted.mastodon.space', account: account, password: password)
  193. expect(user.valid?).to be_falsey
  194. end
  195. end
  196. end
  197. it_behaves_like 'Settings-extended' do
  198. def create!
  199. User.create!(account: Fabricate(:account), email: 'foo@mastodon.space', password: 'abcd1234' )
  200. end
  201. def fabricate
  202. Fabricate(:user)
  203. end
  204. end
  205. describe 'token_for_app' do
  206. let(:user) { Fabricate(:user) }
  207. let(:app) { Fabricate(:application, owner: user) }
  208. it 'returns a token' do
  209. expect(user.token_for_app(app)).to be_a(Doorkeeper::AccessToken)
  210. end
  211. it 'persists a token' do
  212. t = user.token_for_app(app)
  213. expect(user.token_for_app(app)).to eql(t)
  214. end
  215. it 'is nil if user does not own app' do
  216. app.update!(owner: nil)
  217. expect(user.token_for_app(app)).to be_nil
  218. end
  219. end
  220. end