1require 'rubygems/test_case' 2require 'rubygems/commands/owner_command' 3 4class TestGemCommandsOwnerCommand < Gem::TestCase 5 6 def setup 7 super 8 9 @fetcher = Gem::FakeFetcher.new 10 Gem::RemoteFetcher.fetcher = @fetcher 11 Gem.configuration.rubygems_api_key = "ed244fbf2b1a52e012da8616c512fa47f9aa5250" 12 13 @cmd = Gem::Commands::OwnerCommand.new 14 end 15 16 def test_show_owners 17 response = <<EOF 18--- 19- email: user1@example.com 20- email: user2@example.com 21EOF 22 23 @fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, 'OK'] 24 25 use_ui @ui do 26 @cmd.show_owners("freewill") 27 end 28 29 assert_equal Net::HTTP::Get, @fetcher.last_request.class 30 assert_equal Gem.configuration.rubygems_api_key, @fetcher.last_request["Authorization"] 31 32 assert_match %r{Owners for gem: freewill}, @ui.output 33 assert_match %r{- user1@example.com}, @ui.output 34 assert_match %r{- user2@example.com}, @ui.output 35 end 36 37 def test_show_owners_denied 38 response = "You don't have permission to push to this gem" 39 @fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = [response, 403, 'Forbidden'] 40 41 assert_raises Gem::MockGemUi::TermError do 42 use_ui @ui do 43 @cmd.show_owners("freewill") 44 end 45 end 46 47 assert_match response, @ui.output 48 end 49 50 def test_show_owners_key 51 response = "- email: user1@example.com\n" 52 @fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, 'OK'] 53 File.open Gem.configuration.credentials_path, 'a' do |f| 54 f.write ':other: 701229f217cdf23b1344c7b4b54ca97' 55 end 56 Gem.configuration.load_api_keys 57 58 @cmd.handle_options %w(-k other) 59 @cmd.show_owners('freewill') 60 61 assert_equal '701229f217cdf23b1344c7b4b54ca97', @fetcher.last_request['Authorization'] 62 end 63 64 def test_add_owners 65 response = "Owner added successfully." 66 @fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 200, 'OK'] 67 68 use_ui @ui do 69 @cmd.add_owners("freewill", ["user-new1@example.com"]) 70 end 71 72 assert_equal Net::HTTP::Post, @fetcher.last_request.class 73 assert_equal Gem.configuration.rubygems_api_key, @fetcher.last_request["Authorization"] 74 assert_equal "email=user-new1%40example.com", @fetcher.last_request.body 75 76 assert_match response, @ui.output 77 end 78 79 def test_add_owners_denied 80 response = "You don't have permission to push to this gem" 81 @fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 403, 'Forbidden'] 82 83 use_ui @ui do 84 @cmd.add_owners("freewill", ["user-new1@example.com"]) 85 end 86 87 assert_match response, @ui.output 88 end 89 90 def test_add_owners_key 91 response = "Owner added successfully." 92 @fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 200, 'OK'] 93 File.open Gem.configuration.credentials_path, 'a' do |f| 94 f.write ':other: 701229f217cdf23b1344c7b4b54ca97' 95 end 96 Gem.configuration.load_api_keys 97 98 @cmd.handle_options %w(-k other) 99 @cmd.add_owners('freewill', ['user-new1@example.com']) 100 101 assert_equal '701229f217cdf23b1344c7b4b54ca97', @fetcher.last_request['Authorization'] 102 end 103 104 def test_remove_owners 105 response = "Owner removed successfully." 106 @fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 200, 'OK'] 107 108 use_ui @ui do 109 @cmd.remove_owners("freewill", ["user-remove1@example.com"]) 110 end 111 112 assert_equal Net::HTTP::Delete, @fetcher.last_request.class 113 assert_equal Gem.configuration.rubygems_api_key, @fetcher.last_request["Authorization"] 114 assert_equal "email=user-remove1%40example.com", @fetcher.last_request.body 115 116 assert_match response, @ui.output 117 end 118 119 def test_remove_owners_denied 120 response = "You don't have permission to push to this gem" 121 @fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 403, 'Forbidden'] 122 123 use_ui @ui do 124 @cmd.remove_owners("freewill", ["user-remove1@example.com"]) 125 end 126 127 assert_match response, @ui.output 128 end 129 130 def test_remove_owners_key 131 response = "Owner removed successfully." 132 @fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 200, 'OK'] 133 File.open Gem.configuration.credentials_path, 'a' do |f| 134 f.write ':other: 701229f217cdf23b1344c7b4b54ca97' 135 end 136 Gem.configuration.load_api_keys 137 138 @cmd.handle_options %w(-k other) 139 @cmd.remove_owners('freewill', ['user-remove1@example.com']) 140 141 assert_equal '701229f217cdf23b1344c7b4b54ca97', @fetcher.last_request['Authorization'] 142 end 143 144 def test_remove_owners_missing 145 response = 'Owner could not be found.' 146 @fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 404, 'Not Found'] 147 148 use_ui @ui do 149 @cmd.remove_owners("freewill", ["missing@example"]) 150 end 151 152 assert_equal "Removing missing@example: #{response}\n", @ui.output 153 end 154 155end 156