user_spec.rb 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. require 'devise_two_factor/spec_helpers'
  4. RSpec.describe User do
  5. let(:password) { 'abcd1234' }
  6. let(:account) { Fabricate(:account, username: 'alice') }
  7. it_behaves_like 'two_factor_backupable'
  8. describe 'otp_secret' do
  9. it 'is encrypted with OTP_SECRET environment variable' do
  10. user = Fabricate(:user,
  11. encrypted_otp_secret: "Fttsy7QAa0edaDfdfSz094rRLAxc8cJweDQ4BsWH/zozcdVA8o9GLqcKhn2b\nGi/V\n",
  12. encrypted_otp_secret_iv: 'rys3THICkr60BoWC',
  13. encrypted_otp_secret_salt: '_LMkAGvdg7a+sDIKjI3mR2Q==')
  14. expect(user.otp_secret).to eq 'anotpsecretthatshouldbeencrypted'
  15. end
  16. end
  17. describe 'validations' do
  18. it 'is invalid without an account' do
  19. user = Fabricate.build(:user, account: nil)
  20. user.valid?
  21. expect(user).to model_have_error_on_field(:account)
  22. end
  23. it 'is invalid without a valid email' do
  24. user = Fabricate.build(:user, email: 'john@')
  25. user.valid?
  26. expect(user).to model_have_error_on_field(:email)
  27. end
  28. it 'is valid with an invalid e-mail that has already been saved' do
  29. user = Fabricate.build(:user, email: 'invalid-email')
  30. user.save(validate: false)
  31. expect(user.valid?).to be true
  32. end
  33. end
  34. describe 'Normalizations' do
  35. describe 'locale' do
  36. it 'preserves valid locale' do
  37. user = Fabricate.build(:user, locale: 'en')
  38. expect(user.locale).to eq('en')
  39. end
  40. it 'cleans out invalid locale' do
  41. user = Fabricate.build(:user, locale: 'toto')
  42. expect(user.locale).to be_nil
  43. end
  44. end
  45. describe 'time_zone' do
  46. it 'preserves valid timezone' do
  47. user = Fabricate.build(:user, time_zone: 'UTC')
  48. expect(user.time_zone).to eq('UTC')
  49. end
  50. it 'cleans out invalid timezone' do
  51. user = Fabricate.build(:user, time_zone: 'toto')
  52. expect(user.time_zone).to be_nil
  53. end
  54. end
  55. describe 'languages' do
  56. it 'preserves valid options for languages' do
  57. user = Fabricate.build(:user, chosen_languages: ['en', 'fr', ''])
  58. expect(user.chosen_languages).to eq(['en', 'fr'])
  59. end
  60. it 'cleans out empty string from languages' do
  61. user = Fabricate.build(:user, chosen_languages: [''])
  62. expect(user.chosen_languages).to be_nil
  63. end
  64. end
  65. end
  66. describe 'scopes', :sidekiq_inline do
  67. describe 'recent' do
  68. it 'returns an array of recent users ordered by id' do
  69. first_user = Fabricate(:user)
  70. second_user = Fabricate(:user)
  71. expect(described_class.recent).to eq [second_user, first_user]
  72. end
  73. end
  74. describe 'confirmed' do
  75. it 'returns an array of users who are confirmed' do
  76. Fabricate(:user, confirmed_at: nil)
  77. confirmed_user = Fabricate(:user, confirmed_at: Time.zone.now)
  78. expect(described_class.confirmed).to contain_exactly(confirmed_user)
  79. end
  80. end
  81. describe 'inactive' do
  82. it 'returns a relation of inactive users' do
  83. specified = Fabricate(:user, current_sign_in_at: 15.days.ago)
  84. Fabricate(:user, current_sign_in_at: 6.days.ago)
  85. expect(described_class.inactive).to contain_exactly(specified)
  86. end
  87. end
  88. describe 'matches_email' do
  89. it 'returns a relation of users whose email starts with the given string' do
  90. specified = Fabricate(:user, email: 'specified@spec')
  91. Fabricate(:user, email: 'unspecified@spec')
  92. expect(described_class.matches_email('specified')).to contain_exactly(specified)
  93. end
  94. end
  95. describe 'matches_ip' do
  96. it 'returns a relation of users whose ip address is matching with the given CIDR' do
  97. user1 = Fabricate(:user)
  98. user2 = Fabricate(:user)
  99. Fabricate(:session_activation, user: user1, ip: '2160:2160::22', session_id: '1')
  100. Fabricate(:session_activation, user: user1, ip: '2160:2160::23', session_id: '2')
  101. Fabricate(:session_activation, user: user2, ip: '2160:8888::24', session_id: '3')
  102. Fabricate(:session_activation, user: user2, ip: '2160:8888::25', session_id: '4')
  103. expect(described_class.matches_ip('2160:2160::/32')).to contain_exactly(user1)
  104. end
  105. end
  106. end
  107. describe 'blacklist' do
  108. around do |example|
  109. old_blacklist = Rails.configuration.x.email_blacklist
  110. Rails.configuration.x.email_domains_blacklist = 'mvrht.com'
  111. example.run
  112. Rails.configuration.x.email_domains_blacklist = old_blacklist
  113. end
  114. it 'allows a non-blacklisted user to be created' do
  115. user = described_class.new(email: 'foo@example.com', account: account, password: password, agreement: true)
  116. expect(user).to be_valid
  117. end
  118. it 'does not allow a blacklisted user to be created' do
  119. user = described_class.new(email: 'foo@mvrht.com', account: account, password: password, agreement: true)
  120. expect(user).to_not be_valid
  121. end
  122. it 'does not allow a subdomain blacklisted user to be created' do
  123. user = described_class.new(email: 'foo@mvrht.com.topdomain.tld', account: account, password: password, agreement: true)
  124. expect(user).to_not be_valid
  125. end
  126. end
  127. describe '#confirmed?' do
  128. it 'returns true when a confirmed_at is set' do
  129. user = Fabricate.build(:user, confirmed_at: Time.now.utc)
  130. expect(user.confirmed?).to be true
  131. end
  132. it 'returns false if a confirmed_at is nil' do
  133. user = Fabricate.build(:user, confirmed_at: nil)
  134. expect(user.confirmed?).to be false
  135. end
  136. end
  137. describe '#confirm' do
  138. subject { user.confirm }
  139. let(:new_email) { 'new-email@example.com' }
  140. before do
  141. allow(TriggerWebhookWorker).to receive(:perform_async)
  142. end
  143. context 'when the user is already confirmed' do
  144. let!(:user) { Fabricate(:user, confirmed_at: Time.now.utc, approved: true, unconfirmed_email: new_email) }
  145. it 'sets email to unconfirmed_email and does not trigger web hook' do
  146. expect { subject }.to change { user.reload.email }.to(new_email)
  147. expect(TriggerWebhookWorker).to_not have_received(:perform_async).with('account.approved', 'Account', user.account_id)
  148. end
  149. end
  150. context 'when the user is a new user' do
  151. let(:user) { Fabricate(:user, confirmed_at: nil, unconfirmed_email: new_email) }
  152. context 'when the user is already approved' do
  153. before do
  154. Setting.registrations_mode = 'approved'
  155. user.approve!
  156. end
  157. it 'sets email to unconfirmed_email and triggers `account.approved` web hook' do
  158. expect { subject }.to change { user.reload.email }.to(new_email)
  159. expect(TriggerWebhookWorker).to have_received(:perform_async).with('account.approved', 'Account', user.account_id).once
  160. end
  161. end
  162. context 'when the user does not require explicit approval' do
  163. before do
  164. Setting.registrations_mode = 'open'
  165. end
  166. it 'sets email to unconfirmed_email and triggers `account.approved` web hook' do
  167. expect { subject }.to change { user.reload.email }.to(new_email)
  168. expect(TriggerWebhookWorker).to have_received(:perform_async).with('account.approved', 'Account', user.account_id).once
  169. end
  170. end
  171. context 'when the user requires explicit approval but is not approved' do
  172. before do
  173. Setting.registrations_mode = 'approved'
  174. end
  175. it 'sets email to unconfirmed_email and does not trigger web hook' do
  176. expect { subject }.to change { user.reload.email }.to(new_email)
  177. expect(TriggerWebhookWorker).to_not have_received(:perform_async).with('account.approved', 'Account', user.account_id)
  178. end
  179. end
  180. end
  181. end
  182. describe '#approve!' do
  183. subject { user.approve! }
  184. before do
  185. Setting.registrations_mode = 'approved'
  186. allow(TriggerWebhookWorker).to receive(:perform_async)
  187. end
  188. context 'when the user is already confirmed' do
  189. let(:user) { Fabricate(:user, confirmed_at: Time.now.utc, approved: false) }
  190. it 'sets the approved flag and triggers `account.approved` web hook' do
  191. expect { subject }.to change { user.reload.approved? }.to(true)
  192. expect(TriggerWebhookWorker).to have_received(:perform_async).with('account.approved', 'Account', user.account_id).once
  193. end
  194. end
  195. context 'when the user is not confirmed' do
  196. let(:user) { Fabricate(:user, confirmed_at: nil, approved: false) }
  197. it 'sets the approved flag and does not trigger web hook' do
  198. expect { subject }.to change { user.reload.approved? }.to(true)
  199. expect(TriggerWebhookWorker).to_not have_received(:perform_async).with('account.approved', 'Account', user.account_id)
  200. end
  201. end
  202. end
  203. describe '#disable_two_factor!' do
  204. it 'saves false for otp_required_for_login' do
  205. user = Fabricate.build(:user, otp_required_for_login: true)
  206. user.disable_two_factor!
  207. expect(user.reload.otp_required_for_login).to be false
  208. end
  209. it 'saves nil for otp_secret' do
  210. user = Fabricate.build(:user, otp_secret: 'oldotpcode')
  211. user.disable_two_factor!
  212. expect(user.reload.otp_secret).to be_nil
  213. end
  214. it 'saves cleared otp_backup_codes' do
  215. user = Fabricate.build(:user, otp_backup_codes: %w(dummy dummy))
  216. user.disable_two_factor!
  217. expect(user.reload.otp_backup_codes.empty?).to be true
  218. end
  219. end
  220. describe '#send_confirmation_instructions' do
  221. around do |example|
  222. queue_adapter = ActiveJob::Base.queue_adapter
  223. example.run
  224. ActiveJob::Base.queue_adapter = queue_adapter
  225. end
  226. it 'delivers confirmation instructions later' do
  227. user = Fabricate(:user)
  228. ActiveJob::Base.queue_adapter = :test
  229. expect { user.send_confirmation_instructions }.to have_enqueued_job(ActionMailer::MailDeliveryJob)
  230. end
  231. end
  232. describe 'settings' do
  233. it 'is instance of UserSettings' do
  234. user = Fabricate(:user)
  235. expect(user.settings).to be_a UserSettings
  236. end
  237. end
  238. describe '#setting_default_privacy' do
  239. it 'returns default privacy setting if user has configured' do
  240. user = Fabricate(:user)
  241. user.settings[:default_privacy] = 'unlisted'
  242. expect(user.setting_default_privacy).to eq 'unlisted'
  243. end
  244. it "returns 'private' if user has not configured default privacy setting and account is locked" do
  245. user = Fabricate(:account, locked: true).user
  246. expect(user.setting_default_privacy).to eq 'private'
  247. end
  248. it "returns 'public' if user has not configured default privacy setting and account is not locked" do
  249. user = Fabricate(:account, locked: false).user
  250. expect(user.setting_default_privacy).to eq 'public'
  251. end
  252. end
  253. describe 'whitelist' do
  254. around do |example|
  255. old_whitelist = Rails.configuration.x.email_domains_whitelist
  256. Rails.configuration.x.email_domains_whitelist = 'mastodon.space'
  257. example.run
  258. Rails.configuration.x.email_domains_whitelist = old_whitelist
  259. end
  260. it 'does not allow a user to be created unless they are whitelisted' do
  261. user = described_class.new(email: 'foo@example.com', account: account, password: password, agreement: true)
  262. expect(user).to_not be_valid
  263. end
  264. it 'allows a user to be created if they are whitelisted' do
  265. user = described_class.new(email: 'foo@mastodon.space', account: account, password: password, agreement: true)
  266. expect(user).to be_valid
  267. end
  268. it 'does not allow a user with a whitelisted top domain as subdomain in their email address to be created' do
  269. user = described_class.new(email: 'foo@mastodon.space.userdomain.com', account: account, password: password, agreement: true)
  270. expect(user).to_not be_valid
  271. end
  272. context 'with a blacklisted subdomain' do
  273. around do |example|
  274. old_blacklist = Rails.configuration.x.email_blacklist
  275. example.run
  276. Rails.configuration.x.email_domains_blacklist = old_blacklist
  277. end
  278. it 'does not allow a user to be created with a specific blacklisted subdomain even if the top domain is whitelisted' do
  279. Rails.configuration.x.email_domains_blacklist = 'blacklisted.mastodon.space'
  280. user = described_class.new(email: 'foo@blacklisted.mastodon.space', account: account, password: password)
  281. expect(user).to_not be_valid
  282. end
  283. end
  284. end
  285. describe 'token_for_app' do
  286. let(:user) { Fabricate(:user) }
  287. let(:app) { Fabricate(:application, owner: user) }
  288. it 'returns a token' do
  289. expect(user.token_for_app(app)).to be_a(Doorkeeper::AccessToken)
  290. end
  291. it 'persists a token' do
  292. t = user.token_for_app(app)
  293. expect(user.token_for_app(app)).to eql(t)
  294. end
  295. it 'is nil if user does not own app' do
  296. app.update!(owner: nil)
  297. expect(user.token_for_app(app)).to be_nil
  298. end
  299. end
  300. describe '#disable!' do
  301. subject(:user) { Fabricate(:user, disabled: false, current_sign_in_at: current_sign_in_at, last_sign_in_at: nil) }
  302. let(:current_sign_in_at) { Time.zone.now }
  303. before do
  304. user.disable!
  305. end
  306. it 'disables user' do
  307. expect(user).to have_attributes(disabled: true)
  308. end
  309. end
  310. describe '#enable!' do
  311. subject(:user) { Fabricate(:user, disabled: true) }
  312. before do
  313. user.enable!
  314. end
  315. it 'enables user' do
  316. expect(user).to have_attributes(disabled: false)
  317. end
  318. end
  319. describe '#reset_password!' do
  320. subject(:user) { Fabricate(:user, password: 'foobar12345') }
  321. let!(:session_activation) { Fabricate(:session_activation, user: user) }
  322. let!(:access_token) { Fabricate(:access_token, resource_owner_id: user.id) }
  323. let!(:web_push_subscription) { Fabricate(:web_push_subscription, access_token: access_token) }
  324. let(:redis_pipeline_stub) { instance_double(Redis::Namespace, publish: nil) }
  325. before do
  326. allow(redis).to receive(:pipelined).and_yield(redis_pipeline_stub)
  327. user.reset_password!
  328. end
  329. it 'changes the password immediately' do
  330. expect(user.external_or_valid_password?('foobar12345')).to be false
  331. end
  332. it 'deactivates all sessions' do
  333. expect(user.session_activations.count).to eq 0
  334. expect { session_activation.reload }.to raise_error(ActiveRecord::RecordNotFound)
  335. end
  336. it 'revokes all access tokens' do
  337. expect(Doorkeeper::AccessToken.active_for(user).count).to eq 0
  338. end
  339. it 'revokes streaming access for all access tokens' do
  340. expect(redis_pipeline_stub).to have_received(:publish).with("timeline:access_token:#{access_token.id}", Oj.dump(event: :kill)).once
  341. end
  342. it 'removes push subscriptions' do
  343. expect(Web::PushSubscription.where(user: user).or(Web::PushSubscription.where(access_token: access_token)).count).to eq 0
  344. expect { web_push_subscription.reload }.to raise_error(ActiveRecord::RecordNotFound)
  345. end
  346. end
  347. describe '#mark_email_as_confirmed!' do
  348. subject { user.mark_email_as_confirmed! }
  349. let!(:user) { Fabricate(:user, confirmed_at: confirmed_at) }
  350. before { ActionMailer::Base.deliveries.clear }
  351. after { ActionMailer::Base.deliveries.clear }
  352. context 'when user is new' do
  353. let(:confirmed_at) { nil }
  354. it 'confirms user and delivers welcome email', :sidekiq_inline do
  355. subject
  356. expect(user.confirmed_at).to be_present
  357. expect(ActionMailer::Base.deliveries.count).to eq 1
  358. end
  359. end
  360. context 'when user is not new' do
  361. let(:confirmed_at) { Time.zone.now }
  362. it 'confirms user but does not deliver welcome email' do
  363. subject
  364. expect(user.confirmed_at).to be_present
  365. expect(ActionMailer::Base.deliveries.count).to eq 0
  366. end
  367. end
  368. end
  369. describe '#active_for_authentication?' do
  370. subject { user.active_for_authentication? }
  371. let(:user) { Fabricate(:user, disabled: disabled, confirmed_at: confirmed_at) }
  372. context 'when user is disabled' do
  373. let(:disabled) { true }
  374. context 'when user is confirmed' do
  375. let(:confirmed_at) { Time.zone.now }
  376. it { is_expected.to be true }
  377. end
  378. context 'when user is not confirmed' do
  379. let(:confirmed_at) { nil }
  380. it { is_expected.to be true }
  381. end
  382. end
  383. context 'when user is not disabled' do
  384. let(:disabled) { false }
  385. context 'when user is confirmed' do
  386. let(:confirmed_at) { Time.zone.now }
  387. it { is_expected.to be true }
  388. end
  389. context 'when user is not confirmed' do
  390. let(:confirmed_at) { nil }
  391. it { is_expected.to be true }
  392. end
  393. end
  394. end
  395. describe '.those_who_can' do
  396. before { Fabricate(:user, role: UserRole.find_by(name: 'Moderator')) }
  397. context 'when there are not any user roles' do
  398. before { UserRole.destroy_all }
  399. it 'returns an empty list' do
  400. expect(described_class.those_who_can(:manage_blocks)).to eq([])
  401. end
  402. end
  403. context 'when there are not users with the needed role' do
  404. it 'returns an empty list' do
  405. expect(described_class.those_who_can(:manage_blocks)).to eq([])
  406. end
  407. end
  408. context 'when there are users with roles' do
  409. let!(:admin_user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  410. it 'returns the users with the role' do
  411. expect(described_class.those_who_can(:manage_blocks)).to eq([admin_user])
  412. end
  413. end
  414. end
  415. end