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

91 lines
2.7 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 'scopes' do
  23. describe 'recent' do
  24. it 'returns an array of recent users ordered by id' do
  25. user_1 = Fabricate(:user)
  26. user_2 = Fabricate(:user)
  27. expect(User.recent).to match_array([user_2, user_1])
  28. end
  29. end
  30. describe 'admins' do
  31. it 'returns an array of users who are admin' do
  32. user_1 = Fabricate(:user, admin: false)
  33. user_2 = Fabricate(:user, admin: true)
  34. expect(User.admins).to match_array([user_2])
  35. end
  36. end
  37. describe 'confirmed' do
  38. it 'returns an array of users who are confirmed' do
  39. user_1 = Fabricate(:user, confirmed_at: nil)
  40. user_2 = Fabricate(:user, confirmed_at: Time.now)
  41. expect(User.confirmed).to match_array([user_2])
  42. end
  43. end
  44. end
  45. let(:account) { Fabricate(:account, username: 'alice') }
  46. let(:password) { 'abcd1234' }
  47. describe 'blacklist' do
  48. it 'should allow a non-blacklisted user to be created' do
  49. user = User.new(email: 'foo@example.com', account: account, password: password)
  50. expect(user.valid?).to be_truthy
  51. end
  52. it 'should not allow a blacklisted user to be created' do
  53. user = User.new(email: 'foo@mvrht.com', account: account, password: password)
  54. expect(user.valid?).to be_falsey
  55. end
  56. end
  57. describe 'whitelist' do
  58. around(:each) do |example|
  59. old_whitelist = Rails.configuration.x.email_whitelist
  60. Rails.configuration.x.email_domains_whitelist = 'mastodon.space'
  61. example.run
  62. Rails.configuration.x.email_domains_whitelist = old_whitelist
  63. end
  64. it 'should not allow a user to be created unless they are whitelisted' do
  65. user = User.new(email: 'foo@example.com', account: account, password: password)
  66. expect(user.valid?).to be_falsey
  67. end
  68. it 'should allow a user to be created if they are whitelisted' do
  69. user = User.new(email: 'foo@mastodon.space', account: account, password: password)
  70. expect(user.valid?).to be_truthy
  71. end
  72. end
  73. end