account_spec.rb 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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 nil
  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 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 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", 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, 10, 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, 10, 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, 10, 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, 10, 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, 10, 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, 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 'MENTION_RE' do
  536. subject { Account::MENTION_RE }
  537. it 'matches usernames in the middle of a sentence' do
  538. expect(subject.match('Hello to @alice from me')[1]).to eq 'alice'
  539. end
  540. it 'matches usernames in the beginning of status' do
  541. expect(subject.match('@alice Hey how are you?')[1]).to eq 'alice'
  542. end
  543. it 'matches full usernames' do
  544. expect(subject.match('@alice@example.com')[1]).to eq 'alice@example.com'
  545. end
  546. it 'matches full usernames with a dot at the end' do
  547. expect(subject.match('Hello @alice@example.com.')[1]).to eq 'alice@example.com'
  548. end
  549. it 'matches dot-prepended usernames' do
  550. expect(subject.match('.@alice I want everybody to see this')[1]).to eq 'alice'
  551. end
  552. it 'does not match e-mails' do
  553. expect(subject.match('Drop me an e-mail at alice@example.com')).to be_nil
  554. end
  555. it 'does not match URLs' do
  556. expect(subject.match('Check this out https://medium.com/@alice/some-article#.abcdef123')).to be_nil
  557. end
  558. it 'does not match URL query string' do
  559. expect(subject.match('https://example.com/?x=@alice')).to be_nil
  560. end
  561. end
  562. describe 'validations' do
  563. it 'has a valid fabricator' do
  564. account = Fabricate.build(:account)
  565. account.valid?
  566. expect(account).to be_valid
  567. end
  568. it 'is invalid without a username' do
  569. account = Fabricate.build(:account, username: nil)
  570. account.valid?
  571. expect(account).to model_have_error_on_field(:username)
  572. end
  573. it 'squishes the username before validation' do
  574. account = Fabricate(:account, domain: nil, username: " \u3000bob \t \u00a0 \n ")
  575. expect(account.username).to eq 'bob'
  576. end
  577. context 'when is local' do
  578. it 'is invalid if the username is not unique in case-insensitive comparison among local accounts' do
  579. account_1 = Fabricate(:account, username: 'the_doctor')
  580. account_2 = Fabricate.build(:account, username: 'the_Doctor')
  581. account_2.valid?
  582. expect(account_2).to model_have_error_on_field(:username)
  583. end
  584. it 'is invalid if the username is reserved' do
  585. account = Fabricate.build(:account, username: 'support')
  586. account.valid?
  587. expect(account).to model_have_error_on_field(:username)
  588. end
  589. it 'is valid when username is reserved but record has already been created' do
  590. account = Fabricate.build(:account, username: 'support')
  591. account.save(validate: false)
  592. expect(account.valid?).to be true
  593. end
  594. it 'is valid if we are creating an instance actor account with a period' do
  595. account = Fabricate.build(:account, id: -99, actor_type: 'Application', locked: true, username: 'example.com')
  596. expect(account.valid?).to be true
  597. end
  598. it 'is valid if we are creating a possibly-conflicting instance actor account' do
  599. account_1 = Fabricate(:account, username: 'examplecom')
  600. account_2 = Fabricate.build(:account, id: -99, actor_type: 'Application', locked: true, username: 'example.com')
  601. expect(account_2.valid?).to be true
  602. end
  603. it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do
  604. account = Fabricate.build(:account, username: 'the-doctor')
  605. account.valid?
  606. expect(account).to model_have_error_on_field(:username)
  607. end
  608. it 'is invalid if the username contains a period' 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 is longer then 30 characters' do
  614. account = Fabricate.build(:account, username: Faker::Lorem.characters(number: 31))
  615. account.valid?
  616. expect(account).to model_have_error_on_field(:username)
  617. end
  618. it 'is invalid if the display name is longer than 30 characters' do
  619. account = Fabricate.build(:account, display_name: Faker::Lorem.characters(number: 31))
  620. account.valid?
  621. expect(account).to model_have_error_on_field(:display_name)
  622. end
  623. it 'is invalid if the note is longer than 500 characters' do
  624. account = Fabricate.build(:account, note: Faker::Lorem.characters(number: 501))
  625. account.valid?
  626. expect(account).to model_have_error_on_field(:note)
  627. end
  628. end
  629. context 'when is remote' do
  630. it 'is invalid if the username is same among accounts in the same normalized domain' do
  631. Fabricate(:account, domain: 'にゃん', username: 'username')
  632. account = Fabricate.build(:account, domain: 'xn--r9j5b5b', username: 'username')
  633. account.valid?
  634. expect(account).to model_have_error_on_field(:username)
  635. end
  636. it 'is invalid if the username is not unique in case-insensitive comparison among accounts in the same normalized domain' do
  637. Fabricate(:account, domain: 'にゃん', username: 'username')
  638. account = Fabricate.build(:account, domain: 'xn--r9j5b5b', username: 'Username')
  639. account.valid?
  640. expect(account).to model_have_error_on_field(:username)
  641. end
  642. it 'is valid even if the username contains hyphens' do
  643. account = Fabricate.build(:account, domain: 'domain', username: 'the-doctor')
  644. account.valid?
  645. expect(account).to_not model_have_error_on_field(:username)
  646. end
  647. it 'is invalid if the username doesn\'t only contains letters, numbers, underscores and hyphens' do
  648. account = Fabricate.build(:account, domain: 'domain', username: 'the doctor')
  649. account.valid?
  650. expect(account).to model_have_error_on_field(:username)
  651. end
  652. it 'is valid even if the username is longer then 30 characters' do
  653. account = Fabricate.build(:account, domain: 'domain', username: Faker::Lorem.characters(number: 31))
  654. account.valid?
  655. expect(account).not_to model_have_error_on_field(:username)
  656. end
  657. it 'is valid even if the display name is longer than 30 characters' do
  658. account = Fabricate.build(:account, domain: 'domain', display_name: Faker::Lorem.characters(number: 31))
  659. account.valid?
  660. expect(account).not_to model_have_error_on_field(:display_name)
  661. end
  662. it 'is valid even if the note is longer than 500 characters' do
  663. account = Fabricate.build(:account, domain: 'domain', note: Faker::Lorem.characters(number: 501))
  664. account.valid?
  665. expect(account).not_to model_have_error_on_field(:note)
  666. end
  667. end
  668. end
  669. describe 'scopes' do
  670. describe 'alphabetic' do
  671. it 'sorts by alphabetic order of domain and username' do
  672. matches = [
  673. { username: 'a', domain: 'a' },
  674. { username: 'b', domain: 'a' },
  675. { username: 'a', domain: 'b' },
  676. { username: 'b', domain: 'b' },
  677. ].map(&method(:Fabricate).curry(2).call(:account))
  678. expect(Account.where('id > 0').alphabetic).to eq matches
  679. end
  680. end
  681. describe 'matches_display_name' do
  682. it 'matches display name which starts with the given string' do
  683. match = Fabricate(:account, display_name: 'pattern and suffix')
  684. Fabricate(:account, display_name: 'prefix and pattern')
  685. expect(Account.matches_display_name('pattern')).to eq [match]
  686. end
  687. end
  688. describe 'matches_username' do
  689. it 'matches display name which starts with the given string' do
  690. match = Fabricate(:account, username: 'pattern_and_suffix')
  691. Fabricate(:account, username: 'prefix_and_pattern')
  692. expect(Account.matches_username('pattern')).to eq [match]
  693. end
  694. end
  695. describe 'by_domain_and_subdomains' do
  696. it 'returns exact domain matches' do
  697. account = Fabricate(:account, domain: 'example.com')
  698. expect(Account.by_domain_and_subdomains('example.com')).to eq [account]
  699. end
  700. it 'returns subdomains' do
  701. account = Fabricate(:account, domain: 'foo.example.com')
  702. expect(Account.by_domain_and_subdomains('example.com')).to eq [account]
  703. end
  704. it 'does not return partially matching domains' do
  705. account = Fabricate(:account, domain: 'grexample.com')
  706. expect(Account.by_domain_and_subdomains('example.com')).to_not eq [account]
  707. end
  708. end
  709. describe 'remote' do
  710. it 'returns an array of accounts who have a domain' do
  711. account_1 = Fabricate(:account, domain: nil)
  712. account_2 = Fabricate(:account, domain: 'example.com')
  713. expect(Account.remote).to match_array([account_2])
  714. end
  715. end
  716. describe 'local' do
  717. it 'returns an array of accounts who do not have a domain' do
  718. account_1 = Fabricate(:account, domain: nil)
  719. account_2 = Fabricate(:account, domain: 'example.com')
  720. expect(Account.where('id > 0').local).to match_array([account_1])
  721. end
  722. end
  723. describe 'partitioned' do
  724. it 'returns a relation of accounts partitioned by domain' do
  725. matches = ['a', 'b', 'a', 'b']
  726. matches.size.times.to_a.shuffle.each do |index|
  727. matches[index] = Fabricate(:account, domain: matches[index])
  728. end
  729. expect(Account.where('id > 0').partitioned).to match_array(matches)
  730. end
  731. end
  732. describe 'recent' do
  733. it 'returns a relation of accounts sorted by recent creation' do
  734. matches = 2.times.map { Fabricate(:account) }
  735. expect(Account.where('id > 0').recent).to match_array(matches)
  736. end
  737. end
  738. describe 'silenced' do
  739. it 'returns an array of accounts who are silenced' do
  740. account_1 = Fabricate(:account, silenced: true)
  741. account_2 = Fabricate(:account, silenced: false)
  742. expect(Account.silenced).to match_array([account_1])
  743. end
  744. end
  745. describe 'suspended' do
  746. it 'returns an array of accounts who are suspended' do
  747. account_1 = Fabricate(:account, suspended: true)
  748. account_2 = Fabricate(:account, suspended: false)
  749. expect(Account.suspended).to match_array([account_1])
  750. end
  751. end
  752. describe 'searchable' do
  753. let!(:suspended_local) { Fabricate(:account, suspended: true, username: 'suspended_local') }
  754. let!(:suspended_remote) { Fabricate(:account, suspended: true, domain: 'example.org', username: 'suspended_remote') }
  755. let!(:silenced_local) { Fabricate(:account, silenced: true, username: 'silenced_local') }
  756. let!(:silenced_remote) { Fabricate(:account, silenced: true, domain: 'example.org', username: 'silenced_remote') }
  757. let!(:unconfirmed) { Fabricate(:user, confirmed_at: nil).account }
  758. let!(:unapproved) { Fabricate(:user, approved: false).account }
  759. let!(:unconfirmed_unapproved) { Fabricate(:user, confirmed_at: nil, approved: false).account }
  760. let!(:local_account) { Fabricate(:account, username: 'local_account') }
  761. let!(:remote_account) { Fabricate(:account, domain: 'example.org', username: 'remote_account') }
  762. before do
  763. # Accounts get automatically-approved depending on settings, so ensure they aren't approved
  764. unapproved.user.update(approved: false)
  765. unconfirmed_unapproved.user.update(approved: false)
  766. end
  767. it 'returns every usable non-suspended account' do
  768. expect(Account.searchable).to match_array([silenced_local, silenced_remote, local_account, remote_account])
  769. end
  770. it 'does not mess with previously-applied scopes' do
  771. expect(Account.where.not(id: remote_account.id).searchable).to match_array([silenced_local, silenced_remote, local_account])
  772. end
  773. end
  774. end
  775. context 'when is local' do
  776. # Test disabled because test environment omits autogenerating keys for performance
  777. xit 'generates keys' do
  778. account = Account.create!(domain: nil, username: Faker::Internet.user_name(separators: ['_']))
  779. expect(account.keypair.private?).to eq true
  780. end
  781. end
  782. context 'when is remote' do
  783. it 'does not generate keys' do
  784. key = OpenSSL::PKey::RSA.new(1024).public_key
  785. account = Account.create!(domain: 'remote', username: Faker::Internet.user_name(separators: ['_']), public_key: key.to_pem)
  786. expect(account.keypair.params).to eq key.params
  787. end
  788. it 'normalizes domain' do
  789. account = Account.create!(domain: 'にゃん', username: Faker::Internet.user_name(separators: ['_']))
  790. expect(account.domain).to eq 'xn--r9j5b5b'
  791. end
  792. end
  793. include_examples 'AccountAvatar', :account
  794. include_examples 'AccountHeader', :account
  795. describe '#increment_count!' do
  796. subject { Fabricate(:account) }
  797. it 'increments the count in multi-threaded an environment when account_stat is not yet initialized' do
  798. subject
  799. increment_by = 15
  800. wait_for_start = true
  801. threads = Array.new(increment_by) do
  802. Thread.new do
  803. true while wait_for_start
  804. Account.find(subject.id).increment_count!(:followers_count)
  805. end
  806. end
  807. wait_for_start = false
  808. threads.each(&:join)
  809. expect(subject.reload.followers_count).to eq 15
  810. end
  811. end
  812. end