mirror of
https://github.com/discourse/discourse.git
synced 2025-06-02 04:08:41 +08:00
DEV: Update the rubocop-discourse gem
This enables cops related to RSpec `subject`. See https://github.com/discourse/rubocop-discourse/pull/32
This commit is contained in:

committed by
Loïc Guitaut

parent
8e1d049e6b
commit
0f4beab0fb
@ -38,6 +38,8 @@ require "webauthn/security_key_registration_service"
|
||||
# The origin params just need to be whatever your localhost URL for Discourse is.
|
||||
|
||||
RSpec.describe Webauthn::SecurityKeyAuthenticationService do
|
||||
subject(:service) { described_class.new(current_user, params, challenge_params) }
|
||||
|
||||
let(:security_key_user) { current_user }
|
||||
let!(:security_key) do
|
||||
Fabricate(
|
||||
@ -91,24 +93,23 @@ RSpec.describe Webauthn::SecurityKeyAuthenticationService do
|
||||
let(:challenge_params_origin) { "http://localhost:3000" }
|
||||
let(:challenge_params) { { challenge: challenge, rp_id: rp_id, origin: challenge_params_origin } }
|
||||
let(:current_user) { Fabricate(:user) }
|
||||
let(:subject) { described_class.new(current_user, params, challenge_params) }
|
||||
|
||||
it "updates last_used when the security key and params are valid" do
|
||||
expect(subject.authenticate_security_key).to eq(true)
|
||||
expect(service.authenticate_security_key).to eq(true)
|
||||
expect(security_key.reload.last_used).not_to eq(nil)
|
||||
end
|
||||
|
||||
context "when params is blank" do
|
||||
let(:params) { nil }
|
||||
it "returns false with no validation" do
|
||||
expect(subject.authenticate_security_key).to eq(false)
|
||||
expect(service.authenticate_security_key).to eq(false)
|
||||
end
|
||||
end
|
||||
|
||||
context "when params is not blank and not a hash" do
|
||||
let(:params) { "test" }
|
||||
it "returns false with no validation" do
|
||||
expect(subject.authenticate_security_key).to eq(false)
|
||||
expect(service.authenticate_security_key).to eq(false)
|
||||
end
|
||||
end
|
||||
|
||||
@ -116,7 +117,7 @@ RSpec.describe Webauthn::SecurityKeyAuthenticationService do
|
||||
before { security_key.destroy }
|
||||
|
||||
it "raises a NotFoundError" do
|
||||
expect { subject.authenticate_security_key }.to raise_error(
|
||||
expect { service.authenticate_security_key }.to raise_error(
|
||||
Webauthn::NotFoundError,
|
||||
I18n.t("webauthn.validation.not_found_error"),
|
||||
)
|
||||
@ -127,7 +128,7 @@ RSpec.describe Webauthn::SecurityKeyAuthenticationService do
|
||||
let(:security_key_user) { Fabricate(:user) }
|
||||
|
||||
it "raises an OwnershipError" do
|
||||
expect { subject.authenticate_security_key }.to raise_error(
|
||||
expect { service.authenticate_security_key }.to raise_error(
|
||||
Webauthn::OwnershipError,
|
||||
I18n.t("webauthn.validation.ownership_error"),
|
||||
)
|
||||
@ -138,7 +139,7 @@ RSpec.describe Webauthn::SecurityKeyAuthenticationService do
|
||||
let(:client_data_webauthn_type) { "webauthn.explode" }
|
||||
|
||||
it "raises an InvalidTypeError" do
|
||||
expect { subject.authenticate_security_key }.to raise_error(
|
||||
expect { service.authenticate_security_key }.to raise_error(
|
||||
Webauthn::InvalidTypeError,
|
||||
I18n.t("webauthn.validation.invalid_type_error"),
|
||||
)
|
||||
@ -149,7 +150,7 @@ RSpec.describe Webauthn::SecurityKeyAuthenticationService do
|
||||
let(:client_data_challenge) { Base64.strict_encode64("invalid challenge") }
|
||||
|
||||
it "raises a ChallengeMismatchError" do
|
||||
expect { subject.authenticate_security_key }.to raise_error(
|
||||
expect { service.authenticate_security_key }.to raise_error(
|
||||
Webauthn::ChallengeMismatchError,
|
||||
I18n.t("webauthn.validation.challenge_mismatch_error"),
|
||||
)
|
||||
@ -160,7 +161,7 @@ RSpec.describe Webauthn::SecurityKeyAuthenticationService do
|
||||
let(:client_data_origin) { "https://someothersite.com" }
|
||||
|
||||
it "raises a InvalidOriginError" do
|
||||
expect { subject.authenticate_security_key }.to raise_error(
|
||||
expect { service.authenticate_security_key }.to raise_error(
|
||||
Webauthn::InvalidOriginError,
|
||||
I18n.t("webauthn.validation.invalid_origin_error"),
|
||||
)
|
||||
@ -171,7 +172,7 @@ RSpec.describe Webauthn::SecurityKeyAuthenticationService do
|
||||
let(:rp_id) { "bad_rp_id" }
|
||||
|
||||
it "raises a InvalidRelyingPartyIdError" do
|
||||
expect { subject.authenticate_security_key }.to raise_error(
|
||||
expect { service.authenticate_security_key }.to raise_error(
|
||||
Webauthn::InvalidRelyingPartyIdError,
|
||||
I18n.t("webauthn.validation.invalid_relying_party_id_error"),
|
||||
)
|
||||
@ -182,7 +183,7 @@ RSpec.describe Webauthn::SecurityKeyAuthenticationService do
|
||||
let(:signature) { Base64.strict_encode64("badsig") }
|
||||
|
||||
it "raises a PublicKeyError" do
|
||||
expect { subject.authenticate_security_key }.to raise_error(
|
||||
expect { service.authenticate_security_key }.to raise_error(
|
||||
Webauthn::PublicKeyError,
|
||||
I18n.t("webauthn.validation.public_key_error"),
|
||||
)
|
||||
@ -193,7 +194,7 @@ RSpec.describe Webauthn::SecurityKeyAuthenticationService do
|
||||
before { COSE::Algorithm.expects(:find).returns(nil) }
|
||||
|
||||
it "raises a UnknownCOSEAlgorithmError" do
|
||||
expect { subject.authenticate_security_key }.to raise_error(
|
||||
expect { service.authenticate_security_key }.to raise_error(
|
||||
Webauthn::UnknownCOSEAlgorithmError,
|
||||
I18n.t("webauthn.validation.unknown_cose_algorithm_error"),
|
||||
)
|
||||
@ -230,7 +231,7 @@ RSpec.describe Webauthn::SecurityKeyAuthenticationService do
|
||||
end
|
||||
|
||||
it "updates last_used when the security key and params are valid" do
|
||||
expect(subject.authenticate_security_key).to eq(true)
|
||||
expect(service.authenticate_security_key).to eq(true)
|
||||
expect(security_key.reload.last_used).not_to eq(nil)
|
||||
end
|
||||
end
|
||||
|
@ -3,6 +3,8 @@ require "webauthn"
|
||||
require "webauthn/security_key_registration_service"
|
||||
|
||||
RSpec.describe Webauthn::SecurityKeyRegistrationService do
|
||||
subject(:service) { described_class.new(current_user, params, challenge_params) }
|
||||
|
||||
let(:client_data_challenge) { Base64.encode64(challenge) }
|
||||
let(:client_data_webauthn_type) { "webauthn.create" }
|
||||
let(:client_data_origin) { "http://localhost:3000" }
|
||||
@ -34,13 +36,12 @@ RSpec.describe Webauthn::SecurityKeyRegistrationService do
|
||||
let(:challenge_params) { { challenge: challenge, rp_id: rp_id, origin: "http://localhost:3000" } }
|
||||
let(:challenge) { "f1e04530f34a1b6a08d032d8550e23eb8330be04e4166008f26c0e1b42ad" }
|
||||
let(:current_user) { Fabricate(:user) }
|
||||
let(:subject) { described_class.new(current_user, params, challenge_params) }
|
||||
|
||||
context "when the client data webauthn type is not webauthn.create" do
|
||||
let(:client_data_webauthn_type) { "webauthn.explode" }
|
||||
|
||||
it "raises an InvalidTypeError" do
|
||||
expect { subject.register_second_factor_security_key }.to raise_error(
|
||||
expect { service.register_second_factor_security_key }.to raise_error(
|
||||
Webauthn::InvalidTypeError,
|
||||
I18n.t("webauthn.validation.invalid_type_error"),
|
||||
)
|
||||
@ -51,7 +52,7 @@ RSpec.describe Webauthn::SecurityKeyRegistrationService do
|
||||
let(:client_data_challenge) { Base64.encode64("invalid challenge") }
|
||||
|
||||
it "raises a ChallengeMismatchError" do
|
||||
expect { subject.register_second_factor_security_key }.to raise_error(
|
||||
expect { service.register_second_factor_security_key }.to raise_error(
|
||||
Webauthn::ChallengeMismatchError,
|
||||
I18n.t("webauthn.validation.challenge_mismatch_error"),
|
||||
)
|
||||
@ -62,7 +63,7 @@ RSpec.describe Webauthn::SecurityKeyRegistrationService do
|
||||
let(:client_data_origin) { "https://someothersite.com" }
|
||||
|
||||
it "raises a InvalidOriginError" do
|
||||
expect { subject.register_second_factor_security_key }.to raise_error(
|
||||
expect { service.register_second_factor_security_key }.to raise_error(
|
||||
Webauthn::InvalidOriginError,
|
||||
I18n.t("webauthn.validation.invalid_origin_error"),
|
||||
)
|
||||
@ -73,7 +74,7 @@ RSpec.describe Webauthn::SecurityKeyRegistrationService do
|
||||
let(:rp_id) { "bad_rp_id" }
|
||||
|
||||
it "raises a InvalidRelyingPartyIdError" do
|
||||
expect { subject.register_second_factor_security_key }.to raise_error(
|
||||
expect { service.register_second_factor_security_key }.to raise_error(
|
||||
Webauthn::InvalidRelyingPartyIdError,
|
||||
I18n.t("webauthn.validation.invalid_relying_party_id_error"),
|
||||
)
|
||||
@ -87,7 +88,7 @@ RSpec.describe Webauthn::SecurityKeyRegistrationService do
|
||||
end
|
||||
|
||||
it "raises a UnsupportedPublicKeyAlgorithmError" do
|
||||
expect { subject.register_second_factor_security_key }.to raise_error(
|
||||
expect { service.register_second_factor_security_key }.to raise_error(
|
||||
Webauthn::UnsupportedPublicKeyAlgorithmError,
|
||||
I18n.t("webauthn.validation.unsupported_public_key_algorithm_error"),
|
||||
)
|
||||
@ -103,7 +104,7 @@ RSpec.describe Webauthn::SecurityKeyRegistrationService do
|
||||
end
|
||||
|
||||
it "raises a UnsupportedAttestationFormatError" do
|
||||
expect { subject.register_second_factor_security_key }.to raise_error(
|
||||
expect { service.register_second_factor_security_key }.to raise_error(
|
||||
Webauthn::UnsupportedAttestationFormatError,
|
||||
I18n.t("webauthn.validation.unsupported_attestation_format_error"),
|
||||
)
|
||||
@ -117,14 +118,14 @@ RSpec.describe Webauthn::SecurityKeyRegistrationService do
|
||||
context "when the credential id is already in use for any user" do
|
||||
it "raises a CredentialIdInUseError" do
|
||||
# register the key to the current user
|
||||
security_key = subject.register_second_factor_security_key
|
||||
security_key = service.register_second_factor_security_key
|
||||
|
||||
# update the key to be on a different user
|
||||
other_user = Fabricate(:user)
|
||||
security_key.update(user: other_user)
|
||||
|
||||
# error!
|
||||
expect { subject.register_second_factor_security_key }.to raise_error(
|
||||
expect { service.register_second_factor_security_key }.to raise_error(
|
||||
Webauthn::CredentialIdInUseError,
|
||||
I18n.t("webauthn.validation.credential_id_in_use_error"),
|
||||
)
|
||||
@ -137,7 +138,7 @@ RSpec.describe Webauthn::SecurityKeyRegistrationService do
|
||||
end
|
||||
|
||||
it "raises a MalformedAttestationError" do
|
||||
expect { subject.register_second_factor_security_key }.to raise_error(
|
||||
expect { service.register_second_factor_security_key }.to raise_error(
|
||||
Webauthn::MalformedAttestationError,
|
||||
I18n.t("webauthn.validation.malformed_attestation_error"),
|
||||
)
|
||||
|
Reference in New Issue
Block a user