Skip to content

Commit

Permalink
Merge pull request #2900 from tvdeyen/add-site-and-language-serializers
Browse files Browse the repository at this point in the history
Add Language and Site serializers
  • Loading branch information
tvdeyen authored May 28, 2024
2 parents 7fc489a + 1447087 commit 83c4892
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 20 deletions.
8 changes: 8 additions & 0 deletions app/serializers/alchemy/language_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Alchemy
class LanguageSerializer < ActiveModel::Serializer
attributes :id,
:name
end
end
8 changes: 8 additions & 0 deletions app/serializers/alchemy/site_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Alchemy
class SiteSerializer < ActiveModel::Serializer
attributes :id,
:name
end
end
46 changes: 46 additions & 0 deletions spec/requests/alchemy/api/pages_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require "rails_helper"

RSpec.describe "API Pages requests", type: :request do
describe "GET /api/pages/:id" do
let(:page) { create(:alchemy_page, :public) }

subject do
get("/api/pages/#{page.id}")
response
end

let(:json) { JSON.parse(response.body) }

context "with unauthorized user" do
it "returns page JSON with site not included" do
is_expected.to have_http_status(200)
expect(json).to_not include("site")
end

it "returns page JSON with language not included" do
is_expected.to have_http_status(200)
expect(json).to_not include("language")
end
end

context "with authorized user" do
before do
authorize_user(build(:alchemy_dummy_user, :as_author))
end

it "returns page JSON with site included" do
is_expected.to have_http_status(200)
expect(json).to include("site")
expect(json["site"]).to include("id")
expect(json["site"]).to include("name")
end

it "returns page JSON with language included" do
is_expected.to have_http_status(200)
expect(json).to include("language")
expect(json["language"]).to include("id")
expect(json["language"]).to include("name")
end
end
end
end
17 changes: 17 additions & 0 deletions spec/serializers/alchemy/language_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::LanguageSerializer do
subject { described_class.new(language).to_json }

let(:language) { build_stubbed(:alchemy_language) }

it "includes all attributes" do
json = JSON.parse(subject)
expect(json).to eq(
"id" => language.id,
"name" => language.name
)
end
end
22 changes: 2 additions & 20 deletions spec/serializers/alchemy/page_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,11 @@
"elements" => [],
"site" => {
"id" => page.site.id,
"aliases" => nil,
"created_at" => an_instance_of(String),
"host" => "*",
"name" => "Default Site",
"public" => true,
"redirect_to_primary_host" => false,
"updated_at" => an_instance_of(String)
"name" => "Default Site"
},
"language" => {
"country_code" => "",
"created_at" => an_instance_of(String),
"creator_id" => nil,
"default" => true,
"frontpage_name" => "Intro",
"id" => page.language_id,
"language_code" => "en",
"locale" => "en",
"name" => "Your Language",
"page_layout" => "index",
"public" => true,
"site_id" => page.site.id,
"updated_at" => an_instance_of(String),
"updater_id" => nil
"name" => "Your Language"
}
)
end
Expand Down
17 changes: 17 additions & 0 deletions spec/serializers/alchemy/site_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::SiteSerializer do
subject { described_class.new(site).to_json }

let(:site) { build_stubbed(:alchemy_site) }

it "includes all attributes" do
json = JSON.parse(subject)
expect(json).to eq(
"id" => site.id,
"name" => site.name
)
end
end

0 comments on commit 83c4892

Please sign in to comment.