summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_commands_cert_command.rb
blob: 9d5fc1eada128e59be9fe544bfd3d6881a774cff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
######################################################################
# This file is imported from the rubygems project.
# DO NOT make modifications in this repo. They _will_ be reverted!
# File a patch instead and assign it to Ryan Davis or Eric Hodel.
######################################################################

require 'rubygems/test_case'
require 'rubygems/commands/cert_command'

unless defined? OpenSSL then
  warn "`gem cert` tests are being skipped, module OpenSSL not found"
end

class TestGemCommandsCertCommand < Gem::TestCase

  def setup
    super

    @orig_security_trust_dir = Gem::Security::OPT[:trust_dir]
    Gem::Security::OPT[:trust_dir] = @tempdir

    @cmd = Gem::Commands::CertCommand.new

    root = File.expand_path(File.dirname(__FILE__))

    FileUtils.cp File.join(root, 'data', 'gem-private_key.pem'), @tempdir
    FileUtils.cp File.join(root, 'data', 'gem-public_cert.pem'), @tempdir

    @cert_file_name = File.join @tempdir, 'gem-public_cert.pem'
    @pkey_file_name = File.join @tempdir, 'gem-private_key.pem'
  end

  def teardown
    Gem::Security::OPT[:trust_dir] = @orig_security_trust_dir

    super
  end

  def test_execute_add
    use_ui @ui do
      @cmd.send :handle_options, %W[--add #{@cert_file_name}]
    end

    assert_equal "Added '/CN=rubygems/DC=example/DC=com'\n", @ui.output
    assert_equal '', @ui.error
  end

  def test_execute_build
    FileUtils.rm @cert_file_name
    FileUtils.rm @pkey_file_name

    use_ui @ui do
      Dir.chdir @tempdir do
        @cmd.send :handle_options, %W[--build nobody@example.com]
      end
    end

    output = @ui.output.split "\n"

    assert_equal 'Public Cert: gem-public_cert.pem', output.shift
    assert_equal 'Private Key: gem-private_key.pem', output.shift
    assert_equal 'Don\'t forget to move the key file to somewhere private...',
                 output.shift
    assert_equal [], output

    assert_equal '', @ui.error

    assert File.exist?(File.join(@tempdir, 'gem-private_key.pem'))
    assert File.exist?(File.join(@tempdir, 'gem-public_cert.pem'))
  end

  def test_execute_certificate
    use_ui @ui do
      @cmd.send :handle_options, %W[--certificate #{@cert_file_name}]
    end

    assert_equal '', @ui.output
    assert_equal '', @ui.error

    assert_equal File.read(@cert_file_name),
                 @cmd.options[:issuer_cert].to_s
  end

  def test_execute_list
    use_ui @ui do
      @cmd.send :handle_options, %W[--list]
    end

    assert_equal "/CN=rubygems/DC=example/DC=com\n", @ui.output
    assert_equal '', @ui.error
  end

  def test_execute_private_key
    use_ui @ui do
      @cmd.send :handle_options, %W[--private-key #{@pkey_file_name}]
    end

    assert_equal '', @ui.output
    assert_equal '', @ui.error

    assert_equal File.read(@pkey_file_name),
                 @cmd.options[:issuer_key].to_s
  end

  def test_execute_remove
    use_ui @ui do
      @cmd.send :handle_options, %W[--remove rubygems]
    end

    assert_equal "Removed '/CN=rubygems/DC=example/DC=com'\n", @ui.output
    assert_equal '', @ui.error

    refute File.exist?(@cert_file_name)
  end

  def test_execute_sign
    use_ui @ui do
      @cmd.send :handle_options, %W[
        -K #{@pkey_file_name} -C #{@cert_file_name} --sign #{@cert_file_name}
      ]
    end

    assert_equal '', @ui.output
    assert_equal '', @ui.error

    # HACK this test sucks
  end

end if defined? OpenSSL