Limit the Number of API Keys a User Can Have (#7870)
* Limit the Number of API Keys a User Can Have * tweak error message and throw in flaky spec fix
This commit is contained in:
parent
7293df3191
commit
dd7f51f7fa
3 changed files with 24 additions and 4 deletions
|
|
@ -4,4 +4,13 @@ class ApiSecret < ApplicationRecord
|
|||
belongs_to :user
|
||||
|
||||
validates :description, presence: true, length: { maximum: 300 }
|
||||
validate :user_api_secret_count
|
||||
|
||||
private
|
||||
|
||||
def user_api_secret_count
|
||||
return if user && user.api_secrets.count < 20
|
||||
|
||||
errors.add(:user, "API secret limit of 20 per user has been reached")
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -2,8 +2,19 @@ require "rails_helper"
|
|||
|
||||
RSpec.describe ApiSecret, type: :model do
|
||||
describe "validations" do
|
||||
subject { create(:api_secret) }
|
||||
|
||||
it { is_expected.to belong_to(:user) }
|
||||
it { is_expected.to validate_presence_of(:description) }
|
||||
it { is_expected.to validate_length_of(:description).is_at_most(300) }
|
||||
|
||||
it "validates the number of keys a user already has" do
|
||||
user = create(:user)
|
||||
create_list(:api_secret, 19, user_id: user.id)
|
||||
invalid_secret = create(:api_secret, user_id: user.id)
|
||||
|
||||
expect(invalid_secret).not_to be_valid
|
||||
expect(invalid_secret.errors.full_messages.join).to include("limit of 20 per user has been reached")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -13,26 +13,26 @@ RSpec.describe Internal::ModeratorsQuery, type: :query do
|
|||
describe ".call" do
|
||||
context "when no arguments are given" do
|
||||
it "returns all moderators" do
|
||||
expect(described_class.call).to eq([user, user2, user5])
|
||||
expect(described_class.call).to match_array([user, user2, user5])
|
||||
end
|
||||
end
|
||||
|
||||
context "when search is set" do
|
||||
let(:options) { { search: "greg" } }
|
||||
|
||||
it { is_expected.to eq([user, user2]) }
|
||||
it { is_expected.to match_array([user, user2]) }
|
||||
end
|
||||
|
||||
context "when state is tag_moderator" do
|
||||
let(:options) { { state: "tag_moderator" } }
|
||||
|
||||
it { is_expected.to eq([user3]) }
|
||||
it { is_expected.to match_array([user3]) }
|
||||
end
|
||||
|
||||
context "when state is potential" do
|
||||
let(:options) { { state: "potential" } }
|
||||
|
||||
it { is_expected.to eq([user4, user6, user3]) }
|
||||
it { is_expected.to match_array([user4, user6, user3]) }
|
||||
end
|
||||
|
||||
context "when state does not exist" do
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue