mirror of
https://github.com/astuto/astuto.git
synced 2025-12-15 11:17:49 +01:00
Add tests to user model and admin panel requests
This commit is contained in:
@@ -32,9 +32,12 @@ module Admin
|
||||
# for more information
|
||||
|
||||
def authenticate_admin
|
||||
super # apply the generic rules for authentication in the admin panel...
|
||||
unless user_signed_in?
|
||||
flash[:alert] = "You must be logged in to access this page."
|
||||
redirect_to new_user_session_path
|
||||
return
|
||||
end
|
||||
|
||||
# ...plus this one
|
||||
unless current_user.admin?
|
||||
flash[:alert] = "You do not have the privilegies to access this page."
|
||||
redirect_to root_path
|
||||
|
||||
@@ -6,7 +6,7 @@ class User < ApplicationRecord
|
||||
enum role: [:user, :moderator, :admin]
|
||||
after_initialize :set_default_role, if: :new_record?
|
||||
|
||||
validates :full_name, presence: true
|
||||
validates :full_name, presence: true, length: { in: 2..32 }
|
||||
|
||||
def set_default_role
|
||||
self.role ||= :user
|
||||
|
||||
@@ -5,4 +5,20 @@ FactoryBot.define do
|
||||
full_name { "First Last" }
|
||||
password { "password" }
|
||||
end
|
||||
|
||||
factory :moderator, class: User do
|
||||
sequence(:email) { |n| "mod#{n}@example.com" }
|
||||
|
||||
full_name { "First Last" }
|
||||
password { "password" }
|
||||
role { "moderator" }
|
||||
end
|
||||
|
||||
factory :admin, class: User do
|
||||
sequence(:email) { |n| "admin#{n}@example.com" }
|
||||
|
||||
full_name { "First Last" }
|
||||
password { "password" }
|
||||
role { "admin" }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,10 +2,38 @@ require 'rails_helper'
|
||||
|
||||
RSpec.describe User, type: :model do
|
||||
|
||||
let(:user) { User.new(email: 'example@example.com', password: 'password') }
|
||||
let(:user) { FactoryBot.build(:user) }
|
||||
let(:nilname_user) { FactoryBot.build(:user, full_name: nil) }
|
||||
let(:emptyname_user) { FactoryBot.build(:user, full_name: "") }
|
||||
let(:short_user) { FactoryBot.build(:user, full_name: "a") }
|
||||
let(:long_user) { FactoryBot.build(:user, full_name: "a" * 33) }
|
||||
|
||||
it 'creates a user with role "user" by default' do
|
||||
expect(user.role).to eq('user')
|
||||
end
|
||||
|
||||
it 'has a non-nil and non-empty full name' do
|
||||
expect(nilname_user.valid?).to be_falsy
|
||||
expect(emptyname_user.valid?).to be_falsy
|
||||
end
|
||||
|
||||
it 'has a full name between 2 and 32 characters' do
|
||||
expect(short_user.valid?).to be_falsy
|
||||
expect(long_user.valid?).to be_falsy
|
||||
|
||||
|
||||
short_user.full_name = "a" * 2;
|
||||
long_user.full_name = "a" * 32;
|
||||
|
||||
expect(short_user.valid?).to be_truthy
|
||||
expect(long_user.valid?).to be_truthy
|
||||
end
|
||||
|
||||
it 'has an email that must contain a @' do
|
||||
invalid_email_user = User.new(full_name: "Valid name", email: "invalid email", password: "password")
|
||||
|
||||
expect(invalid_email_user.valid?).to be_falsy
|
||||
expect(user.valid?).to be_truthy
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -20,7 +20,7 @@ require 'rspec/rails'
|
||||
# directory. Alternatively, in the individual `*_spec.rb` files, manually
|
||||
# require only the support files necessary.
|
||||
#
|
||||
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
|
||||
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
|
||||
|
||||
# Checks for pending migrations and applies them before tests are run.
|
||||
# If you are not using ActiveRecord, you can remove these lines.
|
||||
|
||||
47
spec/requests/admin_panel_spec.rb
Normal file
47
spec/requests/admin_panel_spec.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Requests to the admin panel', type: :request do
|
||||
let(:user) { FactoryBot.create(:user) }
|
||||
let(:moderator) { FactoryBot.create(:moderator) }
|
||||
let(:admin) { FactoryBot.create(:admin) }
|
||||
|
||||
before(:each) do
|
||||
user.confirm
|
||||
moderator.confirm
|
||||
admin.confirm
|
||||
end
|
||||
|
||||
it 'requires a logged-in admin to administrate Users' do
|
||||
get admin_users_path
|
||||
expect(response).to redirect_to(new_user_session_path)
|
||||
|
||||
sign_in user
|
||||
get admin_users_path
|
||||
expect(response).to redirect_to(root_path)
|
||||
|
||||
sign_in moderator
|
||||
get admin_users_path
|
||||
expect(response).to redirect_to(root_path)
|
||||
|
||||
sign_in admin
|
||||
get admin_users_path
|
||||
expect(response).to have_http_status(:success)
|
||||
end
|
||||
|
||||
it 'requires at least a logged-in moderator to administrate Boards' do
|
||||
get admin_boards_path
|
||||
expect(response).to redirect_to(new_user_session_path)
|
||||
|
||||
sign_in user
|
||||
get admin_boards_path
|
||||
expect(response).to redirect_to(root_path)
|
||||
|
||||
sign_in moderator
|
||||
get admin_boards_path
|
||||
expect(response).to have_http_status(:success)
|
||||
|
||||
sign_in admin
|
||||
get admin_boards_path
|
||||
expect(response).to have_http_status(:success)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user