2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-20 01:44:51 -05:00
|
|
|
RSpec.describe UserSecondFactor do
|
2023-08-24 02:27:38 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
|
2018-02-20 01:44:51 -05:00
|
|
|
describe ".methods" do
|
|
|
|
it "should retain the right order" do
|
|
|
|
expect(described_class.methods[:totp]).to eq(1)
|
2018-06-28 04:12:32 -04:00
|
|
|
expect(described_class.methods[:backup_codes]).to eq(2)
|
2018-02-20 01:44:51 -05:00
|
|
|
end
|
|
|
|
end
|
2023-08-24 02:27:38 -04:00
|
|
|
|
|
|
|
describe "name length validation" do
|
|
|
|
it "allows the name to be nil" do
|
|
|
|
Fabricate(:user_second_factor_totp, user: user, name: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't allow the name to be longer than the limit" do
|
|
|
|
expect do
|
|
|
|
Fabricate(
|
|
|
|
:user_second_factor_totp,
|
|
|
|
user: user,
|
|
|
|
name: "a" * (described_class::MAX_NAME_LENGTH + 1),
|
|
|
|
)
|
|
|
|
end.to raise_error(ActiveRecord::RecordInvalid) do |error|
|
|
|
|
expect(error.message).to include(
|
|
|
|
I18n.t("activerecord.errors.messages.too_long", count: described_class::MAX_NAME_LENGTH),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows a name that is equal to or less than the limit" do
|
|
|
|
expect do
|
|
|
|
Fabricate(
|
|
|
|
:user_second_factor_totp,
|
|
|
|
user: user,
|
|
|
|
name: "a" * described_class::MAX_NAME_LENGTH,
|
|
|
|
)
|
|
|
|
end.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "per-user count validation" do
|
|
|
|
it "doesn't allow a user to have more authenticators than the limit allows" do
|
|
|
|
stub_const(UserSecondFactor, "MAX_TOTPS_PER_USER", 1) do
|
|
|
|
Fabricate(:user_second_factor_totp, user: user)
|
|
|
|
expect do Fabricate(:user_second_factor_totp, user: user) end.to raise_error(
|
|
|
|
ActiveRecord::RecordInvalid,
|
|
|
|
) do |error|
|
|
|
|
expect(error.message).to include(I18n.t("login.too_many_authenticators"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't count backup codes in the authenticators limit" do
|
|
|
|
user.generate_backup_codes
|
|
|
|
expect(user.user_second_factors.backup_codes.count).to eq(10)
|
|
|
|
|
|
|
|
stub_const(UserSecondFactor, "MAX_TOTPS_PER_USER", 1) do
|
|
|
|
Fabricate(:user_second_factor_totp, user: user)
|
|
|
|
expect do Fabricate(:user_second_factor_totp, user: user) end.to raise_error(
|
|
|
|
ActiveRecord::RecordInvalid,
|
|
|
|
) do |error|
|
|
|
|
expect(error.message).to include(I18n.t("login.too_many_authenticators"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't count authenticators from other users" do
|
|
|
|
another_user = Fabricate(:user)
|
|
|
|
Fabricate(:user_second_factor_totp, user: another_user)
|
|
|
|
|
|
|
|
stub_const(UserSecondFactor, "MAX_TOTPS_PER_USER", 1) do
|
|
|
|
Fabricate(:user_second_factor_totp, user: user)
|
|
|
|
expect do Fabricate(:user_second_factor_totp, user: user) end.to raise_error(
|
|
|
|
ActiveRecord::RecordInvalid,
|
|
|
|
) do |error|
|
|
|
|
expect(error.message).to include(I18n.t("login.too_many_authenticators"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-02-20 01:44:51 -05:00
|
|
|
end
|