闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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