闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1560 lines
61 KiB

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