You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

826 lines
26 KiB

8 years ago
8 years ago
  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(:account, user: Fabricate(:user, email: 'foo+bar@domain.org')) }
  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 'accepts ?, \, : and space as delimiter' do
  284. match = Fabricate(
  285. :account,
  286. display_name: 'A & l & i & c & e',
  287. username: 'username',
  288. domain: 'example.com'
  289. )
  290. results = Account.search_for('A?l\i:c e')
  291. expect(results).to eq [match]
  292. end
  293. it 'finds accounts with matching display_name' do
  294. match = Fabricate(
  295. :account,
  296. display_name: "Display Name",
  297. username: "username",
  298. domain: "example.com"
  299. )
  300. results = Account.search_for("display")
  301. expect(results).to eq [match]
  302. end
  303. it 'finds accounts with matching username' do
  304. match = Fabricate(
  305. :account,
  306. display_name: "Display Name",
  307. username: "username",
  308. domain: "example.com"
  309. )
  310. results = Account.search_for("username")
  311. expect(results).to eq [match]
  312. end
  313. it 'finds accounts with matching domain' do
  314. match = Fabricate(
  315. :account,
  316. display_name: "Display Name",
  317. username: "username",
  318. domain: "example.com"
  319. )
  320. results = Account.search_for("example")
  321. expect(results).to eq [match]
  322. end
  323. it 'limits by 10 by default' do
  324. 11.times.each { Fabricate(:account, display_name: "Display Name") }
  325. results = Account.search_for("display")
  326. expect(results.size).to eq 10
  327. end
  328. it 'accepts arbitrary limits' do
  329. 2.times.each { Fabricate(:account, display_name: "Display Name") }
  330. results = Account.search_for("display", 1)
  331. expect(results.size).to eq 1
  332. end
  333. it 'ranks multiple matches higher' do
  334. matches = [
  335. { username: "username", display_name: "username" },
  336. { display_name: "Display Name", username: "username", domain: "example.com" },
  337. ].map(&method(:Fabricate).curry(2).call(:account))
  338. results = Account.search_for("username")
  339. expect(results).to eq matches
  340. end
  341. end
  342. describe '.advanced_search_for' do
  343. it 'accepts ?, \, : and space as delimiter' do
  344. account = Fabricate(:account)
  345. match = Fabricate(
  346. :account,
  347. display_name: 'A & l & i & c & e',
  348. username: 'username',
  349. domain: 'example.com'
  350. )
  351. results = Account.advanced_search_for('A?l\i:c e', account)
  352. expect(results).to eq [match]
  353. end
  354. it 'limits by 10 by default' do
  355. 11.times { 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 { 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 followed accounts higher' do
  365. account = Fabricate(:account)
  366. match = Fabricate(:account, username: "Matching")
  367. followed_match = Fabricate(:account, username: "Matcher")
  368. Fabricate(:follow, account: account, target_account: followed_match)
  369. results = Account.advanced_search_for("match", account)
  370. expect(results).to eq [followed_match, match]
  371. expect(results.first.rank).to be > results.last.rank
  372. end
  373. end
  374. describe '#statuses_count' do
  375. subject { Fabricate(:account) }
  376. it 'counts statuses' do
  377. Fabricate(:status, account: subject)
  378. Fabricate(:status, account: subject)
  379. expect(subject.statuses_count).to eq 2
  380. end
  381. it 'does not count direct statuses' do
  382. Fabricate(:status, account: subject, visibility: :direct)
  383. expect(subject.statuses_count).to eq 0
  384. end
  385. it 'is decremented when status is removed' do
  386. status = Fabricate(:status, account: subject)
  387. expect(subject.statuses_count).to eq 1
  388. status.destroy
  389. expect(subject.statuses_count).to eq 0
  390. end
  391. it 'is decremented when status is removed when account is not preloaded' do
  392. status = Fabricate(:status, account: subject)
  393. expect(subject.reload.statuses_count).to eq 1
  394. clean_status = Status.find(status.id)
  395. expect(clean_status.association(:account).loaded?).to be false
  396. clean_status.destroy
  397. expect(subject.reload.statuses_count).to eq 0
  398. end
  399. end
  400. describe '.following_map' do
  401. it 'returns an hash' do
  402. expect(Account.following_map([], 1)).to be_a Hash
  403. end
  404. end
  405. describe '.followed_by_map' do
  406. it 'returns an hash' do
  407. expect(Account.followed_by_map([], 1)).to be_a Hash
  408. end
  409. end
  410. describe '.blocking_map' do
  411. it 'returns an hash' do
  412. expect(Account.blocking_map([], 1)).to be_a Hash
  413. end
  414. end
  415. describe '.requested_map' do
  416. it 'returns an hash' do
  417. expect(Account.requested_map([], 1)).to be_a Hash
  418. end
  419. end
  420. describe 'MENTION_RE' do
  421. subject { Account::MENTION_RE }
  422. it 'matches usernames in the middle of a sentence' do
  423. expect(subject.match('Hello to @alice from me')[1]).to eq 'alice'
  424. end
  425. it 'matches usernames in the beginning of status' do
  426. expect(subject.match('@alice Hey how are you?')[1]).to eq 'alice'
  427. end
  428. it 'matches full usernames' do
  429. expect(subject.match('@alice@example.com')[1]).to eq 'alice@example.com'
  430. end
  431. it 'matches full usernames with a dot at the end' do
  432. expect(subject.match('Hello @alice@example.com.')[1]).to eq 'alice@example.com'
  433. end
  434. it 'matches dot-prepended usernames' do
  435. expect(subject.match('.@alice I want everybody to see this')[1]).to eq 'alice'
  436. end
  437. it 'does not match e-mails' do
  438. expect(subject.match('Drop me an e-mail at alice@example.com')).to be_nil
  439. end
  440. it 'does not match URLs' do
  441. expect(subject.match('Check this out https://medium.com/@alice/some-article#.abcdef123')).to be_nil
  442. end
  443. xit 'does not match URL querystring' do
  444. expect(subject.match('https://example.com/?x=@alice')).to be_nil
  445. end
  446. end
  447. describe 'validations' do
  448. it 'has a valid fabricator' do
  449. account = Fabricate.build(:account)
  450. account.valid?
  451. expect(account).to be_valid
  452. end
  453. it 'is invalid without a username' do
  454. account = Fabricate.build(:account, username: nil)
  455. account.valid?
  456. expect(account).to model_have_error_on_field(:username)
  457. end
  458. it 'squishes the username before validation' do
  459. account = Fabricate(:account, domain: nil, username: " \u3000bob \t \u00a0 \n ")
  460. expect(account.username).to eq 'bob'
  461. end
  462. context 'when is local' do
  463. it 'is invalid if the username is not unique in case-insensitive comparison among local accounts' do
  464. account_1 = Fabricate(:account, username: 'the_doctor')
  465. account_2 = Fabricate.build(:account, username: 'the_Doctor')
  466. account_2.valid?
  467. expect(account_2).to model_have_error_on_field(:username)
  468. end
  469. it 'is invalid if the username is reserved' do
  470. account = Fabricate.build(:account, username: 'support')
  471. account.valid?
  472. expect(account).to model_have_error_on_field(:username)
  473. end
  474. it 'is valid when username is reserved but record has already been created' do
  475. account = Fabricate.build(:account, username: 'support')
  476. account.save(validate: false)
  477. expect(account.valid?).to be true
  478. end
  479. it 'is valid if we are creating an instance actor account with a period' do
  480. account = Fabricate.build(:account, id: -99, actor_type: 'Application', locked: true, username: 'example.com')
  481. expect(account.valid?).to be true
  482. end
  483. it 'is valid if we are creating a possibly-conflicting instance actor account' do
  484. account_1 = Fabricate(:account, username: 'examplecom')
  485. account_2 = Fabricate.build(:account, id: -99, actor_type: 'Application', locked: true, username: 'example.com')
  486. expect(account_2.valid?).to be true
  487. end
  488. it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do
  489. account = Fabricate.build(:account, username: 'the-doctor')
  490. account.valid?
  491. expect(account).to model_have_error_on_field(:username)
  492. end
  493. it 'is invalid if the username contains a period' do
  494. account = Fabricate.build(:account, username: 'the.doctor')
  495. account.valid?
  496. expect(account).to model_have_error_on_field(:username)
  497. end
  498. it 'is invalid if the username is longer then 30 characters' do
  499. account = Fabricate.build(:account, username: Faker::Lorem.characters(number: 31))
  500. account.valid?
  501. expect(account).to model_have_error_on_field(:username)
  502. end
  503. it 'is invalid if the display name is longer than 30 characters' do
  504. account = Fabricate.build(:account, display_name: Faker::Lorem.characters(number: 31))
  505. account.valid?
  506. expect(account).to model_have_error_on_field(:display_name)
  507. end
  508. it 'is invalid if the note is longer than 500 characters' do
  509. account = Fabricate.build(:account, note: Faker::Lorem.characters(number: 501))
  510. account.valid?
  511. expect(account).to model_have_error_on_field(:note)
  512. end
  513. end
  514. context 'when is remote' do
  515. it 'is invalid if the username is same among accounts in the same normalized domain' do
  516. Fabricate(:account, domain: 'にゃん', username: 'username')
  517. account = Fabricate.build(:account, domain: 'xn--r9j5b5b', username: 'username')
  518. account.valid?
  519. expect(account).to model_have_error_on_field(:username)
  520. end
  521. it 'is invalid if the username is not unique in case-insensitive comparison among accounts in the same normalized domain' do
  522. Fabricate(:account, domain: 'にゃん', username: 'username')
  523. account = Fabricate.build(:account, domain: 'xn--r9j5b5b', username: 'Username')
  524. account.valid?
  525. expect(account).to model_have_error_on_field(:username)
  526. end
  527. it 'is valid even if the username contains hyphens' do
  528. account = Fabricate.build(:account, domain: 'domain', username: 'the-doctor')
  529. account.valid?
  530. expect(account).to_not model_have_error_on_field(:username)
  531. end
  532. it 'is invalid if the username doesn\'t only contains letters, numbers, underscores and hyphens' do
  533. account = Fabricate.build(:account, domain: 'domain', username: 'the doctor')
  534. account.valid?
  535. expect(account).to model_have_error_on_field(:username)
  536. end
  537. it 'is valid even if the username is longer then 30 characters' do
  538. account = Fabricate.build(:account, domain: 'domain', username: Faker::Lorem.characters(number: 31))
  539. account.valid?
  540. expect(account).not_to model_have_error_on_field(:username)
  541. end
  542. it 'is valid even if the display name is longer than 30 characters' do
  543. account = Fabricate.build(:account, domain: 'domain', display_name: Faker::Lorem.characters(number: 31))
  544. account.valid?
  545. expect(account).not_to model_have_error_on_field(:display_name)
  546. end
  547. it 'is valid even if the note is longer than 500 characters' do
  548. account = Fabricate.build(:account, domain: 'domain', note: Faker::Lorem.characters(number: 501))
  549. account.valid?
  550. expect(account).not_to model_have_error_on_field(:note)
  551. end
  552. end
  553. end
  554. describe 'scopes' do
  555. describe 'alphabetic' do
  556. it 'sorts by alphabetic order of domain and username' do
  557. matches = [
  558. { username: 'a', domain: 'a' },
  559. { username: 'b', domain: 'a' },
  560. { username: 'a', domain: 'b' },
  561. { username: 'b', domain: 'b' },
  562. ].map(&method(:Fabricate).curry(2).call(:account))
  563. expect(Account.where('id > 0').alphabetic).to eq matches
  564. end
  565. end
  566. describe 'matches_display_name' do
  567. it 'matches display name which starts with the given string' do
  568. match = Fabricate(:account, display_name: 'pattern and suffix')
  569. Fabricate(:account, display_name: 'prefix and pattern')
  570. expect(Account.matches_display_name('pattern')).to eq [match]
  571. end
  572. end
  573. describe 'matches_username' do
  574. it 'matches display name which starts with the given string' do
  575. match = Fabricate(:account, username: 'pattern_and_suffix')
  576. Fabricate(:account, username: 'prefix_and_pattern')
  577. expect(Account.matches_username('pattern')).to eq [match]
  578. end
  579. end
  580. describe 'by_domain_and_subdomains' do
  581. it 'returns exact domain matches' do
  582. account = Fabricate(:account, domain: 'example.com')
  583. expect(Account.by_domain_and_subdomains('example.com')).to eq [account]
  584. end
  585. it 'returns subdomains' do
  586. account = Fabricate(:account, domain: 'foo.example.com')
  587. expect(Account.by_domain_and_subdomains('example.com')).to eq [account]
  588. end
  589. it 'does not return partially matching domains' do
  590. account = Fabricate(:account, domain: 'grexample.com')
  591. expect(Account.by_domain_and_subdomains('example.com')).to_not eq [account]
  592. end
  593. end
  594. describe 'remote' do
  595. it 'returns an array of accounts who have a domain' do
  596. account_1 = Fabricate(:account, domain: nil)
  597. account_2 = Fabricate(:account, domain: 'example.com')
  598. expect(Account.remote).to match_array([account_2])
  599. end
  600. end
  601. describe 'local' do
  602. it 'returns an array of accounts who do not have a domain' do
  603. account_1 = Fabricate(:account, domain: nil)
  604. account_2 = Fabricate(:account, domain: 'example.com')
  605. expect(Account.where('id > 0').local).to match_array([account_1])
  606. end
  607. end
  608. describe 'partitioned' do
  609. it 'returns a relation of accounts partitioned by domain' do
  610. matches = ['a', 'b', 'a', 'b']
  611. matches.size.times.to_a.shuffle.each do |index|
  612. matches[index] = Fabricate(:account, domain: matches[index])
  613. end
  614. expect(Account.where('id > 0').partitioned).to match_array(matches)
  615. end
  616. end
  617. describe 'recent' do
  618. it 'returns a relation of accounts sorted by recent creation' do
  619. matches = 2.times.map { Fabricate(:account) }
  620. expect(Account.where('id > 0').recent).to match_array(matches)
  621. end
  622. end
  623. describe 'silenced' do
  624. it 'returns an array of accounts who are silenced' do
  625. account_1 = Fabricate(:account, silenced: true)
  626. account_2 = Fabricate(:account, silenced: false)
  627. expect(Account.silenced).to match_array([account_1])
  628. end
  629. end
  630. describe 'suspended' do
  631. it 'returns an array of accounts who are suspended' do
  632. account_1 = Fabricate(:account, suspended: true)
  633. account_2 = Fabricate(:account, suspended: false)
  634. expect(Account.suspended).to match_array([account_1])
  635. end
  636. end
  637. end
  638. context 'when is local' do
  639. # Test disabled because test environment omits autogenerating keys for performance
  640. xit 'generates keys' do
  641. account = Account.create!(domain: nil, username: Faker::Internet.user_name(separators: ['_']))
  642. expect(account.keypair.private?).to eq true
  643. end
  644. end
  645. context 'when is remote' do
  646. it 'does not generate keys' do
  647. key = OpenSSL::PKey::RSA.new(1024).public_key
  648. account = Account.create!(domain: 'remote', username: Faker::Internet.user_name(separators: ['_']), public_key: key.to_pem)
  649. expect(account.keypair.params).to eq key.params
  650. end
  651. it 'normalizes domain' do
  652. account = Account.create!(domain: 'にゃん', username: Faker::Internet.user_name(separators: ['_']))
  653. expect(account.domain).to eq 'xn--r9j5b5b'
  654. end
  655. end
  656. include_examples 'AccountAvatar', :account
  657. include_examples 'AccountHeader', :account
  658. describe '#increment_count!' do
  659. subject { Fabricate(:account) }
  660. it 'increments the count in multi-threaded an environment when account_stat is not yet initialized' do
  661. subject
  662. increment_by = 15
  663. wait_for_start = true
  664. threads = Array.new(increment_by) do
  665. Thread.new do
  666. true while wait_for_start
  667. Account.find(subject.id).increment_count!(:followers_count)
  668. end
  669. end
  670. wait_for_start = false
  671. threads.each(&:join)
  672. expect(subject.reload.followers_count).to eq 15
  673. end
  674. end
  675. end