account_spec.rb 32 KB

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