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.

88 lines
2.6 KiB

7 years ago
7 years ago
7 years ago
  1. require 'rails_helper'
  2. RSpec.describe User, type: :model do
  3. describe 'validations' do
  4. it 'is invalid without an account' do
  5. user = Fabricate.build(:user, account: nil)
  6. user.valid?
  7. expect(user).to model_have_error_on_field(:account)
  8. end
  9. it 'is invalid without a valid locale' do
  10. user = Fabricate.build(:user, locale: 'toto')
  11. user.valid?
  12. expect(user).to model_have_error_on_field(:locale)
  13. end
  14. it 'is invalid without a valid email' do
  15. user = Fabricate.build(:user, email: 'john@')
  16. user.valid?
  17. expect(user).to model_have_error_on_field(:email)
  18. end
  19. end
  20. describe 'scopes' do
  21. describe 'recent' do
  22. it 'returns an array of recent users ordered by id' do
  23. user_1 = Fabricate(:user)
  24. user_2 = Fabricate(:user)
  25. expect(User.recent).to match_array([user_2, user_1])
  26. end
  27. end
  28. describe 'admins' do
  29. it 'returns an array of users who are admin' do
  30. user_1 = Fabricate(:user, admin: false)
  31. user_2 = Fabricate(:user, admin: true)
  32. expect(User.admins).to match_array([user_2])
  33. end
  34. end
  35. describe 'confirmed' do
  36. it 'returns an array of users who are confirmed' do
  37. user_1 = Fabricate(:user, confirmed_at: nil)
  38. user_2 = Fabricate(:user, confirmed_at: Time.now)
  39. expect(User.confirmed).to match_array([user_2])
  40. end
  41. end
  42. end
  43. let(:account) { Fabricate(:account, username: 'alice') }
  44. let(:password) { 'abcd1234' }
  45. describe 'blacklist' do
  46. it 'should allow a non-blacklisted user to be created' do
  47. user = User.new(email: 'foo@example.com', account: account, password: password)
  48. expect(user.valid?).to be_truthy
  49. end
  50. it 'should not allow a blacklisted user to be created' do
  51. user = User.new(email: 'foo@mvrht.com', account: account, password: password)
  52. expect(user.valid?).to be_falsey
  53. end
  54. end
  55. describe 'whitelist' do
  56. around(:each) do |example|
  57. old_whitelist = Rails.configuration.x.email_whitelist
  58. Rails.configuration.x.email_domains_whitelist = 'mastodon.space'
  59. example.run
  60. Rails.configuration.x.email_domains_whitelist = old_whitelist
  61. end
  62. it 'should not allow a user to be created unless they are whitelisted' do
  63. user = User.new(email: 'foo@example.com', account: account, password: password)
  64. expect(user.valid?).to be_falsey
  65. end
  66. it 'should allow a user to be created if they are whitelisted' do
  67. user = User.new(email: 'foo@mastodon.space', account: account, password: password)
  68. expect(user.valid?).to be_truthy
  69. end
  70. end
  71. end