about summary refs log tree commit diff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/admin/settings_controller_spec.rb4
-rw-r--r--spec/controllers/settings/identity_proofs_controller_spec.rb57
-rw-r--r--spec/lib/activitypub/activity/create_spec.rb4
-rw-r--r--spec/lib/activitypub/adapter_spec.rb88
-rw-r--r--spec/services/activitypub/process_account_service_spec.rb45
5 files changed, 191 insertions, 7 deletions
diff --git a/spec/controllers/admin/settings_controller_spec.rb b/spec/controllers/admin/settings_controller_spec.rb
index 34f6bbdae..6cf0ee20a 100644
--- a/spec/controllers/admin/settings_controller_spec.rb
+++ b/spec/controllers/admin/settings_controller_spec.rb
@@ -19,6 +19,10 @@ RSpec.describe Admin::SettingsController, type: :controller do
     end
 
     describe 'PUT #update' do
+      before do
+        allow_any_instance_of(Form::AdminSettings).to receive(:valid?).and_return(true)
+      end
+
       describe 'for a record that doesnt exist' do
         around do |example|
           before = Setting.site_extended_description
diff --git a/spec/controllers/settings/identity_proofs_controller_spec.rb b/spec/controllers/settings/identity_proofs_controller_spec.rb
index 46af3ccf4..5c05eb83c 100644
--- a/spec/controllers/settings/identity_proofs_controller_spec.rb
+++ b/spec/controllers/settings/identity_proofs_controller_spec.rb
@@ -1,6 +1,7 @@
 require 'rails_helper'
 
 describe Settings::IdentityProofsController do
+  include RoutingHelper
   render_views
 
   let(:user) { Fabricate(:user) }
@@ -9,8 +10,15 @@ describe Settings::IdentityProofsController do
   let(:provider) { 'keybase' }
   let(:findable_id) { Faker::Number.number(5) }
   let(:unfindable_id) { Faker::Number.number(5) }
+  let(:new_proof_params) do
+    { provider: provider, provider_username: kbname, token: valid_token, username: user.account.username }
+  end
+  let(:status_text) { "i just proved that i am also #{kbname} on #{provider}." }
+  let(:status_posting_params) do
+    { post_status: '0', status_text: status_text }
+  end
   let(:postable_params) do
-    { account_identity_proof: { provider: provider, provider_username: kbname, token: valid_token } }
+    { account_identity_proof: new_proof_params.merge(status_posting_params) }
   end
 
   before do
@@ -19,10 +27,32 @@ describe Settings::IdentityProofsController do
   end
 
   describe 'new proof creation' do
-    context 'GET #new with no existing proofs' do
-      it 'redirects to :index' do
-        get :new
-        expect(response).to redirect_to settings_identity_proofs_path
+    context 'GET #new' do
+      context 'with all of the correct params' do
+        before do
+          allow_any_instance_of(ProofProvider::Keybase::Badge).to receive(:avatar_url) { full_pack_url('media/images/void.png') }
+        end
+
+        it 'renders the template' do
+          get :new, params: new_proof_params
+          expect(response).to render_template(:new)
+        end
+      end
+
+      context 'without any params' do
+        it 'redirects to :index' do
+          get :new, params: {}
+          expect(response).to redirect_to settings_identity_proofs_path
+        end
+      end
+
+      context 'with params to prove a different, not logged-in user' do
+        let(:wrong_user_params) { new_proof_params.merge(username: 'someone_else') }
+
+        it 'shows a helpful alert' do
+          get :new, params: wrong_user_params
+          expect(flash[:alert]).to eq I18n.t('identity_proofs.errors.wrong_user', proving: 'someone_else', current: user.account.username)
+        end
       end
     end
 
@@ -44,6 +74,23 @@ describe Settings::IdentityProofsController do
           post :create, params: postable_params
           expect(response).to redirect_to root_url
         end
+
+        it 'does not post a status' do
+          expect(PostStatusService).not_to receive(:new)
+          post :create, params: postable_params
+        end
+
+        context 'and the user has requested to post a status' do
+          let(:postable_params_with_status) do
+            postable_params.tap { |p| p[:account_identity_proof][:post_status] = '1' }
+          end
+
+          it 'posts a status' do
+            expect_any_instance_of(PostStatusService).to receive(:call).with(user.account, text: status_text)
+
+            post :create, params: postable_params_with_status
+          end
+        end
       end
 
       context 'when saving fails' do
diff --git a/spec/lib/activitypub/activity/create_spec.rb b/spec/lib/activitypub/activity/create_spec.rb
index 3a1463d95..412609de4 100644
--- a/spec/lib/activitypub/activity/create_spec.rb
+++ b/spec/lib/activitypub/activity/create_spec.rb
@@ -464,7 +464,7 @@ RSpec.describe ActivityPub::Activity::Create do
 
       context 'when a vote to a local poll' do
         let(:poll) { Fabricate(:poll, options: %w(Yellow Blue)) }
