mirror of
https://github.com/discourse/discourse.git
synced 2024-12-15 11:03:41 +08:00
6e161d3e75
The most common thing that we do with fab! is: fab!(:thing) { Fabricate(:thing) } This commit adds a shorthand for this which is just simply: fab!(:thing) i.e. If you omit the block, then, by default, you'll get a `Fabricate`d object using the fabricator of the same name.
59 lines
1.9 KiB
Ruby
59 lines
1.9 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe UserSecurityKey do
|
|
fab!(:user)
|
|
|
|
describe "name length validation" do
|
|
it "doesn't allow the name to be longer than the limit" do
|
|
expect do
|
|
Fabricate(
|
|
:user_security_key_with_random_credential,
|
|
user: user,
|
|
name: "b" * (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's under the limit" do
|
|
expect do
|
|
Fabricate(
|
|
:user_security_key_with_random_credential,
|
|
user: user,
|
|
name: "b" * 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 security keys than the limit allows" do
|
|
stub_const(UserSecurityKey, "MAX_KEYS_PER_USER", 1) do
|
|
Fabricate(:user_security_key_with_random_credential, user: user)
|
|
expect do
|
|
Fabricate(:user_security_key_with_random_credential, user: user)
|
|
end.to raise_error(ActiveRecord::RecordInvalid) do |error|
|
|
expect(error.message).to include(I18n.t("login.too_many_security_keys"))
|
|
end
|
|
end
|
|
end
|
|
|
|
it "doesn't count security keys from other users" do
|
|
another_user = Fabricate(:user)
|
|
Fabricate(:user_security_key_with_random_credential, user: another_user)
|
|
|
|
stub_const(UserSecurityKey, "MAX_KEYS_PER_USER", 1) do
|
|
Fabricate(:user_security_key_with_random_credential, user: user)
|
|
expect do
|
|
Fabricate(:user_security_key_with_random_credential, user: user)
|
|
end.to raise_error(ActiveRecord::RecordInvalid) do |error|
|
|
expect(error.message).to include(I18n.t("login.too_many_security_keys"))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|