main
rb 46 lines 1.57 KB
Raw
1 # frozen_string_literal: true
2
3 require "rails_helper"
4
5 RSpec.describe OgImageService do
6 let(:user) { User.create!(smbcloud_id: 9001, email: "alice@example.com", username: "alice") }
7 let(:repo) do
8 user.repositories.create!(
9 name: "widget", description: "A tiny widget library.",
10 kind: "code", default_branch: "main",
11 disk_path: "/nonexistent/alice/widget.git", stars_count: 7
12 )
13 end
14
15 it "builds a well-formed SVG with the repo name and owner" do
16 svg = described_class.send(:repository_svg, repo)
17 expect(svg).to start_with("<svg")
18 expect(svg).to include("@alice /")
19 expect(svg).to include("widget")
20 expect(svg).to include('width="1200"')
21 expect(svg).to include('height="630"')
22 end
23
24 it "XML-escapes user-controlled text to prevent SVG injection" do
25 repo.name = %q{x"><script>alert(1)</script>}
26 repo.description = "evil & <b>markup</b>"
27 svg = described_class.send(:repository_svg, repo)
28 expect(svg).not_to include("<script>")
29 expect(svg).to include("&lt;script&gt;")
30 expect(svg).to include("&amp;")
31 end
32
33 it "builds the default card without a repository" do
34 svg = described_class.send(:default_svg)
35 expect(svg).to start_with("<svg")
36 expect(svg).to include("Git hosting for the AI era")
37 end
38
39 it "renders PNG bytes, or raises a typed (catchable) error without libvips" do
40 png = described_class.render_default
41 expect(png).to start_with("\x89PNG".b)
42 rescue OgImageService::Error
43 # Graceful, controller-catchable failure on hosts without libvips/SVG support.
44 expect(true).to be(true)
45 end
46 end