1require 'rubygems/test_case'
2require 'rubygems/commands/yank_command'
3
4class TestGemCommandsYankCommand < Gem::TestCase
5  def setup
6    super
7
8    @cmd = Gem::Commands::YankCommand.new
9    @cmd.host = 'http://example'
10
11    @fetcher = Gem::RemoteFetcher.fetcher
12
13    Gem.configuration.rubygems_api_key = 'key'
14    Gem.configuration.api_keys[:KEY]  = 'other'
15  end
16
17  def test_handle_options
18    @cmd.handle_options %w[a --version 1.0 --platform x86-darwin -k KEY]
19
20    assert_equal %w[a],        @cmd.options[:args]
21    assert_equal 'KEY',        @cmd.options[:key]
22    assert_nil                 @cmd.options[:platform]
23    assert_equal req('= 1.0'), @cmd.options[:version]
24  end
25
26  def test_handle_options_missing_argument
27    %w[-v --version -p --platform].each do |option|
28      assert_raises OptionParser::MissingArgument do
29        @cmd.handle_options %W[a #{option}]
30      end
31    end
32  end
33
34  def test_execute
35    yank_uri = 'http://example/api/v1/gems/yank'
36    @fetcher.data[yank_uri] = ['Successfully yanked', 200, 'OK']
37
38    @cmd.options[:args]           = %w[a]
39    @cmd.options[:added_platform] = true
40    @cmd.options[:version]        = req('= 1.0')
41
42    use_ui @ui do
43      @cmd.execute
44    end
45
46    assert_match %r%Yanking gem from http://example%, @ui.output
47    assert_match %r%Successfully yanked%,      @ui.output
48
49    platform = Gem.platforms[1]
50    body = @fetcher.last_request.body.split('&').sort
51    assert_equal %W[gem_name=a platform=#{platform} version=1.0], body
52
53    assert_equal 'key', @fetcher.last_request['Authorization']
54
55    assert_equal [yank_uri], @fetcher.paths
56  end
57
58  def test_execute_key
59    yank_uri = 'http://example/api/v1/gems/yank'
60    @fetcher.data[yank_uri] = ['Successfully yanked', 200, 'OK']
61
62    @cmd.options[:args]    = %w[a]
63    @cmd.options[:version] = req('= 1.0')
64    @cmd.options[:key]     = 'KEY'
65
66    use_ui @ui do
67      @cmd.execute
68    end
69
70    body = @fetcher.last_request.body.split('&').sort
71    assert_equal %w[gem_name=a version=1.0], body
72    assert_equal 'other', @fetcher.last_request['Authorization']
73  end
74
75  def test_execute_undo
76    unyank_uri = 'http://example/api/v1/gems/unyank'
77    @fetcher.data[unyank_uri] = ['Successfully unyanked', 200, 'OK']
78
79    @cmd.options[:args]    = %w[a]
80    @cmd.options[:version] = req('= 1.0')
81    @cmd.options[:undo]    = true
82
83    use_ui @ui do
84      @cmd.execute
85    end
86
87    assert_match %r%Unyanking gem from http://example%, @ui.output
88    assert_match %r%Successfully unyanked%,      @ui.output
89
90    body = @fetcher.last_request.body.split('&').sort
91    assert_equal %w[gem_name=a version=1.0], body
92
93    assert_equal [unyank_uri], @fetcher.paths
94  end
95
96end
97
98