create_spec.rb 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. require 'rails_helper'
  2. RSpec.describe ActivityPub::Activity::Create do
  3. let(:sender) { Fabricate(:account, followers_url: 'http://example.com/followers', domain: 'example.com', uri: 'https://example.com/actor') }
  4. let(:json) do
  5. {
  6. '@context': 'https://www.w3.org/ns/activitystreams',
  7. id: [ActivityPub::TagManager.instance.uri_for(sender), '#foo'].join,
  8. type: 'Create',
  9. actor: ActivityPub::TagManager.instance.uri_for(sender),
  10. object: object_json,
  11. }.with_indifferent_access
  12. end
  13. before do
  14. sender.update(uri: ActivityPub::TagManager.instance.uri_for(sender))
  15. stub_request(:get, 'http://example.com/attachment.png').to_return(request_fixture('avatar.txt'))
  16. stub_request(:get, 'http://example.com/emoji.png').to_return(body: attachment_fixture('emojo.png'))
  17. stub_request(:get, 'http://example.com/emojib.png').to_return(body: attachment_fixture('emojo.png'), headers: { 'Content-Type' => 'application/octet-stream' })
  18. end
  19. describe '#perform' do
  20. context 'when fetching' do
  21. subject { described_class.new(json, sender) }
  22. before do
  23. subject.perform
  24. end
  25. context 'when object publication date is below ISO8601 range' do
  26. let(:object_json) do
  27. {
  28. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  29. type: 'Note',
  30. content: 'Lorem ipsum',
  31. published: '-0977-11-03T08:31:22Z',
  32. }
  33. end
  34. it 'creates status with a valid creation date', :aggregate_failures do
  35. status = sender.statuses.first
  36. expect(status).to_not be_nil
  37. expect(status.text).to eq 'Lorem ipsum'
  38. expect(status.created_at).to be_within(30).of(Time.now.utc)
  39. end
  40. end
  41. context 'when object publication date is above ISO8601 range' do
  42. let(:object_json) do
  43. {
  44. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  45. type: 'Note',
  46. content: 'Lorem ipsum',
  47. published: '10000-11-03T08:31:22Z',
  48. }
  49. end
  50. it 'creates status with a valid creation date', :aggregate_failures do
  51. status = sender.statuses.first
  52. expect(status).to_not be_nil
  53. expect(status.text).to eq 'Lorem ipsum'
  54. expect(status.created_at).to be_within(30).of(Time.now.utc)
  55. end
  56. end
  57. context 'when object has been edited' do
  58. let(:object_json) do
  59. {
  60. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  61. type: 'Note',
  62. content: 'Lorem ipsum',
  63. published: '2022-01-22T15:00:00Z',
  64. updated: '2022-01-22T16:00:00Z',
  65. }
  66. end
  67. it 'creates status with appropriate creation and edition dates', :aggregate_failures do
  68. status = sender.statuses.first
  69. expect(status).to_not be_nil
  70. expect(status.text).to eq 'Lorem ipsum'
  71. expect(status.created_at).to eq '2022-01-22T15:00:00Z'.to_datetime
  72. expect(status.edited?).to be true
  73. expect(status.edited_at).to eq '2022-01-22T16:00:00Z'.to_datetime
  74. end
  75. end
  76. context 'object has update date equal to creation date' do
  77. let(:object_json) do
  78. {
  79. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  80. type: 'Note',
  81. content: 'Lorem ipsum',
  82. published: '2022-01-22T15:00:00Z',
  83. updated: '2022-01-22T15:00:00Z',
  84. }
  85. end
  86. it 'creates status' do
  87. status = sender.statuses.first
  88. expect(status).to_not be_nil
  89. expect(status.text).to eq 'Lorem ipsum'
  90. end
  91. it 'does not mark status as edited' do
  92. status = sender.statuses.first
  93. expect(status).to_not be_nil
  94. expect(status.edited?).to eq false
  95. end
  96. end
  97. context 'unknown object type' do
  98. let(:object_json) do
  99. {
  100. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  101. type: 'Banana',
  102. content: 'Lorem ipsum',
  103. }
  104. end
  105. it 'does not create a status' do
  106. expect(sender.statuses.count).to be_zero
  107. end
  108. end
  109. context 'standalone' do
  110. let(:object_json) do
  111. {
  112. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  113. type: 'Note',
  114. content: 'Lorem ipsum',
  115. }
  116. end
  117. it 'creates status' do
  118. status = sender.statuses.first
  119. expect(status).to_not be_nil
  120. expect(status.text).to eq 'Lorem ipsum'
  121. end
  122. it 'missing to/cc defaults to direct privacy' do
  123. status = sender.statuses.first
  124. expect(status).to_not be_nil
  125. expect(status.visibility).to eq 'direct'
  126. end
  127. end
  128. context 'public with explicit public address' do
  129. let(:object_json) do
  130. {
  131. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  132. type: 'Note',
  133. content: 'Lorem ipsum',
  134. to: 'https://www.w3.org/ns/activitystreams#Public',
  135. }
  136. end
  137. it 'creates status' do
  138. status = sender.statuses.first
  139. expect(status).to_not be_nil
  140. expect(status.visibility).to eq 'public'
  141. end
  142. end
  143. context 'public with as:Public' do
  144. let(:object_json) do
  145. {
  146. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  147. type: 'Note',
  148. content: 'Lorem ipsum',
  149. to: 'as:Public',
  150. }
  151. end
  152. it 'creates status' do
  153. status = sender.statuses.first
  154. expect(status).to_not be_nil
  155. expect(status.visibility).to eq 'public'
  156. end
  157. end
  158. context 'public with Public' do
  159. let(:object_json) do
  160. {
  161. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  162. type: 'Note',
  163. content: 'Lorem ipsum',
  164. to: 'Public',
  165. }
  166. end
  167. it 'creates status' do
  168. status = sender.statuses.first
  169. expect(status).to_not be_nil
  170. expect(status.visibility).to eq 'public'
  171. end
  172. end
  173. context 'unlisted with explicit public address' do
  174. let(:object_json) do
  175. {
  176. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  177. type: 'Note',
  178. content: 'Lorem ipsum',
  179. cc: 'https://www.w3.org/ns/activitystreams#Public',
  180. }
  181. end
  182. it 'creates status' do
  183. status = sender.statuses.first
  184. expect(status).to_not be_nil
  185. expect(status.visibility).to eq 'unlisted'
  186. end
  187. end
  188. context 'unlisted with as:Public' do
  189. let(:object_json) do
  190. {
  191. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  192. type: 'Note',
  193. content: 'Lorem ipsum',
  194. cc: 'as:Public',
  195. }
  196. end
  197. it 'creates status' do
  198. status = sender.statuses.first
  199. expect(status).to_not be_nil
  200. expect(status.visibility).to eq 'unlisted'
  201. end
  202. end
  203. context 'unlisted with Public' do
  204. let(:object_json) do
  205. {
  206. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  207. type: 'Note',
  208. content: 'Lorem ipsum',
  209. cc: 'Public',
  210. }
  211. end
  212. it 'creates status' do
  213. status = sender.statuses.first
  214. expect(status).to_not be_nil
  215. expect(status.visibility).to eq 'unlisted'
  216. end
  217. end
  218. context 'private' do
  219. let(:object_json) do
  220. {
  221. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  222. type: 'Note',
  223. content: 'Lorem ipsum',
  224. to: 'http://example.com/followers',
  225. }
  226. end
  227. it 'creates status' do
  228. status = sender.statuses.first
  229. expect(status).to_not be_nil
  230. expect(status.visibility).to eq 'private'
  231. end
  232. end
  233. context 'private with inlined Collection in audience' do
  234. let(:object_json) do
  235. {
  236. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  237. type: 'Note',
  238. content: 'Lorem ipsum',
  239. to: {
  240. 'type': 'OrderedCollection',
  241. 'id': 'http://example.com/followers',
  242. 'first': 'http://example.com/followers?page=true',
  243. }
  244. }
  245. end
  246. it 'creates status' do
  247. status = sender.statuses.first
  248. expect(status).to_not be_nil
  249. expect(status.visibility).to eq 'private'
  250. end
  251. end
  252. context 'limited' do
  253. let(:recipient) { Fabricate(:account) }
  254. let(:object_json) do
  255. {
  256. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  257. type: 'Note',
  258. content: 'Lorem ipsum',
  259. to: ActivityPub::TagManager.instance.uri_for(recipient),
  260. }
  261. end
  262. it 'creates status' do
  263. status = sender.statuses.first
  264. expect(status).to_not be_nil
  265. expect(status.visibility).to eq 'limited'
  266. end
  267. it 'creates silent mention' do
  268. status = sender.statuses.first
  269. expect(status.mentions.first).to be_silent
  270. end
  271. end
  272. context 'direct' do
  273. let(:recipient) { Fabricate(:account) }
  274. let(:object_json) do
  275. {
  276. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  277. type: 'Note',
  278. content: 'Lorem ipsum',
  279. to: ActivityPub::TagManager.instance.uri_for(recipient),
  280. tag: {
  281. type: 'Mention',
  282. href: ActivityPub::TagManager.instance.uri_for(recipient),
  283. },
  284. }
  285. end
  286. it 'creates status' do
  287. status = sender.statuses.first
  288. expect(status).to_not be_nil
  289. expect(status.visibility).to eq 'direct'
  290. end
  291. end
  292. context 'as a reply' do
  293. let(:original_status) { Fabricate(:status) }
  294. let(:object_json) do
  295. {
  296. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  297. type: 'Note',
  298. content: 'Lorem ipsum',
  299. inReplyTo: ActivityPub::TagManager.instance.uri_for(original_status),
  300. }
  301. end
  302. it 'creates status' do
  303. status = sender.statuses.first
  304. expect(status).to_not be_nil
  305. expect(status.thread).to eq original_status
  306. expect(status.reply?).to be true
  307. expect(status.in_reply_to_account).to eq original_status.account
  308. expect(status.conversation).to eq original_status.conversation
  309. end
  310. end
  311. context 'with mentions' do
  312. let(:recipient) { Fabricate(:account) }
  313. let(:object_json) do
  314. {
  315. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  316. type: 'Note',
  317. content: 'Lorem ipsum',
  318. tag: [
  319. {
  320. type: 'Mention',
  321. href: ActivityPub::TagManager.instance.uri_for(recipient),
  322. },
  323. ],
  324. }
  325. end
  326. it 'creates status' do
  327. status = sender.statuses.first
  328. expect(status).to_not be_nil
  329. expect(status.mentions.map(&:account)).to include(recipient)
  330. end
  331. end
  332. context 'with mentions missing href' do
  333. let(:object_json) do
  334. {
  335. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  336. type: 'Note',
  337. content: 'Lorem ipsum',
  338. tag: [
  339. {
  340. type: 'Mention',
  341. },
  342. ],
  343. }
  344. end
  345. it 'creates status' do
  346. status = sender.statuses.first
  347. expect(status).to_not be_nil
  348. end
  349. end
  350. context 'with media attachments' do
  351. let(:object_json) do
  352. {
  353. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  354. type: 'Note',
  355. content: 'Lorem ipsum',
  356. attachment: [
  357. {
  358. type: 'Document',
  359. mediaType: 'image/png',
  360. url: 'http://example.com/attachment.png',
  361. },
  362. ],
  363. }
  364. end
  365. it 'creates status' do
  366. status = sender.statuses.first
  367. expect(status).to_not be_nil
  368. expect(status.media_attachments.map(&:remote_url)).to include('http://example.com/attachment.png')
  369. end
  370. end
  371. context 'with media attachments with long description' do
  372. let(:object_json) do
  373. {
  374. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  375. type: 'Note',
  376. content: 'Lorem ipsum',
  377. attachment: [
  378. {
  379. type: 'Document',
  380. mediaType: 'image/png',
  381. url: 'http://example.com/attachment.png',
  382. name: '*' * 1500,
  383. },
  384. ],
  385. }
  386. end
  387. it 'creates status' do
  388. status = sender.statuses.first
  389. expect(status).to_not be_nil
  390. expect(status.media_attachments.map(&:description)).to include('*' * 1500)
  391. end
  392. end
  393. context 'with media attachments with long description as summary' do
  394. let(:object_json) do
  395. {
  396. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  397. type: 'Note',
  398. content: 'Lorem ipsum',
  399. attachment: [
  400. {
  401. type: 'Document',
  402. mediaType: 'image/png',
  403. url: 'http://example.com/attachment.png',
  404. summary: '*' * 1500,
  405. },
  406. ],
  407. }
  408. end
  409. it 'creates status' do
  410. status = sender.statuses.first
  411. expect(status).to_not be_nil
  412. expect(status.media_attachments.map(&:description)).to include('*' * 1500)
  413. end
  414. end
  415. context 'with media attachments with focal points' do
  416. let(:object_json) do
  417. {
  418. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  419. type: 'Note',
  420. content: 'Lorem ipsum',
  421. attachment: [
  422. {
  423. type: 'Document',
  424. mediaType: 'image/png',
  425. url: 'http://example.com/attachment.png',
  426. focalPoint: [0.5, -0.7],
  427. },
  428. ],
  429. }
  430. end
  431. it 'creates status' do
  432. status = sender.statuses.first
  433. expect(status).to_not be_nil
  434. expect(status.media_attachments.map(&:focus)).to include('0.5,-0.7')
  435. end
  436. end
  437. context 'with media attachments missing url' do
  438. let(:object_json) do
  439. {
  440. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  441. type: 'Note',
  442. content: 'Lorem ipsum',
  443. attachment: [
  444. {
  445. type: 'Document',
  446. mediaType: 'image/png',
  447. },
  448. ],
  449. }
  450. end
  451. it 'creates status' do
  452. status = sender.statuses.first
  453. expect(status).to_not be_nil
  454. end
  455. end
  456. context 'with hashtags' do
  457. let(:object_json) do
  458. {
  459. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  460. type: 'Note',
  461. content: 'Lorem ipsum',
  462. tag: [
  463. {
  464. type: 'Hashtag',
  465. href: 'http://example.com/blah',
  466. name: '#test',
  467. },
  468. ],
  469. }
  470. end
  471. it 'creates status' do
  472. status = sender.statuses.first
  473. expect(status).to_not be_nil
  474. expect(status.tags.map(&:name)).to include('test')
  475. end
  476. end
  477. context 'with hashtags missing name' do
  478. let(:object_json) do
  479. {
  480. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  481. type: 'Note',
  482. content: 'Lorem ipsum',
  483. tag: [
  484. {
  485. type: 'Hashtag',
  486. href: 'http://example.com/blah',
  487. },
  488. ],
  489. }
  490. end
  491. it 'creates status' do
  492. status = sender.statuses.first
  493. expect(status).to_not be_nil
  494. end
  495. end
  496. context 'with hashtags invalid name' do
  497. let(:object_json) do
  498. {
  499. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  500. type: 'Note',
  501. content: 'Lorem ipsum',
  502. tag: [
  503. {
  504. type: 'Hashtag',
  505. href: 'http://example.com/blah',
  506. name: 'foo, #eh !',
  507. },
  508. ],
  509. }
  510. end
  511. it 'creates status' do
  512. status = sender.statuses.first
  513. expect(status).to_not be_nil
  514. end
  515. end
  516. context 'with emojis' do
  517. let(:object_json) do
  518. {
  519. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  520. type: 'Note',
  521. content: 'Lorem ipsum :tinking:',
  522. tag: [
  523. {
  524. type: 'Emoji',
  525. icon: {
  526. url: 'http://example.com/emoji.png',
  527. },
  528. name: 'tinking',
  529. },
  530. ],
  531. }
  532. end
  533. it 'creates status' do
  534. status = sender.statuses.first
  535. expect(status).to_not be_nil
  536. expect(status.emojis.map(&:shortcode)).to include('tinking')
  537. end
  538. end
  539. context 'with emojis served with invalid content-type' do
  540. let(:object_json) do
  541. {
  542. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  543. type: 'Note',
  544. content: 'Lorem ipsum :tinkong:',
  545. tag: [
  546. {
  547. type: 'Emoji',
  548. icon: {
  549. url: 'http://example.com/emojib.png',
  550. },
  551. name: 'tinkong',
  552. },
  553. ],
  554. }
  555. end
  556. it 'creates status' do
  557. status = sender.statuses.first
  558. expect(status).to_not be_nil
  559. expect(status.emojis.map(&:shortcode)).to include('tinkong')
  560. end
  561. end
  562. context 'with emojis missing name' do
  563. let(:object_json) do
  564. {
  565. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  566. type: 'Note',
  567. content: 'Lorem ipsum :tinking:',
  568. tag: [
  569. {
  570. type: 'Emoji',
  571. icon: {
  572. url: 'http://example.com/emoji.png',
  573. },
  574. },
  575. ],
  576. }
  577. end
  578. it 'creates status' do
  579. status = sender.statuses.first
  580. expect(status).to_not be_nil
  581. end
  582. end
  583. context 'with emojis missing icon' do
  584. let(:object_json) do
  585. {
  586. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  587. type: 'Note',
  588. content: 'Lorem ipsum :tinking:',
  589. tag: [
  590. {
  591. type: 'Emoji',
  592. name: 'tinking',
  593. },
  594. ],
  595. }
  596. end
  597. it 'creates status' do
  598. status = sender.statuses.first
  599. expect(status).to_not be_nil
  600. end
  601. end
  602. context 'with poll' do
  603. let(:object_json) do
  604. {
  605. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  606. type: 'Question',
  607. content: 'Which color was the submarine?',
  608. oneOf: [
  609. {
  610. name: 'Yellow',
  611. replies: {
  612. type: 'Collection',
  613. totalItems: 10,
  614. },
  615. },
  616. {
  617. name: 'Blue',
  618. replies: {
  619. type: 'Collection',
  620. totalItems: 3,
  621. }
  622. },
  623. ],
  624. }
  625. end
  626. it 'creates status' do
  627. status = sender.statuses.first
  628. expect(status).to_not be_nil
  629. expect(status.poll).to_not be_nil
  630. end
  631. it 'creates a poll' do
  632. poll = sender.polls.first
  633. expect(poll).to_not be_nil
  634. expect(poll.status).to_not be_nil
  635. expect(poll.options).to eq %w(Yellow Blue)
  636. expect(poll.cached_tallies).to eq [10, 3]
  637. end
  638. end
  639. context 'when a vote to a local poll' do
  640. let(:poll) { Fabricate(:poll, options: %w(Yellow Blue)) }
  641. let!(:local_status) { Fabricate(:status, poll: poll) }
  642. let(:object_json) do
  643. {
  644. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  645. type: 'Note',
  646. name: 'Yellow',
  647. inReplyTo: ActivityPub::TagManager.instance.uri_for(local_status)
  648. }
  649. end
  650. it 'adds a vote to the poll with correct uri' do
  651. vote = poll.votes.first
  652. expect(vote).to_not be_nil
  653. expect(vote.uri).to eq object_json[:id]
  654. expect(poll.reload.cached_tallies).to eq [1, 0]
  655. end
  656. end
  657. context 'when a vote to an expired local poll' do
  658. let(:poll) do
  659. poll = Fabricate.build(:poll, options: %w(Yellow Blue), expires_at: 1.day.ago)
  660. poll.save(validate: false)
  661. poll
  662. end
  663. let!(:local_status) { Fabricate(:status, poll: poll) }
  664. let(:object_json) do
  665. {
  666. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  667. type: 'Note',
  668. name: 'Yellow',
  669. inReplyTo: ActivityPub::TagManager.instance.uri_for(local_status)
  670. }
  671. end
  672. it 'does not add a vote to the poll' do
  673. expect(poll.votes.first).to be_nil
  674. end
  675. end
  676. end
  677. context 'with an encrypted message' do
  678. let(:recipient) { Fabricate(:account) }
  679. let(:target_device) { Fabricate(:device, account: recipient) }
  680. subject { described_class.new(json, sender, delivery: true, delivered_to_account_id: recipient.id) }
  681. let(:object_json) do
  682. {
  683. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  684. type: 'EncryptedMessage',
  685. attributedTo: {
  686. type: 'Device',
  687. deviceId: '1234',
  688. },
  689. to: {
  690. type: 'Device',
  691. deviceId: target_device.device_id,
  692. },
  693. messageType: 1,
  694. cipherText: 'Foo',
  695. messageFranking: 'Baz678',
  696. digest: {
  697. digestAlgorithm: 'Bar456',
  698. digestValue: 'Foo123',
  699. },
  700. }
  701. end
  702. before do
  703. subject.perform
  704. end
  705. it 'creates an encrypted message' do
  706. encrypted_message = target_device.encrypted_messages.reload.first
  707. expect(encrypted_message).to_not be_nil
  708. expect(encrypted_message.from_device_id).to eq '1234'
  709. expect(encrypted_message.from_account).to eq sender
  710. expect(encrypted_message.type).to eq 1
  711. expect(encrypted_message.body).to eq 'Foo'
  712. expect(encrypted_message.digest).to eq 'Foo123'
  713. end
  714. it 'creates a message franking' do
  715. encrypted_message = target_device.encrypted_messages.reload.first
  716. message_franking = encrypted_message.message_franking
  717. crypt = ActiveSupport::MessageEncryptor.new(SystemKey.current_key, serializer: Oj)
  718. json = crypt.decrypt_and_verify(message_franking)
  719. expect(json['source_account_id']).to eq sender.id
  720. expect(json['target_account_id']).to eq recipient.id
  721. expect(json['original_franking']).to eq 'Baz678'
  722. end
  723. end
  724. context 'when sender is followed by local users' do
  725. subject { described_class.new(json, sender, delivery: true) }
  726. before do
  727. Fabricate(:account).follow!(sender)
  728. subject.perform
  729. end
  730. let(:object_json) do
  731. {
  732. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  733. type: 'Note',
  734. content: 'Lorem ipsum',
  735. }
  736. end
  737. it 'creates status' do
  738. status = sender.statuses.first
  739. expect(status).to_not be_nil
  740. expect(status.text).to eq 'Lorem ipsum'
  741. end
  742. end
  743. context 'when sender replies to local status' do
  744. let!(:local_status) { Fabricate(:status) }
  745. subject { described_class.new(json, sender, delivery: true) }
  746. before do
  747. subject.perform
  748. end
  749. let(:object_json) do
  750. {
  751. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  752. type: 'Note',
  753. content: 'Lorem ipsum',
  754. inReplyTo: ActivityPub::TagManager.instance.uri_for(local_status),
  755. }
  756. end
  757. it 'creates status' do
  758. status = sender.statuses.first
  759. expect(status).to_not be_nil
  760. expect(status.text).to eq 'Lorem ipsum'
  761. end
  762. end
  763. context 'when sender targets a local user' do
  764. let!(:local_account) { Fabricate(:account) }
  765. subject { described_class.new(json, sender, delivery: true) }
  766. before do
  767. subject.perform
  768. end
  769. let(:object_json) do
  770. {
  771. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  772. type: 'Note',
  773. content: 'Lorem ipsum',
  774. to: ActivityPub::TagManager.instance.uri_for(local_account),
  775. }
  776. end
  777. it 'creates status' do
  778. status = sender.statuses.first
  779. expect(status).to_not be_nil
  780. expect(status.text).to eq 'Lorem ipsum'
  781. end
  782. end
  783. context 'when sender cc\'s a local user' do
  784. let!(:local_account) { Fabricate(:account) }
  785. subject { described_class.new(json, sender, delivery: true) }
  786. before do
  787. subject.perform
  788. end
  789. let(:object_json) do
  790. {
  791. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  792. type: 'Note',
  793. content: 'Lorem ipsum',
  794. cc: ActivityPub::TagManager.instance.uri_for(local_account),
  795. }
  796. end
  797. it 'creates status' do
  798. status = sender.statuses.first
  799. expect(status).to_not be_nil
  800. expect(status.text).to eq 'Lorem ipsum'
  801. end
  802. end
  803. context 'when the sender has no relevance to local activity' do
  804. subject { described_class.new(json, sender, delivery: true) }
  805. before do
  806. subject.perform
  807. end
  808. let(:object_json) do
  809. {
  810. id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
  811. type: 'Note',
  812. content: 'Lorem ipsum',
  813. }
  814. end
  815. it 'does not create anything' do
  816. expect(sender.statuses.count).to eq 0
  817. end
  818. end
  819. end
  820. end