Feature: Add web push notification permission in frontend (#766)

Add webpush notification permission in frontend

Co-authored-by: Sojan <sojan@pepalo.com>
This commit is contained in:
Pranav Raj S
2020-05-06 00:10:56 +05:30
committed by GitHub
parent 5bd7a4c511
commit e9131ea558
37 changed files with 651 additions and 318 deletions

View File

@@ -46,7 +46,7 @@ describe ::ContactMergeAction do
expect do
described_class.new(account: new_account, base_contact: base_contact,
mergee_contact: mergee_contact).perform
end .to raise_error('contact does not belong to the account')
end.to raise_error('contact does not belong to the account')
end
end
end

View File

@@ -70,7 +70,7 @@ RSpec.describe 'Contacts API', type: :request do
expect do
post "/api/v1/accounts/#{account.id}/contacts", headers: admin.create_new_auth_token,
params: valid_params
end .to change(Contact, :count).by(1)
end.to change(Contact, :count).by(1)
expect(response).to have_http_status(:success)
end

View File

@@ -17,14 +17,66 @@ RSpec.describe 'Notifications Subscriptions API', type: :request do
it 'creates a notification subscriptions' do
post '/api/v1/notification_subscriptions',
params: { notification_subscription: { subscription_type: 'browser_push', 'subscription_attributes': { test: 'test' } } },
params: {
notification_subscription: {
subscription_type: 'browser_push',
'subscription_attributes': {
endpoint: 'test',
p256dh: 'test',
auth: 'test'
}
}
},
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['subscription_type']).to eq('browser_push')
expect(json_response['subscription_attributes']).to eq({ 'test' => 'test' })
expect(json_response['subscription_attributes']['auth']).to eq('test')
end
it 'returns existing notification subscription if subscription exists' do
subscription = create(:notification_subscription, user: agent)
post '/api/v1/notification_subscriptions',
params: {
notification_subscription: {
subscription_type: 'browser_push',
'subscription_attributes': {
endpoint: 'test',
p256dh: 'test',
auth: 'test'
}
}
},
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['id']).to eq(subscription.id)
end
it 'move notification subscription to user if its of another user' do
subscription = create(:notification_subscription, user: create(:user))
post '/api/v1/notification_subscriptions',
params: {
notification_subscription: {
subscription_type: 'browser_push',
'subscription_attributes': {
endpoint: 'test',
p256dh: 'test',
auth: 'test'
}
}
},
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['id']).to eq(subscription.id)
expect(json_response['user_id']).to eq(agent.id)
end
end
end

View File

@@ -0,0 +1,10 @@
# frozen_string_literal: true
FactoryBot.define do
factory :notification_subscription do
user
identifier { 'test' }
subscription_type { 'browser_push' }
subscription_attributes { { endpoint: 'test', auth: 'test' } }
end
end

View File

@@ -11,16 +11,11 @@ describe NotificationListener do
describe 'conversation_created' do
let(:event_name) { :'conversation.created' }
before do
creation_mailer = double
allow(AgentNotifications::ConversationNotificationsMailer).to receive(:conversation_creation).and_return(creation_mailer)
allow(creation_mailer).to receive(:deliver_later).and_return(true)
end
context 'when conversation is created' do
it 'sends email to inbox members who have notifications turned on' do
it 'creates notifications for inbox members who have notifications turned on' do
notification_setting = agent_with_notification.notification_settings.first
notification_setting.selected_email_flags = [:email_conversation_creation]
notification_setting.selected_push_flags = []
notification_setting.save!
create(:inbox_member, user: agent_with_notification, inbox: inbox)
@@ -29,13 +24,13 @@ describe NotificationListener do
event = Events::Base.new(event_name, Time.zone.now, conversation: conversation)
listener.conversation_created(event)
expect(AgentNotifications::ConversationNotificationsMailer).to have_received(:conversation_creation)
.with(conversation, agent_with_notification)
expect(notification_setting.user.notifications.count).to eq(1)
end
it 'does not send and email to inbox members who have notifications turned off' do
notification_setting = agent_with_notification.notification_settings.first
it 'does not create notification for inbox members who have notifications turned off' do
notification_setting = agent_with_out_notification.notification_settings.first
notification_setting.unselect_all_email_flags
notification_setting.unselect_all_push_flags
notification_setting.save!
create(:inbox_member, user: agent_with_out_notification, inbox: inbox)
@@ -44,8 +39,7 @@ describe NotificationListener do
event = Events::Base.new(event_name, Time.zone.now, conversation: conversation)
listener.conversation_created(event)
expect(AgentNotifications::ConversationNotificationsMailer).not_to have_received(:conversation_creation)
.with(conversation, agent_with_out_notification)
expect(notification_setting.user.notifications.count).to eq(0)
end
end
end

View File

@@ -122,28 +122,19 @@ RSpec.describe Conversation, type: :model do
expect(conversation.reload.assignee).to eq(agent)
end
it 'send assignment mailer' do
allow(AgentNotifications::ConversationNotificationsMailer).to receive(:conversation_assignment).and_return(assignment_mailer)
allow(assignment_mailer).to receive(:deliver_later)
Current.user = conversation.assignee
it 'creates a new notification for the agent' do
expect(update_assignee).to eq(true)
# send_email_notification_to_assignee
expect(AgentNotifications::ConversationNotificationsMailer).to have_received(:conversation_assignment).with(conversation, agent)
expect(assignment_mailer).to have_received(:deliver_later) if ENV.fetch('SMTP_ADDRESS', nil).present?
expect(agent.notifications.count).to eq(1)
end
it 'does not send assignment mailer if notification setting is turned off' do
allow(AgentNotifications::ConversationNotificationsMailer).to receive(:conversation_assignment).and_return(assignment_mailer)
it 'does not create assignment notification if notification setting is turned off' do
notification_setting = agent.notification_settings.first
notification_setting.unselect_all_email_flags
notification_setting.unselect_all_push_flags
notification_setting.save!
expect(update_assignee).to eq(true)
expect(AgentNotifications::ConversationNotificationsMailer).not_to have_received(:conversation_assignment).with(conversation, agent)
expect(agent.notifications.count).to eq(0)
end
end