1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/test/integration/abuses_test.rb

98 lines
3.3 KiB
Ruby
Raw Normal View History

2020-03-13 17:10:38 +01:00
# frozen_string_literal: true
2016-04-06 14:45:39 +02:00
2020-03-30 10:59:11 +02:00
require 'test_helper'
2020-03-13 17:10:38 +01:00
class AbusesTest < ActionDispatch::IntegrationTest
2016-04-06 14:45:39 +02:00
# Abuse report
test 'visitor report an abuse' do
2016-04-13 10:43:25 +02:00
project = Project.take
2016-04-06 14:45:39 +02:00
post '/api/abuses',
2020-03-13 17:10:38 +01:00
params: {
abuse: {
signaled_type: 'Project',
signaled_id: project.id,
first_name: 'William',
last_name: 'Prindle',
email: 'wprindle@iastate.edu',
message: 'This project is in infringement with the patent US5014921 A.'
}
2016-04-06 14:45:39 +02:00
}.to_json,
2020-03-13 17:10:38 +01:00
headers: default_headers
2016-04-06 14:45:39 +02:00
# Check response format & status
assert_equal 201, response.status, response.body
2023-02-24 17:26:55 +01:00
assert_match Mime[:json].to_s, response.content_type
2016-04-06 14:45:39 +02:00
# Check the correct object was signaled
abuse = json_response(response.body)
assert_equal project.id, abuse[:reporting][:signaled_id], 'project ID mismatch'
assert_equal 'Project', abuse[:reporting][:signaled_type], 'signaled object type mismatch'
# Check notifications were sent for every admins
notifications = Notification.where(notification_type_id: NotificationType.find_by(name: 'notify_admin_abuse_reported'),
2023-01-06 17:18:44 +01:00
attached_object_type: 'Abuse',
attached_object_id: abuse[:reporting][:id])
2016-04-06 14:45:39 +02:00
assert_not_empty notifications, 'no notifications were created'
2020-03-13 17:10:38 +01:00
notified_users_ids = notifications.map(&:receiver_id)
2016-04-06 14:45:39 +02:00
User.admins.each do |adm|
assert_includes notified_users_ids, adm.id, "Admin #{adm.id} was not notified"
end
end
2016-04-06 14:58:53 +02:00
# Incomplete abuse report
test 'visitor send an invalid report' do
project = Project.first
post '/api/abuses',
2020-03-13 17:10:38 +01:00
params: {
abuse: {
signaled_type: 'Project',
signaled_id: project.id,
first_name: 'John',
last_name: 'Wrong',
email: '',
message: ''
}
2016-04-06 14:58:53 +02:00
}.to_json,
2020-03-13 17:10:38 +01:00
headers: default_headers
2016-04-06 14:58:53 +02:00
assert_equal 422, response.status, response.body
2023-01-06 17:18:44 +01:00
assert_match(/can't be blank/, response.body)
end
test 'admin list all abuses' do
login_as(User.admins.first, scope: :user)
get '/api/abuses'
# Check response format & status
assert_equal 200, response.status, response.body
2023-02-24 17:26:55 +01:00
assert_match Mime[:json].to_s, response.content_type
2023-01-06 17:18:44 +01:00
# Check the abuses
abuses = json_response(response.body)
assert_equal Abuse.count, abuses[:abuses].length
assert_not_nil abuses[:abuses].first[:id]
assert_not_nil abuses[:abuses].first[:signaled_type]
assert_not_nil abuses[:abuses].first[:signaled_id]
assert_not_nil abuses[:abuses].first[:first_name]
assert_not_nil abuses[:abuses].first[:last_name]
assert_not_nil abuses[:abuses].first[:email]
assert_not_nil abuses[:abuses].first[:message]
assert_not_nil abuses[:abuses].first[:created_at]
assert_not_nil abuses[:abuses].first[:signaled]
assert_not_nil abuses[:abuses].first[:signaled][:name]
assert_not_nil abuses[:abuses].first[:signaled][:slug]
assert_not_nil abuses[:abuses].first[:signaled][:published_at]
assert_not_nil abuses[:abuses].first[:signaled][:author]
end
test 'admin delete an abuse' do
login_as(User.admins.first, scope: :user)
delete '/api/abuses/1'
assert_response :success
assert_empty response.body
2016-04-06 14:58:53 +02:00
end
2016-04-06 15:22:32 +02:00
end