diff --git a/app/controllers/api/brands_controller.rb b/app/controllers/api/brands_controller.rb new file mode 100644 index 0000000000..aca23beb94 --- /dev/null +++ b/app/controllers/api/brands_controller.rb @@ -0,0 +1,12 @@ +class Api::BrandsController < ApplicationController + def show + expires_in 5.minutes, public: true + render json: brand.as_json(only: %i[name slug]) + end + +private + + def brand + Brand.find_by!(id: params[:brand_id]) + end +end diff --git a/config/routes.rb b/config/routes.rb index 6cf6a3bafb..f8ec935158 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -299,6 +299,8 @@ get "/:tag", to: "api/form_documents#show", as: :form_document, constraints: { tag: /draft|live|archived/ } get "/group", to: "api/form_documents#group", as: :form_group end + + get "brands/:brand_id", to: "api/brands#show", as: :brand end get "/maintenance" => "errors#maintenance", as: :maintenance_page diff --git a/spec/requests/api/brands_controller_spec.rb b/spec/requests/api/brands_controller_spec.rb new file mode 100644 index 0000000000..d49db49f80 --- /dev/null +++ b/spec/requests/api/brands_controller_spec.rb @@ -0,0 +1,41 @@ +require "rails_helper" + +RSpec.describe Api::BrandsController, type: :request do + let(:headers) { { "ACCEPT": "application/json" } } + + describe "#show" do + context "when the brand exists" do + let(:brand) { create :brand, name: "Golden Zephyr", slug: "golden-zephyr" } + + before do + get "/api/v2/brands/#{brand.id}", headers: + end + + it "returns http success" do + expect(response).to have_http_status(:success) + end + + it "returns the brand configuration" do + expect(response.parsed_body).to eq({ + "name" => "Golden Zephyr", + "slug" => "golden-zephyr", + }) + end + + it "sets the response to be cached for 5 minutes" do + expect(response.headers["Cache-Control"]).to eq("max-age=300, public") + end + end + + context "when the brand does not exist" do + before do + get "/api/v2/brands/0", headers: + end + + it "returns http not found" do + expect(response).to have_http_status(:not_found) + expect(response.headers["Content-Type"]).to eq("application/json; charset=utf-8") + end + end + end +end