2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2011-01-28 18:46:47 -05:00
|
|
|
require 'rubygems/test_case'
|
2010-02-21 21:52:35 -05:00
|
|
|
require 'rubygems/commands/owner_command'
|
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
class TestGemCommandsOwnerCommand < Gem::TestCase
|
2010-02-21 21:52:35 -05:00
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
ENV["RUBYGEMS_HOST"] = nil
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_ui = Gem::MockGemUi.new
|
|
|
|
@stub_fetcher = Gem::FakeFetcher.new
|
|
|
|
Gem::RemoteFetcher.fetcher = @stub_fetcher
|
|
|
|
Gem.configuration = nil
|
2010-02-21 21:52:35 -05:00
|
|
|
Gem.configuration.rubygems_api_key = "ed244fbf2b1a52e012da8616c512fa47f9aa5250"
|
|
|
|
|
|
|
|
@cmd = Gem::Commands::OwnerCommand.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_show_owners
|
|
|
|
response = <<EOF
|
|
|
|
---
|
|
|
|
- email: user1@example.com
|
2016-04-06 02:01:14 -04:00
|
|
|
id: 1
|
|
|
|
handle: user1
|
2010-02-21 21:52:35 -05:00
|
|
|
- email: user2@example.com
|
2016-04-06 02:01:14 -04:00
|
|
|
- id: 3
|
|
|
|
handle: user3
|
|
|
|
- id: 4
|
2010-02-21 21:52:35 -05:00
|
|
|
EOF
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, 'OK']
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2010-02-21 21:52:35 -05:00
|
|
|
@cmd.show_owners("freewill")
|
|
|
|
end
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_equal Net::HTTP::Get, @stub_fetcher.last_request.class
|
|
|
|
assert_equal Gem.configuration.rubygems_api_key, @stub_fetcher.last_request["Authorization"]
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_match %r{Owners for gem: freewill}, @stub_ui.output
|
|
|
|
assert_match %r{- user1@example.com}, @stub_ui.output
|
|
|
|
assert_match %r{- user2@example.com}, @stub_ui.output
|
|
|
|
assert_match %r{- user3}, @stub_ui.output
|
|
|
|
assert_match %r{- 4}, @stub_ui.output
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
|
|
|
|
2018-02-16 03:08:06 -05:00
|
|
|
def test_show_owners_dont_load_objects
|
|
|
|
skip "testing a psych-only API" unless defined?(::Psych::DisallowedClass)
|
|
|
|
|
|
|
|
response = <<EOF
|
|
|
|
---
|
|
|
|
- email: !ruby/object:Object {}
|
|
|
|
id: 1
|
|
|
|
handle: user1
|
|
|
|
- email: user2@example.com
|
|
|
|
- id: 3
|
|
|
|
handle: user3
|
|
|
|
- id: 4
|
|
|
|
EOF
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, 'OK']
|
2018-02-16 03:08:06 -05:00
|
|
|
|
|
|
|
assert_raises Psych::DisallowedClass do
|
|
|
|
use_ui @ui do
|
|
|
|
@cmd.show_owners("freewill")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def test_show_owners_setting_up_host_through_env_var
|
|
|
|
response = "- email: user1@example.com\n"
|
|
|
|
host = "http://rubygems.example"
|
|
|
|
ENV["RUBYGEMS_HOST"] = host
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_fetcher.data["#{host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, 'OK']
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2013-09-14 04:59:02 -04:00
|
|
|
@cmd.show_owners("freewill")
|
|
|
|
end
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_match %r{Owners for gem: freewill}, @stub_ui.output
|
|
|
|
assert_match %r{- user1@example.com}, @stub_ui.output
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_show_owners_setting_up_host
|
|
|
|
response = "- email: user1@example.com\n"
|
|
|
|
host = "http://rubygems.example"
|
|
|
|
@cmd.host = host
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_fetcher.data["#{host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, 'OK']
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2013-09-14 04:59:02 -04:00
|
|
|
@cmd.show_owners("freewill")
|
|
|
|
end
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_match %r{Owners for gem: freewill}, @stub_ui.output
|
|
|
|
assert_match %r{- user1@example.com}, @stub_ui.output
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
def test_show_owners_denied
|
|
|
|
response = "You don't have permission to push to this gem"
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = [response, 403, 'Forbidden']
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
assert_raises Gem::MockGemUi::TermError do
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2010-02-21 21:52:35 -05:00
|
|
|
@cmd.show_owners("freewill")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_match response, @stub_ui.output
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
def test_show_owners_key
|
|
|
|
response = "- email: user1@example.com\n"
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, 'OK']
|
2011-03-01 04:41:32 -05:00
|
|
|
File.open Gem.configuration.credentials_path, 'a' do |f|
|
|
|
|
f.write ':other: 701229f217cdf23b1344c7b4b54ca97'
|
|
|
|
end
|
|
|
|
Gem.configuration.load_api_keys
|
|
|
|
|
|
|
|
@cmd.handle_options %w(-k other)
|
|
|
|
@cmd.show_owners('freewill')
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_equal '701229f217cdf23b1344c7b4b54ca97', @stub_fetcher.last_request['Authorization']
|
2011-03-01 04:41:32 -05:00
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
def test_add_owners
|
|
|
|
response = "Owner added successfully."
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 200, 'OK']
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2010-02-21 21:52:35 -05:00
|
|
|
@cmd.add_owners("freewill", ["user-new1@example.com"])
|
|
|
|
end
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_equal Net::HTTP::Post, @stub_fetcher.last_request.class
|
|
|
|
assert_equal Gem.configuration.rubygems_api_key, @stub_fetcher.last_request["Authorization"]
|
|
|
|
assert_equal "email=user-new1%40example.com", @stub_fetcher.last_request.body
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_match response, @stub_ui.output
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_owners_denied
|
|
|
|
response = "You don't have permission to push to this gem"
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 403, 'Forbidden']
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2012-11-29 01:52:18 -05:00
|
|
|
@cmd.add_owners("freewill", ["user-new1@example.com"])
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_match response, @stub_ui.output
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def test_add_owner_with_host_option_through_execute
|
|
|
|
host = "http://rubygems.example"
|
|
|
|
add_owner_response = "Owner added successfully."
|
|
|
|
show_owners_response = "- email: user1@example.com\n"
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_fetcher.data["#{host}/api/v1/gems/freewill/owners"] = [add_owner_response, 200, 'OK']
|
|
|
|
@stub_fetcher.data["#{host}/api/v1/gems/freewill/owners.yaml"] = [show_owners_response, 200, 'OK']
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
@cmd.handle_options %W[--host #{host} --add user-new1@example.com freewill]
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2013-09-14 04:59:02 -04:00
|
|
|
@cmd.execute
|
|
|
|
end
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_match add_owner_response, @stub_ui.output
|
|
|
|
assert_match %r{Owners for gem: freewill}, @stub_ui.output
|
|
|
|
assert_match %r{- user1@example.com}, @stub_ui.output
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
def test_add_owners_key
|
|
|
|
response = "Owner added successfully."
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 200, 'OK']
|
2011-03-01 04:41:32 -05:00
|
|
|
File.open Gem.configuration.credentials_path, 'a' do |f|
|
|
|
|
f.write ':other: 701229f217cdf23b1344c7b4b54ca97'
|
|
|
|
end
|
|
|
|
Gem.configuration.load_api_keys
|
|
|
|
|
|
|
|
@cmd.handle_options %w(-k other)
|
|
|
|
@cmd.add_owners('freewill', ['user-new1@example.com'])
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_equal '701229f217cdf23b1344c7b4b54ca97', @stub_fetcher.last_request['Authorization']
|
2011-03-01 04:41:32 -05:00
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
def test_remove_owners
|
|
|
|
response = "Owner removed successfully."
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 200, 'OK']
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2010-02-21 21:52:35 -05:00
|
|
|
@cmd.remove_owners("freewill", ["user-remove1@example.com"])
|
|
|
|
end
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_equal Net::HTTP::Delete, @stub_fetcher.last_request.class
|
|
|
|
assert_equal Gem.configuration.rubygems_api_key, @stub_fetcher.last_request["Authorization"]
|
|
|
|
assert_equal "email=user-remove1%40example.com", @stub_fetcher.last_request.body
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_match response, @stub_ui.output
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_remove_owners_denied
|
|
|
|
response = "You don't have permission to push to this gem"
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 403, 'Forbidden']
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2012-11-29 01:52:18 -05:00
|
|
|
@cmd.remove_owners("freewill", ["user-remove1@example.com"])
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_match response, @stub_ui.output
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
2011-03-01 04:41:32 -05:00
|
|
|
|
|
|
|
def test_remove_owners_key
|
|
|
|
response = "Owner removed successfully."
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 200, 'OK']
|
2011-03-01 04:41:32 -05:00
|
|
|
File.open Gem.configuration.credentials_path, 'a' do |f|
|
|
|
|
f.write ':other: 701229f217cdf23b1344c7b4b54ca97'
|
|
|
|
end
|
|
|
|
Gem.configuration.load_api_keys
|
|
|
|
|
|
|
|
@cmd.handle_options %w(-k other)
|
|
|
|
@cmd.remove_owners('freewill', ['user-remove1@example.com'])
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_equal '701229f217cdf23b1344c7b4b54ca97', @stub_fetcher.last_request['Authorization']
|
2011-03-01 04:41:32 -05:00
|
|
|
end
|
2013-07-08 18:41:03 -04:00
|
|
|
|
|
|
|
def test_remove_owners_missing
|
|
|
|
response = 'Owner could not be found.'
|
2018-05-30 09:01:35 -04:00
|
|
|
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 404, 'Not Found']
|
2013-07-08 18:41:03 -04:00
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
use_ui @stub_ui do
|
2013-07-08 18:41:03 -04:00
|
|
|
@cmd.remove_owners("freewill", ["missing@example"])
|
|
|
|
end
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_equal "Removing missing@example: #{response}\n", @stub_ui.output
|
2013-07-08 18:41:03 -04:00
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|