Skip to content

Commit bf61731

Browse files
Copilothsbt
authored andcommitted
[ruby/rubygems] Add test coverage for skipping auto-attestation
ruby/rubygems@6cd04a57da Co-authored-by: hsbt <12301+hsbt@users.noreply.github.com>
1 parent e35eaf5 commit bf61731

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

test/rubygems/test_gem_commands_push_command.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,55 @@ def test_execute_attestation_fallback
155155
@fetcher.last_request["Content-Type"]
156156
end
157157

158+
def test_execute_attestation_skipped_on_non_rubygems_host
159+
@spec, @path = util_gem "freebird", "1.0.1" do |spec|
160+
spec.metadata["allowed_push_host"] = "https://privategemserver.example"
161+
end
162+
163+
@response = "Successfully registered gem: freebird (1.0.1)"
164+
@fetcher.data["#{@spec.metadata["allowed_push_host"]}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
165+
166+
@cmd.options[:args] = [@path]
167+
168+
attest_called = false
169+
@cmd.stub(:attest!, proc { attest_called = true; raise "attest! should not be called" }) do
170+
@cmd.execute
171+
end
172+
173+
refute attest_called, "attest! should not be called for non-rubygems.org hosts"
174+
assert_equal Gem::Net::HTTP::Post, @fetcher.last_request.class
175+
assert_equal Gem.read_binary(@path), @fetcher.last_request.body
176+
assert_equal "application/octet-stream",
177+
@fetcher.last_request["Content-Type"]
178+
end
179+
180+
def test_execute_attestation_skipped_on_jruby
181+
@response = "Successfully registered gem: freewill (1.0.0)"
182+
@fetcher.data["#{Gem.host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
183+
184+
@cmd.options[:args] = [@path]
185+
186+
attest_called = false
187+
engine = RUBY_ENGINE
188+
Object.send :remove_const, :RUBY_ENGINE
189+
Object.const_set :RUBY_ENGINE, "jruby"
190+
191+
begin
192+
@cmd.stub(:attest!, proc { attest_called = true; raise "attest! should not be called" }) do
193+
@cmd.execute
194+
end
195+
196+
refute attest_called, "attest! should not be called on JRuby"
197+
assert_equal Gem::Net::HTTP::Post, @fetcher.last_request.class
198+
assert_equal Gem.read_binary(@path), @fetcher.last_request.body
199+
assert_equal "application/octet-stream",
200+
@fetcher.last_request["Content-Type"]
201+
ensure
202+
Object.send :remove_const, :RUBY_ENGINE
203+
Object.const_set :RUBY_ENGINE, engine
204+
end
205+
end
206+
158207
def test_execute_allowed_push_host
159208
@spec, @path = util_gem "freebird", "1.0.1" do |spec|
160209
spec.metadata["allowed_push_host"] = "https://privategemserver.example"

0 commit comments

Comments
 (0)