|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2026, by Samuel Williams. |
| 5 | + |
| 6 | +require_relative "../lib/bake/gem/github/publisher" |
| 7 | + |
| 8 | +describe Bake::Gem::GitHub::Publisher do |
| 9 | + with "#registry_digest" do |
| 10 | + let(:publisher) {subject.allocate} |
| 11 | + let(:http) {Object.new} |
| 12 | + let(:responses) {{}} |
| 13 | + let(:version_path) {"/api/v2/rubygems/example/versions/1.0.1.json?platform=ruby"} |
| 14 | + let(:download_path) {"/downloads/example-1.0.1.gem"} |
| 15 | + |
| 16 | + def response(code, body = "") |
| 17 | + result = Net::HTTPResponse::CODE_TO_OBJ.fetch(code).new("1.1", code, "") |
| 18 | + mock(result) do |wrapper| |
| 19 | + wrapper.replace(:body){body} |
| 20 | + end |
| 21 | + result |
| 22 | + end |
| 23 | + |
| 24 | + before do |
| 25 | + mock(http) do |wrapper| |
| 26 | + wrapper.replace(:get){|path| responses.fetch(path)} |
| 27 | + end |
| 28 | + mock(Net::HTTP) do |wrapper| |
| 29 | + wrapper.replace(:start) do |*arguments, **options, &block| |
| 30 | + block.call(http) |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + it "recognizes an unpublished version without requesting the missing download" do |
| 36 | + responses[version_path] = response("404") |
| 37 | + expect(publisher.send(:registry_digest, "example", "1.0.1")).to be_nil |
| 38 | + end |
| 39 | + |
| 40 | + it "hashes the actual published package bytes" do |
| 41 | + responses[version_path] = response("200", "{}") |
| 42 | + responses[download_path] = response("200", "gem bytes\x00\xff".b) |
| 43 | + expect(publisher.send(:registry_digest, "example", "1.0.1")).to be == Digest::SHA256.hexdigest("gem bytes\x00\xff".b) |
| 44 | + end |
| 45 | + |
| 46 | + ["403", "500"].each do |code| |
| 47 | + it "rejects version API errors", unique: code do |
| 48 | + responses[version_path] = response(code) |
| 49 | + expect{publisher.send(:registry_digest, "example", "1.0.1")}.to raise_exception(RuntimeError, message: be == "Registry request failed: #{code}") |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + it "rejects a forbidden download for an existing version" do |
| 54 | + responses[version_path] = response("200", "{}") |
| 55 | + responses[download_path] = response("403") |
| 56 | + expect{publisher.send(:registry_digest, "example", "1.0.1")}.to raise_exception(RuntimeError, message: be == "Registry request failed: 403") |
| 57 | + end |
| 58 | + |
| 59 | + it "rejects a missing download for an existing version" do |
| 60 | + responses[version_path] = response("200", "{}") |
| 61 | + responses[download_path] = response("404") |
| 62 | + expect{publisher.send(:registry_digest, "example", "1.0.1")}.to raise_exception(RuntimeError, message: be =~ /Published gem download is missing/) |
| 63 | + end |
| 64 | + end |
| 65 | +end |
0 commit comments