require "rails_helper" RSpec.describe PasswordComplexity do describe ".violation" do it "accepts a password with 3 character classes" do expect(described_class.violation("NewPass123")).to be_nil end it "accepts symbols instead of one letter class" do expect(described_class.violation("newpass1!")).to be_nil end it "rejects passwords that are too short" do expect(described_class.violation("Ab1!")).to eq(:too_short) end it "rejects passwords with fewer than 3 classes" do expect(described_class.violation("password123")).to eq(:too_weak) expect(described_class.violation("PASSWORD123")).to eq(:too_weak) expect(described_class.violation("Password")).to eq(:too_weak) end end describe ".same_as_current?" do let!(:user) { User.create!(email: "same@example.com", name: "Same", password: "Password123", role: "coach") } it "detects when the new password matches the current one" do expect(described_class.same_as_current?(user, "Password123")).to eq(true) expect(described_class.same_as_current?(user, "OtherPass123")).to eq(false) end end describe "User validation" do it "blocks weak passwords on create" do user = User.new(email: "weak@example.com", name: "Weak", password: "password123", role: "coach") expect(user).not_to be_valid expect(user.errors[:password]).to be_present end it "allows strong passwords on create" do user = User.new(email: "strong@example.com", name: "Strong", password: "NewPass123", role: "coach") expect(user).to be_valid end end end