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.

165 lines
5.3 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 'validations' do
  6. it 'is invalid without an account' do
  7. user = Fabricate.build(:user, account: nil)
  8. user.valid?
  9. expect(user).to model_have_error_on_field(:account)
  10. end
  11. it 'is invalid without a valid locale' do
  12. user = Fabricate.build(:user, locale: 'toto')
  13. user.valid?
  14. expect(user).to model_have_error_on_field(:locale)
  15. end
  16. it 'is invalid without a valid email' do
  17. user = Fabricate.build(:user, email: 'john@')
  18. user.valid?
  19. expect(user).to model_have_error_on_field(:email)
  20. end
  21. end
  22. describe 'settings' do
  23. it 'inherits default settings from default yml' do
  24. expect(Setting.boost_modal).to eq false
  25. expect(Setting.interactions['must_be_follower']).to eq false
  26. user = User.new
  27. expect(user.settings.boost_modal).to eq false
  28. expect(user.settings.interactions['must_be_follower']).to eq false
  29. end
  30. it 'can update settings' do
  31. user = Fabricate(:user)
  32. expect(user.settings['interactions']['must_be_follower']).to eq false
  33. user.settings['interactions'] = user.settings['interactions'].merge('must_be_follower' => true)
  34. user.reload
  35. expect(user.settings['interactions']['must_be_follower']).to eq true
  36. end
  37. xit 'does not mutate defaults via the cache' do
  38. user = Fabricate(:user)
  39. user.settings['interactions']['must_be_follower'] = true
  40. # TODO
  41. # This mutates the global settings default such that future user
  42. # instances will inherit the incorrect starting values
  43. other = Fabricate(:user)
  44. expect(other.settings['interactions']['must_be_follower']).to eq false
  45. end
  46. end
  47. describe 'scopes' do
  48. describe 'recent' do
  49. it 'returns an array of recent users ordered by id' do
  50. user_1 = Fabricate(:user)
  51. user_2 = Fabricate(:user)
  52. expect(User.recent).to match_array([user_2, user_1])
  53. end
  54. end
  55. describe 'admins' do
  56. it 'returns an array of users who are admin' do
  57. user_1 = Fabricate(:user, admin: false)
  58. user_2 = Fabricate(:user, admin: true)
  59. expect(User.admins).to match_array([user_2])
  60. end
  61. end
  62. describe 'confirmed' do
  63. it 'returns an array of users who are confirmed' do
  64. user_1 = Fabricate(:user, confirmed_at: nil)
  65. user_2 = Fabricate(:user, confirmed_at: Time.now)
  66. expect(User.confirmed).to match_array([user_2])
  67. end
  68. end
  69. end
  70. let(:account) { Fabricate(:account, username: 'alice') }
  71. let(:password) { 'abcd1234' }
  72. describe 'blacklist' do
  73. around(:each) do |example|
  74. old_blacklist = Rails.configuration.x.email_blacklist
  75. Rails.configuration.x.email_domains_blacklist = 'mvrht.com'
  76. example.run
  77. Rails.configuration.x.email_domains_blacklist = old_blacklist
  78. end
  79. it 'should allow a non-blacklisted user to be created' do
  80. user = User.new(email: 'foo@example.com', account: account, password: password)
  81. expect(user.valid?).to be_truthy
  82. end
  83. it 'should not allow a blacklisted user to be created' do
  84. user = User.new(email: 'foo@mvrht.com', account: account, password: password)
  85. expect(user.valid?).to be_falsey
  86. end
  87. it 'should not allow a subdomain blacklisted user to be created' do
  88. user = User.new(email: 'foo@mvrht.com.topdomain.tld', account: account, password: password)
  89. expect(user.valid?).to be_falsey
  90. end
  91. end
  92. describe '#confirmed?' do
  93. it 'returns true when a confirmed_at is set' do
  94. user = Fabricate.build(:user, confirmed_at: Time.now.utc)
  95. expect(user.confirmed?).to be true
  96. end
  97. it 'returns false if a confirmed_at is nil' do
  98. user = Fabricate.build(:user, confirmed_at: nil)
  99. expect(user.confirmed?).to be false
  100. end
  101. end
  102. describe 'whitelist' do
  103. around(:each) do |example|
  104. old_whitelist = Rails.configuration.x.email_whitelist
  105. Rails.configuration.x.email_domains_whitelist = 'mastodon.space'
  106. example.run
  107. Rails.configuration.x.email_domains_whitelist = old_whitelist
  108. end
  109. it 'should not allow a user to be created unless they are whitelisted' do
  110. user = User.new(email: 'foo@example.com', account: account, password: password)
  111. expect(user.valid?).to be_falsey
  112. end
  113. it 'should allow a user to be created if they are whitelisted' do
  114. user = User.new(email: 'foo@mastodon.space', account: account, password: password)
  115. expect(user.valid?).to be_truthy
  116. end
  117. it 'should not allow a user with a whitelisted top domain as subdomain in their email address to be created' do
  118. user = User.new(email: 'foo@mastodon.space.userdomain.com', account: account, password: password)
  119. expect(user.valid?).to be_falsey
  120. end
  121. it 'should not allow a user to be created with a specific blacklisted subdomain even if the top domain is whitelisted' do
  122. old_blacklist = Rails.configuration.x.email_blacklist
  123. Rails.configuration.x.email_domains_blacklist = 'blacklisted.mastodon.space'
  124. user = User.new(email: 'foo@blacklisted.mastodon.space', account: account, password: password)
  125. expect(user.valid?).to be_falsey
  126. Rails.configuration.x.email_domains_blacklist = old_blacklist
  127. end
  128. end
  129. end