account_spec.rb 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Account do
  4. context 'with an account record' do
  5. subject { Fabricate(:account) }
  6. let(:bob) { Fabricate(:account, username: 'bob') }
  7. describe '#suspend!' do
  8. it 'marks the account as suspended' do
  9. subject.suspend!
  10. expect(subject.suspended?).to be true
  11. end
  12. it 'creates a deletion request' do
  13. subject.suspend!
  14. expect(AccountDeletionRequest.where(account: subject).exists?).to be true
  15. end
  16. context 'when the account is of a local user' do
  17. subject { local_user_account }
  18. let!(:local_user_account) { Fabricate(:user, email: 'foo+bar@domain.org').account }
  19. it 'creates a canonical domain block' do
  20. subject.suspend!
  21. expect(CanonicalEmailBlock.block?(subject.user_email)).to be true
  22. end
  23. context 'when a canonical domain block already exists for that email' do
  24. before do
  25. Fabricate(:canonical_email_block, email: subject.user_email)
  26. end
  27. it 'does not raise an error' do
  28. expect { subject.suspend! }.to_not raise_error
  29. end
  30. end
  31. end
  32. end
  33. describe '#follow!' do
  34. it 'creates a follow' do
  35. follow = subject.follow!(bob)
  36. expect(follow).to be_instance_of Follow
  37. expect(follow.account).to eq subject
  38. expect(follow.target_account).to eq bob
  39. end
  40. end
  41. describe '#unfollow!' do
  42. before do
  43. subject.follow!(bob)
  44. end
  45. it 'destroys a follow' do
  46. unfollow = subject.unfollow!(bob)
  47. expect(unfollow).to be_instance_of Follow
  48. expect(unfollow.account).to eq subject
  49. expect(unfollow.target_account).to eq bob
  50. expect(unfollow.destroyed?).to be true
  51. end
  52. end
  53. describe '#following?' do
  54. it 'returns true when the target is followed' do
  55. subject.follow!(bob)
  56. expect(subject.following?(bob)).to be true
  57. end
  58. it 'returns false if the target is not followed' do
  59. expect(subject.following?(bob)).to be false
  60. end
  61. end
  62. end
  63. describe '#local?' do
  64. it 'returns true when the account is local' do
  65. account = Fabricate(:account, domain: nil)
  66. expect(account.local?).to be true
  67. end
  68. it 'returns false when the account is on a different domain' do
  69. account = Fabricate(:account, domain: 'foreign.tld')
  70. expect(account.local?).to be false
  71. end
  72. end
  73. describe 'Local domain user methods' do
  74. subject { Fabricate(:account, domain: nil, username: 'alice') }
  75. around do |example|
  76. before = Rails.configuration.x.local_domain
  77. example.run
  78. Rails.configuration.x.local_domain = before
  79. end
  80. describe '#to_webfinger_s' do
  81. it 'returns a webfinger string for the account' do
  82. Rails.configuration.x.local_domain = 'example.com'
  83. expect(subject.to_webfinger_s).to eq 'acct:alice@example.com'
  84. end
  85. end
  86. describe '#local_username_and_domain' do
  87. it 'returns the username and local domain for the account' do
  88. Rails.configuration.x.local_domain = 'example.com'
  89. expect(subject.local_username_and_domain).to eq 'alice@example.com'
  90. end
  91. end
  92. end
  93. describe '#acct' do
  94. it 'returns username for local users' do
  95. account = Fabricate(:account, domain: nil, username: 'alice')
  96. expect(account.acct).to eql 'alice'
  97. end
  98. it 'returns username@domain for foreign users' do
  99. account = Fabricate(:account, domain: 'foreign.tld', username: 'alice')
  100. expect(account.acct).to eql 'alice@foreign.tld'
  101. end
  102. end
  103. describe '#save_with_optional_media!' do
  104. before do
  105. stub_request(:get, 'https://remote.test/valid_avatar').to_return(request_fixture('avatar.txt'))
  106. stub_request(:get, 'https://remote.test/invalid_avatar').to_return(request_fixture('feed.txt'))
  107. end
  108. let(:account) do
  109. Fabricate(:account,
  110. avatar_remote_url: 'https://remote.test/valid_avatar',
  111. header_remote_url: 'https://remote.test/valid_avatar')
  112. end
  113. let!(:expectation) { account.dup }
  114. context 'with valid properties' do
  115. before do
  116. account.save_with_optional_media!
  117. end
  118. it 'unchanges avatar, header, avatar_remote_url, and header_remote_url' do
  119. expect(account.avatar_remote_url).to eq expectation.avatar_remote_url
  120. expect(account.header_remote_url).to eq expectation.header_remote_url
  121. expect(account.avatar_file_name).to eq expectation.avatar_file_name
  122. expect(account.header_file_name).to eq expectation.header_file_name
  123. end
  124. end
  125. context 'with invalid properties' do
  126. before do
  127. account.avatar_remote_url = 'https://remote.test/invalid_avatar'
  128. account.save_with_optional_media!
  129. end
  130. it 'sets default avatar, header, avatar_remote_url, and header_remote_url' do
  131. expect(account.avatar_remote_url).to eq 'https://remote.test/invalid_avatar'
  132. expect(account.header_remote_url).to eq expectation.header_remote_url
  133. expect(account.avatar_file_name).to be_nil
  134. expect(account.header_file_name).to eq expectation.header_file_name
  135. end
  136. end
  137. end
  138. describe '#possibly_stale?' do
  139. let(:account) { Fabricate(:account, last_webfingered_at: last_webfingered_at) }
  140. context 'when last_webfingered_at is nil' do
  141. let(:last_webfingered_at) { nil }
  142. it 'returns true' do
  143. expect(account.possibly_stale?).to be true
  144. end
  145. end
  146. context 'when last_webfingered_at is more than 24 hours before' do
  147. let(:last_webfingered_at) { 25.hours.ago }
  148. it 'returns true' do
  149. expect(account.possibly_stale?).to be true
  150. end
  151. end
  152. context 'when last_webfingered_at is less than 24 hours before' do
  153. let(:last_webfingered_at) { 23.hours.ago }
  154. it 'returns false' do
  155. expect(account.possibly_stale?).to be false
  156. end
  157. end
  158. end
  159. describe '#refresh!' do
  160. let(:account) { Fabricate(:account, domain: domain) }
  161. let(:acct) { account.acct }
  162. context 'when domain is nil' do
  163. let(:domain) { nil }
  164. it 'returns nil' do
  165. expect(account.refresh!).to be_nil
  166. end
  167. it 'does not call ResolveAccountService#call' do
  168. service = instance_double(ResolveAccountService, call: nil)
  169. allow(ResolveAccountService).to receive(:new).and_return(service)
  170. account.refresh!
  171. expect(service).to_not have_received(:call).with(acct)
  172. end
  173. end
  174. context 'when domain is present' do
  175. let(:domain) { 'example.com' }
  176. it 'calls ResolveAccountService#call' do
  177. service = instance_double(ResolveAccountService, call: nil)
  178. allow(ResolveAccountService).to receive(:new).and_return(service)
  179. account.refresh!
  180. expect(service).to have_received(:call).with(acct).once
  181. end
  182. end
  183. end
  184. describe '#to_param' do
  185. it 'returns username' do
  186. account = Fabricate(:account, username: 'alice')
  187. expect(account.to_param).to eq 'alice'
  188. end
  189. end
  190. describe '#keypair' do
  191. it 'returns an RSA key pair' do
  192. account = Fabricate(:account)
  193. expect(account.keypair).to be_instance_of OpenSSL::PKey::RSA
  194. end
  195. end
  196. describe '#object_type' do
  197. it 'is always a person' do
  198. account = Fabricate(:account)
  199. expect(account.object_type).to be :person
  200. end
  201. end
  202. describe '#favourited?' do
  203. subject { Fabricate(:account) }
  204. let(:original_status) do
  205. author = Fabricate(:account, username: 'original')
  206. Fabricate(:status, account: author)
  207. end
  208. context 'when the status is a reblog of another status' do
  209. let(:original_reblog) do
  210. author = Fabricate(:account, username: 'original_reblogger')
  211. Fabricate(:status, reblog: original_status, account: author)
  212. end
  213. it 'is true when this account has favourited it' do
  214. Fabricate(:favourite, status: original_reblog, account: subject)
  215. expect(subject.favourited?(original_status)).to be true
  216. end
  217. it 'is false when this account has not favourited it' do
  218. expect(subject.favourited?(original_status)).to be false
  219. end
  220. end
  221. context 'when the status is an original status' do
  222. it 'is true when this account has favourited it' do
  223. Fabricate(:favourite, status: original_status, account: subject)
  224. expect(subject.favourited?(original_status)).to be true
  225. end
  226. it 'is false when this account has not favourited it' do
  227. expect(subject.favourited?(original_status)).to be false
  228. end
  229. end
  230. end
  231. describe '#reblogged?' do
  232. subject { Fabricate(:account) }
  233. let(:original_status) do
  234. author = Fabricate(:account, username: 'original')
  235. Fabricate(:status, account: author)
  236. end
  237. context 'when the status is a reblog of another status' do
  238. let(:original_reblog) do
  239. author = Fabricate(:account, username: 'original_reblogger')
  240. Fabricate(:status, reblog: original_status, account: author)
  241. end
  242. it 'is true when this account has reblogged it' do
  243. Fabricate(:status, reblog: original_reblog, account: subject)
  244. expect(subject.reblogged?(original_reblog)).to be true
  245. end
  246. it 'is false when this account has not reblogged it' do
  247. expect(subject.reblogged?(original_reblog)).to be false
  248. end
  249. end
  250. context 'when the status is an original status' do
  251. it 'is true when this account has reblogged it' do
  252. Fabricate(:status, reblog: original_status, account: subject)
  253. expect(subject.reblogged?(original_status)).to be true
  254. end
  255. it 'is false when this account has not reblogged it' do
  256. expect(subject.reblogged?(original_status)).to be false
  257. end
  258. end
  259. end
  260. describe '#excluded_from_timeline_account_ids' do
  261. it 'includes account ids of blockings, blocked_bys and mutes' do
  262. account = Fabricate(:account)
  263. block = Fabricate(:block, account: account)
  264. mute = Fabricate(:mute, account: account)
  265. block_by = Fabricate(:block, target_account: account)
  266. results = account.excluded_from_timeline_account_ids
  267. expect(results.size).to eq 3
  268. expect(results).to include(block.target_account.id)
  269. expect(results).to include(mute.target_account.id)
  270. expect(results).to include(block_by.account.id)
  271. end
  272. end
  273. describe '#excluded_from_timeline_domains' do
  274. it 'returns the domains blocked by the account' do
  275. account = Fabricate(:account)
  276. account.block_domain!('domain')
  277. expect(account.excluded_from_timeline_domains).to contain_exactly('domain')
  278. end
  279. end
  280. describe '.search_for' do
  281. before do
  282. _missing = Fabricate(
  283. :account,
  284. display_name: 'Missing',
  285. username: 'missing',
  286. domain: 'missing.com'
  287. )
  288. end
  289. it 'does not return suspended users' do
  290. Fabricate(
  291. :account,
  292. display_name: 'Display Name',
  293. username: 'username',
  294. domain: 'example.com',
  295. suspended: true
  296. )
  297. results = described_class.search_for('username')
  298. expect(results).to eq []
  299. end
  300. it 'does not return unapproved users' do
  301. match = Fabricate(
  302. :account,
  303. display_name: 'Display Name',
  304. username: 'username'
  305. )
  306. match.user.update(approved: false)
  307. results = described_class.search_for('username')
  308. expect(results).to eq []
  309. end
  310. it 'does not return unconfirmed users' do
  311. match = Fabricate(
  312. :account,
  313. display_name: 'Display Name',
  314. username: 'username'
  315. )
  316. match.user.update(confirmed_at: nil)
  317. results = described_class.search_for('username')
  318. expect(results).to eq []
  319. end
  320. it 'accepts ?, \, : and space as delimiter' do
  321. match = Fabricate(
  322. :account,
  323. display_name: 'A & l & i & c & e',
  324. username: 'username',
  325. domain: 'example.com'
  326. )
  327. results = described_class.search_for('A?l\i:c e')
  328. expect(results).to eq [match]
  329. end
  330. it 'finds accounts with matching display_name' do
  331. match = Fabricate(
  332. :account,
  333. display_name: 'Display Name',
  334. username: 'username',
  335. domain: 'example.com'
  336. )
  337. results = described_class.search_for('display')
  338. expect(results).to eq [match]
  339. end
  340. it 'finds accounts with matching username' do
  341. match = Fabricate(
  342. :account,
  343. display_name: 'Display Name',
  344. username: 'username',
  345. domain: 'example.com'
  346. )
  347. results = described_class.search_for('username')
  348. expect(results).to eq [match]
  349. end
  350. it 'finds accounts with matching domain' do
  351. match = Fabricate(
  352. :account,
  353. display_name: 'Display Name',
  354. username: 'username',
  355. domain: 'example.com'
  356. )
  357. results = described_class.search_for('example')
  358. expect(results).to eq [match]
  359. end
  360. it 'limits via constant by default' do
  361. stub_const('Account::Search::DEFAULT_LIMIT', 1)
  362. 2.times.each { Fabricate(:account, display_name: 'Display Name') }
  363. results = described_class.search_for('display')
  364. expect(results.size).to eq 1
  365. end
  366. it 'accepts arbitrary limits' do
  367. 2.times.each { Fabricate(:account, display_name: 'Display Name') }
  368. results = described_class.search_for('display', limit: 1)
  369. expect(results.size).to eq 1
  370. end
  371. it 'ranks multiple matches higher' do
  372. matches = [
  373. { username: 'username', display_name: 'username' },
  374. { display_name: 'Display Name', username: 'username', domain: 'example.com' },
  375. ].map(&method(:Fabricate).curry(2).call(:account))
  376. results = described_class.search_for('username')
  377. expect(results).to eq matches
  378. end
  379. end
  380. describe '.advanced_search_for' do
  381. let(:account) { Fabricate(:account) }
  382. context 'when limiting search to followed accounts' do
  383. it 'accepts ?, \, : and space as delimiter' do
  384. match = Fabricate(
  385. :account,
  386. display_name: 'A & l & i & c & e',
  387. username: 'username',
  388. domain: 'example.com'
  389. )
  390. account.follow!(match)
  391. results = described_class.advanced_search_for('A?l\i:c e', account, limit: 10, following: true)
  392. expect(results).to eq [match]
  393. end
  394. it 'does not return non-followed accounts' do
  395. Fabricate(
  396. :account,
  397. display_name: 'A & l & i & c & e',
  398. username: 'username',
  399. domain: 'example.com'
  400. )
  401. results = described_class.advanced_search_for('A?l\i:c e', account, limit: 10, following: true)
  402. expect(results).to eq []
  403. end
  404. it 'does not return suspended users' do
  405. Fabricate(
  406. :account,
  407. display_name: 'Display Name',
  408. username: 'username',
  409. domain: 'example.com',
  410. suspended: true
  411. )
  412. results = described_class.advanced_search_for('username', account, limit: 10, following: true)
  413. expect(results).to eq []
  414. end
  415. it 'does not return unapproved users' do
  416. match = Fabricate(
  417. :account,
  418. display_name: 'Display Name',
  419. username: 'username'
  420. )
  421. match.user.update(approved: false)
  422. results = described_class.advanced_search_for('username', account, limit: 10, following: true)
  423. expect(results).to eq []
  424. end
  425. it 'does not return unconfirmed users' do
  426. match = Fabricate(
  427. :account,
  428. display_name: 'Display Name',
  429. username: 'username'
  430. )
  431. match.user.update(confirmed_at: nil)
  432. results = described_class.advanced_search_for('username', account, limit: 10, following: true)
  433. expect(results).to eq []
  434. end
  435. end
  436. it 'does not return suspended users' do
  437. Fabricate(
  438. :account,
  439. display_name: 'Display Name',
  440. username: 'username',
  441. domain: 'example.com',
  442. suspended: true
  443. )
  444. results = described_class.advanced_search_for('username', account)
  445. expect(results).to eq []
  446. end
  447. it 'does not return unapproved users' do
  448. match = Fabricate(
  449. :account,
  450. display_name: 'Display Name',
  451. username: 'username'
  452. )
  453. match.user.update(approved: false)
  454. results = described_class.advanced_search_for('username', account)
  455. expect(results).to eq []
  456. end
  457. it 'does not return unconfirmed users' do
  458. match = Fabricate(
  459. :account,
  460. display_name: 'Display Name',
  461. username: 'username'
  462. )
  463. match.user.update(confirmed_at: nil)
  464. results = described_class.advanced_search_for('username', account)
  465. expect(results).to eq []
  466. end
  467. it 'accepts ?, \, : and space as delimiter' do
  468. match = Fabricate(
  469. :account,
  470. display_name: 'A & l & i & c & e',
  471. username: 'username',
  472. domain: 'example.com'
  473. )
  474. results = described_class.advanced_search_for('A?l\i:c e', account)
  475. expect(results).to eq [match]
  476. end
  477. it 'limits by 10 by default' do
  478. stub_const('Account::Search::DEFAULT_LIMIT', 1)
  479. 2.times { Fabricate(:account, display_name: 'Display Name') }
  480. results = described_class.advanced_search_for('display', account)
  481. expect(results.size).to eq 1
  482. end
  483. it 'accepts arbitrary limits' do
  484. 2.times { Fabricate(:account, display_name: 'Display Name') }
  485. results = described_class.advanced_search_for('display', account, limit: 1)
  486. expect(results.size).to eq 1
  487. end
  488. it 'ranks followed accounts higher' do
  489. match = Fabricate(:account, username: 'Matching')
  490. followed_match = Fabricate(:account, username: 'Matcher')
  491. Fabricate(:follow, account: account, target_account: followed_match)
  492. results = described_class.advanced_search_for('match', account)
  493. expect(results).to eq [followed_match, match]
  494. expect(results.first.rank).to be > results.last.rank
  495. end
  496. end
  497. describe '#statuses_count' do
  498. subject { Fabricate(:account) }
  499. it 'counts statuses' do
  500. Fabricate(:status, account: subject)
  501. Fabricate(:status, account: subject)
  502. expect(subject.statuses_count).to eq 2
  503. end
  504. it 'does not count direct statuses' do
  505. Fabricate(:status, account: subject, visibility: :direct)
  506. expect(subject.statuses_count).to eq 0
  507. end
  508. it 'is decremented when status is removed' do
  509. status = Fabricate(:status, account: subject)
  510. expect(subject.statuses_count).to eq 1
  511. status.destroy
  512. expect(subject.statuses_count).to eq 0
  513. end
  514. it 'is decremented when status is removed when account is not preloaded' do
  515. status = Fabricate(:status, account: subject)
  516. expect(subject.reload.statuses_count).to eq 1
  517. clean_status = Status.find(status.id)
  518. expect(clean_status.association(:account).loaded?).to be false
  519. clean_status.destroy
  520. expect(subject.reload.statuses_count).to eq 0
  521. end
  522. end
  523. describe '.following_map' do
  524. it 'returns an hash' do
  525. expect(described_class.following_map([], 1)).to be_a Hash
  526. end
  527. end
  528. describe '.followed_by_map' do
  529. it 'returns an hash' do
  530. expect(described_class.followed_by_map([], 1)).to be_a Hash
  531. end
  532. end
  533. describe '.blocking_map' do
  534. it 'returns an hash' do
  535. expect(described_class.blocking_map([], 1)).to be_a Hash
  536. end
  537. end
  538. describe '.requested_map' do
  539. it 'returns an hash' do
  540. expect(described_class.requested_map([], 1)).to be_a Hash
  541. end
  542. end
  543. describe '.requested_by_map' do
  544. it 'returns an hash' do
  545. expect(described_class.requested_by_map([], 1)).to be_a Hash
  546. end
  547. end
  548. describe 'MENTION_RE' do
  549. subject { Account::MENTION_RE }
  550. it 'matches usernames in the middle of a sentence' do
  551. expect(subject.match('Hello to @alice from me')[1]).to eq 'alice'
  552. end
  553. it 'matches usernames in the beginning of status' do
  554. expect(subject.match('@alice Hey how are you?')[1]).to eq 'alice'
  555. end
  556. it 'matches full usernames' do
  557. expect(subject.match('@alice@example.com')[1]).to eq 'alice@example.com'
  558. end
  559. it 'matches full usernames with a dot at the end' do
  560. expect(subject.match('Hello @alice@example.com.')[1]).to eq 'alice@example.com'
  561. end
  562. it 'matches dot-prepended usernames' do
  563. expect(subject.match('.@alice I want everybody to see this')[1]).to eq 'alice'
  564. end
  565. it 'does not match e-mails' do
  566. expect(subject.match('Drop me an e-mail at alice@example.com')).to be_nil
  567. end
  568. it 'does not match URLs' do
  569. expect(subject.match('Check this out https://medium.com/@alice/some-article#.abcdef123')).to be_nil
  570. end
  571. it 'does not match URL query string' do
  572. expect(subject.match('https://example.com/?x=@alice')).to be_nil
  573. end
  574. end
  575. describe 'validations' do
  576. it 'is invalid without a username' do
  577. account = Fabricate.build(:account, username: nil)
  578. account.valid?
  579. expect(account).to model_have_error_on_field(:username)
  580. end
  581. it 'squishes the username before validation' do
  582. account = Fabricate(:account, domain: nil, username: " \u3000bob \t \u00a0 \n ")
  583. expect(account.username).to eq 'bob'
  584. end
  585. context 'when is local' do
  586. it 'is invalid if the username is not unique in case-insensitive comparison among local accounts' do
  587. _account = Fabricate(:account, username: 'the_doctor')
  588. non_unique_account = Fabricate.build(:account, username: 'the_Doctor')
  589. non_unique_account.valid?
  590. expect(non_unique_account).to model_have_error_on_field(:username)
  591. end
  592. it 'is invalid if the username is reserved' do
  593. account = Fabricate.build(:account, username: 'support')
  594. account.valid?
  595. expect(account).to model_have_error_on_field(:username)
  596. end
  597. it 'is valid when username is reserved but record has already been created' do
  598. account = Fabricate.build(:account, username: 'support')
  599. account.save(validate: false)
  600. expect(account.valid?).to be true
  601. end
  602. it 'is valid if we are creating an instance actor account with a period' do
  603. account = Fabricate.build(:account, id: -99, actor_type: 'Application', locked: true, username: 'example.com')
  604. expect(account.valid?).to be true
  605. end
  606. it 'is valid if we are creating a possibly-conflicting instance actor account' do
  607. _account = Fabricate(:account, username: 'examplecom')
  608. instance_account = Fabricate.build(:account, id: -99, actor_type: 'Application', locked: true, username: 'example.com')
  609. expect(instance_account.valid?).to be true
  610. end
  611. it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do
  612. account = Fabricate.build(:account, username: 'the-doctor')
  613. account.valid?
  614. expect(account).to model_have_error_on_field(:username)
  615. end
  616. it 'is invalid if the username contains a period' do
  617. account = Fabricate.build(:account, username: 'the.doctor')
  618. account.valid?
  619. expect(account).to model_have_error_on_field(:username)
  620. end
  621. it 'is invalid if the username is longer than 30 characters' do
  622. account = Fabricate.build(:account, username: Faker::Lorem.characters(number: 31))
  623. account.valid?
  624. expect(account).to model_have_error_on_field(:username)
  625. end
  626. it 'is invalid if the display name is longer than 30 characters' do
  627. account = Fabricate.build(:account, display_name: Faker::Lorem.characters(number: 31))
  628. account.valid?
  629. expect(account).to model_have_error_on_field(:display_name)
  630. end
  631. it 'is invalid if the note is longer than 500 characters' do
  632. account = Fabricate.build(:account, note: Faker::Lorem.characters(number: 501))
  633. account.valid?
  634. expect(account).to model_have_error_on_field(:note)
  635. end
  636. end
  637. context 'when is remote' do
  638. it 'is invalid if the username is same among accounts in the same normalized domain' do
  639. Fabricate(:account, domain: 'にゃん', username: 'username')
  640. account = Fabricate.build(:account, domain: 'xn--r9j5b5b', username: 'username')
  641. account.valid?
  642. expect(account).to model_have_error_on_field(:username)
  643. end
  644. it 'is invalid if the username is not unique in case-insensitive comparison among accounts in the same normalized domain' do
  645. Fabricate(:account, domain: 'にゃん', username: 'username')
  646. account = Fabricate.build(:account, domain: 'xn--r9j5b5b', username: 'Username')
  647. account.valid?
  648. expect(account).to model_have_error_on_field(:username)
  649. end
  650. it 'is valid even if the username contains hyphens' do
  651. account = Fabricate.build(:account, domain: 'domain', username: 'the-doctor')
  652. account.valid?
  653. expect(account).to_not model_have_error_on_field(:username)
  654. end
  655. it 'is invalid if the username doesn\'t only contains letters, numbers, underscores and hyphens' do
  656. account = Fabricate.build(:account, domain: 'domain', username: 'the doctor')
  657. account.valid?
  658. expect(account).to model_have_error_on_field(:username)
  659. end
  660. it 'is valid even if the username is longer than 30 characters' do
  661. account = Fabricate.build(:account, domain: 'domain', username: Faker::Lorem.characters(number: 31))
  662. account.valid?
  663. expect(account).to_not model_have_error_on_field(:username)
  664. end
  665. it 'is valid even if the display name is longer than 30 characters' do
  666. account = Fabricate.build(:account, domain: 'domain', display_name: Faker::Lorem.characters(number: 31))
  667. account.valid?
  668. expect(account).to_not model_have_error_on_field(:display_name)
  669. end
  670. it 'is valid even if the note is longer than 500 characters' do
  671. account = Fabricate.build(:account, domain: 'domain', note: Faker::Lorem.characters(number: 501))
  672. account.valid?
  673. expect(account).to_not model_have_error_on_field(:note)
  674. end
  675. end
  676. end
  677. describe 'scopes' do
  678. describe 'alphabetic' do
  679. it 'sorts by alphabetic order of domain and username' do
  680. matches = [
  681. { username: 'a', domain: 'a' },
  682. { username: 'b', domain: 'a' },
  683. { username: 'a', domain: 'b' },
  684. { username: 'b', domain: 'b' },
  685. ].map(&method(:Fabricate).curry(2).call(:account))
  686. expect(described_class.where('id > 0').alphabetic).to eq matches
  687. end
  688. end
  689. describe 'matches_display_name' do
  690. it 'matches display name which starts with the given string' do
  691. match = Fabricate(:account, display_name: 'pattern and suffix')
  692. Fabricate(:account, display_name: 'prefix and pattern')
  693. expect(described_class.matches_display_name('pattern')).to eq [match]
  694. end
  695. end
  696. describe 'matches_username' do
  697. it 'matches display name which starts with the given string' do
  698. match = Fabricate(:account, username: 'pattern_and_suffix')
  699. Fabricate(:account, username: 'prefix_and_pattern')
  700. expect(described_class.matches_username('pattern')).to eq [match]
  701. end
  702. end
  703. describe 'by_domain_and_subdomains' do
  704. it 'returns exact domain matches' do
  705. account = Fabricate(:account, domain: 'example.com')
  706. expect(described_class.by_domain_and_subdomains('example.com')).to eq [account]
  707. end
  708. it 'returns subdomains' do
  709. account = Fabricate(:account, domain: 'foo.example.com')
  710. expect(described_class.by_domain_and_subdomains('example.com')).to eq [account]
  711. end
  712. it 'does not return partially matching domains' do
  713. account = Fabricate(:account, domain: 'grexample.com')
  714. expect(described_class.by_domain_and_subdomains('example.com')).to_not eq [account]
  715. end
  716. end
  717. describe 'remote' do
  718. it 'returns an array of accounts who have a domain' do
  719. _account = Fabricate(:account, domain: nil)
  720. account_with_domain = Fabricate(:account, domain: 'example.com')
  721. expect(described_class.remote).to contain_exactly(account_with_domain)
  722. end
  723. end
  724. describe 'local' do
  725. it 'returns an array of accounts who do not have a domain' do
  726. local_account = Fabricate(:account, domain: nil)
  727. _account_with_domain = Fabricate(:account, domain: 'example.com')
  728. expect(described_class.where('id > 0').local).to contain_exactly(local_account)
  729. end
  730. end
  731. describe 'partitioned' do
  732. it 'returns a relation of accounts partitioned by domain' do
  733. matches = %w(a b a b)
  734. matches.size.times.to_a.shuffle.each do |index|
  735. matches[index] = Fabricate(:account, domain: matches[index])
  736. end
  737. expect(described_class.where('id > 0').partitioned).to match_array(matches)
  738. end
  739. end
  740. describe 'recent' do
  741. it 'returns a relation of accounts sorted by recent creation' do
  742. matches = Array.new(2) { Fabricate(:account) }
  743. expect(described_class.where('id > 0').recent).to match_array(matches)
  744. end
  745. end
  746. describe 'silenced' do
  747. it 'returns an array of accounts who are silenced' do
  748. silenced_account = Fabricate(:account, silenced: true)
  749. _account = Fabricate(:account, silenced: false)
  750. expect(described_class.silenced).to contain_exactly(silenced_account)
  751. end
  752. end
  753. describe 'suspended' do
  754. it 'returns an array of accounts who are suspended' do
  755. suspended_account = Fabricate(:account, suspended: true)
  756. _account = Fabricate(:account, suspended: false)
  757. expect(described_class.suspended).to contain_exactly(suspended_account)
  758. end
  759. end
  760. describe 'searchable' do
  761. let!(:suspended_local) { Fabricate(:account, suspended: true, username: 'suspended_local') }
  762. let!(:suspended_remote) { Fabricate(:account, suspended: true, domain: 'example.org', username: 'suspended_remote') }
  763. let!(:silenced_local) { Fabricate(:account, silenced: true, username: 'silenced_local') }
  764. let!(:silenced_remote) { Fabricate(:account, silenced: true, domain: 'example.org', username: 'silenced_remote') }
  765. let!(:unconfirmed) { Fabricate(:user, confirmed_at: nil).account }
  766. let!(:unapproved) { Fabricate(:user, approved: false).account }
  767. let!(:unconfirmed_unapproved) { Fabricate(:user, confirmed_at: nil, approved: false).account }
  768. let!(:local_account) { Fabricate(:account, username: 'local_account') }
  769. let!(:remote_account) { Fabricate(:account, domain: 'example.org', username: 'remote_account') }
  770. before do
  771. # Accounts get automatically-approved depending on settings, so ensure they aren't approved
  772. unapproved.user.update(approved: false)
  773. unconfirmed_unapproved.user.update(approved: false)
  774. end
  775. it 'returns every usable non-suspended account' do
  776. expect(described_class.searchable).to contain_exactly(silenced_local, silenced_remote, local_account, remote_account)
  777. end
  778. it 'does not mess with previously-applied scopes' do
  779. expect(described_class.where.not(id: remote_account.id).searchable).to contain_exactly(silenced_local, silenced_remote, local_account)
  780. end
  781. end
  782. end
  783. context 'when is local' do
  784. it 'generates keys' do
  785. account = described_class.create!(domain: nil, username: Faker::Internet.user_name(separators: ['_']))
  786. expect(account.keypair).to be_private
  787. expect(account.keypair).to be_public
  788. end
  789. end
  790. context 'when is remote' do
  791. it 'does not generate keys' do
  792. key = OpenSSL::PKey::RSA.new(1024).public_key
  793. account = described_class.create!(domain: 'remote', uri: 'https://remote/actor', username: Faker::Internet.user_name(separators: ['_']), public_key: key.to_pem)
  794. expect(account.keypair.params).to eq key.params
  795. end
  796. it 'normalizes domain' do
  797. account = described_class.create!(domain: 'にゃん', uri: 'https://xn--r9j5b5b/actor', username: Faker::Internet.user_name(separators: ['_']))
  798. expect(account.domain).to eq 'xn--r9j5b5b'
  799. end
  800. end
  801. include_examples 'AccountAvatar', :account
  802. include_examples 'AccountHeader', :account
  803. describe '#increment_count!' do
  804. subject { Fabricate(:account) }
  805. it 'increments the count in multi-threaded an environment when account_stat is not yet initialized' do
  806. subject
  807. increment_by = 15
  808. wait_for_start = true
  809. threads = Array.new(increment_by) do
  810. Thread.new do
  811. true while wait_for_start
  812. described_class.find(subject.id).increment_count!(:followers_count)
  813. end
  814. end
  815. wait_for_start = false
  816. threads.each(&:join)
  817. expect(subject.reload.followers_count).to eq 15
  818. end
  819. end
  820. end