1
0

account_interactions_spec.rb 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. require 'rails_helper'
  2. describe AccountInteractions do
  3. let(:account) { Fabricate(:account, username: 'account') }
  4. let(:account_id) { account.id }
  5. let(:account_ids) { [account_id] }
  6. let(:target_account) { Fabricate(:account, username: 'target') }
  7. let(:target_account_id) { target_account.id }
  8. let(:target_account_ids) { [target_account_id] }
  9. describe '.following_map' do
  10. subject { Account.following_map(target_account_ids, account_id) }
  11. context 'account with Follow' do
  12. it 'returns { target_account_id => true }' do
  13. Fabricate(:follow, account: account, target_account: target_account)
  14. is_expected.to eq(target_account_id => { reblogs: true })
  15. end
  16. end
  17. context 'account without Follow' do
  18. it 'returns {}' do
  19. is_expected.to eq({})
  20. end
  21. end
  22. end
  23. describe '.followed_by_map' do
  24. subject { Account.followed_by_map(target_account_ids, account_id) }
  25. context 'account with Follow' do
  26. it 'returns { target_account_id => true }' do
  27. Fabricate(:follow, account: target_account, target_account: account)
  28. is_expected.to eq(target_account_id => true)
  29. end
  30. end
  31. context 'account without Follow' do
  32. it 'returns {}' do
  33. is_expected.to eq({})
  34. end
  35. end
  36. end
  37. describe '.blocking_map' do
  38. subject { Account.blocking_map(target_account_ids, account_id) }
  39. context 'account with Block' do
  40. it 'returns { target_account_id => true }' do
  41. Fabricate(:block, account: account, target_account: target_account)
  42. is_expected.to eq(target_account_id => true)
  43. end
  44. end
  45. context 'account without Block' do
  46. it 'returns {}' do
  47. is_expected.to eq({})
  48. end
  49. end
  50. end
  51. describe '.muting_map' do
  52. subject { Account.muting_map(target_account_ids, account_id) }
  53. context 'account with Mute' do
  54. before do
  55. Fabricate(:mute, target_account: target_account, account: account, hide_notifications: hide)
  56. end
  57. context 'if Mute#hide_notifications?' do
  58. let(:hide) { true }
  59. it 'returns { target_account_id => { notifications: true } }' do
  60. is_expected.to eq(target_account_id => { notifications: true })
  61. end
  62. end
  63. context 'unless Mute#hide_notifications?' do
  64. let(:hide) { false }
  65. it 'returns { target_account_id => { notifications: false } }' do
  66. is_expected.to eq(target_account_id => { notifications: false })
  67. end
  68. end
  69. end
  70. context 'account without Mute' do
  71. it 'returns {}' do
  72. is_expected.to eq({})
  73. end
  74. end
  75. end
  76. describe '#follow!' do
  77. it 'creates and returns Follow' do
  78. expect do
  79. expect(account.follow!(target_account)).to be_kind_of Follow
  80. end.to change { account.following.count }.by 1
  81. end
  82. end
  83. describe '#block' do
  84. it 'creates and returns Block' do
  85. expect do
  86. expect(account.block!(target_account)).to be_kind_of Block
  87. end.to change { account.block_relationships.count }.by 1
  88. end
  89. end
  90. describe '#mute!' do
  91. subject { account.mute!(target_account, notifications: arg_notifications) }
  92. context 'Mute does not exist yet' do
  93. context 'arg :notifications is nil' do
  94. let(:arg_notifications) { nil }
  95. it 'creates Mute, and returns Mute' do
  96. expect do
  97. expect(subject).to be_kind_of Mute
  98. end.to change { account.mute_relationships.count }.by 1
  99. end
  100. end
  101. context 'arg :notifications is false' do
  102. let(:arg_notifications) { false }
  103. it 'creates Mute, and returns Mute' do
  104. expect do
  105. expect(subject).to be_kind_of Mute
  106. end.to change { account.mute_relationships.count }.by 1
  107. end
  108. end
  109. context 'arg :notifications is true' do
  110. let(:arg_notifications) { true }
  111. it 'creates Mute, and returns Mute' do
  112. expect do
  113. expect(subject).to be_kind_of Mute
  114. end.to change { account.mute_relationships.count }.by 1
  115. end
  116. end
  117. end
  118. context 'Mute already exists' do
  119. before do
  120. account.mute_relationships << mute
  121. end
  122. let(:mute) do
  123. Fabricate(:mute,
  124. account: account,
  125. target_account: target_account,
  126. hide_notifications: hide_notifications)
  127. end
  128. context 'mute.hide_notifications is true' do
  129. let(:hide_notifications) { true }
  130. context 'arg :notifications is nil' do
  131. let(:arg_notifications) { nil }
  132. it 'returns Mute without updating mute.hide_notifications' do
  133. expect do
  134. expect(subject).to be_kind_of Mute
  135. end.not_to change { mute.reload.hide_notifications? }.from(true)
  136. end
  137. end
  138. context 'arg :notifications is false' do
  139. let(:arg_notifications) { false }
  140. it 'returns Mute, and updates mute.hide_notifications false' do
  141. expect do
  142. expect(subject).to be_kind_of Mute
  143. end.to change { mute.reload.hide_notifications? }.from(true).to(false)
  144. end
  145. end
  146. context 'arg :notifications is true' do
  147. let(:arg_notifications) { true }
  148. it 'returns Mute without updating mute.hide_notifications' do
  149. expect do
  150. expect(subject).to be_kind_of Mute
  151. end.not_to change { mute.reload.hide_notifications? }.from(true)
  152. end
  153. end
  154. end
  155. context 'mute.hide_notifications is false' do
  156. let(:hide_notifications) { false }
  157. context 'arg :notifications is nil' do
  158. let(:arg_notifications) { nil }
  159. it 'returns Mute, and updates mute.hide_notifications true' do
  160. expect do
  161. expect(subject).to be_kind_of Mute
  162. end.to change { mute.reload.hide_notifications? }.from(false).to(true)
  163. end
  164. end
  165. context 'arg :notifications is false' do
  166. let(:arg_notifications) { false }
  167. it 'returns Mute without updating mute.hide_notifications' do
  168. expect do
  169. expect(subject).to be_kind_of Mute
  170. end.not_to change { mute.reload.hide_notifications? }.from(false)
  171. end
  172. end
  173. context 'arg :notifications is true' do
  174. let(:arg_notifications) { true }
  175. it 'returns Mute, and updates mute.hide_notifications true' do
  176. expect do
  177. expect(subject).to be_kind_of Mute
  178. end.to change { mute.reload.hide_notifications? }.from(false).to(true)
  179. end
  180. end
  181. end
  182. end
  183. end
  184. describe '#mute_conversation!' do
  185. let(:conversation) { Fabricate(:conversation) }
  186. subject { account.mute_conversation!(conversation) }
  187. it 'creates and returns ConversationMute' do
  188. expect do
  189. is_expected.to be_kind_of ConversationMute
  190. end.to change { account.conversation_mutes.count }.by 1
  191. end
  192. end
  193. describe '#block_domain!' do
  194. let(:domain) { 'example.com' }
  195. subject { account.block_domain!(domain) }
  196. it 'creates and returns AccountDomainBlock' do
  197. expect do
  198. is_expected.to be_kind_of AccountDomainBlock
  199. end.to change { account.domain_blocks.count }.by 1
  200. end
  201. end
  202. describe '#unfollow!' do
  203. subject { account.unfollow!(target_account) }
  204. context 'following target_account' do
  205. it 'returns destroyed Follow' do
  206. account.active_relationships.create(target_account: target_account)
  207. is_expected.to be_kind_of Follow
  208. expect(subject).to be_destroyed
  209. end
  210. end
  211. context 'not following target_account' do
  212. it 'returns nil' do
  213. is_expected.to be_nil
  214. end
  215. end
  216. end
  217. describe '#unblock!' do
  218. subject { account.unblock!(target_account) }
  219. context 'blocking target_account' do
  220. it 'returns destroyed Block' do
  221. account.block_relationships.create(target_account: target_account)
  222. is_expected.to be_kind_of Block
  223. expect(subject).to be_destroyed
  224. end
  225. end
  226. context 'not blocking target_account' do
  227. it 'returns nil' do
  228. is_expected.to be_nil
  229. end
  230. end
  231. end
  232. describe '#unmute!' do
  233. subject { account.unmute!(target_account) }
  234. context 'muting target_account' do
  235. it 'returns destroyed Mute' do
  236. account.mute_relationships.create(target_account: target_account)
  237. is_expected.to be_kind_of Mute
  238. expect(subject).to be_destroyed
  239. end
  240. end
  241. context 'not muting target_account' do
  242. it 'returns nil' do
  243. is_expected.to be_nil
  244. end
  245. end
  246. end
  247. describe '#unmute_conversation!' do
  248. let(:conversation) { Fabricate(:conversation) }
  249. subject { account.unmute_conversation!(conversation) }
  250. context 'muting the conversation' do
  251. it 'returns destroyed ConversationMute' do
  252. account.conversation_mutes.create(conversation: conversation)
  253. is_expected.to be_kind_of ConversationMute
  254. expect(subject).to be_destroyed
  255. end
  256. end
  257. context 'not muting the conversation' do
  258. it 'returns nil' do
  259. is_expected.to be nil
  260. end
  261. end
  262. end
  263. describe '#unblock_domain!' do
  264. let(:domain) { 'example.com' }
  265. subject { account.unblock_domain!(domain) }
  266. context 'blocking the domain' do
  267. it 'returns destroyed AccountDomainBlock' do
  268. account_domain_block = Fabricate(:account_domain_block, domain: domain)
  269. account.domain_blocks << account_domain_block
  270. is_expected.to be_kind_of AccountDomainBlock
  271. expect(subject).to be_destroyed
  272. end
  273. end
  274. context 'unblocking the domain' do
  275. it 'returns nil' do
  276. is_expected.to be_nil
  277. end
  278. end
  279. end
  280. describe '#following?' do
  281. subject { account.following?(target_account) }
  282. context 'following target_account' do
  283. it 'returns true' do
  284. account.active_relationships.create(target_account: target_account)
  285. is_expected.to be true
  286. end
  287. end
  288. context 'not following target_account' do
  289. it 'returns false' do
  290. is_expected.to be false
  291. end
  292. end
  293. end
  294. describe '#blocking?' do
  295. subject { account.blocking?(target_account) }
  296. context 'blocking target_account' do
  297. it 'returns true' do
  298. account.block_relationships.create(target_account: target_account)
  299. is_expected.to be true
  300. end
  301. end
  302. context 'not blocking target_account' do
  303. it 'returns false' do
  304. is_expected.to be false
  305. end
  306. end
  307. end
  308. describe '#domain_blocking?' do
  309. let(:domain) { 'example.com' }
  310. subject { account.domain_blocking?(domain) }
  311. context 'blocking the domain' do
  312. it' returns true' do
  313. account_domain_block = Fabricate(:account_domain_block, domain: domain)
  314. account.domain_blocks << account_domain_block
  315. is_expected.to be true
  316. end
  317. end
  318. context 'not blocking the domain' do
  319. it 'returns false' do
  320. is_expected.to be false
  321. end
  322. end
  323. end
  324. describe '#muting?' do
  325. subject { account.muting?(target_account) }
  326. context 'muting target_account' do
  327. it 'returns true' do
  328. mute = Fabricate(:mute, account: account, target_account: target_account)
  329. account.mute_relationships << mute
  330. is_expected.to be true
  331. end
  332. end
  333. context 'not muting target_account' do
  334. it 'returns false' do
  335. is_expected.to be false
  336. end
  337. end
  338. end
  339. describe '#muting_conversation?' do
  340. let(:conversation) { Fabricate(:conversation) }
  341. subject { account.muting_conversation?(conversation) }
  342. context 'muting the conversation' do
  343. it 'returns true' do
  344. account.conversation_mutes.create(conversation: conversation)
  345. is_expected.to be true
  346. end
  347. end
  348. context 'not muting the conversation' do
  349. it 'returns false' do
  350. is_expected.to be false
  351. end
  352. end
  353. end
  354. describe '#muting_notifications?' do
  355. before do
  356. mute = Fabricate(:mute, target_account: target_account, account: account, hide_notifications: hide)
  357. account.mute_relationships << mute
  358. end
  359. subject { account.muting_notifications?(target_account) }
  360. context 'muting notifications of target_account' do
  361. let(:hide) { true }
  362. it 'returns true' do
  363. is_expected.to be true
  364. end
  365. end
  366. context 'not muting notifications of target_account' do
  367. let(:hide) { false }
  368. it 'returns false' do
  369. is_expected.to be false
  370. end
  371. end
  372. end
  373. describe '#requested?' do
  374. subject { account.requested?(target_account) }
  375. context 'requested by target_account' do
  376. it 'returns true' do
  377. Fabricate(:follow_request, account: account, target_account: target_account)
  378. is_expected.to be true
  379. end
  380. end
  381. context 'not requested by target_account' do
  382. it 'returns false' do
  383. is_expected.to be false
  384. end
  385. end
  386. end
  387. describe '#favourited?' do
  388. let(:status) { Fabricate(:status, account: account, favourites: favourites) }
  389. subject { account.favourited?(status) }
  390. context 'favorited' do
  391. let(:favourites) { [Fabricate(:favourite, account: account)] }
  392. it 'returns true' do
  393. is_expected.to be true
  394. end
  395. end
  396. context 'not favorited' do
  397. let(:favourites) { [] }
  398. it 'returns false' do
  399. is_expected.to be false
  400. end
  401. end
  402. end
  403. describe '#reblogged?' do
  404. let(:status) { Fabricate(:status, account: account, reblogs: reblogs) }
  405. subject { account.reblogged?(status) }
  406. context 'reblogged' do
  407. let(:reblogs) { [Fabricate(:status, account: account)] }
  408. it 'returns true' do
  409. is_expected.to be true
  410. end
  411. end
  412. context 'not reblogged' do
  413. let(:reblogs) { [] }
  414. it 'returns false' do
  415. is_expected.to be false
  416. end
  417. end
  418. end
  419. describe '#pinned?' do
  420. let(:status) { Fabricate(:status, account: account) }
  421. subject { account.pinned?(status) }
  422. context 'pinned' do
  423. it 'returns true' do
  424. Fabricate(:status_pin, account: account, status: status)
  425. is_expected.to be true
  426. end
  427. end
  428. context 'not pinned' do
  429. it 'returns false' do
  430. is_expected.to be false
  431. end
  432. end
  433. end
  434. describe 'muting an account' do
  435. let(:me) { Fabricate(:account, username: 'Me') }
  436. let(:you) { Fabricate(:account, username: 'You') }
  437. context 'with the notifications option unspecified' do
  438. before do
  439. me.mute!(you)
  440. end
  441. it 'defaults to muting notifications' do
  442. expect(me.muting_notifications?(you)).to be true
  443. end
  444. end
  445. context 'with the notifications option set to false' do
  446. before do
  447. me.mute!(you, notifications: false)
  448. end
  449. it 'does not mute notifications' do
  450. expect(me.muting_notifications?(you)).to be false
  451. end
  452. end
  453. context 'with the notifications option set to true' do
  454. before do
  455. me.mute!(you, notifications: true)
  456. end
  457. it 'does mute notifications' do
  458. expect(me.muting_notifications?(you)).to be true
  459. end
  460. end
  461. end
  462. describe 'ignoring reblogs from an account' do
  463. before do
  464. @me = Fabricate(:account, username: 'Me')
  465. @you = Fabricate(:account, username: 'You')
  466. end
  467. context 'with the reblogs option unspecified' do
  468. before do
  469. @me.follow!(@you)
  470. end
  471. it 'defaults to showing reblogs' do
  472. expect(@me.muting_reblogs?(@you)).to be(false)
  473. end
  474. end
  475. context 'with the reblogs option set to false' do
  476. before do
  477. @me.follow!(@you, reblogs: false)
  478. end
  479. it 'does mute reblogs' do
  480. expect(@me.muting_reblogs?(@you)).to be(true)
  481. end
  482. end
  483. context 'with the reblogs option set to true' do
  484. before do
  485. @me.follow!(@you, reblogs: true)
  486. end
  487. it 'does not mute reblogs' do
  488. expect(@me.muting_reblogs?(@you)).to be(false)
  489. end
  490. end
  491. end
  492. end