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