discourse/spec/requests/push_notification_controller_spec.rb
Daniel Waterworth 6e161d3e75
DEV: Allow fab! without block ()
The most common thing that we do with fab! is:

    fab!(:thing) { Fabricate(:thing) }

This commit adds a shorthand for this which is just simply:

    fab!(:thing)

i.e. If you omit the block, then, by default, you'll get a `Fabricate`d object using the fabricator of the same name.
2023-11-09 16:47:59 -06:00

112 lines
3.0 KiB
Ruby

# frozen_string_literal: true
RSpec.describe PushNotificationController do
fab!(:user)
context "when 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 "when 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 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
2.times do
post "/push_notifications/subscribe.json",
params: {
username: user.username,
subscription: {
endpoint: "endpoint",
keys: {
p256dh: "256dh",
auth: "auth",
},
},
send_confirmation: false,
}
end
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