Skip to content

A platform for community discussion. Free, open, simple.

Rewrite push notifications controller specs as request specs.

Guo Xiang Tan21007a4a8d76
diff --git a/spec/controllers/push_notification_controller_spec.rb b/spec/controllers/push_notification_controller_spec.rb
deleted file mode 100644
index 06569214e1f..00000000000
--- a/spec/controllers/push_notification_controller_spec.rb
+++ /dev/null
@@ -1,48 +0,0 @@
-require 'rails_helper'
-
-describe PushNotificationController do
-
- context "logged out" do
- it "should not allow subscribe" do
- get :subscribe, params: { username: "test", subscription: { endpoint: "endpoint", keys: { p256dh: "256dh", auth: "auth" } }, send_confirmation: false, format: :json }, format: :json
- expect(response).not_to be_success
- json = JSON.parse(response.body)
-
- expect(response).not_to be_success
- end
- end
-
- context "logged in" do
-
- let(:user) { log_in }
-
- it "should subscribe" do
- get :subscribe, params: { username: user.username, subscription: { endpoint: "endpoint", keys: { p256dh: "256dh", auth: "auth" } }, send_confirmation: false, format: :json }, format: :json
- expect(response).to be_success
- json = JSON.parse(response.body)
-
- expect(response).to be_success
- end
-
- it "should unsubscribe with existing subscription" do
- sub = { endpoint: "endpoint", keys: { p256dh: "256dh", auth: "auth" } }
- PushSubscription.create(user: user, data: sub.to_json)
-
- get :unsubscribe, params: { username: user.username, subscription: sub, format: :json }, format: :json
- expect(response).to be_success
- json = JSON.parse(response.body)
-
- expect(response).to be_success
- end
-
- it "should unsubscribe without subscription" do
-
- get :unsubscribe, params: { username: user.username, subscription: { endpoint: "endpoint", keys: { p256dh: "256dh", auth: "auth" } }, format: :json }, format: :json
- expect(response).to be_success
- json = JSON.parse(response.body)
-
- expect(response).to be_success
- end
- end
-
-end
diff --git a/spec/requests/push_notification_controller_spec.rb b/spec/requests/push_notification_controller_spec.rb
new file mode 100644
index 00000000000..ef89ff9ed9a
--- /dev/null
+++ b/spec/requests/push_notification_controller_spec.rb
@@ -0,0 +1,74 @@
+require 'rails_helper'
+
+describe PushNotificationController do
+ let(:user) { Fabricate(:user) }
+
+ context "logged out" do
+ it "should not allow subscribe" do
+ post '/push_notifications/subscribe.json', params: {
+ username: "test",
+ subscription: {
+ endpoint: "endpoint",
+ keys: {
+ p256dh: "256dh",
+ auth: "auth"
+ }
+ },
+ send_confirmation: false
+ }
+
+ expect(response.status).to eq(403)
+ end
+ end
+
+ context "logged in" do
+ before { sign_in(user) }
+
+ it "should subscribe" do
+ post '/push_notifications/subscribe.json', params: {
+ username: user.username,
+ subscription: {
+ endpoint: "endpoint",
+ keys: {
+ p256dh: "256dh",
+ auth: "auth"
+ }
+ },
+ send_confirmation: false
+ }
+
+ expect(response.status).to eq(200)
+ expect(user.push_subscriptions.count).to eq(1)
+ end
+
+ it "should unsubscribe with existing subscription" do
+ sub = { endpoint: "endpoint", keys: { p256dh: "256dh", auth: "auth" } }
+ PushSubscription.create!(user: user, data: sub.to_json)
+
+ post '/push_notifications/unsubscribe.json', params: {
+ username: user.username,
+ subscription: sub
+ }
+
+ expect(response.status).to eq(200)
+ expect(user.push_subscriptions).to eq([])
+ end
+
+ it "should unsubscribe without subscription" do
+ post '/push_notifications/unsubscribe.json', params: {
+ username: user.username,
+ subscription: {
+ endpoint: "endpoint",
+ keys: {
+ p256dh: "256dh",
+ auth: "auth"
+ }
+ }
+ }
+
+ expect(response.status).to eq(200)
+ expect(user.push_subscriptions).to eq([])
+ end
+ end
+
+end