-        let!(:local_status) { Fabricate(:status, owned_poll: poll) }
+        let!(:local_status) { Fabricate(:status, poll: poll) }
 
         let(:object_json) do
           {
@@ -489,7 +489,7 @@ RSpec.describe ActivityPub::Activity::Create do
           poll.save(validate: false)
           poll
         end
-        let!(:local_status) { Fabricate(:status, owned_poll: poll) }
+        let!(:local_status) { Fabricate(:status, poll: poll) }
 
         let(:object_json) do
           {
diff --git a/spec/lib/activitypub/adapter_spec.rb b/spec/lib/activitypub/adapter_spec.rb
new file mode 100644
index 000000000..ea03797aa
--- /dev/null
+++ b/spec/lib/activitypub/adapter_spec.rb
@@ -0,0 +1,88 @@
+require 'rails_helper'
+
+RSpec.describe ActivityPub::Adapter do
+  class TestObject < ActiveModelSerializers::Model
+    attributes :foo
+  end
+
+  class TestWithBasicContextSerializer < ActivityPub::Serializer
+    attributes :foo
+  end
+
+  class TestWithNamedContextSerializer < ActivityPub::Serializer
+    context :security
+    attributes :foo
+  end
+
+  class TestWithNestedNamedContextSerializer < ActivityPub::Serializer
+    attributes :foo
+
+    has_one :virtual_object, key: :baz, serializer: TestWithNamedContextSerializer
+
+    def virtual_object
+      object
+    end
+  end
+
+  class TestWithContextExtensionSerializer < ActivityPub::Serializer
+    context_extensions :sensitive
+    attributes :foo
+  end
+
+  class TestWithNestedContextExtensionSerializer < ActivityPub::Serializer
+    context_extensions :manually_approves_followers
+    attributes :foo
+
+    has_one :virtual_object, key: :baz, serializer: TestWithContextExtensionSerializer
+
+    def virtual_object
+      object
+    end
+  end
+
+  describe '#serializable_hash' do
+    let(:serializer_class) {}
+
+    subject { ActiveModelSerializers::SerializableResource.new(TestObject.new(foo: 'bar'), serializer: serializer_class, adapter: described_class).as_json }
+
+    context 'when serializer defines no context' do
+      let(:serializer_class) { TestWithBasicContextSerializer }
+
+      it 'renders a basic @context' do
+        expect(subject).to include({ '@context' => 'https://www.w3.org/ns/activitystreams' })
+      end
+    end
+
+    context 'when serializer defines a named context' do
+      let(:serializer_class) { TestWithNamedContextSerializer }
+
+      it 'renders a @context with both items' do
+        expect(subject).to include({ '@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'] })
+      end
+    end
+
+    context 'when serializer has children that define a named context' do
+      let(:serializer_class) { TestWithNestedNamedContextSerializer }
+
+      it 'renders a @context with both items' do
+        expect(subject).to include({ '@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'] })
+      end
+    end
+
+    context 'when serializer defines context extensions' do
+      let(:serializer_class) { TestWithContextExtensionSerializer }
+
+      it 'renders a @context with the extension' do
+        expect(subject).to include({ '@context' => ['https://www.w3.org/ns/activitystreams', { 'sensitive' => 'as:sensitive' }] })
+      end
+    end
+
+    context 'when serializer has children that define context extensions' do
+      let(:serializer_class) { TestWithNestedContextExtensionSerializer }
+
+      it 'renders a @context with both extensions' do
+        expect(subject).to include({ '@context' => ['https://www.w3.org/ns/activitystreams', { 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers', 'sensitive' => 'as:sensitive' }] })
+      end
+    end
+  end
+end
diff --git a/spec/services/activitypub/process_account_service_spec.rb b/spec/services/activitypub/process_account_service_spec.rb
index d3318b2ed..5141e3f16 100644
--- a/spec/services/activitypub/process_account_service_spec.rb
+++ b/spec/services/activitypub/process_account_service_spec.rb
@@ -28,4 +28,49 @@ RSpec.describe ActivityPub::ProcessAccountService, type: :service do
       expect(account.fields[1].value).to eq 'Unit test'
     end
   end
+
+  context 'identity proofs' do
+    let(:payload) do
+      {
+        id: 'https://foo.test',
+        type: 'Actor',
+        inbox: 'https://foo.test/inbox',
+        attachment: [
+          { type: 'IdentityProof', name: 'Alice', signatureAlgorithm: 'keybase', signatureValue: 'a' * 66 },
+        ],
+      }.with_indifferent_access
+    end
+
+    it 'parses out of attachment' do
+      allow(ProofProvider::Keybase::Worker).to receive(:perform_async)
+
+      account = subject.call('alice', 'example.com', payload)
+
+      expect(account.identity_proofs.count).to eq 1
+
+      proof = account.identity_proofs.first
+
+      expect(proof.provider).to eq 'keybase'
+      expect(proof.provider_username).to eq 'Alice'
+      expect(proof.token).to eq 'a' * 66
+    end
+
+    it 'removes no longer present proofs' do
+      allow(ProofProvider::Keybase::Worker).to receive(:perform_async)
+
+      account   = Fabricate(:account, username: 'alice', domain: 'example.com')
+      old_proof = Fabricate(:account_identity_proof, account: account, provider: 'keybase', provider_username: 'Bob', token: 'b' * 66)
+
+      subject.call('alice', 'example.com', payload)
+
+      expect(account.identity_proofs.count).to eq 1
+      expect(account.identity_proofs.find_by(id: old_proof.id)).to be_nil
+    end
+
+    it 'queues a validity check on the proof' do
+      allow(ProofProvider::Keybase::Worker).to receive(:perform_async)
+      account = subject.call('alice', 'example.com', payload)
+      expect(ProofProvider::Keybase::Worker).to have_received(:perform_async)
+    end
+  end
 end