| 1 | # frozen_string_literal: true |
| 2 | |
| 3 | require "rails_helper" |
| 4 | |
| 5 | # The OG endpoint must always answer crawlers with a cacheable PNG (a generated |
| 6 | # card where libvips is available, a static fallback otherwise) and must never |
| 7 | # render a card for a private repo. |
| 8 | RSpec.describe "OG share-card images", type: :request do |
| 9 | let(:owner) { User.create!(smbcloud_id: 6001, email: "alice@example.com", username: "alice") } |
| 10 | let(:other) { User.create!(smbcloud_id: 6002, email: "bob@example.com", username: "bob") } |
| 11 | |
| 12 | let!(:public_repo) do |
| 13 | owner.repositories.create!( |
| 14 | name: "widget", description: "A tiny widget library.", |
| 15 | kind: "code", default_branch: "main", |
| 16 | disk_path: "/nonexistent/alice/widget.git", is_private: false, stars_count: 7 |
| 17 | ) |
| 18 | end |
| 19 | |
| 20 | let!(:dotted_repo) do |
| 21 | owner.repositories.create!( |
| 22 | name: "Qwen2.5-GGUF", description: "Quantized weights.", |
| 23 | kind: "model", default_branch: "main", |
| 24 | disk_path: "/nonexistent/alice/qwen.git", is_private: false |
| 25 | ) |
| 26 | end |
| 27 | |
| 28 | let!(:private_repo) do |
| 29 | other.repositories.create!( |
| 30 | name: "secret-internal-tool", description: "private", |
| 31 | kind: "code", default_branch: "main", |
| 32 | disk_path: "/nonexistent/bob/secret.git", is_private: true |
| 33 | ) |
| 34 | end |
| 35 | |
| 36 | it "returns a cacheable PNG for a public repo" do |
| 37 | get "/og/alice/widget.png" |
| 38 | expect(response).to have_http_status(:ok) |
| 39 | expect(response.media_type).to eq("image/png") |
| 40 | expect(response.headers["Cache-Control"]).to include("public") |
| 41 | expect(response.headers["ETag"]).to be_present |
| 42 | end |
| 43 | |
| 44 | it "routes dotted repo names correctly" do |
| 45 | get "/og/alice/Qwen2.5-GGUF.png" |
| 46 | expect(response).to have_http_status(:ok) |
| 47 | expect(response.media_type).to eq("image/png") |
| 48 | end |
| 49 | |
| 50 | it "serves a 304 on a conditional GET with a matching ETag" do |
| 51 | get "/og/alice/widget.png" |
| 52 | etag = response.headers["ETag"] |
| 53 | expect(etag).to be_present |
| 54 | |
| 55 | get "/og/alice/widget.png", headers: { "If-None-Match" => etag } |
| 56 | expect(response).to have_http_status(:not_modified) |
| 57 | end |
| 58 | |
| 59 | it "renders the generic sitewide card" do |
| 60 | get "/og.png" |
| 61 | expect(response).to have_http_status(:ok) |
| 62 | expect(response.media_type).to eq("image/png") |
| 63 | end |
| 64 | |
| 65 | it "404s for a private repo (no card, even though it exists)" do |
| 66 | get "/og/bob/secret-internal-tool.png" |
| 67 | expect(response).to have_http_status(:not_found) |
| 68 | end |
| 69 | |
| 70 | it "404s for an unknown repo" do |
| 71 | get "/og/alice/does-not-exist.png" |
| 72 | expect(response).to have_http_status(:not_found) |
| 73 | end |
| 74 | end |