1
0

user_spec.rb 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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, chosen_languages: [''])
  37. user.valid?
  38. expect(user.chosen_languages).to eq nil
  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 'confirmed' do
  50. it 'returns an array of users who are confirmed' do
  51. user_1 = Fabricate(:user, confirmed_at: nil)
  52. user_2 = Fabricate(:user, confirmed_at: Time.zone.now)
  53. expect(User.confirmed).to match_array([user_2])
  54. end
  55. end
  56. describe 'inactive' do
  57. it 'returns a relation of inactive users' do
  58. specified = Fabricate(:user, current_sign_in_at: 15.days.ago)
  59. Fabricate(:user, current_sign_in_at: 6.days.ago)
  60. expect(User.inactive).to match_array([specified])
  61. end
  62. end
  63. describe 'matches_email' do
  64. it 'returns a relation of users whose email starts with the given string' do
  65. specified = Fabricate(:user, email: 'specified@spec')
  66. Fabricate(:user, email: 'unspecified@spec')
  67. expect(User.matches_email('specified')).to match_array([specified])
  68. end
  69. end
  70. describe 'matches_ip' do
  71. it 'returns a relation of users whose ip address is matching with the given CIDR' do
  72. user1 = Fabricate(:user)
  73. user2 = Fabricate(:user)
  74. Fabricate(:session_activation, user: user1, ip: '2160:2160::22', session_id: '1')
  75. Fabricate(:session_activation, user: user1, ip: '2160:2160::23', session_id: '2')
  76. Fabricate(:session_activation, user: user2, ip: '2160:8888::24', session_id: '3')
  77. Fabricate(:session_activation, user: user2, ip: '2160:8888::25', session_id: '4')
  78. expect(User.matches_ip('2160:2160::/32')).to match_array([user1])
  79. end
  80. end
  81. end
  82. let(:account) { Fabricate(:account, username: 'alice') }
  83. let(:password) { 'abcd1234' }
  84. describe 'blacklist' do
  85. around(:each) do |example|
  86. old_blacklist = Rails.configuration.x.email_blacklist
  87. Rails.configuration.x.email_domains_blacklist = 'mvrht.com'
  88. example.run
  89. Rails.configuration.x.email_domains_blacklist = old_blacklist
  90. end
  91. it 'should allow a non-blacklisted user to be created' do
  92. user = User.new(email: 'foo@example.com', account: account, password: password, agreement: true)
  93. expect(user.valid?).to be_truthy
  94. end
  95. it 'should not allow a blacklisted user to be created' do
  96. user = User.new(email: 'foo@mvrht.com', account: account, password: password, agreement: true)
  97. expect(user.valid?).to be_falsey
  98. end
  99. it 'should not allow a subdomain blacklisted user to be created' do
  100. user = User.new(email: 'foo@mvrht.com.topdomain.tld', account: account, password: password, agreement: true)
  101. expect(user.valid?).to be_falsey
  102. end
  103. end
  104. describe '#confirmed?' do
  105. it 'returns true when a confirmed_at is set' do
  106. user = Fabricate.build(:user, confirmed_at: Time.now.utc)
  107. expect(user.confirmed?).to be true
  108. end
  109. it 'returns false if a confirmed_at is nil' do
  110. user = Fabricate.build(:user, confirmed_at: nil)
  111. expect(user.confirmed?).to be false
  112. end
  113. end
  114. describe '#confirm' do
  115. let(:new_email) { 'new-email@example.com' }
  116. subject { user.confirm }
  117. before do
  118. allow(TriggerWebhookWorker).to receive(:perform_async)
  119. end
  120. context 'when the user is already confirmed' do
  121. let!(:user) { Fabricate(:user, confirmed_at: Time.now.utc, approved: true, unconfirmed_email: new_email) }
  122. it 'sets email to unconfirmed_email' do
  123. expect { subject }.to change { user.reload.email }.to(new_email)
  124. end
  125. it 'does not trigger the account.approved Web Hook' do
  126. subject
  127. expect(TriggerWebhookWorker).not_to have_received(:perform_async).with('account.approved', 'Account', user.account_id)
  128. end
  129. end
  130. context 'when the user is a new user' do
  131. let(:user) { Fabricate(:user, confirmed_at: nil, unconfirmed_email: new_email) }
  132. context 'when the user is already approved' do
  133. around(:example) do |example|
  134. registrations_mode = Setting.registrations_mode
  135. Setting.registrations_mode = 'approved'
  136. example.run
  137. Setting.registrations_mode = registrations_mode
  138. end
  139. before do
  140. user.approve!
  141. end
  142. it 'sets email to unconfirmed_email' do
  143. expect { subject }.to change { user.reload.email }.to(new_email)
  144. end
  145. it 'triggers the account.approved Web Hook' do
  146. user.confirm
  147. expect(TriggerWebhookWorker).to have_received(:perform_async).with('account.approved', 'Account', user.account_id).once
  148. end
  149. end
  150. context 'when the user does not require explicit approval' do
  151. around(:example) do |example|
  152. registrations_mode = Setting.registrations_mode
  153. Setting.registrations_mode = 'open'
  154. example.run
  155. Setting.registrations_mode = registrations_mode
  156. end
  157. it 'sets email to unconfirmed_email' do
  158. expect { subject }.to change { user.reload.email }.to(new_email)
  159. end
  160. it 'triggers the account.approved Web Hook' do
  161. user.confirm
  162. expect(TriggerWebhookWorker).to have_received(:perform_async).with('account.approved', 'Account', user.account_id).once
  163. end
  164. end
  165. context 'when the user requires explicit approval but is not approved' do
  166. around(:example) do |example|
  167. registrations_mode = Setting.registrations_mode
  168. Setting.registrations_mode = 'approved'
  169. example.run
  170. Setting.registrations_mode = registrations_mode
  171. end
  172. it 'sets email to unconfirmed_email' do
  173. expect { subject }.to change { user.reload.email }.to(new_email)
  174. end
  175. it 'does not trigger the account.approved Web Hook' do
  176. subject
  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. around(:example) do |example|
  185. registrations_mode = Setting.registrations_mode
  186. Setting.registrations_mode = 'approved'
  187. example.run
  188. Setting.registrations_mode = registrations_mode
  189. end
  190. before do
  191. allow(TriggerWebhookWorker).to receive(:perform_async)
  192. end
  193. context 'when the user is already confirmed' do
  194. let(:user) { Fabricate(:user, confirmed_at: Time.now.utc, approved: false) }
  195. it 'sets the approved flag' do
  196. expect { subject }.to change { user.reload.approved? }.to(true)
  197. end
  198. it 'triggers the account.approved Web Hook' do
  199. subject
  200. expect(TriggerWebhookWorker).to have_received(:perform_async).with('account.approved', 'Account', user.account_id).once
  201. end
  202. end
  203. context 'when the user is not confirmed' do
  204. let(:user) { Fabricate(:user, confirmed_at: nil, approved: false) }
  205. it 'sets the approved flag' do
  206. expect { subject }.to change { user.reload.approved? }.to(true)
  207. end
  208. it 'does not trigger the account.approved Web Hook' do
  209. subject
  210. expect(TriggerWebhookWorker).not_to have_received(:perform_async).with('account.approved', 'Account', user.account_id)
  211. end
  212. end
  213. end
  214. describe '#disable_two_factor!' do
  215. it 'saves false for otp_required_for_login' do
  216. user = Fabricate.build(:user, otp_required_for_login: true)
  217. user.disable_two_factor!
  218. expect(user.reload.otp_required_for_login).to be false
  219. end
  220. it 'saves nil for otp_secret' do
  221. user = Fabricate.build(:user, otp_secret: 'oldotpcode')
  222. user.disable_two_factor!
  223. expect(user.reload.otp_secret).to be nil
  224. end
  225. it 'saves cleared otp_backup_codes' do
  226. user = Fabricate.build(:user, otp_backup_codes: %w(dummy dummy))
  227. user.disable_two_factor!
  228. expect(user.reload.otp_backup_codes.empty?).to be true
  229. end
  230. end
  231. describe '#send_confirmation_instructions' do
  232. around do |example|
  233. queue_adapter = ActiveJob::Base.queue_adapter
  234. example.run
  235. ActiveJob::Base.queue_adapter = queue_adapter
  236. end
  237. it 'delivers confirmation instructions later' do
  238. user = Fabricate(:user)
  239. ActiveJob::Base.queue_adapter = :test
  240. expect { user.send_confirmation_instructions }.to have_enqueued_job(ActionMailer::MailDeliveryJob)
  241. end
  242. end
  243. describe 'settings' do
  244. it 'is instance of Settings::ScopedSettings' do
  245. user = Fabricate(:user)
  246. expect(user.settings).to be_kind_of Settings::ScopedSettings
  247. end
  248. end
  249. describe '#setting_default_privacy' do
  250. it 'returns default privacy setting if user has configured' do
  251. user = Fabricate(:user)
  252. user.settings[:default_privacy] = 'unlisted'
  253. expect(user.setting_default_privacy).to eq 'unlisted'
  254. end
  255. it "returns 'private' if user has not configured default privacy setting and account is locked" do
  256. user = Fabricate(:account, locked: true).user
  257. expect(user.setting_default_privacy).to eq 'private'
  258. end
  259. it "returns 'public' if user has not configured default privacy setting and account is not locked" do
  260. user = Fabricate(:account, locked: false).user
  261. expect(user.setting_default_privacy).to eq 'public'
  262. end
  263. end
  264. describe 'whitelist' do
  265. around(:each) do |example|
  266. old_whitelist = Rails.configuration.x.email_domains_whitelist
  267. Rails.configuration.x.email_domains_whitelist = 'mastodon.space'
  268. example.run
  269. Rails.configuration.x.email_domains_whitelist = old_whitelist
  270. end
  271. it 'should not allow a user to be created unless they are whitelisted' do
  272. user = User.new(email: 'foo@example.com', account: account, password: password, agreement: true)
  273. expect(user.valid?).to be_falsey
  274. end
  275. it 'should allow a user to be created if they are whitelisted' do
  276. user = User.new(email: 'foo@mastodon.space', account: account, password: password, agreement: true)
  277. expect(user.valid?).to be_truthy
  278. end
  279. it 'should not allow a user with a whitelisted top domain as subdomain in their email address to be created' do
  280. user = User.new(email: 'foo@mastodon.space.userdomain.com', account: account, password: password, agreement: true)
  281. expect(user.valid?).to be_falsey
  282. end
  283. context do
  284. around do |example|
  285. old_blacklist = Rails.configuration.x.email_blacklist
  286. example.run
  287. Rails.configuration.x.email_domains_blacklist = old_blacklist
  288. end
  289. it 'should not allow a user to be created with a specific blacklisted subdomain even if the top domain is whitelisted' do
  290. Rails.configuration.x.email_domains_blacklist = 'blacklisted.mastodon.space'
  291. user = User.new(email: 'foo@blacklisted.mastodon.space', account: account, password: password)
  292. expect(user.valid?).to be_falsey
  293. end
  294. end
  295. end
  296. it_behaves_like 'Settings-extended' do
  297. def create!
  298. User.create!(account: Fabricate(:account, user: nil), email: 'foo@mastodon.space', password: 'abcd1234', agreement: true)
  299. end
  300. def fabricate
  301. Fabricate(:user)
  302. end
  303. end
  304. describe 'token_for_app' do
  305. let(:user) { Fabricate(:user) }
  306. let(:app) { Fabricate(:application, owner: user) }
  307. it 'returns a token' do
  308. expect(user.token_for_app(app)).to be_a(Doorkeeper::AccessToken)
  309. end
  310. it 'persists a token' do
  311. t = user.token_for_app(app)
  312. expect(user.token_for_app(app)).to eql(t)
  313. end
  314. it 'is nil if user does not own app' do
  315. app.update!(owner: nil)
  316. expect(user.token_for_app(app)).to be_nil
  317. end
  318. end
  319. describe '#disable!' do
  320. subject(:user) { Fabricate(:user, disabled: false, current_sign_in_at: current_sign_in_at, last_sign_in_at: nil) }
  321. let(:current_sign_in_at) { Time.zone.now }
  322. before do
  323. user.disable!
  324. end
  325. it 'disables user' do
  326. expect(user).to have_attributes(disabled: true)
  327. end
  328. end
  329. describe '#enable!' do
  330. subject(:user) { Fabricate(:user, disabled: true) }
  331. before do
  332. user.enable!
  333. end
  334. it 'enables user' do
  335. expect(user).to have_attributes(disabled: false)
  336. end
  337. end
  338. describe '#reset_password!' do
  339. subject(:user) { Fabricate(:user, password: 'foobar12345') }
  340. let!(:session_activation) { Fabricate(:session_activation, user: user) }
  341. let!(:access_token) { Fabricate(:access_token, resource_owner_id: user.id) }
  342. let!(:web_push_subscription) { Fabricate(:web_push_subscription, access_token: access_token) }
  343. before do
  344. user.reset_password!
  345. end
  346. it 'changes the password immediately' do
  347. expect(user.external_or_valid_password?('foobar12345')).to be false
  348. end
  349. it 'deactivates all sessions' do
  350. expect(user.session_activations.count).to eq 0
  351. end
  352. it 'revokes all access tokens' do
  353. expect(Doorkeeper::AccessToken.active_for(user).count).to eq 0
  354. end
  355. it 'removes push subscriptions' do
  356. expect(Web::PushSubscription.where(user: user).or(Web::PushSubscription.where(access_token: access_token)).count).to eq 0
  357. end
  358. end
  359. describe '#confirm!' do
  360. subject(:user) { Fabricate(:user, confirmed_at: confirmed_at) }
  361. before do
  362. ActionMailer::Base.deliveries.clear
  363. user.confirm!
  364. end
  365. after { ActionMailer::Base.deliveries.clear }
  366. context 'when user is new' do
  367. let(:confirmed_at) { nil }
  368. it 'confirms user' do
  369. expect(user.confirmed_at).to be_present
  370. end
  371. it 'delivers mails' do
  372. expect(ActionMailer::Base.deliveries.count).to eq 2
  373. end
  374. end
  375. context 'when user is not new' do
  376. let(:confirmed_at) { Time.zone.now }
  377. it 'confirms user' do
  378. expect(user.confirmed_at).to be_present
  379. end
  380. it 'does not deliver mail' do
  381. expect(ActionMailer::Base.deliveries.count).to eq 0
  382. end
  383. end
  384. end
  385. describe '#active_for_authentication?' do
  386. subject { user.active_for_authentication? }
  387. let(:user) { Fabricate(:user, disabled: disabled, confirmed_at: confirmed_at) }
  388. context 'when user is disabled' do
  389. let(:disabled) { true }
  390. context 'when user is confirmed' do
  391. let(:confirmed_at) { Time.zone.now }
  392. it { is_expected.to be true }
  393. end
  394. context 'when user is not confirmed' do
  395. let(:confirmed_at) { nil }
  396. it { is_expected.to be true }
  397. end
  398. end
  399. context 'when user is not disabled' do
  400. let(:disabled) { false }
  401. context 'when user is confirmed' do
  402. let(:confirmed_at) { Time.zone.now }
  403. it { is_expected.to be true }
  404. end
  405. context 'when user is not confirmed' do
  406. let(:confirmed_at) { nil }
  407. it { is_expected.to be true }
  408. end
  409. end
  410. end
  411. describe '.those_who_can' do
  412. pending
  413. end
  414. end