fetch_remote_key_service_spec.rb 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ActivityPub::FetchRemoteKeyService, type: :service do
  4. subject { described_class.new }
  5. let(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice' }] } }
  6. let(:public_key_pem) do
  7. <<~TEXT
  8. -----BEGIN PUBLIC KEY-----
  9. MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu3L4vnpNLzVH31MeWI39
  10. 4F0wKeJFsLDAsNXGeOu0QF2x+h1zLWZw/agqD2R3JPU9/kaDJGPIV2Sn5zLyUA9S
  11. 6swCCMOtn7BBR9g9sucgXJmUFB0tACH2QSgHywMAybGfmSb3LsEMNKsGJ9VsvYoh
  12. 8lDET6X4Pyw+ZJU0/OLo/41q9w+OrGtlsTm/PuPIeXnxa6BLqnDaxC+4IcjG/FiP
  13. ahNCTINl/1F/TgSSDZ4Taf4U9XFEIFw8wmgploELozzIzKq+t8nhQYkgAkt64euW
  14. pva3qL5KD1mTIZQEP+LZvh3s2WHrLi3fhbdRuwQ2c0KkJA2oSTFPDpqqbPGZ3Qvu
  15. HQIDAQAB
  16. -----END PUBLIC KEY-----
  17. TEXT
  18. end
  19. let(:public_key_id) { 'https://example.com/alice#main-key' }
  20. let(:key_json) do
  21. {
  22. id: public_key_id,
  23. owner: 'https://example.com/alice',
  24. publicKeyPem: public_key_pem,
  25. }
  26. end
  27. let(:actor_public_key) { key_json }
  28. let(:actor) do
  29. {
  30. '@context': [
  31. 'https://www.w3.org/ns/activitystreams',
  32. 'https://w3id.org/security/v1',
  33. ],
  34. id: 'https://example.com/alice',
  35. type: 'Person',
  36. preferredUsername: 'alice',
  37. name: 'Alice',
  38. summary: 'Foo bar',
  39. inbox: 'http://example.com/alice/inbox',
  40. publicKey: actor_public_key,
  41. }
  42. end
  43. before do
  44. stub_request(:get, 'https://example.com/alice').to_return(body: Oj.dump(actor))
  45. stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
  46. end
  47. describe '#call' do
  48. let(:account) { subject.call(public_key_id) }
  49. context 'when the key is a sub-object from the actor' do
  50. before do
  51. stub_request(:get, public_key_id).to_return(body: Oj.dump(actor))
  52. end
  53. it 'returns the expected account' do
  54. expect(account.uri).to eq 'https://example.com/alice'
  55. end
  56. end
  57. context 'when the key is a separate document' do
  58. let(:public_key_id) { 'https://example.com/alice-public-key.json' }
  59. before do
  60. stub_request(:get, public_key_id).to_return(body: Oj.dump(key_json.merge({ '@context': ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'] })))
  61. end
  62. it 'returns the expected account' do
  63. expect(account.uri).to eq 'https://example.com/alice'
  64. end
  65. end
  66. context 'when the key and owner do not match' do
  67. let(:public_key_id) { 'https://example.com/fake-public-key.json' }
  68. let(:actor_public_key) { 'https://example.com/alice-public-key.json' }
  69. before do
  70. stub_request(:get, public_key_id).to_return(body: Oj.dump(key_json.merge({ '@context': ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'] })))
  71. end
  72. it 'returns the nil' do
  73. expect(account).to be_nil
  74. end
  75. end
  76. end
  77. end