Prohlížeč zdrojového kódu
spec/requests/source_spec.rb
require "rails_helper"
RSpec.describe "Source", type: :request do
before { SourceBrowser.reload! }
describe "GET /source" do
it "returns success" do
get source_index_path
expect(response).to have_http_status(:success)
end
it "lists top-level entries" do
get source_index_path
expect(response.body).to include("app")
expect(response.body).to include("config")
expect(response.body).to include("spec")
end
end
describe "GET /source/*path/" do
it "returns 404 for a directory path" do
get source_show_path(path: "app/services")
expect(response).to have_http_status(:not_found)
end
it "shows a Ruby file with syntax highlighting" do
get source_show_path(path: "config/routes.rb")
expect(response).to have_http_status(:success)
expect(response.body).to include('<span class="k">')
end
it "shows a spec file" do
get source_show_path(path: "spec/rails_helper.rb")
expect(response).to have_http_status(:success)
end
it "shows a stylesheet" do
get source_show_path(path: "app/assets/stylesheets/application.scss")
expect(response).to have_http_status(:success)
end
it "shows a javascript file with full layout" do
get source_show_path(path: "app/javascript/application.js")
expect(response).to have_http_status(:success)
expect(response.body).to include("<!DOCTYPE html>")
expect(response.body).to include("Ruby Webování")
end
it "returns 404 for path traversal" do
get source_show_path(path: "../../etc/passwd")
expect(response).to have_http_status(:not_found)
end
it "returns 404 for non-whitelisted files" do
get source_show_path(path: "config/database.yml")
expect(response).to have_http_status(:not_found)
end
context "with a binary file" do
let(:image_path) { Rails.root.join("app/assets/images/test.png") }
let(:binary_content) { "\x89PNG\r\n\x1a\n" }
before do
image_path.binwrite(binary_content)
SourceBrowser.reload!
end
after { image_path.delete if image_path.exist? }
it "serves the binary file as an attachment with correct content" do
get source_show_path(path: "app/assets/images/test.png")
expect(response).to have_http_status(:success)
expect(response.headers["Content-Disposition"]).to include("attachment")
expect(response.body.b).to eq(binary_content.b)
end
end
end
end