user_spec.rb 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 'otp_secret' do
  6. it 'is encrypted with OTP_SECRET environment variable' do
  7. user = Fabricate(:user,
  8. encrypted_otp_secret: "Fttsy7QAa0edaDfdfSz094rRLAxc8cJweDQ4BsWH/zozcdVA8o9GLqcKhn2b\nGi/V\n",
  9. encrypted_otp_secret_iv: 'rys3THICkr60BoWC',
  10. encrypted_otp_secret_salt: '_LMkAGvdg7a+sDIKjI3mR2Q==')
  11. expect(user.otp_secret).to eq 'anotpsecretthatshouldbeencrypted'
  12. end
  13. end
  14. describe 'validations' do
  15. it 'is invalid without an account' do
  16. user = Fabricate.build(:user, account: nil)
  17. user.valid?
  18. expect(user).to model_have_error_on_field(:account)
  19. end
  20. it 'is invalid without a valid locale' do
  21. user = Fabricate.build(:user, locale: 'toto')
  22. user.valid?
  23. expect(user).to model_have_error_on_field(:locale)
  24. end
  25. it 'is invalid without a valid email' do
  26. user = Fabricate.build(:user, email: 'john@')
  27. user.valid?
  28. expect(user).to model_have_error_on_field(:email)
  29. end
  30. it 'is valid with an invalid e-mail that has already been saved' do
  31. user = Fabricate.build(:user, email: 'invalid-email')
  32. user.save(validate: false)
  33. expect(user.valid?).to be true
  34. end
  35. it 'cleans out empty string from languages' do
  36. user = Fabricate.build(:user, filtered_languages: [''])
  37. user.valid?
  38. expect(user.filtered_languages).to eq []
  39. end
  40. end
  41. describe 'scopes' do
  42. describe 'recent' do
  43. it 'returns an array of recent users ordered by id' do
  44. user_1 = Fabricate(:user)
  45. user_2 = Fabricate(:user)
  46. expect(User.recent).to eq [user_2, user_1]
  47. end
  48. end
  49. describe 'admins' do
  50. it 'returns an array of users who are admin' do
  51. user_1 = Fabricate(:user, admin: false)
  52. user_2 = Fabricate(:user, admin: true)
  53. expect(User.admins).to match_array([user_2])
  54. end
  55. end
  56. describe 'confirmed' do
  57. it 'returns an array of users who are confirmed' do
  58. user_1 = Fabricate(:user, confirmed_at: nil)
  59. user_2 = Fabricate(:user, confirmed_at: Time.now)
  60. expect(User.confirmed).to match_array([user_2])
  61. end
  62. end
  63. describe 'inactive' do
  64. it 'returns a relation of inactive users' do
  65. specified = Fabricate(:user, current_sign_in_at: 15.days.ago)
  66. Fabricate(:user, current_sign_in_at: 13.days.ago)
  67. expect(User.inactive).to match_array([specified])
  68. end
  69. end
  70. describe 'matches_email' do
  71. it 'returns a relation of users whose email starts with the given string' do
  72. specified = Fabricate(:user, email: 'specified@spec')
  73. Fabricate(:user, email: 'unspecified@spec')
  74. expect(User.matches_email('specified')).to match_array([specified])
  75. end
  76. end
  77. describe 'with_recent_ip_address' do
  78. it 'returns a relation of users who is, or was at last time, online with the given IP address' do
  79. specifieds = [
  80. Fabricate(:user, current_sign_in_ip: '0.0.0.42', last_sign_in_ip: '0.0.0.0'),
  81. Fabricate(:user, current_sign_in_ip: nil, last_sign_in_ip: '0.0.0.42')
  82. ]
  83. Fabricate(:user, current_sign_in_ip: '0.0.0.0', last_sign_in_ip: '0.0.0.0')
  84. expect(User.with_recent_ip_address('0.0.0.42')).to match_array(specifieds)
  85. end
  86. end
  87. end
  88. let(:account) { Fabricate(:account, username: 'alice') }
  89. let(:password) { 'abcd1234' }
  90. describe 'blacklist' do
  91. around(:each) do |example|
  92. old_blacklist = Rails.configuration.x.email_blacklist
  93. Rails.configuration.x.email_domains_blacklist = 'mvrht.com'
  94. example.run
  95. Rails.configuration.x.email_domains_blacklist = old_blacklist
  96. end
  97. it 'should allow a non-blacklisted user to be created' do
  98. user = User.new(email: 'foo@example.com', account: account, password: password)
  99. expect(user.valid?).to be_truthy
  100. end
  101. it 'should not allow a blacklisted user to be created' do
  102. user = User.new(email: 'foo@mvrht.com', account: account, password: password)
  103. expect(user.valid?).to be_falsey
  104. end
  105. it 'should not allow a subdomain blacklisted user to be created' do
  106. user = User.new(email: 'foo@mvrht.com.topdomain.tld', account: account, password: password)
  107. expect(user.valid?).to be_falsey
  108. end
  109. end
  110. describe '#confirmed?' do
  111. it 'returns true when a confirmed_at is set' do
  112. user = Fabricate.build(:user, confirmed_at: Time.now.utc)
  113. expect(user.confirmed?).to be true
  114. end
  115. it 'returns false if a confirmed_at is nil' do
  116. user = Fabricate.build(:user, confirmed_at: nil)
  117. expect(user.confirmed?).to be false
  118. end
  119. end
  120. describe '#disable_two_factor!' do
  121. it 'saves false for otp_required_for_login' do
  122. user = Fabricate.build(:user, otp_required_for_login: true)
  123. user.disable_two_factor!
  124. expect(user.reload.otp_required_for_login).to be false
  125. end
  126. it 'saves cleared otp_backup_codes' do
  127. user = Fabricate.build(:user, otp_backup_codes: %w(dummy dummy))
  128. user.disable_two_factor!
  129. expect(user.reload.otp_backup_codes.empty?).to be true
  130. end
  131. end
  132. describe '#send_confirmation_instructions' do
  133. around do |example|
  134. queue_adapter = ActiveJob::Base.queue_adapter
  135. example.run
  136. ActiveJob::Base.queue_adapter = queue_adapter
  137. end
  138. it 'delivers confirmation instructions later' do
  139. user = Fabricate(:user)
  140. ActiveJob::Base.queue_adapter = :test
  141. expect { user.send_confirmation_instructions }.to have_enqueued_job(ActionMailer::DeliveryJob)
  142. end
  143. end
  144. describe '#setting_auto_play_gif' do
  145. it 'returns auto-play gif setting' do
  146. user = Fabricate(:user)
  147. user.settings[:auto_play_gif] = false
  148. expect(user.setting_auto_play_gif).to eq false
  149. end
  150. end
  151. describe '#setting_system_font_ui' do
  152. it 'returns system font ui setting' do
  153. user = Fabricate(:user)
  154. user.settings[:system_font_ui] = false
  155. expect(user.setting_system_font_ui).to eq false
  156. end
  157. end
  158. describe '#setting_boost_modal' do
  159. it 'returns boost modal setting' do
  160. user = Fabricate(:user)
  161. user.settings[:boost_modal] = false
  162. expect(user.setting_boost_modal).to eq false
  163. end
  164. end
  165. describe '#setting_default_privacy' do
  166. it 'returns default privacy setting if user has configured' do
  167. user = Fabricate(:user)
  168. user.settings[:default_privacy] = 'unlisted'
  169. expect(user.setting_default_privacy).to eq 'unlisted'
  170. end
  171. it "returns 'private' if user has not configured default privacy setting and account is locked" do
  172. user = Fabricate(:user, account: Fabricate(:account, locked: true))
  173. expect(user.setting_default_privacy).to eq 'private'
  174. end
  175. it "returns 'public' if user has not configured default privacy setting and account is not locked" do
  176. user = Fabricate(:user, account: Fabricate(:account, locked: false))
  177. expect(user.setting_default_privacy).to eq 'public'
  178. end
  179. end
  180. describe '#setting_unfollow_modal' do
  181. it 'returns unfollow modal setting' do
  182. user = Fabricate(:user)
  183. user.settings[:unfollow_modal] = true
  184. expect(user.setting_unfollow_modal).to eq true
  185. end
  186. end
  187. describe '#setting_delete_modal' do
  188. it 'returns delete modal setting' do
  189. user = Fabricate(:user)
  190. user.settings[:delete_modal] = false
  191. expect(user.setting_delete_modal).to eq false
  192. end
  193. end
  194. describe 'whitelist' do
  195. around(:each) do |example|
  196. old_whitelist = Rails.configuration.x.email_whitelist
  197. Rails.configuration.x.email_domains_whitelist = 'mastodon.space'
  198. example.run
  199. Rails.configuration.x.email_domains_whitelist = old_whitelist
  200. end
  201. it 'should not allow a user to be created unless they are whitelisted' do
  202. user = User.new(email: 'foo@example.com', account: account, password: password)
  203. expect(user.valid?).to be_falsey
  204. end
  205. it 'should allow a user to be created if they are whitelisted' do
  206. user = User.new(email: 'foo@mastodon.space', account: account, password: password)
  207. expect(user.valid?).to be_truthy
  208. end
  209. it 'should not allow a user with a whitelisted top domain as subdomain in their email address to be created' do
  210. user = User.new(email: 'foo@mastodon.space.userdomain.com', account: account, password: password)
  211. expect(user.valid?).to be_falsey
  212. end
  213. context do
  214. around do |example|
  215. old_blacklist = Rails.configuration.x.email_blacklist
  216. example.run
  217. Rails.configuration.x.email_domains_blacklist = old_blacklist
  218. end
  219. it 'should not allow a user to be created with a specific blacklisted subdomain even if the top domain is whitelisted' do
  220. Rails.configuration.x.email_domains_blacklist = 'blacklisted.mastodon.space'
  221. user = User.new(email: 'foo@blacklisted.mastodon.space', account: account, password: password)
  222. expect(user.valid?).to be_falsey
  223. end
  224. end
  225. end
  226. it_behaves_like 'Settings-extended' do
  227. def create!
  228. User.create!(account: Fabricate(:account), email: 'foo@mastodon.space', password: 'abcd1234' )
  229. end
  230. def fabricate
  231. Fabricate(:user)
  232. end
  233. end
  234. describe 'token_for_app' do
  235. let(:user) { Fabricate(:user) }
  236. let(:app) { Fabricate(:application, owner: user) }
  237. it 'returns a token' do
  238. expect(user.token_for_app(app)).to be_a(Doorkeeper::AccessToken)
  239. end
  240. it 'persists a token' do
  241. t = user.token_for_app(app)
  242. expect(user.token_for_app(app)).to eql(t)
  243. end
  244. it 'is nil if user does not own app' do
  245. app.update!(owner: nil)
  246. expect(user.token_for_app(app)).to be_nil
  247. end
  248. end
  249. end