Rewrite push notifications controller specs as request specs.

* Improve assertions to test for the outcome we expected instead
  of just asserting for a 200 response.

* Remove duplicated assertion.
This commit is contained in:
Guo Xiang Tan 2018-05-07 15:40:46 +08:00
parent 32147d4ff9
commit 21007a4a8d
2 changed files with 74 additions and 48 deletions

View File

@ -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

View File

@ -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