user_spec.rb 16 KB

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