atom_serializer_spec.rb 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. require 'rails_helper'
  2. RSpec.describe AtomSerializer do
  3. shared_examples 'follow request salmon' do
  4. it 'appends author element with account' do
  5. account = Fabricate(:account, domain: nil, username: 'username')
  6. follow_request = Fabricate(:follow_request, account: account)
  7. follow_request_salmon = serialize(follow_request)
  8. expect(follow_request_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  9. end
  10. it 'appends activity:object-type element with activity type' do
  11. follow_request = Fabricate(:follow_request)
  12. follow_request_salmon = serialize(follow_request)
  13. object_type = follow_request_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  14. expect(object_type.text).to eq TagManager::TYPES[:activity]
  15. end
  16. it 'appends activity:verb element with request_friend type' do
  17. follow_request = Fabricate(:follow_request)
  18. follow_request_salmon = serialize(follow_request)
  19. verb = follow_request_salmon.nodes.find { |node| node.name == 'activity:verb' }
  20. expect(verb.text).to eq TagManager::VERBS[:request_friend]
  21. end
  22. it 'appends activity:object with target account' do
  23. target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id')
  24. follow_request = Fabricate(:follow_request, target_account: target_account)
  25. follow_request_salmon = serialize(follow_request)
  26. object = follow_request_salmon.nodes.find { |node| node.name == 'activity:object' }
  27. expect(object.id.text).to eq 'https://domain/id'
  28. end
  29. end
  30. shared_examples 'namespaces' do
  31. it 'adds namespaces' do
  32. element = serialize
  33. expect(element['xmlns']).to eq TagManager::XMLNS
  34. expect(element['xmlns:thr']).to eq TagManager::THR_XMLNS
  35. expect(element['xmlns:activity']).to eq TagManager::AS_XMLNS
  36. expect(element['xmlns:poco']).to eq TagManager::POCO_XMLNS
  37. expect(element['xmlns:media']).to eq TagManager::MEDIA_XMLNS
  38. expect(element['xmlns:ostatus']).to eq TagManager::OS_XMLNS
  39. expect(element['xmlns:mastodon']).to eq TagManager::MTDN_XMLNS
  40. end
  41. end
  42. shared_examples 'no namespaces' do
  43. it 'does not add namespaces' do
  44. expect(serialize['xmlns']).to eq nil
  45. end
  46. end
  47. shared_examples 'status attributes' do
  48. it 'appends summary element with spoiler text if present' do
  49. status = Fabricate(:status, language: :ca, spoiler_text: 'spoiler text')
  50. element = serialize(status)
  51. summary = element.summary
  52. expect(summary['xml:lang']).to eq 'ca'
  53. expect(summary.text).to eq 'spoiler text'
  54. end
  55. it 'does not append summary element with spoiler text if not present' do
  56. status = Fabricate(:status, spoiler_text: '')
  57. element = serialize(status)
  58. element.nodes.each { |node| expect(node.name).not_to eq 'summary' }
  59. end
  60. it 'appends content element with formatted status' do
  61. status = Fabricate(:status, language: :ca, text: 'text')
  62. element = serialize(status)
  63. content = element.content
  64. expect(content[:type]).to eq 'html'
  65. expect(content['xml:lang']).to eq 'ca'
  66. expect(content.text).to eq '<p>text</p>'
  67. end
  68. it 'appends link elements for mentioned accounts' do
  69. account = Fabricate(:account, username: 'username')
  70. status = Fabricate(:status)
  71. Fabricate(:mention, account: account, status: status)
  72. element = serialize(status)
  73. mentioned = element.nodes.find do |node|
  74. node.name == 'link' &&
  75. node[:rel] == 'mentioned' &&
  76. node['ostatus:object-type'] == TagManager::TYPES[:person]
  77. end
  78. expect(mentioned[:href]).to eq 'https://cb6e6126.ngrok.io/users/username'
  79. end
  80. end
  81. describe 'render' do
  82. it 'returns XML with emojis' do
  83. element = Ox::Element.new('tag')
  84. element << '💩'
  85. xml = AtomSerializer.render(element)
  86. expect(xml).to eq "<?xml version=\"1.0\"?>\n<tag>💩</tag>\n"
  87. end
  88. it 'returns XML, stripping invalid characters like \b and \v' do
  89. element = Ox::Element.new('tag')
  90. element << "im l33t\b haxo\b\vr"
  91. xml = AtomSerializer.render(element)
  92. expect(xml).to eq "<?xml version=\"1.0\"?>\n<tag>im l33t haxor</tag>\n"
  93. end
  94. end
  95. describe '#author' do
  96. context 'when note is present' do
  97. it 'appends poco:note element with note for local account' do
  98. account = Fabricate(:account, domain: nil, note: '<p>note</p>')
  99. author = AtomSerializer.new.author(account)
  100. note = author.nodes.find { |node| node.name == 'poco:note' }
  101. expect(note.text).to eq '<p>note</p>'
  102. end
  103. it 'appends poco:note element with tags-stripped note for remote account' do
  104. account = Fabricate(:account, domain: 'remote', note: '<p>note</p>')
  105. author = AtomSerializer.new.author(account)
  106. note = author.nodes.find { |node| node.name == 'poco:note' }
  107. expect(note.text).to eq 'note'
  108. end
  109. it 'appends summary element with type attribute and simplified note if present' do
  110. account = Fabricate(:account, note: 'note')
  111. author = AtomSerializer.new.author(account)
  112. expect(author.summary.text).to eq '<p>note</p>'
  113. expect(author.summary[:type]).to eq 'html'
  114. end
  115. end
  116. context 'when note is not present' do
  117. it 'does not append poco:note element' do
  118. account = Fabricate(:account, note: '')
  119. author = AtomSerializer.new.author(account)
  120. author.nodes.each { |node| expect(node.name).not_to eq 'poco:note' }
  121. end
  122. it 'does not append summary element' do
  123. account = Fabricate(:account, note: '')
  124. author = AtomSerializer.new.author(account)
  125. author.nodes.each { |node| expect(node.name).not_to eq 'summary' }
  126. end
  127. end
  128. it 'returns author element' do
  129. account = Fabricate(:account)
  130. author = AtomSerializer.new.author(account)
  131. expect(author.name).to eq 'author'
  132. end
  133. it 'appends activity:object-type element with person type' do
  134. account = Fabricate(:account, domain: nil, username: 'username')
  135. author = AtomSerializer.new.author(account)
  136. object_type = author.nodes.find { |node| node.name == 'activity:object-type' }
  137. expect(object_type.text).to eq TagManager::TYPES[:person]
  138. end
  139. it 'appends email element with username and domain for local account' do
  140. account = Fabricate(:account, username: 'username')
  141. author = AtomSerializer.new.author(account)
  142. expect(author.email.text).to eq 'username@cb6e6126.ngrok.io'
  143. end
  144. it 'appends email element with username and domain for remote user' do
  145. account = Fabricate(:account, domain: 'domain', username: 'username')
  146. author = AtomSerializer.new.author(account)
  147. expect(author.email.text).to eq 'username@domain'
  148. end
  149. it 'appends link element for an alternative' do
  150. account = Fabricate(:account, domain: nil, username: 'username')
  151. author = AtomSerializer.new.author(account)
  152. link = author.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' }
  153. expect(link[:type]).to eq 'text/html'
  154. expect(link[:rel]).to eq 'alternate'
  155. expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/@username'
  156. end
  157. it 'has link element for avatar if present' do
  158. account = Fabricate(:account, avatar: attachment_fixture('avatar.gif'))
  159. author = AtomSerializer.new.author(account)
  160. link = author.nodes.find { |node| node.name == 'link' && node[:rel] == 'avatar' }
  161. expect(link[:type]).to eq 'image/gif'
  162. expect(link['media:width']).to eq '120'
  163. expect(link['media:height']).to eq '120'
  164. expect(link[:href]).to match /^https:\/\/cb6e6126.ngrok.io\/system\/accounts\/avatars\/.+\/original\/avatar.gif/
  165. end
  166. it 'does not have link element for avatar if not present' do
  167. account = Fabricate(:account, avatar: nil)
  168. author = AtomSerializer.new.author(account)
  169. author.nodes.each do |node|
  170. expect(node[:rel]).not_to eq 'avatar' if node.name == 'link'
  171. end
  172. end
  173. it 'appends link element for header if present' do
  174. account = Fabricate(:account, header: attachment_fixture('avatar.gif'))
  175. author = AtomSerializer.new.author(account)
  176. link = author.nodes.find { |node| node.name == 'link' && node[:rel] == 'header' }
  177. expect(link[:type]).to eq 'image/gif'
  178. expect(link['media:width']).to eq '700'
  179. expect(link['media:height']).to eq '335'
  180. expect(link[:href]).to match /^https:\/\/cb6e6126.ngrok.io\/system\/accounts\/headers\/.+\/original\/avatar.gif/
  181. end
  182. it 'does not append link element for header if not present' do
  183. account = Fabricate(:account, header: nil)
  184. author = AtomSerializer.new.author(account)
  185. author.nodes.each do |node|
  186. expect(node[:rel]).not_to eq 'header' if node.name == 'link'
  187. end
  188. end
  189. it 'appends poco:displayName element with display name if present' do
  190. account = Fabricate(:account, display_name: 'display name')
  191. author = AtomSerializer.new.author(account)
  192. display_name = author.nodes.find { |node| node.name == 'poco:displayName' }
  193. expect(display_name.text).to eq 'display name'
  194. end
  195. it 'does not append poco:displayName element with display name if not present' do
  196. account = Fabricate(:account, display_name: '')
  197. author = AtomSerializer.new.author(account)
  198. author.nodes.each { |node| expect(node.name).not_to eq 'poco:displayName' }
  199. end
  200. it "appends mastodon:scope element with 'private' if locked" do
  201. account = Fabricate(:account, locked: true)
  202. author = AtomSerializer.new.author(account)
  203. scope = author.nodes.find { |node| node.name == 'mastodon:scope' }
  204. expect(scope.text).to eq 'private'
  205. end
  206. it "appends mastodon:scope element with 'public' if unlocked" do
  207. account = Fabricate(:account, locked: false)
  208. author = AtomSerializer.new.author(account)
  209. scope = author.nodes.find { |node| node.name == 'mastodon:scope' }
  210. expect(scope.text).to eq 'public'
  211. end
  212. it 'includes URI' do
  213. account = Fabricate(:account, domain: nil, username: 'username')
  214. author = AtomSerializer.new.author(account)
  215. expect(author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  216. expect(author.uri.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  217. end
  218. it 'includes username' do
  219. account = Fabricate(:account, username: 'username')
  220. author = AtomSerializer.new.author(account)
  221. name = author.nodes.find { |node| node.name == 'name' }
  222. username = author.nodes.find { |node| node.name == 'poco:preferredUsername' }
  223. expect(name.text).to eq 'username'
  224. expect(username.text).to eq 'username'
  225. end
  226. end
  227. describe '#entry' do
  228. shared_examples 'not root' do
  229. include_examples 'no namespaces' do
  230. def serialize
  231. subject
  232. end
  233. end
  234. it 'does not append author element' do
  235. subject.nodes.each { |node| expect(node.name).not_to eq 'author' }
  236. end
  237. end
  238. context 'it is root' do
  239. include_examples 'namespaces' do
  240. def serialize
  241. stream_entry = Fabricate(:stream_entry)
  242. AtomSerializer.new.entry(stream_entry, true)
  243. end
  244. end
  245. it 'appends author element' do
  246. account = Fabricate(:account, username: 'username')
  247. status = Fabricate(:status, account: account)
  248. entry = AtomSerializer.new.entry(status.stream_entry, true)
  249. expect(entry.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  250. end
  251. end
  252. context 'if status is present' do
  253. include_examples 'status attributes' do
  254. def serialize(status)
  255. AtomSerializer.new.entry(status.stream_entry, true)
  256. end
  257. end
  258. it 'appends link element for the public collection if status is publicly visible' do
  259. status = Fabricate(:status, visibility: :public)
  260. entry = AtomSerializer.new.entry(status.stream_entry)
  261. mentioned_person = entry.nodes.find do |node|
  262. node.name == 'link' &&
  263. node[:rel] == 'mentioned' &&
  264. node['ostatus:object-type'] == TagManager::TYPES[:collection]
  265. end
  266. expect(mentioned_person[:href]).to eq TagManager::COLLECTIONS[:public]
  267. end
  268. it 'does not append link element for the public collection if status is not publicly visible' do
  269. status = Fabricate(:status, visibility: :private)
  270. entry = AtomSerializer.new.entry(status.stream_entry)
  271. entry.nodes.each do |node|
  272. if node.name == 'link' &&
  273. node[:rel] == 'mentioned' &&
  274. node['ostatus:object-type'] == TagManager::TYPES[:collection]
  275. expect(mentioned_collection[:href]).not_to eq TagManager::COLLECTIONS[:public]
  276. end
  277. end
  278. end
  279. it 'appends category elements for tags' do
  280. tag = Fabricate(:tag, name: 'tag')
  281. status = Fabricate(:status, tags: [ tag ])
  282. entry = AtomSerializer.new.entry(status.stream_entry)
  283. expect(entry.category[:term]).to eq 'tag'
  284. end
  285. it 'appends category element for NSFW if status is sensitive' do
  286. status = Fabricate(:status, sensitive: true)
  287. entry = AtomSerializer.new.entry(status.stream_entry)
  288. expect(entry.category[:term]).to eq 'nsfw'
  289. end
  290. it 'appends link elements for media attachments' do
  291. file = attachment_fixture('attachment.jpg')
  292. media_attachment = Fabricate(:media_attachment, file: file)
  293. status = Fabricate(:status, media_attachments: [ media_attachment ])
  294. entry = AtomSerializer.new.entry(status.stream_entry)
  295. enclosure = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'enclosure' }
  296. expect(enclosure[:type]).to eq 'image/jpeg'
  297. expect(enclosure[:length]).to eq '57822'
  298. expect(enclosure[:href]).to match /^https:\/\/cb6e6126.ngrok.io\/system\/media_attachments\/files\/.+\/original\/attachment.jpg$/
  299. end
  300. it 'appends mastodon:scope element with visibility' do
  301. status = Fabricate(:status, visibility: :public)
  302. entry = AtomSerializer.new.entry(status.stream_entry)
  303. scope = entry.nodes.find { |node| node.name == 'mastodon:scope' }
  304. expect(scope.text).to eq 'public'
  305. end
  306. it 'returns element whose rendered view triggers creation when processed' do
  307. remote_account = Account.create!(username: 'username')
  308. remote_status = Fabricate(:status, account: remote_account)
  309. remote_status.stream_entry.update!(created_at: '2000-01-01T00:00:00Z')
  310. entry = AtomSerializer.new.entry(remote_status.stream_entry, true)
  311. xml = AtomSerializer.render(entry).gsub('cb6e6126.ngrok.io', 'remote')
  312. remote_status.destroy!
  313. remote_account.destroy!
  314. account = Account.create!(
  315. domain: 'remote',
  316. username: 'username',
  317. last_webfingered_at: Time.now.utc,
  318. )
  319. ProcessFeedService.new.call(xml, account)
  320. expect(Status.find_by(uri: "tag:remote,2000-01-01:objectId=#{remote_status.id}:objectType=Status")).to be_instance_of Status
  321. end
  322. end
  323. context 'if status is not present' do
  324. it 'appends content element saying status is deleted' do
  325. status = Fabricate(:status)
  326. status.destroy!
  327. entry = AtomSerializer.new.entry(status.stream_entry)
  328. expect(entry.content.text).to eq 'Deleted status'
  329. end
  330. it 'appends title element saying the status is deleted' do
  331. account = Fabricate(:account, username: 'username')
  332. status = Fabricate(:status, account: account)
  333. status.destroy!
  334. entry = AtomSerializer.new.entry(status.stream_entry)
  335. expect(entry.title.text).to eq 'username deleted status'
  336. end
  337. end
  338. context 'it is not root' do
  339. let(:stream_entry) { Fabricate(:stream_entry) }
  340. subject { AtomSerializer.new.entry(stream_entry, false) }
  341. include_examples 'not root'
  342. end
  343. context 'without root parameter' do
  344. let(:stream_entry) { Fabricate(:stream_entry) }
  345. subject { AtomSerializer.new.entry(stream_entry) }
  346. include_examples 'not root'
  347. end
  348. it 'returns entry element' do
  349. stream_entry = Fabricate(:stream_entry)
  350. entry = AtomSerializer.new.entry(stream_entry)
  351. expect(entry.name).to eq 'entry'
  352. end
  353. it 'appends id element with unique tag' do
  354. status = Fabricate(:status, reblog_of_id: nil)
  355. status.stream_entry.update!(created_at: '2000-01-01T00:00:00Z')
  356. entry = AtomSerializer.new.entry(status.stream_entry)
  357. expect(entry.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status"
  358. end
  359. it 'appends published element with created date' do
  360. stream_entry = Fabricate(:stream_entry, created_at: '2000-01-01T00:00:00Z')
  361. entry = AtomSerializer.new.entry(stream_entry)
  362. expect(entry.published.text).to eq '2000-01-01T00:00:00Z'
  363. end
  364. it 'appends updated element with updated date' do
  365. stream_entry = Fabricate(:stream_entry, updated_at: '2000-01-01T00:00:00Z')
  366. entry = AtomSerializer.new.entry(stream_entry)
  367. expect(entry.updated.text).to eq '2000-01-01T00:00:00Z'
  368. end
  369. it 'appends title element with status title' do
  370. account = Fabricate(:account, username: 'username')
  371. status = Fabricate(:status, account: account, reblog_of_id: nil)
  372. entry = AtomSerializer.new.entry(status.stream_entry)
  373. expect(entry.title.text).to eq 'New status by username'
  374. end
  375. it 'appends activity:object-type element with object type' do
  376. status = Fabricate(:status)
  377. entry = AtomSerializer.new.entry(status.stream_entry)
  378. object_type = entry.nodes.find { |node| node.name == 'activity:object-type' }
  379. expect(object_type.text).to eq TagManager::TYPES[:note]
  380. end
  381. it 'appends activity:verb element with object type' do
  382. status = Fabricate(:status)
  383. entry = AtomSerializer.new.entry(status.stream_entry)
  384. object_type = entry.nodes.find { |node| node.name == 'activity:verb' }
  385. expect(object_type.text).to eq TagManager::VERBS[:post]
  386. end
  387. it 'appends activity:object element with target if present' do
  388. reblogged = Fabricate(:status, created_at: '2000-01-01T00:00:00Z')
  389. reblog = Fabricate(:status, reblog: reblogged)
  390. entry = AtomSerializer.new.entry(reblog.stream_entry)
  391. object = entry.nodes.find { |node| node.name == 'activity:object' }
  392. expect(object.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{reblogged.id}:objectType=Status"
  393. end
  394. it 'does not append activity:object element if target is not present' do
  395. status = Fabricate(:status, reblog_of_id: nil)
  396. entry = AtomSerializer.new.entry(status.stream_entry)
  397. entry.nodes.each { |node| expect(node.name).not_to eq 'activity:object' }
  398. end
  399. it 'appends link element for an alternative' do
  400. account = Fabricate(:account, username: 'username')
  401. status = Fabricate(:status, account: account)
  402. entry = AtomSerializer.new.entry(status.stream_entry)
  403. link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' }
  404. expect(link[:type]).to eq 'text/html'
  405. expect(link[:href]).to eq "https://cb6e6126.ngrok.io/users/username/updates/#{status.stream_entry.id}"
  406. end
  407. it 'appends link element for itself' do
  408. account = Fabricate(:account, username: 'username')
  409. status = Fabricate(:status, account: account)
  410. entry = AtomSerializer.new.entry(status.stream_entry)
  411. link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'self' }
  412. expect(link[:type]).to eq 'application/atom+xml'
  413. expect(link[:href]).to eq "https://cb6e6126.ngrok.io/users/username/updates/#{status.stream_entry.id}.atom"
  414. end
  415. it 'appends thr:in-reply-to element if threaded' do
  416. in_reply_to_status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z', reblog_of_id: nil)
  417. reply_status = Fabricate(:status, in_reply_to_id: in_reply_to_status.id)
  418. entry = AtomSerializer.new.entry(reply_status.stream_entry)
  419. in_reply_to = entry.nodes.find { |node| node.name == 'thr:in-reply-to' }
  420. expect(in_reply_to[:ref]).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{in_reply_to_status.id}:objectType=Status"
  421. end
  422. it 'does not append thr:in-reply-to element if not threaded' do
  423. status = Fabricate(:status)
  424. entry = AtomSerializer.new.entry(status.stream_entry)
  425. entry.nodes.each { |node| expect(node.name).not_to eq 'thr:in-reply-to' }
  426. end
  427. it 'appends ostatus:conversation if conversation id is present' do
  428. status = Fabricate(:status)
  429. status.conversation.update!(created_at: '2000-01-01T00:00:00Z')
  430. entry = AtomSerializer.new.entry(status.stream_entry)
  431. conversation = entry.nodes.find { |node| node.name == 'ostatus:conversation' }
  432. expect(conversation[:ref]).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.conversation_id}:objectType=Conversation"
  433. end
  434. it 'does not append ostatus:conversation if conversation id is not present' do
  435. status = Fabricate.build(:status, conversation_id: nil)
  436. status.save!(validate: false)
  437. entry = AtomSerializer.new.entry(status.stream_entry)
  438. entry.nodes.each { |node| expect(node.name).not_to eq 'ostatus:conversation' }
  439. end
  440. end
  441. describe '#feed' do
  442. include_examples 'namespaces' do
  443. def serialize
  444. account = Fabricate(:account)
  445. AtomSerializer.new.feed(account, [])
  446. end
  447. end
  448. it 'returns feed element' do
  449. account = Fabricate(:account)
  450. feed = AtomSerializer.new.feed(account, [])
  451. expect(feed.name).to eq 'feed'
  452. end
  453. it 'appends id element with account Atom URL' do
  454. account = Fabricate(:account, username: 'username')
  455. feed = AtomSerializer.new.feed(account, [])
  456. expect(feed.id.text).to eq 'https://cb6e6126.ngrok.io/users/username.atom'
  457. end
  458. it 'appends title element with account display name if present' do
  459. account = Fabricate(:account, display_name: 'display name')
  460. feed = AtomSerializer.new.feed(account, [])
  461. expect(feed.title.text).to eq 'display name'
  462. end
  463. it 'does not append title element with account username if account display name is not present' do
  464. account = Fabricate(:account, display_name: '', username: 'username')
  465. feed = AtomSerializer.new.feed(account, [])
  466. expect(feed.title.text).to eq 'username'
  467. end
  468. it 'appends subtitle element with account note' do
  469. account = Fabricate(:account, note: 'note')
  470. feed = AtomSerializer.new.feed(account, [])
  471. expect(feed.subtitle.text).to eq 'note'
  472. end
  473. it 'appends updated element with date account got updated' do
  474. account = Fabricate(:account, updated_at: '2000-01-01T00:00:00Z')
  475. feed = AtomSerializer.new.feed(account, [])
  476. expect(feed.updated.text).to eq '2000-01-01T00:00:00Z'
  477. end
  478. it 'appends logo element with full asset URL for original account avatar' do
  479. account = Fabricate(:account, avatar: attachment_fixture('avatar.gif'))
  480. feed = AtomSerializer.new.feed(account, [])
  481. expect(feed.logo.text).to match /^https:\/\/cb6e6126.ngrok.io\/system\/accounts\/avatars\/.+\/original\/avatar.gif/
  482. end
  483. it 'appends author element' do
  484. account = Fabricate(:account, username: 'username')
  485. feed = AtomSerializer.new.feed(account, [])
  486. expect(feed.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  487. end
  488. it 'appends link element for an alternative' do
  489. account = Fabricate(:account, username: 'username')
  490. feed = AtomSerializer.new.feed(account, [])
  491. link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' }
  492. expect(link[:type]).to eq 'text/html'
  493. expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/@username'
  494. end
  495. it 'appends link element for itself' do
  496. account = Fabricate(:account, username: 'username')
  497. feed = AtomSerializer.new.feed(account, [])
  498. link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'self' }
  499. expect(link[:type]).to eq 'application/atom+xml'
  500. expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/users/username.atom'
  501. end
  502. it 'appends link element for the next if it has 20 stream entries' do
  503. account = Fabricate(:account, username: 'username')
  504. stream_entry = Fabricate(:stream_entry)
  505. feed = AtomSerializer.new.feed(account, Array.new(20, stream_entry))
  506. link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'next' }
  507. expect(link[:type]).to eq 'application/atom+xml'
  508. expect(link[:href]).to eq "https://cb6e6126.ngrok.io/users/username.atom?max_id=#{stream_entry.id}"
  509. end
  510. it 'does not append link element for the next if it does not have 20 stream entries' do
  511. account = Fabricate(:account, username: 'username')
  512. feed = AtomSerializer.new.feed(account, [])
  513. feed.nodes.each do |node|
  514. expect(node[:rel]).not_to eq 'next' if node.name == 'link'
  515. end
  516. end
  517. it 'appends link element for hub' do
  518. account = Fabricate(:account, username: 'username')
  519. feed = AtomSerializer.new.feed(account, [])
  520. link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'hub' }
  521. expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/api/push'
  522. end
  523. it 'appends link element for Salmon' do
  524. account = Fabricate(:account, username: 'username')
  525. feed = AtomSerializer.new.feed(account, [])
  526. link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'salmon' }
  527. expect(link[:href]).to start_with 'https://cb6e6126.ngrok.io/api/salmon/'
  528. end
  529. it 'appends stream entries' do
  530. account = Fabricate(:account, username: 'username')
  531. status = Fabricate(:status, account: account)
  532. feed = AtomSerializer.new.feed(account, [status.stream_entry])
  533. expect(feed.entry.title.text).to eq 'New status by username'
  534. end
  535. end
  536. describe '#block_salmon' do
  537. include_examples 'namespaces' do
  538. def serialize
  539. block = Fabricate(:block)
  540. AtomSerializer.new.block_salmon(block)
  541. end
  542. end
  543. it 'returns entry element' do
  544. block = Fabricate(:block)
  545. block_salmon = AtomSerializer.new.block_salmon(block)
  546. expect(block_salmon.name).to eq 'entry'
  547. end
  548. it 'appends id element with unique tag' do
  549. block = Fabricate(:block)
  550. time_before = Time.now
  551. block_salmon = AtomSerializer.new.block_salmon(block)
  552. time_after = Time.now
  553. expect(block_salmon.id.text).to(
  554. eq(TagManager.instance.unique_tag(time_before.utc, block.id, 'Block'))
  555. .or(eq(TagManager.instance.unique_tag(time_after.utc, block.id, 'Block')))
  556. )
  557. end
  558. it 'appends title element with description' do
  559. account = Fabricate(:account, domain: nil, username: 'account')
  560. target_account = Fabricate(:account, domain: 'remote', username: 'target_account')
  561. block = Fabricate(:block, account: account, target_account: target_account)
  562. block_salmon = AtomSerializer.new.block_salmon(block)
  563. expect(block_salmon.title.text).to eq 'account no longer wishes to interact with target_account@remote'
  564. end
  565. it 'appends author element with account' do
  566. account = Fabricate(:account, domain: nil, username: 'account')
  567. block = Fabricate(:block, account: account)
  568. block_salmon = AtomSerializer.new.block_salmon(block)
  569. expect(block_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/account'
  570. end
  571. it 'appends activity:object-type element with activity type' do
  572. block = Fabricate(:block)
  573. block_salmon = AtomSerializer.new.block_salmon(block)
  574. object_type = block_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  575. expect(object_type.text).to eq TagManager::TYPES[:activity]
  576. end
  577. it 'appends activity:verb element with block' do
  578. block = Fabricate(:block)
  579. block_salmon = AtomSerializer.new.block_salmon(block)
  580. verb = block_salmon.nodes.find { |node| node.name == 'activity:verb' }
  581. expect(verb.text).to eq TagManager::VERBS[:block]
  582. end
  583. it 'appends activity:object element with target account' do
  584. target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id')
  585. block = Fabricate(:block, target_account: target_account)
  586. block_salmon = AtomSerializer.new.block_salmon(block)
  587. object = block_salmon.nodes.find { |node| node.name == 'activity:object' }
  588. expect(object.id.text).to eq 'https://domain/id'
  589. end
  590. it 'returns element whose rendered view triggers block when processed' do
  591. block = Fabricate(:block)
  592. block_salmon = AtomSerializer.new.block_salmon(block)
  593. xml = AtomSerializer.render(block_salmon)
  594. envelope = OStatus2::Salmon.new.pack(xml, block.account.keypair)
  595. block.destroy!
  596. ProcessInteractionService.new.call(envelope, block.target_account)
  597. expect(block.account.blocking?(block.target_account)).to be true
  598. end
  599. end
  600. describe '#unblock_salmon' do
  601. include_examples 'namespaces' do
  602. def serialize
  603. block = Fabricate(:block)
  604. AtomSerializer.new.unblock_salmon(block)
  605. end
  606. end
  607. it 'returns entry element' do
  608. block = Fabricate(:block)
  609. unblock_salmon = AtomSerializer.new.unblock_salmon(block)
  610. expect(unblock_salmon.name).to eq 'entry'
  611. end
  612. it 'appends id element with unique tag' do
  613. block = Fabricate(:block)
  614. time_before = Time.now
  615. unblock_salmon = AtomSerializer.new.unblock_salmon(block)
  616. time_after = Time.now
  617. expect(unblock_salmon.id.text).to(
  618. eq(TagManager.instance.unique_tag(time_before.utc, block.id, 'Block'))
  619. .or(eq(TagManager.instance.unique_tag(time_after.utc, block.id, 'Block')))
  620. )
  621. end
  622. it 'appends title element with description' do
  623. account = Fabricate(:account, domain: nil, username: 'account')
  624. target_account = Fabricate(:account, domain: 'remote', username: 'target_account')
  625. block = Fabricate(:block, account: account, target_account: target_account)
  626. unblock_salmon = AtomSerializer.new.unblock_salmon(block)
  627. expect(unblock_salmon.title.text).to eq 'account no longer blocks target_account@remote'
  628. end
  629. it 'appends author element with account' do
  630. account = Fabricate(:account, domain: nil, username: 'account')
  631. block = Fabricate(:block, account: account)
  632. unblock_salmon = AtomSerializer.new.unblock_salmon(block)
  633. expect(unblock_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/account'
  634. end
  635. it 'appends activity:object-type element with activity type' do
  636. block = Fabricate(:block)
  637. unblock_salmon = AtomSerializer.new.unblock_salmon(block)
  638. object_type = unblock_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  639. expect(object_type.text).to eq TagManager::TYPES[:activity]
  640. end
  641. it 'appends activity:verb element with block' do
  642. block = Fabricate(:block)
  643. unblock_salmon = AtomSerializer.new.unblock_salmon(block)
  644. verb = unblock_salmon.nodes.find { |node| node.name == 'activity:verb' }
  645. expect(verb.text).to eq TagManager::VERBS[:unblock]
  646. end
  647. it 'appends activity:object element with target account' do
  648. target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id')
  649. block = Fabricate(:block, target_account: target_account)
  650. unblock_salmon = AtomSerializer.new.unblock_salmon(block)
  651. object = unblock_salmon.nodes.find { |node| node.name == 'activity:object' }
  652. expect(object.id.text).to eq 'https://domain/id'
  653. end
  654. it 'returns element whose rendered view triggers block when processed' do
  655. block = Fabricate(:block)
  656. unblock_salmon = AtomSerializer.new.unblock_salmon(block)
  657. xml = AtomSerializer.render(unblock_salmon)
  658. envelope = OStatus2::Salmon.new.pack(xml, block.account.keypair)
  659. ProcessInteractionService.new.call(envelope, block.target_account)
  660. expect{ block.reload }.to raise_error ActiveRecord::RecordNotFound
  661. end
  662. end
  663. describe '#favourite_salmon' do
  664. include_examples 'namespaces' do
  665. def serialize
  666. favourite = Fabricate(:favourite)
  667. AtomSerializer.new.favourite_salmon(favourite)
  668. end
  669. end
  670. it 'returns entry element' do
  671. favourite = Fabricate(:favourite)
  672. favourite_salmon = AtomSerializer.new.favourite_salmon(favourite)
  673. expect(favourite_salmon.name).to eq 'entry'
  674. end
  675. it 'appends id element with unique tag' do
  676. favourite = Fabricate(:favourite, created_at: '2000-01-01T00:00:00Z')
  677. favourite_salmon = AtomSerializer.new.favourite_salmon(favourite)
  678. expect(favourite_salmon.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{favourite.id}:objectType=Favourite"
  679. end
  680. it 'appends author element with account' do
  681. account = Fabricate(:account, domain: nil, username: 'username')
  682. favourite = Fabricate(:favourite, account: account)
  683. favourite_salmon = AtomSerializer.new.favourite_salmon(favourite)
  684. expect(favourite_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  685. end
  686. it 'appends activity:object-type element with activity type' do
  687. favourite = Fabricate(:favourite)
  688. favourite_salmon = AtomSerializer.new.favourite_salmon(favourite)
  689. object_type = favourite_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  690. expect(object_type.text).to eq 'http://activitystrea.ms/schema/1.0/activity'
  691. end
  692. it 'appends activity:verb element with favorite' do
  693. favourite = Fabricate(:favourite)
  694. favourite_salmon = AtomSerializer.new.favourite_salmon(favourite)
  695. verb = favourite_salmon.nodes.find { |node| node.name == 'activity:verb' }
  696. expect(verb.text).to eq TagManager::VERBS[:favorite]
  697. end
  698. it 'appends activity:object element with status' do
  699. status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z')
  700. favourite = Fabricate(:favourite, status: status)
  701. favourite_salmon = AtomSerializer.new.favourite_salmon(favourite)
  702. object = favourite_salmon.nodes.find { |node| node.name == 'activity:object' }
  703. expect(object.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status"
  704. end
  705. it 'appends thr:in-reply-to element for status' do
  706. status_account = Fabricate(:account, username: 'username')
  707. status = Fabricate(:status, account: status_account, created_at: '2000-01-01T00:00:00Z')
  708. favourite = Fabricate(:favourite, status: status)
  709. favourite_salmon = AtomSerializer.new.favourite_salmon(favourite)
  710. in_reply_to = favourite_salmon.nodes.find { |node| node.name == 'thr:in-reply-to' }
  711. expect(in_reply_to.ref).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status"
  712. expect(in_reply_to.href).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}"
  713. end
  714. it 'includes description' do
  715. account = Fabricate(:account, domain: nil, username: 'account')
  716. status_account = Fabricate(:account, domain: 'remote', username: 'status_account')
  717. status = Fabricate(:status, account: status_account)
  718. favourite = Fabricate(:favourite, account: account, status: status)
  719. favourite_salmon = AtomSerializer.new.favourite_salmon(favourite)
  720. expect(favourite_salmon.title.text).to eq 'account favourited a status by status_account@remote'
  721. expect(favourite_salmon.content.text).to eq 'account favourited a status by status_account@remote'
  722. end
  723. it 'returns element whose rendered view triggers favourite when processed' do
  724. favourite = Fabricate(:favourite)
  725. favourite_salmon = AtomSerializer.new.favourite_salmon(favourite)
  726. xml = AtomSerializer.render(favourite_salmon)
  727. envelope = OStatus2::Salmon.new.pack(xml, favourite.account.keypair)
  728. favourite.destroy!
  729. ProcessInteractionService.new.call(envelope, favourite.status.account)
  730. expect(favourite.account.favourited?(favourite.status)).to be true
  731. end
  732. end
  733. describe '#unfavourite_salmon' do
  734. include_examples 'namespaces' do
  735. def serialize
  736. favourite = Fabricate(:favourite)
  737. AtomSerializer.new.favourite_salmon(favourite)
  738. end
  739. end
  740. it 'returns entry element' do
  741. favourite = Fabricate(:favourite)
  742. unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite)
  743. expect(unfavourite_salmon.name).to eq 'entry'
  744. end
  745. it 'appends id element with unique tag' do
  746. favourite = Fabricate(:favourite)
  747. time_before = Time.now
  748. unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite)
  749. time_after = Time.now
  750. expect(unfavourite_salmon.id.text).to(
  751. eq(TagManager.instance.unique_tag(time_before.utc, favourite.id, 'Favourite'))
  752. .or(eq(TagManager.instance.unique_tag(time_after.utc, favourite.id, 'Favourite')))
  753. )
  754. end
  755. it 'appends author element with account' do
  756. account = Fabricate(:account, domain: nil, username: 'username')
  757. favourite = Fabricate(:favourite, account: account)
  758. unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite)
  759. expect(unfavourite_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  760. end
  761. it 'appends activity:object-type element with activity type' do
  762. favourite = Fabricate(:favourite)
  763. unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite)
  764. object_type = unfavourite_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  765. expect(object_type.text).to eq 'http://activitystrea.ms/schema/1.0/activity'
  766. end
  767. it 'appends activity:verb element with favorite' do
  768. favourite = Fabricate(:favourite)
  769. unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite)
  770. verb = unfavourite_salmon.nodes.find { |node| node.name == 'activity:verb' }
  771. expect(verb.text).to eq TagManager::VERBS[:unfavorite]
  772. end
  773. it 'appends activity:object element with status' do
  774. status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z')
  775. favourite = Fabricate(:favourite, status: status)
  776. unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite)
  777. object = unfavourite_salmon.nodes.find { |node| node.name == 'activity:object' }
  778. expect(object.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status"
  779. end
  780. it 'appends thr:in-reply-to element for status' do
  781. status_account = Fabricate(:account, username: 'username')
  782. status = Fabricate(:status, account: status_account, created_at: '2000-01-01T00:00:00Z')
  783. favourite = Fabricate(:favourite, status: status)
  784. unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite)
  785. in_reply_to = unfavourite_salmon.nodes.find { |node| node.name == 'thr:in-reply-to' }
  786. expect(in_reply_to.ref).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status"
  787. expect(in_reply_to.href).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}"
  788. end
  789. it 'includes description' do
  790. account = Fabricate(:account, domain: nil, username: 'account')
  791. status_account = Fabricate(:account, domain: 'remote', username: 'status_account')
  792. status = Fabricate(:status, account: status_account)
  793. favourite = Fabricate(:favourite, account: account, status: status)
  794. unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite)
  795. expect(unfavourite_salmon.title.text).to eq 'account no longer favourites a status by status_account@remote'
  796. expect(unfavourite_salmon.content.text).to eq 'account no longer favourites a status by status_account@remote'
  797. end
  798. it 'returns element whose rendered view triggers unfavourite when processed' do
  799. favourite = Fabricate(:favourite)
  800. unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite)
  801. xml = AtomSerializer.render(unfavourite_salmon)
  802. envelope = OStatus2::Salmon.new.pack(xml, favourite.account.keypair)
  803. ProcessInteractionService.new.call(envelope, favourite.status.account)
  804. expect { favourite.reload }.to raise_error ActiveRecord::RecordNotFound
  805. end
  806. end
  807. describe '#follow_salmon' do
  808. include_examples 'namespaces' do
  809. def serialize
  810. follow = Fabricate(:follow)
  811. AtomSerializer.new.follow_salmon(follow)
  812. end
  813. end
  814. it 'returns entry element' do
  815. follow = Fabricate(:follow)
  816. follow_salmon = AtomSerializer.new.follow_salmon(follow)
  817. expect(follow_salmon.name).to eq 'entry'
  818. end
  819. it 'appends id element with unique tag' do
  820. follow = Fabricate(:follow, created_at: '2000-01-01T00:00:00Z')
  821. follow_salmon = AtomSerializer.new.follow_salmon(follow)
  822. expect(follow_salmon.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{follow.id}:objectType=Follow"
  823. end
  824. it 'appends author element with account' do
  825. account = Fabricate(:account, domain: nil, username: 'username')
  826. follow = Fabricate(:follow, account: account)
  827. follow_salmon = AtomSerializer.new.follow_salmon(follow)
  828. expect(follow_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  829. end
  830. it 'appends activity:object-type element with activity type' do
  831. follow = Fabricate(:follow)
  832. follow_salmon = AtomSerializer.new.follow_salmon(follow)
  833. object_type = follow_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  834. expect(object_type.text).to eq TagManager::TYPES[:activity]
  835. end
  836. it 'appends activity:verb element with follow' do
  837. follow = Fabricate(:follow)
  838. follow_salmon = AtomSerializer.new.follow_salmon(follow)
  839. verb = follow_salmon.nodes.find { |node| node.name == 'activity:verb' }
  840. expect(verb.text).to eq TagManager::VERBS[:follow]
  841. end
  842. it 'appends activity:object element with target account' do
  843. target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id')
  844. follow = Fabricate(:follow, target_account: target_account)
  845. follow_salmon = AtomSerializer.new.follow_salmon(follow)
  846. object = follow_salmon.nodes.find { |node| node.name == 'activity:object' }
  847. expect(object.id.text).to eq 'https://domain/id'
  848. end
  849. it 'includes description' do
  850. account = Fabricate(:account, domain: nil, username: 'account')
  851. target_account = Fabricate(:account, domain: 'remote', username: 'target_account')
  852. follow = Fabricate(:follow, account: account, target_account: target_account)
  853. follow_salmon = AtomSerializer.new.follow_salmon(follow)
  854. expect(follow_salmon.title.text).to eq 'account started following target_account@remote'
  855. expect(follow_salmon.content.text).to eq 'account started following target_account@remote'
  856. end
  857. it 'returns element whose rendered view triggers follow when processed' do
  858. follow = Fabricate(:follow)
  859. follow_salmon = AtomSerializer.new.follow_salmon(follow)
  860. xml = AtomSerializer.render(follow_salmon)
  861. follow.destroy!
  862. envelope = OStatus2::Salmon.new.pack(xml, follow.account.keypair)
  863. ProcessInteractionService.new.call(envelope, follow.target_account)
  864. expect(follow.account.following?(follow.target_account)).to be true
  865. end
  866. end
  867. describe '#unfollow_salmon' do
  868. include_examples 'namespaces' do
  869. def serialize
  870. follow = Fabricate(:follow)
  871. follow.destroy!
  872. AtomSerializer.new.unfollow_salmon(follow)
  873. end
  874. end
  875. it 'returns entry element' do
  876. follow = Fabricate(:follow)
  877. follow.destroy!
  878. unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow)
  879. expect(unfollow_salmon.name).to eq 'entry'
  880. end
  881. it 'appends id element with unique tag' do
  882. follow = Fabricate(:follow)
  883. follow.destroy!
  884. time_before = Time.now
  885. unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow)
  886. time_after = Time.now
  887. expect(unfollow_salmon.id.text).to(
  888. eq(TagManager.instance.unique_tag(time_before.utc, follow.id, 'Follow'))
  889. .or(eq(TagManager.instance.unique_tag(time_after.utc, follow.id, 'Follow')))
  890. )
  891. end
  892. it 'appends title element with description' do
  893. account = Fabricate(:account, domain: nil, username: 'account')
  894. target_account = Fabricate(:account, domain: 'remote', username: 'target_account')
  895. follow = Fabricate(:follow, account: account, target_account: target_account)
  896. follow.destroy!
  897. unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow)
  898. expect(unfollow_salmon.title.text).to eq 'account is no longer following target_account@remote'
  899. end
  900. it 'appends content element with description' do
  901. account = Fabricate(:account, domain: nil, username: 'account')
  902. target_account = Fabricate(:account, domain: 'remote', username: 'target_account')
  903. follow = Fabricate(:follow, account: account, target_account: target_account)
  904. follow.destroy!
  905. unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow)
  906. expect(unfollow_salmon.content.text).to eq 'account is no longer following target_account@remote'
  907. end
  908. it 'appends author element with account' do
  909. account = Fabricate(:account, domain: nil, username: 'username')
  910. follow = Fabricate(:follow, account: account)
  911. follow.destroy!
  912. unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow)
  913. expect(unfollow_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  914. end
  915. it 'appends activity:object-type element with activity type' do
  916. follow = Fabricate(:follow)
  917. follow.destroy!
  918. unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow)
  919. object_type = unfollow_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  920. expect(object_type.text).to eq TagManager::TYPES[:activity]
  921. end
  922. it 'appends activity:verb element with follow' do
  923. follow = Fabricate(:follow)
  924. follow.destroy!
  925. unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow)
  926. verb = unfollow_salmon.nodes.find { |node| node.name == 'activity:verb' }
  927. expect(verb.text).to eq TagManager::VERBS[:unfollow]
  928. end
  929. it 'appends activity:object element with target account' do
  930. target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id')
  931. follow = Fabricate(:follow, target_account: target_account)
  932. follow.destroy!
  933. unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow)
  934. object = unfollow_salmon.nodes.find { |node| node.name == 'activity:object' }
  935. expect(object.id.text).to eq 'https://domain/id'
  936. end
  937. it 'returns element whose rendered view triggers unfollow when processed' do
  938. follow = Fabricate(:follow)
  939. follow.destroy!
  940. unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow)
  941. xml = AtomSerializer.render(unfollow_salmon)
  942. follow.account.follow!(follow.target_account)
  943. envelope = OStatus2::Salmon.new.pack(xml, follow.account.keypair)
  944. ProcessInteractionService.new.call(envelope, follow.target_account)
  945. expect(follow.account.following?(follow.target_account)).to be false
  946. end
  947. end
  948. describe '#follow_request_salmon' do
  949. include_examples 'namespaces' do
  950. def serialize
  951. follow_request = Fabricate(:follow_request)
  952. AtomSerializer.new.follow_request_salmon(follow_request)
  953. end
  954. end
  955. context do
  956. def serialize(follow_request)
  957. AtomSerializer.new.follow_request_salmon(follow_request)
  958. end
  959. it_behaves_like 'follow request salmon'
  960. it 'appends id element with unique tag' do
  961. follow_request = Fabricate(:follow_request, created_at: '2000-01-01T00:00:00Z')
  962. follow_request_salmon = serialize(follow_request)
  963. expect(follow_request_salmon.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{follow_request.id}:objectType=FollowRequest"
  964. end
  965. it 'appends title element with description' do
  966. account = Fabricate(:account, domain: nil, username: 'account')
  967. target_account = Fabricate(:account, domain: 'remote', username: 'target_account')
  968. follow_request = Fabricate(:follow_request, account: account, target_account: target_account)
  969. follow_request_salmon = serialize(follow_request)
  970. expect(follow_request_salmon.title.text).to eq 'account requested to follow target_account@remote'
  971. end
  972. it 'returns element whose rendered view triggers follow request when processed' do
  973. follow_request = Fabricate(:follow_request)
  974. follow_request_salmon = serialize(follow_request)
  975. xml = AtomSerializer.render(follow_request_salmon)
  976. envelope = OStatus2::Salmon.new.pack(xml, follow_request.account.keypair)
  977. follow_request.destroy!
  978. ProcessInteractionService.new.call(envelope, follow_request.target_account)
  979. expect(follow_request.account.requested?(follow_request.target_account)).to eq true
  980. end
  981. end
  982. end
  983. describe '#authorize_follow_request_salmon' do
  984. include_examples 'namespaces' do
  985. def serialize
  986. follow_request = Fabricate(:follow_request)
  987. AtomSerializer.new.authorize_follow_request_salmon(follow_request)
  988. end
  989. end
  990. it_behaves_like 'follow request salmon' do
  991. def serialize(follow_request)
  992. authorize_follow_request_salmon = AtomSerializer.new.authorize_follow_request_salmon(follow_request)
  993. authorize_follow_request_salmon.nodes.find { |node| node.name == 'activity:object' }
  994. end
  995. end
  996. it 'appends id element with unique tag' do
  997. follow_request = Fabricate(:follow_request)
  998. time_before = Time.now
  999. authorize_follow_request_salmon = AtomSerializer.new.authorize_follow_request_salmon(follow_request)
  1000. time_after = Time.now
  1001. expect(authorize_follow_request_salmon.id.text).to(
  1002. eq(TagManager.instance.unique_tag(time_before.utc, follow_request.id, 'FollowRequest'))
  1003. .or(eq(TagManager.instance.unique_tag(time_after.utc, follow_request.id, 'FollowRequest')))
  1004. )
  1005. end
  1006. it 'appends title element with description' do
  1007. account = Fabricate(:account, domain: 'remote', username: 'account')
  1008. target_account = Fabricate(:account, domain: nil, username: 'target_account')
  1009. follow_request = Fabricate(:follow_request, account: account, target_account: target_account)
  1010. authorize_follow_request_salmon = AtomSerializer.new.authorize_follow_request_salmon(follow_request)
  1011. expect(authorize_follow_request_salmon.title.text).to eq 'target_account authorizes follow request by account@remote'
  1012. end
  1013. it 'appends activity:object-type element with activity type' do
  1014. follow_request = Fabricate(:follow_request)
  1015. authorize_follow_request_salmon = AtomSerializer.new.authorize_follow_request_salmon(follow_request)
  1016. object_type = authorize_follow_request_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  1017. expect(object_type.text).to eq TagManager::TYPES[:activity]
  1018. end
  1019. it 'appends activity:verb element with authorize' do
  1020. follow_request = Fabricate(:follow_request)
  1021. authorize_follow_request_salmon = AtomSerializer.new.authorize_follow_request_salmon(follow_request)
  1022. verb = authorize_follow_request_salmon.nodes.find { |node| node.name == 'activity:verb' }
  1023. expect(verb.text).to eq TagManager::VERBS[:authorize]
  1024. end
  1025. it 'returns element whose rendered view creates follow from follow request when processed' do
  1026. follow_request = Fabricate(:follow_request)
  1027. authorize_follow_request_salmon = AtomSerializer.new.authorize_follow_request_salmon(follow_request)
  1028. xml = AtomSerializer.render(authorize_follow_request_salmon)
  1029. envelope = OStatus2::Salmon.new.pack(xml, follow_request.target_account.keypair)
  1030. ProcessInteractionService.new.call(envelope, follow_request.account)
  1031. expect(follow_request.account.following?(follow_request.target_account)).to eq true
  1032. expect { follow_request.reload }.to raise_error ActiveRecord::RecordNotFound
  1033. end
  1034. end
  1035. describe '#reject_follow_request_salmon' do
  1036. include_examples 'namespaces' do
  1037. def serialize
  1038. follow_request = Fabricate(:follow_request)
  1039. AtomSerializer.new.reject_follow_request_salmon(follow_request)
  1040. end
  1041. end
  1042. it_behaves_like 'follow request salmon' do
  1043. def serialize(follow_request)
  1044. reject_follow_request_salmon = AtomSerializer.new.reject_follow_request_salmon(follow_request)
  1045. reject_follow_request_salmon.nodes.find { |node| node.name == 'activity:object' }
  1046. end
  1047. end
  1048. it 'appends id element with unique tag' do
  1049. follow_request = Fabricate(:follow_request)
  1050. time_before = Time.now
  1051. reject_follow_request_salmon = AtomSerializer.new.reject_follow_request_salmon(follow_request)
  1052. time_after = Time.now
  1053. expect(reject_follow_request_salmon.id.text).to(
  1054. eq(TagManager.instance.unique_tag(time_before.utc, follow_request.id, 'FollowRequest'))
  1055. .or(TagManager.instance.unique_tag(time_after.utc, follow_request.id, 'FollowRequest'))
  1056. )
  1057. end
  1058. it 'appends title element with description' do
  1059. account = Fabricate(:account, domain: 'remote', username: 'account')
  1060. target_account = Fabricate(:account, domain: nil, username: 'target_account')
  1061. follow_request = Fabricate(:follow_request, account: account, target_account: target_account)
  1062. reject_follow_request_salmon = AtomSerializer.new.reject_follow_request_salmon(follow_request)
  1063. expect(reject_follow_request_salmon.title.text).to eq 'target_account rejects follow request by account@remote'
  1064. end
  1065. it 'appends activity:object-type element with activity type' do
  1066. follow_request = Fabricate(:follow_request)
  1067. reject_follow_request_salmon = AtomSerializer.new.reject_follow_request_salmon(follow_request)
  1068. object_type = reject_follow_request_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  1069. expect(object_type.text).to eq TagManager::TYPES[:activity]
  1070. end
  1071. it 'appends activity:verb element with authorize' do
  1072. follow_request = Fabricate(:follow_request)
  1073. reject_follow_request_salmon = AtomSerializer.new.reject_follow_request_salmon(follow_request)
  1074. verb = reject_follow_request_salmon.nodes.find { |node| node.name == 'activity:verb' }
  1075. expect(verb.text).to eq TagManager::VERBS[:reject]
  1076. end
  1077. it 'returns element whose rendered view deletes follow request when processed' do
  1078. follow_request = Fabricate(:follow_request)
  1079. reject_follow_request_salmon = AtomSerializer.new.reject_follow_request_salmon(follow_request)
  1080. xml = AtomSerializer.render(reject_follow_request_salmon)
  1081. envelope = OStatus2::Salmon.new.pack(xml, follow_request.target_account.keypair)
  1082. ProcessInteractionService.new.call(envelope, follow_request.account)
  1083. expect(follow_request.account.following?(follow_request.target_account)).to eq false
  1084. expect { follow_request.reload }.to raise_error ActiveRecord::RecordNotFound
  1085. end
  1086. end
  1087. describe '#object' do
  1088. include_examples 'status attributes' do
  1089. def serialize(status)
  1090. AtomSerializer.new.object(status)
  1091. end
  1092. end
  1093. it 'returns activity:object element' do
  1094. status = Fabricate(:status)
  1095. object = AtomSerializer.new.object(status)
  1096. expect(object.name).to eq 'activity:object'
  1097. end
  1098. it 'appends id element with URL for status' do
  1099. status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z')
  1100. object = AtomSerializer.new.object(status)
  1101. expect(object.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status"
  1102. end
  1103. it 'appends published element with created date' do
  1104. status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z')
  1105. object = AtomSerializer.new.object(status)
  1106. expect(object.published.text).to eq '2000-01-01T00:00:00Z'
  1107. end
  1108. it 'appends updated element with updated date' do
  1109. status = Fabricate(:status, updated_at: '2000-01-01T00:00:00Z')
  1110. object = AtomSerializer.new.object(status)
  1111. expect(object.updated.text).to eq '2000-01-01T00:00:00Z'
  1112. end
  1113. it 'appends title element with title' do
  1114. account = Fabricate(:account, username: 'username')
  1115. status = Fabricate(:status, account: account)
  1116. object = AtomSerializer.new.object(status)
  1117. expect(object.title.text).to eq 'New status by username'
  1118. end
  1119. it 'appends author element with account' do
  1120. account = Fabricate(:account, username: 'username')
  1121. status = Fabricate(:status, account: account)
  1122. entry = AtomSerializer.new.object(status)
  1123. expect(entry.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  1124. end
  1125. it 'appends activity:object-type element with object type' do
  1126. status = Fabricate(:status)
  1127. entry = AtomSerializer.new.object(status)
  1128. object_type = entry.nodes.find { |node| node.name == 'activity:object-type' }
  1129. expect(object_type.text).to eq TagManager::TYPES[:note]
  1130. end
  1131. it 'appends activity:verb element with verb' do
  1132. status = Fabricate(:status)
  1133. entry = AtomSerializer.new.object(status)
  1134. object_type = entry.nodes.find { |node| node.name == 'activity:verb' }
  1135. expect(object_type.text).to eq TagManager::VERBS[:post]
  1136. end
  1137. it 'appends link element for an alternative' do
  1138. account = Fabricate(:account, username: 'username')
  1139. status = Fabricate(:status, account: account)
  1140. entry = AtomSerializer.new.object(status)
  1141. link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' }
  1142. expect(link[:type]).to eq 'text/html'
  1143. expect(link[:href]).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}"
  1144. end
  1145. it 'appends thr:in-reply-to element if it is a reply and thread is not nil' do
  1146. account = Fabricate(:account, username: 'username')
  1147. thread = Fabricate(:status, account: account, created_at: '2000-01-01T00:00:00Z')
  1148. reply = Fabricate(:status, thread: thread)
  1149. entry = AtomSerializer.new.object(reply)
  1150. in_reply_to = entry.nodes.find { |node| node.name == 'thr:in-reply-to' }
  1151. expect(in_reply_to.ref).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{thread.id}:objectType=Status"
  1152. expect(in_reply_to.href).to eq "https://cb6e6126.ngrok.io/@username/#{thread.id}"
  1153. end
  1154. it 'does not append thr:in-reply-to element if thread is nil' do
  1155. status = Fabricate(:status, thread: nil)
  1156. entry = AtomSerializer.new.object(status)
  1157. entry.nodes.each { |node| expect(node.name).not_to eq 'thr:in-reply-to' }
  1158. end
  1159. it 'does not append ostatus:conversation element if conversation_id is nil' do
  1160. status = Fabricate.build(:status, conversation_id: nil)
  1161. status.save!(validate: false)
  1162. entry = AtomSerializer.new.object(status)
  1163. entry.nodes.each { |node| expect(node.name).not_to eq 'ostatus:conversation' }
  1164. end
  1165. it 'appends ostatus:conversation element if conversation_id is not nil' do
  1166. status = Fabricate(:status)
  1167. status.conversation.update!(created_at: '2000-01-01T00:00:00Z')
  1168. entry = AtomSerializer.new.object(status)
  1169. conversation = entry.nodes.find { |node| node.name == 'ostatus:conversation' }
  1170. expect(conversation[:ref]).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.conversation.id}:objectType=Conversation"
  1171. end
  1172. end
  1173. end