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

109 lines
3.2 KiB

  1. require 'rails_helper'
  2. RSpec.describe VerifyLinkService, type: :service do
  3. subject { described_class.new }
  4. context 'given a local account' do
  5. let(:account) { Fabricate(:account, username: 'alice') }
  6. let(:field) { Account::Field.new(account, 'name' => 'Website', 'value' => 'http://example.com') }
  7. before do
  8. stub_request(:head, 'https://redirect.me/abc').to_return(status: 301, headers: { 'Location' => ActivityPub::TagManager.instance.url_for(account) })
  9. stub_request(:get, 'http://example.com').to_return(status: 200, body: html)
  10. subject.call(field)
  11. end
  12. context 'when a link contains an <a> back' do
  13. let(:html) do
  14. <<-HTML
  15. <!doctype html>
  16. <body>
  17. <a href="#{ActivityPub::TagManager.instance.url_for(account)}" rel="me">Follow me on Mastodon</a>
  18. </body>
  19. HTML
  20. end
  21. it 'marks the field as verified' do
  22. expect(field.verified?).to be true
  23. end
  24. end
  25. context 'when a link contains an <a rel="noopener"> back' do
  26. let(:html) do
  27. <<-HTML
  28. <!doctype html>
  29. <body>
  30. <a href="#{ActivityPub::TagManager.instance.url_for(account)}" rel="noopener me" target="_blank">Follow me on Mastodon</a>
  31. </body>
  32. HTML
  33. end
  34. it 'marks the field as verified' do
  35. expect(field.verified?).to be true
  36. end
  37. end
  38. context 'when a link contains a <link> back' do
  39. let(:html) do
  40. <<-HTML
  41. <!doctype html>
  42. <head>
  43. <link type="text/html" href="#{ActivityPub::TagManager.instance.url_for(account)}" rel="me" />
  44. </head>
  45. HTML
  46. end
  47. it 'marks the field as verified' do
  48. expect(field.verified?).to be true
  49. end
  50. end
  51. context 'when a link goes through a redirect back' do
  52. let(:html) do
  53. <<-HTML
  54. <!doctype html>
  55. <head>
  56. <link type="text/html" href="https://redirect.me/abc" rel="me" />
  57. </head>
  58. HTML
  59. end
  60. it 'marks the field as verified' do
  61. expect(field.verified?).to be true
  62. end
  63. end
  64. context 'when a link does not contain a link back' do
  65. let(:html) { '' }
  66. it 'marks the field as verified' do
  67. expect(field.verified?).to be false
  68. end
  69. end
  70. end
  71. context 'given a remote account' do
  72. let(:account) { Fabricate(:account, username: 'alice', domain: 'example.com', url: 'https://profile.example.com/alice') }
  73. let(:field) { Account::Field.new(account, 'name' => 'Website', 'value' => '<a href="http://example.com" rel="me"><span class="invisible">http://</span><span class="">example.com</span><span class="invisible"></span></a>') }
  74. before do
  75. stub_request(:get, 'http://example.com').to_return(status: 200, body: html)
  76. subject.call(field)
  77. end
  78. context 'when a link contains an <a> back' do
  79. let(:html) do
  80. <<-HTML
  81. <!doctype html>
  82. <body>
  83. <a href="https://profile.example.com/alice" rel="me">Follow me on Mastodon</a>
  84. </body>
  85. HTML
  86. end
  87. it 'marks the field as verified' do
  88. expect(field.verified?).to be true
  89. end
  90. end
  91. end
  92. end