2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe PushNotificationController do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2018-05-07 03:40:46 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when logged out" do
|
2018-05-07 03:40:46 -04:00
|
|
|
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
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when logged in" do
|
2018-05-07 03:40:46 -04:00
|
|
|
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
|
|
|
|
|
2018-05-08 18:14:58 -04:00
|
|
|
it "should fix duplicate subscriptions" do
|
|
|
|
subscription = {
|
|
|
|
endpoint: "endpoint",
|
|
|
|
keys: {
|
|
|
|
p256dh: "256dh",
|
|
|
|
auth: "auth"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PushSubscription.create user: user, data: subscription.to_json
|
|
|
|
post '/push_notifications/subscribe.json', params: {
|
|
|
|
username: user.username,
|
|
|
|
subscription: subscription,
|
|
|
|
send_confirmation: false
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(user.push_subscriptions.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not create duplicate subscriptions" do
|
2018-05-08 20:14:14 -04:00
|
|
|
2.times do
|
|
|
|
post '/push_notifications/subscribe.json', params: {
|
|
|
|
username: user.username,
|
|
|
|
subscription: {
|
|
|
|
endpoint: "endpoint",
|
|
|
|
keys: {
|
|
|
|
p256dh: "256dh",
|
|
|
|
auth: "auth"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
send_confirmation: false
|
|
|
|
}
|
|
|
|
end
|
2018-05-08 18:14:58 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(user.push_subscriptions.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2018-05-07 03:40:46 -04:00
|
|
|
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
|