about summary refs log tree commit diff
path: root/spec/services
diff options
context:
space:
mode:
authorMatt Jankowski <mjankowski@thoughtbot.com>2017-05-08 18:45:02 -0400
committerEugen Rochko <eugen@zeonfederated.com>2017-05-09 00:45:02 +0200
commit5bea42412e41b1121af04dbe6ef5c9bed22219b8 (patch)
treef121a12cd27f567f8d7d7f7066ff302240d24dd2 /spec/services
parent04166c4a35c97f5540252617c7858bf20d315a7e (diff)
Services specs for subscribe and unsubscribe (#2928)
* Add specs for unsubscribe service

* Fix non existent methods in unsubscribe service

* Clean up status handling in subscribe service
Diffstat (limited to 'spec/services')
-rw-r--r--spec/services/subscribe_service_spec.rb5
-rw-r--r--spec/services/unsubscribe_service_spec.rb37
2 files changed, 42 insertions, 0 deletions
diff --git a/spec/services/subscribe_service_spec.rb b/spec/services/subscribe_service_spec.rb
index 01cdb29c0..5db91ad99 100644
--- a/spec/services/subscribe_service_spec.rb
+++ b/spec/services/subscribe_service_spec.rb
@@ -35,4 +35,9 @@ RSpec.describe SubscribeService do
     stub_request(:post, 'http://hub.example.com/').to_return(status: 503)
     expect { subject.call(account) }.to raise_error(/Subscription attempt failed/)
   end
+
+  it 'fails loudly if rate limited' do
+    stub_request(:post, 'http://hub.example.com/').to_return(status: 429)
+    expect { subject.call(account) }.to raise_error(/Subscription attempt failed/)
+  end
 end
diff --git a/spec/services/unsubscribe_service_spec.rb b/spec/services/unsubscribe_service_spec.rb
new file mode 100644
index 000000000..c81772037
--- /dev/null
+++ b/spec/services/unsubscribe_service_spec.rb
@@ -0,0 +1,37 @@
+require 'rails_helper'
+
+RSpec.describe UnsubscribeService do
+  let(:account) { Fabricate(:account, username: 'bob', domain: 'example.com', hub_url: 'http://hub.example.com') }
+  subject { UnsubscribeService.new }
+
+  it 'removes the secret and resets expiration on account' do
+    stub_request(:post, 'http://hub.example.com/').to_return(status: 204)
+    subject.call(account)
+    account.reload
+
+    expect(account.secret).to be_blank
+    expect(account.subscription_expires_at).to be_blank
+  end
+
+  it 'logs error on subscription failure' do
+    logger = stub_logger
+    stub_request(:post, 'http://hub.example.com/').to_return(status: 404)
+    subject.call(account)
+
+    expect(logger).to have_received(:debug).with(/unsubscribe for bob@example.com failed/)
+  end
+
+  it 'logs error on connection failure' do
+    logger = stub_logger
+    stub_request(:post, 'http://hub.example.com/').to_raise(HTTP::Error)
+    subject.call(account)
+
+    expect(logger).to have_received(:debug).with(/PuSH subscription request for bob@example.com could not be made due to HTTP or SSL error/)
+  end
+
+  def stub_logger
+    double(debug: nil).tap do |logger|
+      allow(Rails).to receive(:logger).and_return(logger)
+    end
+  end
+end