闭社主体 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.

134 lines
4.1 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. it 'should allow a non-blacklisted user to be created' do
  74. user = User.new(email: 'foo@example.com', account: account, password: password)
  75. expect(user.valid?).to be_truthy
  76. end
  77. it 'should not allow a blacklisted user to be created' do
  78. user = User.new(email: 'foo@mvrht.com', account: account, password: password)
  79. expect(user.valid?).to be_falsey
  80. end
  81. end
  82. describe '#confirmed?' do
  83. it 'returns true when a confirmed_at is set' do
  84. user = Fabricate.build(:user, confirmed_at: Time.now.utc)
  85. expect(user.confirmed?).to be true
  86. end
  87. it 'returns false if a confirmed_at is nil' do
  88. user = Fabricate.build(:user, confirmed_at: nil)
  89. expect(user.confirmed?).to be false
  90. end
  91. end
  92. describe 'whitelist' do
  93. around(:each) do |example|
  94. old_whitelist = Rails.configuration.x.email_whitelist
  95. Rails.configuration.x.email_domains_whitelist = 'mastodon.space'
  96. example.run
  97. Rails.configuration.x.email_domains_whitelist = old_whitelist
  98. end
  99. it 'should not allow a user to be created unless they are whitelisted' do
  100. user = User.new(email: 'foo@example.com', account: account, password: password)
  101. expect(user.valid?).to be_falsey
  102. end
  103. it 'should allow a user to be created if they are whitelisted' do
  104. user = User.new(email: 'foo@mastodon.space', account: account, password: password)
  105. expect(user.valid?).to be_truthy
  106. end
  107. end
  108. end