闭社主体 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.

1554 lines
60 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 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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::AtomSerializer.new.author(account)
  152. link = author.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' && node[:type] == 'text/html' }
  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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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. OStatus::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 = OStatus::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. OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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 = OStatus::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[:href]).to match /^https:\/\/cb6e6126.ngrok.io\/system\/media_attachments\/files\/.+\/original\/attachment.jpg$/
  298. end
  299. it 'appends mastodon:scope element with visibility' do
  300. status = Fabricate(:status, visibility: :public)
  301. entry = OStatus::AtomSerializer.new.entry(status.stream_entry)
  302. scope = entry.nodes.find { |node| node.name == 'mastodon:scope' }
  303. expect(scope.text).to eq 'public'
  304. end
  305. it 'returns element whose rendered view triggers creation when processed' do
  306. remote_account = Account.create!(username: 'username')
  307. remote_status = Fabricate(:status, account: remote_account, created_at: '2000-01-01T00:00:00Z')
  308. entry = OStatus::AtomSerializer.new.entry(remote_status.stream_entry, true)
  309. entry.nodes.delete_if { |node| node[:type] == 'application/activity+json' } # Remove ActivityPub link to simplify test
  310. xml = OStatus::AtomSerializer.render(entry).gsub('cb6e6126.ngrok.io', 'remote')
  311. remote_status.destroy!
  312. remote_account.destroy!
  313. account = Account.create!(
  314. domain: 'remote',
  315. username: 'username',
  316. last_webfingered_at: Time.now.utc
  317. )
  318. ProcessFeedService.new.call(xml, account)
  319. expect(Status.find_by(uri: "https://remote/users/#{remote_status.account.to_param}/statuses/#{remote_status.id}")).to be_instance_of Status
  320. end
  321. end
  322. context 'if status is not present' do
  323. it 'appends content element saying status is deleted' do
  324. status = Fabricate(:status)
  325. status.destroy!
  326. entry = OStatus::AtomSerializer.new.entry(status.stream_entry)
  327. expect(entry.content.text).to eq 'Deleted status'
  328. end
  329. it 'appends title element saying the status is deleted' do
  330. account = Fabricate(:account, username: 'username')
  331. status = Fabricate(:status, account: account)
  332. status.destroy!
  333. entry = OStatus::AtomSerializer.new.entry(status.stream_entry)
  334. expect(entry.title.text).to eq 'username deleted status'
  335. end
  336. end
  337. context 'it is not root' do
  338. let(:stream_entry) { Fabricate(:stream_entry) }
  339. subject { OStatus::AtomSerializer.new.entry(stream_entry, false) }
  340. include_examples 'not root'
  341. end
  342. context 'without root parameter' do
  343. let(:stream_entry) { Fabricate(:stream_entry) }
  344. subject { OStatus::AtomSerializer.new.entry(stream_entry) }
  345. include_examples 'not root'
  346. end
  347. it 'returns entry element' do
  348. stream_entry = Fabricate(:stream_entry)
  349. entry = OStatus::AtomSerializer.new.entry(stream_entry)
  350. expect(entry.name).to eq 'entry'
  351. end
  352. it 'appends id element with unique tag' do
  353. status = Fabricate(:status, reblog_of_id: nil, created_at: '2000-01-01T00:00:00Z')
  354. entry = OStatus::AtomSerializer.new.entry(status.stream_entry)
  355. expect(entry.id.text).to eq "https://cb6e6126.ngrok.io/users/#{status.account.to_param}/statuses/#{status.id}"
  356. end
  357. it 'appends published element with created date' do
  358. stream_entry = Fabricate(:stream_entry, created_at: '2000-01-01T00:00:00Z')
  359. entry = OStatus::AtomSerializer.new.entry(stream_entry)
  360. expect(entry.published.text).to eq '2000-01-01T00:00:00Z'
  361. end
  362. it 'appends updated element with updated date' do
  363. stream_entry = Fabricate(:stream_entry, updated_at: '2000-01-01T00:00:00Z')
  364. entry = OStatus::AtomSerializer.new.entry(stream_entry)
  365. expect(entry.updated.text).to eq '2000-01-01T00:00:00Z'
  366. end
  367. it 'appends title element with status title' do
  368. account = Fabricate(:account, username: 'username')
  369. status = Fabricate(:status, account: account, reblog_of_id: nil)
  370. entry = OStatus::AtomSerializer.new.entry(status.stream_entry)
  371. expect(entry.title.text).to eq 'New status by username'
  372. end
  373. it 'appends activity:object-type element with object type' do
  374. status = Fabricate(:status)
  375. entry = OStatus::AtomSerializer.new.entry(status.stream_entry)
  376. object_type = entry.nodes.find { |node| node.name == 'activity:object-type' }
  377. expect(object_type.text).to eq TagManager::TYPES[:note]
  378. end
  379. it 'appends activity:verb element with object type' do
  380. status = Fabricate(:status)
  381. entry = OStatus::AtomSerializer.new.entry(status.stream_entry)
  382. object_type = entry.nodes.find { |node| node.name == 'activity:verb' }
  383. expect(object_type.text).to eq TagManager::VERBS[:post]
  384. end
  385. it 'appends activity:object element with target if present' do
  386. reblogged = Fabricate(:status, created_at: '2000-01-01T00:00:00Z')
  387. reblog = Fabricate(:status, reblog: reblogged)
  388. entry = OStatus::AtomSerializer.new.entry(reblog.stream_entry)
  389. object = entry.nodes.find { |node| node.name == 'activity:object' }
  390. expect(object.id.text).to eq "https://cb6e6126.ngrok.io/users/#{reblogged.account.to_param}/statuses/#{reblogged.id}"
  391. end
  392. it 'does not append activity:object element if target is not present' do
  393. status = Fabricate(:status, reblog_of_id: nil)
  394. entry = OStatus::AtomSerializer.new.entry(status.stream_entry)
  395. entry.nodes.each { |node| expect(node.name).not_to eq 'activity:object' }
  396. end
  397. it 'appends link element for an alternative' do
  398. account = Fabricate(:account, username: 'username')
  399. status = Fabricate(:status, account: account)
  400. entry = OStatus::AtomSerializer.new.entry(status.stream_entry)
  401. link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' && node[:type] == 'text/html' }
  402. expect(link[:type]).to eq 'text/html'
  403. expect(link[:href]).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}"
  404. end
  405. it 'appends link element for itself' do
  406. account = Fabricate(:account, username: 'username')
  407. status = Fabricate(:status, account: account)
  408. entry = OStatus::AtomSerializer.new.entry(status.stream_entry)
  409. link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'self' }
  410. expect(link[:type]).to eq 'application/atom+xml'
  411. expect(link[:href]).to eq "https://cb6e6126.ngrok.io/users/username/updates/#{status.stream_entry.id}.atom"
  412. end
  413. it 'appends thr:in-reply-to element if threaded' do
  414. in_reply_to_status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z', reblog_of_id: nil)
  415. reply_status = Fabricate(:status, in_reply_to_id: in_reply_to_status.id)
  416. entry = OStatus::AtomSerializer.new.entry(reply_status.stream_entry)
  417. in_reply_to = entry.nodes.find { |node| node.name == 'thr:in-reply-to' }
  418. 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}"
  419. end
  420. it 'does not append thr:in-reply-to element if not threaded' do
  421. status = Fabricate(:status)
  422. entry = OStatus::AtomSerializer.new.entry(status.stream_entry)
  423. entry.nodes.each { |node| expect(node.name).not_to eq 'thr:in-reply-to' }
  424. end
  425. it 'appends ostatus:conversation if conversation id is present' do
  426. status = Fabricate(:status)
  427. status.conversation.update!(created_at: '2000-01-01T00:00:00Z')
  428. entry = OStatus::AtomSerializer.new.entry(status.stream_entry)
  429. conversation = entry.nodes.find { |node| node.name == 'ostatus:conversation' }
  430. expect(conversation[:ref]).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.conversation_id}:objectType=Conversation"
  431. end
  432. it 'does not append ostatus:conversation if conversation id is not present' do
  433. status = Fabricate.build(:status, conversation_id: nil)
  434. status.save!(validate: false)
  435. entry = OStatus::AtomSerializer.new.entry(status.stream_entry)
  436. entry.nodes.each { |node| expect(node.name).not_to eq 'ostatus:conversation' }
  437. end
  438. end
  439. describe '#feed' do
  440. include_examples 'namespaces' do
  441. def serialize
  442. account = Fabricate(:account)
  443. OStatus::AtomSerializer.new.feed(account, [])
  444. end
  445. end
  446. it 'returns feed element' do
  447. account = Fabricate(:account)
  448. feed = OStatus::AtomSerializer.new.feed(account, [])
  449. expect(feed.name).to eq 'feed'
  450. end
  451. it 'appends id element with account Atom URL' do
  452. account = Fabricate(:account, username: 'username')
  453. feed = OStatus::AtomSerializer.new.feed(account, [])
  454. expect(feed.id.text).to eq 'https://cb6e6126.ngrok.io/users/username.atom'
  455. end
  456. it 'appends title element with account display name if present' do
  457. account = Fabricate(:account, display_name: 'display name')
  458. feed = OStatus::AtomSerializer.new.feed(account, [])
  459. expect(feed.title.text).to eq 'display name'
  460. end
  461. it 'does not append title element with account username if account display name is not present' do
  462. account = Fabricate(:account, display_name: '', username: 'username')
  463. feed = OStatus::AtomSerializer.new.feed(account, [])
  464. expect(feed.title.text).to eq 'username'
  465. end
  466. it 'appends subtitle element with account note' do
  467. account = Fabricate(:account, note: 'note')
  468. feed = OStatus::AtomSerializer.new.feed(account, [])
  469. expect(feed.subtitle.text).to eq 'note'
  470. end
  471. it 'appends updated element with date account got updated' do
  472. account = Fabricate(:account, updated_at: '2000-01-01T00:00:00Z')
  473. feed = OStatus::AtomSerializer.new.feed(account, [])
  474. expect(feed.updated.text).to eq '2000-01-01T00:00:00Z'
  475. end
  476. it 'appends logo element with full asset URL for original account avatar' do
  477. account = Fabricate(:account, avatar: attachment_fixture('avatar.gif'))
  478. feed = OStatus::AtomSerializer.new.feed(account, [])
  479. expect(feed.logo.text).to match /^https:\/\/cb6e6126.ngrok.io\/system\/accounts\/avatars\/.+\/original\/avatar.gif/
  480. end
  481. it 'appends author element' do
  482. account = Fabricate(:account, username: 'username')
  483. feed = OStatus::AtomSerializer.new.feed(account, [])
  484. expect(feed.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  485. end
  486. it 'appends link element for an alternative' do
  487. account = Fabricate(:account, username: 'username')
  488. feed = OStatus::AtomSerializer.new.feed(account, [])
  489. link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' && node[:type] == 'text/html' }
  490. expect(link[:type]).to eq 'text/html'
  491. expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/@username'
  492. end
  493. it 'appends link element for itself' do
  494. account = Fabricate(:account, username: 'username')
  495. feed = OStatus::AtomSerializer.new.feed(account, [])
  496. link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'self' }
  497. expect(link[:type]).to eq 'application/atom+xml'
  498. expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/users/username.atom'
  499. end
  500. it 'appends link element for the next if it has 20 stream entries' do
  501. account = Fabricate(:account, username: 'username')
  502. stream_entry = Fabricate(:stream_entry)
  503. feed = OStatus::AtomSerializer.new.feed(account, Array.new(20, stream_entry))
  504. link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'next' }
  505. expect(link[:type]).to eq 'application/atom+xml'
  506. expect(link[:href]).to eq "https://cb6e6126.ngrok.io/users/username.atom?max_id=#{stream_entry.id}"
  507. end
  508. it 'does not append link element for the next if it does not have 20 stream entries' do
  509. account = Fabricate(:account, username: 'username')
  510. feed = OStatus::AtomSerializer.new.feed(account, [])
  511. feed.nodes.each do |node|
  512. expect(node[:rel]).not_to eq 'next' if node.name == 'link'
  513. end
  514. end
  515. it 'appends link element for hub' do
  516. account = Fabricate(:account, username: 'username')
  517. feed = OStatus::AtomSerializer.new.feed(account, [])
  518. link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'hub' }
  519. expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/api/push'
  520. end
  521. it 'appends link element for Salmon' do
  522. account = Fabricate(:account, username: 'username')
  523. feed = OStatus::AtomSerializer.new.feed(account, [])
  524. link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'salmon' }
  525. expect(link[:href]).to start_with 'https://cb6e6126.ngrok.io/api/salmon/'
  526. end
  527. it 'appends stream entries' do
  528. account = Fabricate(:account, username: 'username')
  529. status = Fabricate(:status, account: account)
  530. feed = OStatus::AtomSerializer.new.feed(account, [status.stream_entry])
  531. expect(feed.entry.title.text).to eq 'New status by username'
  532. end
  533. end
  534. describe '#block_salmon' do
  535. include_examples 'namespaces' do
  536. def serialize
  537. block = Fabricate(:block)
  538. OStatus::AtomSerializer.new.block_salmon(block)
  539. end
  540. end
  541. it 'returns entry element' do
  542. block = Fabricate(:block)
  543. block_salmon = OStatus::AtomSerializer.new.block_salmon(block)
  544. expect(block_salmon.name).to eq 'entry'
  545. end
  546. it 'appends id element with unique tag' do
  547. block = Fabricate(:block)
  548. time_before = Time.now
  549. block_salmon = OStatus::AtomSerializer.new.block_salmon(block)
  550. time_after = Time.now
  551. expect(block_salmon.id.text).to(
  552. eq(TagManager.instance.unique_tag(time_before.utc, block.id, 'Block'))
  553. .or(eq(TagManager.instance.unique_tag(time_after.utc, block.id, 'Block')))
  554. )
  555. end
  556. it 'appends title element with description' do
  557. account = Fabricate(:account, domain: nil, username: 'account')
  558. target_account = Fabricate(:account, domain: 'remote', username: 'target_account')
  559. block = Fabricate(:block, account: account, target_account: target_account)
  560. block_salmon = OStatus::AtomSerializer.new.block_salmon(block)
  561. expect(block_salmon.title.text).to eq 'account no longer wishes to interact with target_account@remote'
  562. end
  563. it 'appends author element with account' do
  564. account = Fabricate(:account, domain: nil, username: 'account')
  565. block = Fabricate(:block, account: account)
  566. block_salmon = OStatus::AtomSerializer.new.block_salmon(block)
  567. expect(block_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/account'
  568. end
  569. it 'appends activity:object-type element with activity type' do
  570. block = Fabricate(:block)
  571. block_salmon = OStatus::AtomSerializer.new.block_salmon(block)
  572. object_type = block_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  573. expect(object_type.text).to eq TagManager::TYPES[:activity]
  574. end
  575. it 'appends activity:verb element with block' do
  576. block = Fabricate(:block)
  577. block_salmon = OStatus::AtomSerializer.new.block_salmon(block)
  578. verb = block_salmon.nodes.find { |node| node.name == 'activity:verb' }
  579. expect(verb.text).to eq TagManager::VERBS[:block]
  580. end
  581. it 'appends activity:object element with target account' do
  582. target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id')
  583. block = Fabricate(:block, target_account: target_account)
  584. block_salmon = OStatus::AtomSerializer.new.block_salmon(block)
  585. object = block_salmon.nodes.find { |node| node.name == 'activity:object' }
  586. expect(object.id.text).to eq 'https://domain/id'
  587. end
  588. it 'returns element whose rendered view triggers block when processed' do
  589. block = Fabricate(:block)
  590. block_salmon = OStatus::AtomSerializer.new.block_salmon(block)
  591. xml = OStatus::AtomSerializer.render(block_salmon)
  592. envelope = OStatus2::Salmon.new.pack(xml, block.account.keypair)
  593. block.destroy!
  594. ProcessInteractionService.new.call(envelope, block.target_account)
  595. expect(block.account.blocking?(block.target_account)).to be true
  596. end
  597. end
  598. describe '#unblock_salmon' do
  599. include_examples 'namespaces' do
  600. def serialize
  601. block = Fabricate(:block)
  602. OStatus::AtomSerializer.new.unblock_salmon(block)
  603. end
  604. end
  605. it 'returns entry element' do
  606. block = Fabricate(:block)
  607. unblock_salmon = OStatus::AtomSerializer.new.unblock_salmon(block)
  608. expect(unblock_salmon.name).to eq 'entry'
  609. end
  610. it 'appends id element with unique tag' do
  611. block = Fabricate(:block)
  612. time_before = Time.now
  613. unblock_salmon = OStatus::AtomSerializer.new.unblock_salmon(block)
  614. time_after = Time.now
  615. expect(unblock_salmon.id.text).to(
  616. eq(TagManager.instance.unique_tag(time_before.utc, block.id, 'Block'))
  617. .or(eq(TagManager.instance.unique_tag(time_after.utc, block.id, 'Block')))
  618. )
  619. end
  620. it 'appends title element with description' do
  621. account = Fabricate(:account, domain: nil, username: 'account')
  622. target_account = Fabricate(:account, domain: 'remote', username: 'target_account')
  623. block = Fabricate(:block, account: account, target_account: target_account)
  624. unblock_salmon = OStatus::AtomSerializer.new.unblock_salmon(block)
  625. expect(unblock_salmon.title.text).to eq 'account no longer blocks target_account@remote'
  626. end
  627. it 'appends author element with account' do
  628. account = Fabricate(:account, domain: nil, username: 'account')
  629. block = Fabricate(:block, account: account)
  630. unblock_salmon = OStatus::AtomSerializer.new.unblock_salmon(block)
  631. expect(unblock_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/account'
  632. end
  633. it 'appends activity:object-type element with activity type' do
  634. block = Fabricate(:block)
  635. unblock_salmon = OStatus::AtomSerializer.new.unblock_salmon(block)
  636. object_type = unblock_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  637. expect(object_type.text).to eq TagManager::TYPES[:activity]
  638. end
  639. it 'appends activity:verb element with block' do
  640. block = Fabricate(:block)
  641. unblock_salmon = OStatus::AtomSerializer.new.unblock_salmon(block)
  642. verb = unblock_salmon.nodes.find { |node| node.name == 'activity:verb' }
  643. expect(verb.text).to eq TagManager::VERBS[:unblock]
  644. end
  645. it 'appends activity:object element with target account' do
  646. target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id')
  647. block = Fabricate(:block, target_account: target_account)
  648. unblock_salmon = OStatus::AtomSerializer.new.unblock_salmon(block)
  649. object = unblock_salmon.nodes.find { |node| node.name == 'activity:object' }
  650. expect(object.id.text).to eq 'https://domain/id'
  651. end
  652. it 'returns element whose rendered view triggers block when processed' do
  653. block = Fabricate(:block)
  654. unblock_salmon = OStatus::AtomSerializer.new.unblock_salmon(block)
  655. xml = OStatus::AtomSerializer.render(unblock_salmon)
  656. envelope = OStatus2::Salmon.new.pack(xml, block.account.keypair)
  657. ProcessInteractionService.new.call(envelope, block.target_account)
  658. expect{ block.reload }.to raise_error ActiveRecord::RecordNotFound
  659. end
  660. end
  661. describe '#favourite_salmon' do
  662. include_examples 'namespaces' do
  663. def serialize
  664. favourite = Fabricate(:favourite)
  665. OStatus::AtomSerializer.new.favourite_salmon(favourite)
  666. end
  667. end
  668. it 'returns entry element' do
  669. favourite = Fabricate(:favourite)
  670. favourite_salmon = OStatus::AtomSerializer.new.favourite_salmon(favourite)
  671. expect(favourite_salmon.name).to eq 'entry'
  672. end
  673. it 'appends id element with unique tag' do
  674. favourite = Fabricate(:favourite, created_at: '2000-01-01T00:00:00Z')
  675. favourite_salmon = OStatus::AtomSerializer.new.favourite_salmon(favourite)
  676. expect(favourite_salmon.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{favourite.id}:objectType=Favourite"
  677. end
  678. it 'appends author element with account' do
  679. account = Fabricate(:account, domain: nil, username: 'username')
  680. favourite = Fabricate(:favourite, account: account)
  681. favourite_salmon = OStatus::AtomSerializer.new.favourite_salmon(favourite)
  682. expect(favourite_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  683. end
  684. it 'appends activity:object-type element with activity type' do
  685. favourite = Fabricate(:favourite)
  686. favourite_salmon = OStatus::AtomSerializer.new.favourite_salmon(favourite)
  687. object_type = favourite_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  688. expect(object_type.text).to eq 'http://activitystrea.ms/schema/1.0/activity'
  689. end
  690. it 'appends activity:verb element with favorite' do
  691. favourite = Fabricate(:favourite)
  692. favourite_salmon = OStatus::AtomSerializer.new.favourite_salmon(favourite)
  693. verb = favourite_salmon.nodes.find { |node| node.name == 'activity:verb' }
  694. expect(verb.text).to eq TagManager::VERBS[:favorite]
  695. end
  696. it 'appends activity:object element with status' do
  697. status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z')
  698. favourite = Fabricate(:favourite, status: status)
  699. favourite_salmon = OStatus::AtomSerializer.new.favourite_salmon(favourite)
  700. object = favourite_salmon.nodes.find { |node| node.name == 'activity:object' }
  701. expect(object.id.text).to eq "https://cb6e6126.ngrok.io/users/#{status.account.to_param}/statuses/#{status.id}"
  702. end
  703. it 'appends thr:in-reply-to element for status' do
  704. status_account = Fabricate(:account, username: 'username')
  705. status = Fabricate(:status, account: status_account, created_at: '2000-01-01T00:00:00Z')
  706. favourite = Fabricate(:favourite, status: status)
  707. favourite_salmon = OStatus::AtomSerializer.new.favourite_salmon(favourite)
  708. in_reply_to = favourite_salmon.nodes.find { |node| node.name == 'thr:in-reply-to' }
  709. expect(in_reply_to.ref).to eq "https://cb6e6126.ngrok.io/users/#{status.account.to_param}/statuses/#{status.id}"
  710. expect(in_reply_to.href).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}"
  711. end
  712. it 'includes description' do
  713. account = Fabricate(:account, domain: nil, username: 'account')
  714. status_account = Fabricate(:account, domain: 'remote', username: 'status_account')
  715. status = Fabricate(:status, account: status_account)
  716. favourite = Fabricate(:favourite, account: account, status: status)
  717. favourite_salmon = OStatus::AtomSerializer.new.favourite_salmon(favourite)
  718. expect(favourite_salmon.title.text).to eq 'account favourited a status by status_account@remote'
  719. expect(favourite_salmon.content.text).to eq 'account favourited a status by status_account@remote'
  720. end
  721. it 'returns element whose rendered view triggers favourite when processed' do
  722. favourite = Fabricate(:favourite)
  723. favourite_salmon = OStatus::AtomSerializer.new.favourite_salmon(favourite)
  724. xml = OStatus::AtomSerializer.render(favourite_salmon)
  725. envelope = OStatus2::Salmon.new.pack(xml, favourite.account.keypair)
  726. favourite.destroy!
  727. ProcessInteractionService.new.call(envelope, favourite.status.account)
  728. expect(favourite.account.favourited?(favourite.status)).to be true
  729. end
  730. end
  731. describe '#unfavourite_salmon' do
  732. include_examples 'namespaces' do
  733. def serialize
  734. favourite = Fabricate(:favourite)
  735. OStatus::AtomSerializer.new.favourite_salmon(favourite)
  736. end
  737. end
  738. it 'returns entry element' do
  739. favourite = Fabricate(:favourite)
  740. unfavourite_salmon = OStatus::AtomSerializer.new.unfavourite_salmon(favourite)
  741. expect(unfavourite_salmon.name).to eq 'entry'
  742. end
  743. it 'appends id element with unique tag' do
  744. favourite = Fabricate(:favourite)
  745. time_before = Time.now
  746. unfavourite_salmon = OStatus::AtomSerializer.new.unfavourite_salmon(favourite)
  747. time_after = Time.now
  748. expect(unfavourite_salmon.id.text).to(
  749. eq(TagManager.instance.unique_tag(time_before.utc, favourite.id, 'Favourite'))
  750. .or(eq(TagManager.instance.unique_tag(time_after.utc, favourite.id, 'Favourite')))
  751. )
  752. end
  753. it 'appends author element with account' do
  754. account = Fabricate(:account, domain: nil, username: 'username')
  755. favourite = Fabricate(:favourite, account: account)
  756. unfavourite_salmon = OStatus::AtomSerializer.new.unfavourite_salmon(favourite)
  757. expect(unfavourite_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  758. end
  759. it 'appends activity:object-type element with activity type' do
  760. favourite = Fabricate(:favourite)
  761. unfavourite_salmon = OStatus::AtomSerializer.new.unfavourite_salmon(favourite)
  762. object_type = unfavourite_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  763. expect(object_type.text).to eq 'http://activitystrea.ms/schema/1.0/activity'
  764. end
  765. it 'appends activity:verb element with favorite' do
  766. favourite = Fabricate(:favourite)
  767. unfavourite_salmon = OStatus::AtomSerializer.new.unfavourite_salmon(favourite)
  768. verb = unfavourite_salmon.nodes.find { |node| node.name == 'activity:verb' }
  769. expect(verb.text).to eq TagManager::VERBS[:unfavorite]
  770. end
  771. it 'appends activity:object element with status' do
  772. status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z')
  773. favourite = Fabricate(:favourite, status: status)
  774. unfavourite_salmon = OStatus::AtomSerializer.new.unfavourite_salmon(favourite)
  775. object = unfavourite_salmon.nodes.find { |node| node.name == 'activity:object' }
  776. expect(object.id.text).to eq "https://cb6e6126.ngrok.io/users/#{status.account.to_param}/statuses/#{status.id}"
  777. end
  778. it 'appends thr:in-reply-to element for status' do
  779. status_account = Fabricate(:account, username: 'username')
  780. status = Fabricate(:status, account: status_account, created_at: '2000-01-01T00:00:00Z')
  781. favourite = Fabricate(:favourite, status: status)
  782. unfavourite_salmon = OStatus::AtomSerializer.new.unfavourite_salmon(favourite)
  783. in_reply_to = unfavourite_salmon.nodes.find { |node| node.name == 'thr:in-reply-to' }
  784. expect(in_reply_to.ref).to eq "https://cb6e6126.ngrok.io/users/#{status.account.to_param}/statuses/#{status.id}"
  785. expect(in_reply_to.href).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}"
  786. end
  787. it 'includes description' do
  788. account = Fabricate(:account, domain: nil, username: 'account')
  789. status_account = Fabricate(:account, domain: 'remote', username: 'status_account')
  790. status = Fabricate(:status, account: status_account)
  791. favourite = Fabricate(:favourite, account: account, status: status)
  792. unfavourite_salmon = OStatus::AtomSerializer.new.unfavourite_salmon(favourite)
  793. expect(unfavourite_salmon.title.text).to eq 'account no longer favourites a status by status_account@remote'
  794. expect(unfavourite_salmon.content.text).to eq 'account no longer favourites a status by status_account@remote'
  795. end
  796. it 'returns element whose rendered view triggers unfavourite when processed' do
  797. favourite = Fabricate(:favourite)
  798. unfavourite_salmon = OStatus::AtomSerializer.new.unfavourite_salmon(favourite)
  799. xml = OStatus::AtomSerializer.render(unfavourite_salmon)
  800. envelope = OStatus2::Salmon.new.pack(xml, favourite.account.keypair)
  801. ProcessInteractionService.new.call(envelope, favourite.status.account)
  802. expect { favourite.reload }.to raise_error ActiveRecord::RecordNotFound
  803. end
  804. end
  805. describe '#follow_salmon' do
  806. include_examples 'namespaces' do
  807. def serialize
  808. follow = Fabricate(:follow)
  809. OStatus::AtomSerializer.new.follow_salmon(follow)
  810. end
  811. end
  812. it 'returns entry element' do
  813. follow = Fabricate(:follow)
  814. follow_salmon = OStatus::AtomSerializer.new.follow_salmon(follow)
  815. expect(follow_salmon.name).to eq 'entry'
  816. end
  817. it 'appends id element with unique tag' do
  818. follow = Fabricate(:follow, created_at: '2000-01-01T00:00:00Z')
  819. follow_salmon = OStatus::AtomSerializer.new.follow_salmon(follow)
  820. expect(follow_salmon.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{follow.id}:objectType=Follow"
  821. end
  822. it 'appends author element with account' do
  823. account = Fabricate(:account, domain: nil, username: 'username')
  824. follow = Fabricate(:follow, account: account)
  825. follow_salmon = OStatus::AtomSerializer.new.follow_salmon(follow)
  826. expect(follow_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  827. end
  828. it 'appends activity:object-type element with activity type' do
  829. follow = Fabricate(:follow)
  830. follow_salmon = OStatus::AtomSerializer.new.follow_salmon(follow)
  831. object_type = follow_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  832. expect(object_type.text).to eq TagManager::TYPES[:activity]
  833. end
  834. it 'appends activity:verb element with follow' do
  835. follow = Fabricate(:follow)
  836. follow_salmon = OStatus::AtomSerializer.new.follow_salmon(follow)
  837. verb = follow_salmon.nodes.find { |node| node.name == 'activity:verb' }
  838. expect(verb.text).to eq TagManager::VERBS[:follow]
  839. end
  840. it 'appends activity:object element with target account' do
  841. target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id')
  842. follow = Fabricate(:follow, target_account: target_account)
  843. follow_salmon = OStatus::AtomSerializer.new.follow_salmon(follow)
  844. object = follow_salmon.nodes.find { |node| node.name == 'activity:object' }
  845. expect(object.id.text).to eq 'https://domain/id'
  846. end
  847. it 'includes description' do
  848. account = Fabricate(:account, domain: nil, username: 'account')
  849. target_account = Fabricate(:account, domain: 'remote', username: 'target_account')
  850. follow = Fabricate(:follow, account: account, target_account: target_account)
  851. follow_salmon = OStatus::AtomSerializer.new.follow_salmon(follow)
  852. expect(follow_salmon.title.text).to eq 'account started following target_account@remote'
  853. expect(follow_salmon.content.text).to eq 'account started following target_account@remote'
  854. end
  855. it 'returns element whose rendered view triggers follow when processed' do
  856. follow = Fabricate(:follow)
  857. follow_salmon = OStatus::AtomSerializer.new.follow_salmon(follow)
  858. xml = OStatus::AtomSerializer.render(follow_salmon)
  859. follow.destroy!
  860. envelope = OStatus2::Salmon.new.pack(xml, follow.account.keypair)
  861. ProcessInteractionService.new.call(envelope, follow.target_account)
  862. expect(follow.account.following?(follow.target_account)).to be true
  863. end
  864. end
  865. describe '#unfollow_salmon' do
  866. include_examples 'namespaces' do
  867. def serialize
  868. follow = Fabricate(:follow)
  869. follow.destroy!
  870. OStatus::AtomSerializer.new.unfollow_salmon(follow)
  871. end
  872. end
  873. it 'returns entry element' do
  874. follow = Fabricate(:follow)
  875. follow.destroy!
  876. unfollow_salmon = OStatus::AtomSerializer.new.unfollow_salmon(follow)
  877. expect(unfollow_salmon.name).to eq 'entry'
  878. end
  879. it 'appends id element with unique tag' do
  880. follow = Fabricate(:follow)
  881. follow.destroy!
  882. time_before = Time.now
  883. unfollow_salmon = OStatus::AtomSerializer.new.unfollow_salmon(follow)
  884. time_after = Time.now
  885. expect(unfollow_salmon.id.text).to(
  886. eq(TagManager.instance.unique_tag(time_before.utc, follow.id, 'Follow'))
  887. .or(eq(TagManager.instance.unique_tag(time_after.utc, follow.id, 'Follow')))
  888. )
  889. end
  890. it 'appends title element with description' do
  891. account = Fabricate(:account, domain: nil, username: 'account')
  892. target_account = Fabricate(:account, domain: 'remote', username: 'target_account')
  893. follow = Fabricate(:follow, account: account, target_account: target_account)
  894. follow.destroy!
  895. unfollow_salmon = OStatus::AtomSerializer.new.unfollow_salmon(follow)
  896. expect(unfollow_salmon.title.text).to eq 'account is no longer following target_account@remote'
  897. end
  898. it 'appends content element with description' do
  899. account = Fabricate(:account, domain: nil, username: 'account')
  900. target_account = Fabricate(:account, domain: 'remote', username: 'target_account')
  901. follow = Fabricate(:follow, account: account, target_account: target_account)
  902. follow.destroy!
  903. unfollow_salmon = OStatus::AtomSerializer.new.unfollow_salmon(follow)
  904. expect(unfollow_salmon.content.text).to eq 'account is no longer following target_account@remote'
  905. end
  906. it 'appends author element with account' do
  907. account = Fabricate(:account, domain: nil, username: 'username')
  908. follow = Fabricate(:follow, account: account)
  909. follow.destroy!
  910. unfollow_salmon = OStatus::AtomSerializer.new.unfollow_salmon(follow)
  911. expect(unfollow_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  912. end
  913. it 'appends activity:object-type element with activity type' do
  914. follow = Fabricate(:follow)
  915. follow.destroy!
  916. unfollow_salmon = OStatus::AtomSerializer.new.unfollow_salmon(follow)
  917. object_type = unfollow_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  918. expect(object_type.text).to eq TagManager::TYPES[:activity]
  919. end
  920. it 'appends activity:verb element with follow' do
  921. follow = Fabricate(:follow)
  922. follow.destroy!
  923. unfollow_salmon = OStatus::AtomSerializer.new.unfollow_salmon(follow)
  924. verb = unfollow_salmon.nodes.find { |node| node.name == 'activity:verb' }
  925. expect(verb.text).to eq TagManager::VERBS[:unfollow]
  926. end
  927. it 'appends activity:object element with target account' do
  928. target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id')
  929. follow = Fabricate(:follow, target_account: target_account)
  930. follow.destroy!
  931. unfollow_salmon = OStatus::AtomSerializer.new.unfollow_salmon(follow)
  932. object = unfollow_salmon.nodes.find { |node| node.name == 'activity:object' }
  933. expect(object.id.text).to eq 'https://domain/id'
  934. end
  935. it 'returns element whose rendered view triggers unfollow when processed' do
  936. follow = Fabricate(:follow)
  937. follow.destroy!
  938. unfollow_salmon = OStatus::AtomSerializer.new.unfollow_salmon(follow)
  939. xml = OStatus::AtomSerializer.render(unfollow_salmon)
  940. follow.account.follow!(follow.target_account)
  941. envelope = OStatus2::Salmon.new.pack(xml, follow.account.keypair)
  942. ProcessInteractionService.new.call(envelope, follow.target_account)
  943. expect(follow.account.following?(follow.target_account)).to be false
  944. end
  945. end
  946. describe '#follow_request_salmon' do
  947. include_examples 'namespaces' do
  948. def serialize
  949. follow_request = Fabricate(:follow_request)
  950. OStatus::AtomSerializer.new.follow_request_salmon(follow_request)
  951. end
  952. end
  953. context do
  954. def serialize(follow_request)
  955. OStatus::AtomSerializer.new.follow_request_salmon(follow_request)
  956. end
  957. it_behaves_like 'follow request salmon'
  958. it 'appends id element with unique tag' do
  959. follow_request = Fabricate(:follow_request, created_at: '2000-01-01T00:00:00Z')
  960. follow_request_salmon = serialize(follow_request)
  961. expect(follow_request_salmon.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{follow_request.id}:objectType=FollowRequest"
  962. end
  963. it 'appends title element with description' do
  964. account = Fabricate(:account, domain: nil, username: 'account')
  965. target_account = Fabricate(:account, domain: 'remote', username: 'target_account')
  966. follow_request = Fabricate(:follow_request, account: account, target_account: target_account)
  967. follow_request_salmon = serialize(follow_request)
  968. expect(follow_request_salmon.title.text).to eq 'account requested to follow target_account@remote'
  969. end
  970. it 'returns element whose rendered view triggers follow request when processed' do
  971. follow_request = Fabricate(:follow_request)
  972. follow_request_salmon = serialize(follow_request)
  973. xml = OStatus::AtomSerializer.render(follow_request_salmon)
  974. envelope = OStatus2::Salmon.new.pack(xml, follow_request.account.keypair)
  975. follow_request.destroy!
  976. ProcessInteractionService.new.call(envelope, follow_request.target_account)
  977. expect(follow_request.account.requested?(follow_request.target_account)).to eq true
  978. end
  979. end
  980. end
  981. describe '#authorize_follow_request_salmon' do
  982. include_examples 'namespaces' do
  983. def serialize
  984. follow_request = Fabricate(:follow_request)
  985. OStatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request)
  986. end
  987. end
  988. it_behaves_like 'follow request salmon' do
  989. def serialize(follow_request)
  990. authorize_follow_request_salmon = OStatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request)
  991. authorize_follow_request_salmon.nodes.find { |node| node.name == 'activity:object' }
  992. end
  993. end
  994. it 'appends id element with unique tag' do
  995. follow_request = Fabricate(:follow_request)
  996. time_before = Time.now
  997. authorize_follow_request_salmon = OStatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request)
  998. time_after = Time.now
  999. expect(authorize_follow_request_salmon.id.text).to(
  1000. eq(TagManager.instance.unique_tag(time_before.utc, follow_request.id, 'FollowRequest'))
  1001. .or(eq(TagManager.instance.unique_tag(time_after.utc, follow_request.id, 'FollowRequest')))
  1002. )
  1003. end
  1004. it 'appends title element with description' do
  1005. account = Fabricate(:account, domain: 'remote', username: 'account')
  1006. target_account = Fabricate(:account, domain: nil, username: 'target_account')
  1007. follow_request = Fabricate(:follow_request, account: account, target_account: target_account)
  1008. authorize_follow_request_salmon = OStatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request)
  1009. expect(authorize_follow_request_salmon.title.text).to eq 'target_account authorizes follow request by account@remote'
  1010. end
  1011. it 'appends activity:object-type element with activity type' do
  1012. follow_request = Fabricate(:follow_request)
  1013. authorize_follow_request_salmon = OStatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request)
  1014. object_type = authorize_follow_request_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  1015. expect(object_type.text).to eq TagManager::TYPES[:activity]
  1016. end
  1017. it 'appends activity:verb element with authorize' do
  1018. follow_request = Fabricate(:follow_request)
  1019. authorize_follow_request_salmon = OStatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request)
  1020. verb = authorize_follow_request_salmon.nodes.find { |node| node.name == 'activity:verb' }
  1021. expect(verb.text).to eq TagManager::VERBS[:authorize]
  1022. end
  1023. it 'returns element whose rendered view creates follow from follow request when processed' do
  1024. follow_request = Fabricate(:follow_request)
  1025. authorize_follow_request_salmon = OStatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request)
  1026. xml = OStatus::AtomSerializer.render(authorize_follow_request_salmon)
  1027. envelope = OStatus2::Salmon.new.pack(xml, follow_request.target_account.keypair)
  1028. ProcessInteractionService.new.call(envelope, follow_request.account)
  1029. expect(follow_request.account.following?(follow_request.target_account)).to eq true
  1030. expect { follow_request.reload }.to raise_error ActiveRecord::RecordNotFound
  1031. end
  1032. end
  1033. describe '#reject_follow_request_salmon' do
  1034. include_examples 'namespaces' do
  1035. def serialize
  1036. follow_request = Fabricate(:follow_request)
  1037. OStatus::AtomSerializer.new.reject_follow_request_salmon(follow_request)
  1038. end
  1039. end
  1040. it_behaves_like 'follow request salmon' do
  1041. def serialize(follow_request)
  1042. reject_follow_request_salmon = OStatus::AtomSerializer.new.reject_follow_request_salmon(follow_request)
  1043. reject_follow_request_salmon.nodes.find { |node| node.name == 'activity:object' }
  1044. end
  1045. end
  1046. it 'appends id element with unique tag' do
  1047. follow_request = Fabricate(:follow_request)
  1048. time_before = Time.now
  1049. reject_follow_request_salmon = OStatus::AtomSerializer.new.reject_follow_request_salmon(follow_request)
  1050. time_after = Time.now
  1051. expect(reject_follow_request_salmon.id.text).to(
  1052. eq(TagManager.instance.unique_tag(time_before.utc, follow_request.id, 'FollowRequest'))
  1053. .or(TagManager.instance.unique_tag(time_after.utc, follow_request.id, 'FollowRequest'))
  1054. )
  1055. end
  1056. it 'appends title element with description' do
  1057. account = Fabricate(:account, domain: 'remote', username: 'account')
  1058. target_account = Fabricate(:account, domain: nil, username: 'target_account')
  1059. follow_request = Fabricate(:follow_request, account: account, target_account: target_account)
  1060. reject_follow_request_salmon = OStatus::AtomSerializer.new.reject_follow_request_salmon(follow_request)
  1061. expect(reject_follow_request_salmon.title.text).to eq 'target_account rejects follow request by account@remote'
  1062. end
  1063. it 'appends activity:object-type element with activity type' do
  1064. follow_request = Fabricate(:follow_request)
  1065. reject_follow_request_salmon = OStatus::AtomSerializer.new.reject_follow_request_salmon(follow_request)
  1066. object_type = reject_follow_request_salmon.nodes.find { |node| node.name == 'activity:object-type' }
  1067. expect(object_type.text).to eq TagManager::TYPES[:activity]
  1068. end
  1069. it 'appends activity:verb element with authorize' do
  1070. follow_request = Fabricate(:follow_request)
  1071. reject_follow_request_salmon = OStatus::AtomSerializer.new.reject_follow_request_salmon(follow_request)
  1072. verb = reject_follow_request_salmon.nodes.find { |node| node.name == 'activity:verb' }
  1073. expect(verb.text).to eq TagManager::VERBS[:reject]
  1074. end
  1075. it 'returns element whose rendered view deletes follow request when processed' do
  1076. follow_request = Fabricate(:follow_request)
  1077. reject_follow_request_salmon = OStatus::AtomSerializer.new.reject_follow_request_salmon(follow_request)
  1078. xml = OStatus::AtomSerializer.render(reject_follow_request_salmon)
  1079. envelope = OStatus2::Salmon.new.pack(xml, follow_request.target_account.keypair)
  1080. ProcessInteractionService.new.call(envelope, follow_request.account)
  1081. expect(follow_request.account.following?(follow_request.target_account)).to eq false
  1082. expect { follow_request.reload }.to raise_error ActiveRecord::RecordNotFound
  1083. end
  1084. end
  1085. describe '#object' do
  1086. include_examples 'status attributes' do
  1087. def serialize(status)
  1088. OStatus::AtomSerializer.new.object(status)
  1089. end
  1090. end
  1091. it 'returns activity:object element' do
  1092. status = Fabricate(:status)
  1093. object = OStatus::AtomSerializer.new.object(status)
  1094. expect(object.name).to eq 'activity:object'
  1095. end
  1096. it 'appends id element with URL for status' do
  1097. status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z')
  1098. object = OStatus::AtomSerializer.new.object(status)
  1099. expect(object.id.text).to eq "https://cb6e6126.ngrok.io/users/#{status.account.to_param}/statuses/#{status.id}"
  1100. end
  1101. it 'appends published element with created date' do
  1102. status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z')
  1103. object = OStatus::AtomSerializer.new.object(status)
  1104. expect(object.published.text).to eq '2000-01-01T00:00:00Z'
  1105. end
  1106. it 'appends updated element with updated date' do
  1107. status = Fabricate(:status)
  1108. status.updated_at = '2000-01-01T00:00:00Z'
  1109. object = OStatus::AtomSerializer.new.object(status)
  1110. expect(object.updated.text).to eq '2000-01-01T00:00:00Z'
  1111. end
  1112. it 'appends title element with title' do
  1113. account = Fabricate(:account, username: 'username')
  1114. status = Fabricate(:status, account: account)
  1115. object = OStatus::AtomSerializer.new.object(status)
  1116. expect(object.title.text).to eq 'New status by username'
  1117. end
  1118. it 'appends author element with account' do
  1119. account = Fabricate(:account, username: 'username')
  1120. status = Fabricate(:status, account: account)
  1121. entry = OStatus::AtomSerializer.new.object(status)
  1122. expect(entry.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username'
  1123. end
  1124. it 'appends activity:object-type element with object type' do
  1125. status = Fabricate(:status)
  1126. entry = OStatus::AtomSerializer.new.object(status)
  1127. object_type = entry.nodes.find { |node| node.name == 'activity:object-type' }
  1128. expect(object_type.text).to eq TagManager::TYPES[:note]
  1129. end
  1130. it 'appends activity:verb element with verb' do
  1131. status = Fabricate(:status)
  1132. entry = OStatus::AtomSerializer.new.object(status)
  1133. object_type = entry.nodes.find { |node| node.name == 'activity:verb' }
  1134. expect(object_type.text).to eq TagManager::VERBS[:post]
  1135. end
  1136. it 'appends link element for an alternative' do
  1137. account = Fabricate(:account, username: 'username')
  1138. status = Fabricate(:status, account: account)
  1139. entry = OStatus::AtomSerializer.new.object(status)
  1140. link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' && node[:type] == 'text/html' }
  1141. expect(link[:type]).to eq 'text/html'
  1142. expect(link[:href]).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}"
  1143. end
  1144. it 'appends thr:in-reply-to element if it is a reply and thread is not nil' do
  1145. account = Fabricate(:account, username: 'username')
  1146. thread = Fabricate(:status, account: account, created_at: '2000-01-01T00:00:00Z')
  1147. reply = Fabricate(:status, thread: thread)
  1148. entry = OStatus::AtomSerializer.new.object(reply)
  1149. in_reply_to = entry.nodes.find { |node| node.name == 'thr:in-reply-to' }
  1150. expect(in_reply_to.ref).to eq "https://cb6e6126.ngrok.io/users/#{thread.account.to_param}/statuses/#{thread.id}"
  1151. expect(in_reply_to.href).to eq "https://cb6e6126.ngrok.io/@username/#{thread.id}"
  1152. end
  1153. it 'does not append thr:in-reply-to element if thread is nil' do
  1154. status = Fabricate(:status, thread: nil)
  1155. entry = OStatus::AtomSerializer.new.object(status)
  1156. entry.nodes.each { |node| expect(node.name).not_to eq 'thr:in-reply-to' }
  1157. end
  1158. it 'does not append ostatus:conversation element if conversation_id is nil' do
  1159. status = Fabricate.build(:status, conversation_id: nil)
  1160. status.save!(validate: false)
  1161. entry = OStatus::AtomSerializer.new.object(status)
  1162. entry.nodes.each { |node| expect(node.name).not_to eq 'ostatus:conversation' }
  1163. end
  1164. it 'appends ostatus:conversation element if conversation_id is not nil' do
  1165. status = Fabricate(:status)
  1166. status.conversation.update!(created_at: '2000-01-01T00:00:00Z')
  1167. entry = OStatus::AtomSerializer.new.object(status)
  1168. conversation = entry.nodes.find { |node| node.name == 'ostatus:conversation' }
  1169. expect(conversation[:ref]).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.conversation.id}:objectType=Conversation"
  1170. end
  1171. end
  1172. end