about summary refs log tree commit diff
path: root/spec/services/process_interaction_service_spec.rb
blob: 3ea7aec59bca6f8ef4c8660ad39181dc907f7196 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'rails_helper'

RSpec.describe ProcessInteractionService do
  let(:receiver) { Fabricate(:user, email: 'alice@example.com', account: Fabricate(:account, username: 'alice')).account }
  let(:sender)   { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  let(:remote_sender) { Fabricate(:account, username: 'carol', domain: 'localdomain.com', uri: 'https://webdomain.com/users/carol') }

  subject { ProcessInteractionService.new }

  describe 'status delete slap' do
    let(:remote_status) { Fabricate(:status, account: remote_sender) }
    let(:envelope) { OStatus2::Salmon.new.pack(payload, sender.keypair) }
    let(:payload) {
      <<~XML
        <entry xmlns="http://www.w3.org/2005/Atom" xmlns:activity="http://activitystrea.ms/spec/1.0/">
          <author>
            <email>carol@localdomain.com</email>
            <name>carol</name>
            <uri>https://webdomain.com/users/carol</uri>
          </author>

          <id>#{remote_status.id}</id>
          <activity:verb>http://activitystrea.ms/schema/1.0/delete</activity:verb>
        </entry>
      XML
    }

    before do
      receiver.update(locked: true)
      remote_sender.update(private_key: sender.private_key, public_key: remote_sender.public_key)
    end

    it 'deletes a record' do
      expect(RemovalWorker).to receive(:perform_async).with(remote_status.id)
      subject.call(envelope, receiver)
    end
  end

  describe 'follow request slap' do
    before do
      receiver.update(locked: true)

      payload = <<XML
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:activity="http://activitystrea.ms/spec/1.0/">
  <author>
    <name>bob</name>
    <uri>https://cb6e6126.ngrok.io/users/bob</uri>
  </author>

  <id>someIdHere</id>
  <activity:verb>http://activitystrea.ms/schema/1.0/request-friend</activity:verb>
</entry>
XML

      envelope = OStatus2::Salmon.new.pack(payload, sender.keypair)
      subject.call(envelope, receiver)
    end

    it 'creates a record' do
      expect(FollowRequest.find_by(account: sender, target_account: receiver)).to_not be_nil
    end
  end

  describe 'follow request slap from known remote user identified by email' do
    before do
      receiver.update(locked: true)
      # Copy already-generated key
      remote_sender.update(private_key: sender.private_key, public_key: remote_sender.public_key)

      payload = <<XML
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:activity="http://activitystrea.ms/spec/1.0/">
  <author>
    <email>carol@localdomain.com</email>
    <name>carol</name>
    <uri>https://webdomain.com/users/carol</uri>
  </author>

  <id>someIdHere</id>
  <activity:verb>http://activitystrea.ms/schema/1.0/request-friend</activity:verb>
</entry>
XML

      envelope = OStatus2::Salmon.new.pack(payload, remote_sender.keypair)
      subject.call(envelope, receiver)
    end

    it 'creates a record' do
      expect(FollowRequest.find_by(account: remote_sender, target_account: receiver)).to_not be_nil
    end
  end

  describe 'follow request authorization slap' do
    before do
      receiver.update(locked: true)
      FollowRequest.create(account: sender, target_account: receiver)

      payload = <<XML
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:activity="http://activitystrea.ms/spec/1.0/">
  <author>
    <name>alice</name>
    <uri>https://cb6e6126.ngrok.io/users/alice</uri>
  </author>

  <id>someIdHere</id>
  <activity:verb>http://activitystrea.ms/schema/1.0/authorize</activity:verb>
</entry>
XML

      envelope = OStatus2::Salmon.new.pack(payload, receiver.keypair)
      subject.call(envelope, sender)
    end

    it 'creates a follow relationship' do
      expect(Follow.find_by(account: sender, target_account: receiver)).to_not be_nil
    end

    it 'removes the follow request' do
      expect(FollowRequest.find_by(account: sender, target_account: receiver)).to be_nil
    end
  end

  describe 'follow request rejection slap' do
    before do
      receiver.update(locked: true)
      FollowRequest.create(account: sender, target_account: receiver)

      payload = <<XML
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:activity="http://activitystrea.ms/spec/1.0/">
  <author>
    <name>alice</name>
    <uri>https://cb6e6126.ngrok.io/users/alice</uri>
  </author>

  <id>someIdHere</id>
  <activity:verb>http://activitystrea.ms/schema/1.0/reject</activity:verb>
</entry>
XML

      envelope = OStatus2::Salmon.new.pack(payload, receiver.keypair)
      subject.call(envelope, sender)
    end

    it 'does not create a follow relationship' do
      expect(Follow.find_by(account: sender, target_account: receiver)).to be_nil
    end

    it 'removes the follow request' do
      expect(FollowRequest.find_by(account: sender, target_account: receiver)).to be_nil
    end
  end
end