Skip to content

Commit

Permalink
Modify api key error when gem does not belong to the user
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshenny committed Apr 24, 2023
1 parent e997bb9 commit 9c7a8d7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/models/api_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def mfa_enabled?
def rubygem_id=(id)
self.ownership = id.blank? ? nil : user.ownerships.find_by!(rubygem_id: id)
rescue ActiveRecord::RecordNotFound
errors.add :rubygem, "that is selected cannot be scoped to this key"
errors.add :rubygem, "must be a gem that you are an owner of"
end

def rubygem_name=(name)
Expand Down
4 changes: 2 additions & 2 deletions test/functional/api_keys_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class ApiKeysControllerTest < ActionController::TestCase
should "display error with invalid id" do
post :create, params: { api_key: { name: "gem scope", add_owner: true, rubygem_id: -1 } }

assert_equal "Rubygem that is selected cannot be scoped to this key", flash[:error]
assert_equal "Rubygem must be a gem that you are an owner of", flash[:error]
assert_empty @user.reload.api_keys
end

Expand Down Expand Up @@ -235,7 +235,7 @@ class ApiKeysControllerTest < ActionController::TestCase
assert_no_changes @api_key do
patch :update, params: { api_key: { rubygem_id: -1 }, id: @api_key.id }

assert_equal "Rubygem that is selected cannot be scoped to this key", flash[:error]
assert_equal "Rubygem must be a gem that you are an owner of", flash[:error]
end
end

Expand Down
5 changes: 2 additions & 3 deletions test/models/api_key_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ class ApiKeyTest < ActiveSupport::TestCase

should "add error when id is not associated with the user" do
api_key = ApiKey.new(hashed_key: SecureRandom.hex(24), push_rubygem: true, user: @ownership.user, rubygem_id: -1)

assert_contains api_key.errors[:rubygem], "that is selected cannot be scoped to this key"
assert_contains api_key.errors[:rubygem], "must be a gem that you are an owner of"
end
end

Expand All @@ -121,7 +120,7 @@ class ApiKeyTest < ActiveSupport::TestCase
should "add error when gem is not associated with the user" do
rubygem = create(:rubygem, name: "another-gem")
api_key = ApiKey.new(hashed_key: SecureRandom.hex(24), push_rubygem: true, user: @ownership.user, rubygem_name: rubygem.name)
assert_contains api_key.errors[:rubygem], "that is selected cannot be scoped to this key"
assert_contains api_key.errors[:rubygem], "must be a gem that you are an owner of"
end

should "add error when name is not a valid gem name" do
Expand Down
4 changes: 2 additions & 2 deletions test/system/api_keys_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class ApiKeysTest < ApplicationSystemTestCase
click_button "Create"

assert page.has_css? ".flash"
assert page.has_content? "Rubygem that is selected cannot be scoped to this key"
assert page.has_content? "Rubygem must be a gem that you are an owner of"
assert_empty @user.api_keys
end

Expand Down Expand Up @@ -211,7 +211,7 @@ class ApiKeysTest < ApplicationSystemTestCase
click_button "Update"

assert page.has_css? ".flash"
assert page.has_content? "Rubygem that is selected cannot be scoped to this key"
assert page.has_content? "Rubygem must be a gem that you are an owner of"
assert_equal @ownership.rubygem, api_key.reload.rubygem
end

Expand Down

0 comments on commit 9c7a8d7

Please sign in to comment.