foreman/test/controllers/notification_recipients_controller_test.rb @ 9d43fc71
363cab56 | Ohad Levy | require 'test_helper'
|
|
edd5310f | Dominic Cleal | require 'notifications_test_helper'
|
|
363cab56 | Ohad Levy | ||
class NotificationRecipientsControllerTest < ActionController::TestCase
|
|||
setup do
|
|||
b9c2117e | Dominic Cleal | @request.headers['Accept'] = Mime[:json]
|
|
@request.headers['Content-Type'] = Mime[:json].to_s
|
|||
363cab56 | Ohad Levy | end
|
|
test "should get index" do
|
|||
d041d4bb | Dominic Cleal | get :index, params: { :format => 'json' }, session: set_session_user
|
|
363cab56 | Ohad Levy | assert_response :success
|
|
response = ActiveSupport::JSON.decode(@response.body)
|
|||
assert_empty response['notifications']
|
|||
d6a059c8 | Ohad Levy | assert_equal 0, response['notifications'].size
|
|
363cab56 | Ohad Levy | end
|
|
test "should get index" do
|
|||
notification = add_notification
|
|||
d041d4bb | Dominic Cleal | get :index, params: { :format => 'json' }, session: set_session_user
|
|
363cab56 | Ohad Levy | assert_response :success
|
|
assert_equal notification.notification_blueprint.message, response['notifications'][0]['text']
|
|||
end
|
|||
test "should be able to update seen flag" do
|
|||
add_notification
|
|||
9d43fc71 | Michael Moll | get :index, params: { :format => 'json' }, session: set_session_user
|
|
d041d4bb | Dominic Cleal | put :update, params: { :format => 'json', :id => first_notification,
|
|
:notification_recipient => {:seen => true} }, session: set_session_user
|
|||
363cab56 | Ohad Levy | assert_response :success
|
|
assert response['seen']
|
|||
end
|
|||
test "should be able to delete notification" do
|
|||
add_notification
|
|||
9d43fc71 | Michael Moll | get :index, params: { :format => 'json' }, session: set_session_user
|
|
363cab56 | Ohad Levy | notice_id = first_notification
|
|
assert NotificationRecipient.find(notice_id)
|
|||
d041d4bb | Dominic Cleal | delete :destroy, params: { :id => notice_id }
|
|
363cab56 | Ohad Levy | refute NotificationRecipient.find_by_id(notice_id)
|
|
assert_response :success
|
|||
end
|
|||
test "should get 404 on invalid notification deletion" do
|
|||
9d43fc71 | Michael Moll | get :index, params: { :format => 'json' }, session: set_session_user
|
|
363cab56 | Ohad Levy | notice_id = 1
|
|
refute response['notifications'].map{|n| n['id']}.include?(notice_id)
|
|||
refute NotificationRecipient.find_by_id(notice_id)
|
|||
d041d4bb | Dominic Cleal | delete :destroy, params: { :id => notice_id }
|
|
363cab56 | Ohad Levy | assert_response :not_found
|
|
end
|
|||
test "should not get notifications if settings login is disabled" do
|
|||
SETTINGS[:login] = false
|
|||
9d43fc71 | Michael Moll | get :index, params: { :format => 'json' }
|
|
363cab56 | Ohad Levy | SETTINGS[:login] = true
|
|
assert_response :not_found
|
|||
end
|
|||
d9d745e5 | Ohad Levy | test "should not respond with expired notifications" do
|
|
notification = add_notification
|
|||
notification.update_attribute(:expired_at, Time.now.utc - 48.hours)
|
|||
d041d4bb | Dominic Cleal | get :index, params: { :format => 'json' }, session: set_session_user
|
|
d9d745e5 | Ohad Levy | assert_response :success
|
|
response = ActiveSupport::JSON.decode(@response.body)
|
|||
assert_empty response['notifications']
|
|||
end
|
|||
edd5310f | Dominic Cleal | context "with seeded notification types" do
|
|
include NotificationBlueprintSeeds
|
|||
a36689ab | Ohad Levy | ||
edd5310f | Dominic Cleal | test "notification when host is destroyed" do
|
|
8c6bc83e | Marek Hulan | host = FactoryBot.create(:host)
|
|
edd5310f | Dominic Cleal | assert host.destroy
|
|
d041d4bb | Dominic Cleal | get :index, params: { :format => 'json' }, session: set_session_user
|
|
edd5310f | Dominic Cleal | assert_response :success
|
|
response = ActiveSupport::JSON.decode(@response.body)
|
|||
assert_equal 1, response['notifications'].size
|
|||
assert_equal "#{host} has been deleted successfully", response['notifications'][0]["text"]
|
|||
end
|
|||
a36689ab | Ohad Levy | ||
edd5310f | Dominic Cleal | test "notification when host is built" do
|
|
8c6bc83e | Marek Hulan | host = FactoryBot.create(:host, owner: User.current)
|
|
edd5310f | Dominic Cleal | assert host.update_attribute(:build, true)
|
|
assert host.built
|
|||
d041d4bb | Dominic Cleal | get :index, params: { :format => 'json' }, session: set_session_user
|
|
edd5310f | Dominic Cleal | assert_response :success
|
|
response = ActiveSupport::JSON.decode(@response.body)
|
|||
assert_equal 1, response['notifications'].size
|
|||
assert_equal "#{host} has been provisioned successfully", response['notifications'][0]["text"]
|
|||
end
|
|||
test "notification when host has no owner" do
|
|||
8c6bc83e | Marek Hulan | host = FactoryBot.create(:host, :managed)
|
|
8dcc90be | Marek Hulán | assert host.update_attribute(:owner_id, nil)
|
|
Host::Managed.where(:owner_id => nil).update_all(:owner_type => nil) # owner type must be set by hack because of sti.rb
|
|||
host.reload
|
|||
assert host.update_attribute(:build, false)
|
|||
edd5310f | Dominic Cleal | assert_nil host.owner
|
|
8dcc90be | Marek Hulán | ||
setup_user 'edit', 'hosts'
|
|||
host.stub :owner_suggestion, nil do
|
|||
assert host.built
|
|||
end
|
|||
as_admin do
|
|||
d041d4bb | Dominic Cleal | get :index, params: { :format => 'json' }, session: set_session_user
|
|
8dcc90be | Marek Hulán | end
|
|
edd5310f | Dominic Cleal | assert_response :success
|
|
response = ActiveSupport::JSON.decode(@response.body)
|
|||
assert_equal 1, response['notifications'].size
|
|||
assert_equal "#{host} has no owner set", response['notifications'][0]["text"]
|
|||
end
|
|||
a36689ab | Ohad Levy | end
|
|
ea0d94e6 | Ohad Levy | test 'group mark as read' do
|
|
add_notification
|
|||
query = {user_id: User.current.id, seen: false}
|
|||
assert_equal 1, NotificationRecipient.where(query).count
|
|||
d041d4bb | Dominic Cleal | put :update_group_as_read, params: { :group => 'Testing' }, session: set_session_user
|
|
ea0d94e6 | Ohad Levy | assert_response :success
|
|
assert_equal 1, NotificationRecipient.where(query.merge({seen: true})).count
|
|||
assert_equal 0, NotificationRecipient.where(query).count
|
|||
end
|
|||
test 'group mark as read twice' do
|
|||
add_notification
|
|||
d041d4bb | Dominic Cleal | put :update_group_as_read, params: { :group => 'Testing' }, session: set_session_user
|
|
ea0d94e6 | Ohad Levy | assert_response :success
|
|
d041d4bb | Dominic Cleal | put :update_group_as_read, params: { :group => 'Testing' }, session: set_session_user
|
|
ea0d94e6 | Ohad Levy | assert_response :not_modified
|
|
end
|
|||
test 'invalid group mark as read' do
|
|||
d041d4bb | Dominic Cleal | put :update_group_as_read, params: { :group => 'unknown;INSERT INTO users (user_id, group)' }, session: set_session_user
|
|
ea0d94e6 | Ohad Levy | assert_response :not_modified
|
|
end
|
|||
test 'group mark as read only update the correct group' do
|
|||
add_notification('Group1')
|
|||
add_notification('Group2')
|
|||
query = {user_id: User.current.id, seen: false}
|
|||
assert_equal 2, NotificationRecipient.where(query).count
|
|||
d041d4bb | Dominic Cleal | put :update_group_as_read, params: { :group => 'Group1' }, session: set_session_user
|
|
ea0d94e6 | Ohad Levy | assert_response :success
|
|
assert_equal 1, NotificationRecipient.where(query.merge({seen: true})).count
|
|||
assert_equal 0, NotificationRecipient.where(query).
|
|||
joins(:notification_blueprint).
|
|||
where(notification_blueprints: { group: 'Group1' }).count
|
|||
assert_equal 1, NotificationRecipient.where(query).
|
|||
joins(:notification_blueprint).
|
|||
where(notification_blueprints: { group: 'Group2' }).count
|
|||
end
|
|||
59cdda31 | Gilad Lekner | test 'delete group notifications' do
|
|
add_notification
|
|||
query = {user_id: User.current.id}
|
|||
assert_equal 1, NotificationRecipient.where(query).count
|
|||
delete :destroy_group, params: { :group => 'Testing' }, session: set_session_user
|
|||
assert_response :success
|
|||
assert_equal 0, NotificationRecipient.where(query).count
|
|||
end
|
|||
test 'group delete when multiple groups exists' do
|
|||
add_notification('Group1')
|
|||
add_notification('Group2')
|
|||
query = {user_id: User.current.id}
|
|||
assert_equal 2, NotificationRecipient.where(query).count
|
|||
delete :destroy_group, params: { :group => 'Group1' }, session: set_session_user
|
|||
assert_response :success
|
|||
assert_equal 1, NotificationRecipient.where(query).count
|
|||
assert_equal 0, NotificationRecipient.where(query).
|
|||
joins(:notification_blueprint).
|
|||
where(notification_blueprints: { group: 'Group1' }).count
|
|||
assert_equal 1, NotificationRecipient.where(query).
|
|||
joins(:notification_blueprint).
|
|||
where(notification_blueprints: { group: 'Group2' }).count
|
|||
end
|
|||
363cab56 | Ohad Levy | private
|
|
ea0d94e6 | Ohad Levy | def add_notification(group = 'Testing')
|
|
8c6bc83e | Marek Hulan | type = FactoryBot.create(:notification_blueprint,
|
|
ea0d94e6 | Ohad Levy | :group => group,
|
|
363cab56 | Ohad Levy | :message => 'this test just executed successfully')
|
|
8c6bc83e | Marek Hulan | FactoryBot.create(:notification, :notification_blueprint => type, :audience => 'global')
|
|
363cab56 | Ohad Levy | end
|
|
def response
|
|||
ActiveSupport::JSON.decode(@response.body)
|
|||
end
|
|||
def first_notification
|
|||
response['notifications'].first['id']
|
|||
end
|
|||
end
|