1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-20 14:54:15 +01:00

(test) abuses, age_range, categories

This commit is contained in:
Sylvain 2023-01-06 17:18:44 +01:00
parent 3289073e85
commit b14329497b
7 changed files with 201 additions and 27 deletions

View File

@ -1,6 +1,6 @@
user_is_admin = (current_user and current_user.admin?)
# frozen_string_literal: true
json.array!(@age_ranges) do |ar|
json.extract! ar, :id, :name
json.related_to ar.events.count if user_is_admin
json.related_to ar.events.count if current_user&.admin?
end

View File

@ -0,0 +1,10 @@
abuse_1:
id: 1
signaled_id: 1
signaled_type: 'Project'
first_name: 'Jean'
last_name: 'Dupont'
email: 'jean.dupont@gmail.com'
message: 'Ce projet est offensant pour ma religion'
created_at: '2023-01-06 16:39:40.584472'
updated_at: '2023-01-06 16:39:40.584472'

View File

@ -3,19 +3,6 @@
require 'test_helper'
class AbusesTest < ActionDispatch::IntegrationTest
# Called before every test method runs. Can be used
# to set up fixture information.
def setup
# Do nothing
end
# Called after every test method runs. Can be used to tear
# down fixture information.
def teardown
# Do nothing
end
# Abuse report
test 'visitor report an abuse' do
project = Project.take
@ -43,7 +30,9 @@ class AbusesTest < ActionDispatch::IntegrationTest
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'), attached_object_type: 'Abuse', attached_object_id: abuse[:reporting][:id])
notifications = Notification.where(notification_type_id: NotificationType.find_by_name('notify_admin_abuse_reported'), # rubocop:disable Rails/DynamicFindBy
attached_object_type: 'Abuse',
attached_object_id: abuse[:reporting][:id])
assert_not_empty notifications, 'no notifications were created'
notified_users_ids = notifications.map(&:receiver_id)
User.admins.each do |adm|
@ -69,6 +58,40 @@ class AbusesTest < ActionDispatch::IntegrationTest
headers: default_headers
assert_equal 422, response.status, response.body
assert_match /can't be blank/, response.body
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
assert_equal Mime[:json], response.content_type
# 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
end
end

View File

@ -76,8 +76,8 @@ class AccountingPeriodTest < ActionDispatch::IntegrationTest
end
test 'admin tries to close today' do
start_at = Date.today.beginning_of_day.iso8601
end_at = Date.today.end_of_day.iso8601
start_at = DateTime.current.beginning_of_day.iso8601
end_at = DateTime.current.end_of_day.iso8601
post '/api/accounting_periods',
params: {
@ -110,6 +110,17 @@ class AccountingPeriodTest < ActionDispatch::IntegrationTest
period_id = AccountingPeriod.first.id
get "/api/accounting_periods/#{period_id}/archive"
assert_match /^attachment; filename=/, response.headers['Content-Disposition']
assert_match(/^attachment; filename=/, response.headers['Content-Disposition'])
end
test 'list all periods' do
get '/api/accounting_periods'
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the periods
periods = json_response(response.body)
assert_equal AccountingPeriod.count, periods.length
end
end

View File

@ -1,5 +1,7 @@
# frozen_string_literal: true
require 'test_helper'
class AdminsTest < ActionDispatch::IntegrationTest
# Called before every test method runs. Can be used
# to set up fixture information.
@ -8,13 +10,6 @@ class AdminsTest < ActionDispatch::IntegrationTest
login_as(@admin, scope: :user)
end
# Called after every test method runs. Can be used to tear
# down fixture information.
def teardown
# Do nothing
end
test 'create an admin' do
post '/api/admins',
params: {
@ -67,4 +62,10 @@ class AdminsTest < ActionDispatch::IntegrationTest
admins[:admins][0][:profile_attributes][:user_avatar][:id],
'admin avatar does not match'
end
test 'admin cannot delete himself' do
delete "/api/admins/#{@admin.id}"
assert_response :unauthorized
end
end

View File

@ -0,0 +1,64 @@
# frozen_string_literal: true
require 'test_helper'
class AgeRangesTest < ActionDispatch::IntegrationTest
def setup
@admin = User.find_by(username: 'admin')
login_as(@admin, scope: :user)
end
test 'create an age range' do
post '/api/age_ranges',
params: {
name: '8 - 10 ans'
}.to_json,
headers: default_headers
# Check response format & status
assert_equal 201, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the correct age range was created
res = json_response(response.body)
range = AgeRange.where(id: res[:id]).first
assert_not_nil range, 'range was not created in database'
assert_equal '8 - 10 ans', res[:name]
end
test 'update an age range' do
patch '/api/age_ranges/1',
params: {
name: "Jusqu'à 17 ans"
}.to_json,
headers: default_headers
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the age range was updated
res = json_response(response.body)
assert_equal 1, res[:id]
assert_equal res[:name], "Jusqu'à 17 ans"
end
test 'list all age ranges' do
get '/api/age_ranges'
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the list items are ok
ranges = json_response(response.body)
assert_equal AgeRange.count, ranges.count
end
test 'delete an age range' do
delete '/api/age_ranges/1'
assert_response :success
assert_empty response.body
end
end

View File

@ -0,0 +1,65 @@
# frozen_string_literal: true
require 'test_helper'
class CategoriesTest < ActionDispatch::IntegrationTest
def setup
@admin = User.find_by(username: 'admin')
login_as(@admin, scope: :user)
end
test 'create a category' do
post '/api/categories',
params: {
name: 'Workshop'
}.to_json,
headers: default_headers
# Check response format & status
assert_equal 201, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the correct category was created
res = json_response(response.body)
range = Category.where(id: res[:id]).first
assert_not_nil range, 'category was not created in database'
assert_equal 'Workshop', res[:name]
end
test 'update a category' do
patch '/api/categories/1',
params: {
name: 'Stage pratique'
}.to_json,
headers: default_headers
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the category was updated
res = json_response(response.body)
assert_equal 1, res[:id]
assert_equal res[:name], 'Stage pratique'
end
test 'list all categories' do
get '/api/categories'
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the list items are ok
cats = json_response(response.body)
assert_equal Category.count, cats.count
end
test 'delete a category' do
cat = Category.create!(name: 'delete me')
delete "/api/categories/#{cat.id}"
assert_response :success
assert_empty response.body
end
end