|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe User, type: :model do |
| 6 | + subject { build(:user) } |
| 7 | + |
| 8 | + describe 'validations' do |
| 9 | + it { is_expected.to validate_presence_of(:email) } |
| 10 | + it { is_expected.to validate_presence_of(:full_name) } |
| 11 | + it { is_expected.to validate_presence_of(:role) } |
| 12 | + it { is_expected.to validate_inclusion_of(:role).in_array(Constants::User::ROLES) } |
| 13 | + it { is_expected.to validate_uniqueness_of(:email).case_insensitive } |
| 14 | + |
| 15 | + it 'rejects invalid email format' do |
| 16 | + user = build(:user, email: 'not-an-email') |
| 17 | + expect(user).not_to be_valid |
| 18 | + expect(user.errors[:email]).to be_present |
| 19 | + end |
| 20 | + |
| 21 | + it 'accepts valid email format' do |
| 22 | + user = build(:user, email: 'valid@example.com') |
| 23 | + expect(user).to be_valid |
| 24 | + end |
| 25 | + |
| 26 | + it 'requires password to contain uppercase letter' do |
| 27 | + user = build(:user, password: 'alllowercase1', password_confirmation: 'alllowercase1') |
| 28 | + expect(user).not_to be_valid |
| 29 | + end |
| 30 | + |
| 31 | + it 'accepts a strong password' do |
| 32 | + user = build(:user, password: 'StrongPass1!', password_confirmation: 'StrongPass1!') |
| 33 | + expect(user).to be_valid |
| 34 | + end |
| 35 | + |
| 36 | + it 'requires minimum password length of 8' do |
| 37 | + user = build(:user, password: 'Ab1', password_confirmation: 'Ab1') |
| 38 | + expect(user).not_to be_valid |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + describe 'associations' do |
| 43 | + it { is_expected.to belong_to(:organization) } |
| 44 | + it { is_expected.to have_many(:notifications).dependent(:destroy) } |
| 45 | + it { is_expected.to have_many(:audit_logs).dependent(:destroy) } |
| 46 | + it { is_expected.to have_many(:password_reset_tokens).dependent(:destroy) } |
| 47 | + end |
| 48 | + |
| 49 | + describe 'scopes' do |
| 50 | + let!(:organization) { create(:organization) } |
| 51 | + let!(:active_user) { create(:user, organization: organization, last_login_at: 1.hour.ago) } |
| 52 | + let!(:never_logged) { create(:user, organization: organization, last_login_at: nil) } |
| 53 | + |
| 54 | + describe '.by_role' do |
| 55 | + it 'filters users by role' do |
| 56 | + owner = create(:user, :owner, organization: organization) |
| 57 | + expect(User.by_role('owner')).to include(owner) |
| 58 | + expect(User.by_role('owner')).not_to include(active_user) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + describe '.active' do |
| 63 | + it 'includes users who have logged in' do |
| 64 | + expect(User.active).to include(active_user) |
| 65 | + end |
| 66 | + |
| 67 | + it 'excludes users who never logged in' do |
| 68 | + expect(User.active).not_to include(never_logged) |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + describe 'callbacks' do |
| 74 | + it 'downcases email before save' do |
| 75 | + user = create(:user, email: 'UPPER@EXAMPLE.COM') |
| 76 | + expect(user.reload.email).to eq('upper@example.com') |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + describe '#admin?' do |
| 81 | + it 'returns true for admin role' do |
| 82 | + expect(build(:user, :admin).admin?).to be(true) |
| 83 | + end |
| 84 | + |
| 85 | + it 'returns false for coach role' do |
| 86 | + expect(build(:user, :coach).admin?).to be(false) |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + describe '#owner?' do |
| 91 | + it 'returns true for owner role' do |
| 92 | + expect(build(:user, :owner).owner?).to be(true) |
| 93 | + end |
| 94 | + |
| 95 | + it 'returns false for admin role' do |
| 96 | + expect(build(:user, :admin).owner?).to be(false) |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + describe '#can_manage_players?' do |
| 101 | + it 'returns true for owner, admin, and coach' do |
| 102 | + %w[owner admin coach].each do |role| |
| 103 | + expect(build(:user, role: role).can_manage_players?).to be(true), |
| 104 | + "expected #{role} to manage players" |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + it 'returns false for analyst and viewer' do |
| 109 | + %w[analyst viewer].each do |role| |
| 110 | + expect(build(:user, role: role).can_manage_players?).to be(false), |
| 111 | + "expected #{role} not to manage players" |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + describe '#can_view_analytics?' do |
| 117 | + it 'returns true for owner, admin, coach, and analyst' do |
| 118 | + %w[owner admin coach analyst].each do |role| |
| 119 | + expect(build(:user, role: role).can_view_analytics?).to be(true) |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + it 'returns false for viewer' do |
| 124 | + expect(build(:user, :viewer).can_view_analytics?).to be(false) |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + describe '#update_last_login!' do |
| 129 | + it 'sets last_login_at to the current time' do |
| 130 | + user = create(:user) |
| 131 | + expect { user.update_last_login! }.to change { user.reload.last_login_at }.from(nil) |
| 132 | + end |
| 133 | + end |
| 134 | +end |
0 commit comments