fetch_remote_key_service_spec.rb 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. require 'rails_helper'
  2. RSpec.describe ActivityPub::FetchRemoteKeyService, type: :service do
  3. subject { ActivityPub::FetchRemoteKeyService.new }
  4. let(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice' }] } }
  5. let(:public_key_pem) do
  6. "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu3L4vnpNLzVH31MeWI39\n4F0wKeJFsLDAsNXGeOu0QF2x+h1zLWZw/agqD2R3JPU9/kaDJGPIV2Sn5zLyUA9S\n6swCCMOtn7BBR9g9sucgXJmUFB0tACH2QSgHywMAybGfmSb3LsEMNKsGJ9VsvYoh\n8lDET6X4Pyw+ZJU0/OLo/41q9w+OrGtlsTm/PuPIeXnxa6BLqnDaxC+4IcjG/FiP\nahNCTINl/1F/TgSSDZ4Taf4U9XFEIFw8wmgploELozzIzKq+t8nhQYkgAkt64euW\npva3qL5KD1mTIZQEP+LZvh3s2WHrLi3fhbdRuwQ2c0KkJA2oSTFPDpqqbPGZ3Qvu\nHQIDAQAB\n-----END PUBLIC KEY-----\n"
  7. end
  8. let(:public_key_id) { 'https://example.com/alice#main-key' }
  9. let(:key_json) do
  10. {
  11. id: public_key_id,
  12. owner: 'https://example.com/alice',
  13. publicKeyPem: public_key_pem,
  14. }
  15. end
  16. let(:actor_public_key) { key_json }
  17. let(:actor) do
  18. {
  19. '@context': [
  20. 'https://www.w3.org/ns/activitystreams',
  21. 'https://w3id.org/security/v1',
  22. ],
  23. id: 'https://example.com/alice',
  24. type: 'Person',
  25. preferredUsername: 'alice',
  26. name: 'Alice',
  27. summary: 'Foo bar',
  28. inbox: 'http://example.com/alice/inbox',
  29. publicKey: actor_public_key,
  30. }
  31. end
  32. before do
  33. stub_request(:get, 'https://example.com/alice').to_return(body: Oj.dump(actor), headers: { 'Content-Type': 'application/activity+json' })
  34. 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' })
  35. end
  36. describe '#call' do
  37. let(:account) { subject.call(public_key_id) }
  38. context 'when the key is a sub-object from the actor' do
  39. before do
  40. stub_request(:get, public_key_id).to_return(body: Oj.dump(actor), headers: { 'Content-Type': 'application/activity+json' })
  41. end
  42. it 'returns the expected account' do
  43. expect(account.uri).to eq 'https://example.com/alice'
  44. end
  45. end
  46. context 'when the key is a separate document' do
  47. let(:public_key_id) { 'https://example.com/alice-public-key.json' }
  48. before do
  49. 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'] })), headers: { 'Content-Type': 'application/activity+json' })
  50. end
  51. it 'returns the expected account' do
  52. expect(account.uri).to eq 'https://example.com/alice'
  53. end
  54. end
  55. context 'when the key and owner do not match' do
  56. let(:public_key_id) { 'https://example.com/fake-public-key.json' }
  57. let(:actor_public_key) { 'https://example.com/alice-public-key.json' }
  58. before do
  59. 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'] })), headers: { 'Content-Type': 'application/activity+json' })
  60. end
  61. it 'returns the nil' do
  62. expect(account).to be_nil
  63. end
  64. end
  65. end
  66. end