account_spec.rb 27 KB

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