decidim-restfull-blogs 0.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. checksums.yaml +7 -0
  2. data/app/controllers/decidim/api/rest_full/blogs/blogs_controller.rb +174 -0
  3. data/app/controllers/decidim/api/rest_full/components/blog_components_controller.rb +71 -0
  4. data/app/serializers/decidim/api/rest_full/blogs/blog_component_serializer.rb +25 -0
  5. data/app/serializers/decidim/api/rest_full/blogs/blog_serializer.rb +70 -0
  6. data/app/serializers/decidim/api/rest_full/blogs/post_serializer.rb +12 -0
  7. data/app/services/decidim/rest_full/blogs/blogs_operations.rb +108 -0
  8. data/config/locales/decidim_rest_full_blogs.en.yml +13 -0
  9. data/lib/decidim/rest_full/blogs/engine.rb +55 -0
  10. data/lib/decidim/rest_full/blogs/test_definitions.rb +6 -0
  11. data/lib/decidim/rest_full/test/definitions/blog.rb +112 -0
  12. data/lib/decidim/rest_full/test/definitions/blog_component_schema.rb +11 -0
  13. data/lib/decidim/rest_full/test/definitions/blogs.rb +3 -0
  14. data/lib/decidim/rest_full/test/definitions/tags/blog_tag.rb +26 -0
  15. data/lib/decidim-restfull-blogs.rb +13 -0
  16. data/spec/requests/decidim/api/rest_full/blogs/blogs_controller_create_spec.rb +83 -0
  17. data/spec/requests/decidim/api/rest_full/blogs/blogs_controller_index_spec.rb +168 -0
  18. data/spec/requests/decidim/api/rest_full/blogs/blogs_controller_show_pagination_spec.rb +125 -0
  19. data/spec/requests/decidim/api/rest_full/blogs/blogs_controller_show_spec.rb +234 -0
  20. data/spec/requests/decidim/api/rest_full/components/blog_components_controller_index_spec.rb +79 -0
  21. data/spec/requests/decidim/api/rest_full/components/blog_components_controller_show_spec.rb +54 -0
  22. data/spec/requests/decidim/api/rest_full/scenarios/blog_published_at_desc_prev_next_chain_spec.rb +71 -0
  23. metadata +99 -0
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Definitions
6
+ module Tags
7
+ BLOG = {
8
+ name: "Blogs",
9
+ description: <<~TXT.strip
10
+ Blog posts (`Decidim::Blogs::Post`) on components with the `:blogs` manifest.
11
+
12
+ **Visibility** is driven by `published_at`, not a separate published flag:
13
+ - `published_at` is `null` or in the **future** → the post is **not** shown in public index/show (unless you impersonate the author).
14
+ - `published_at` in the **past** → the post is visible to readers with `blogs.read`.
15
+
16
+ **Read**: `GET /blogs` and `GET /blogs/:id` (conditional caching on GET).
17
+
18
+ **Write** (`blogs.write`): create and update posts (async by default; `POST /blogs/sync`, `PUT /blogs/:id/sync`). **Delete** (`blogs.destroy`): remove a post (`DELETE /blogs/:id`, async; `DELETE /blogs/:id/sync` inline).
19
+ TXT
20
+ }.freeze
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ Decidim::RestFull::Test::OpenApiTagRegistry.register_tag(Decidim::RestFull::Definitions::Tags::BLOG)
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "decidim-restfull-core"
4
+
5
+ module Decidim
6
+ module RestFull
7
+ module Blogs
8
+ ENGINE_ROOT = File.expand_path("..", __dir__).freeze
9
+ end
10
+ end
11
+ end
12
+
13
+ require "decidim/rest_full/blogs/engine"
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+
5
+ RSpec.describe Decidim::Api::RestFull::Blogs::BlogsController do
6
+ path "/blogs" do
7
+ post "Create blog post (async)" do
8
+ tags "Blogs"
9
+ consumes "application/json"
10
+ produces "application/json"
11
+ operationId "createBlogPostAsync"
12
+ description <<~README
13
+ Enqueue creation of a blog post. Poll `GET /jobs/:uuid`.
14
+
15
+ Set `published_at` to control visibility (see **Blogs** tag). Requires `blogs.write` and impersonation.
16
+ README
17
+ parameter name: :body, in: :body, required: true,
18
+ schema: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:blog_post_create_payload) }
19
+
20
+ describe_api_endpoint(
21
+ controller: Decidim::Api::RestFull::Blogs::BlogsController,
22
+ action: :create,
23
+ security_types: [:impersonationFlow],
24
+ scopes: ["blogs"],
25
+ permissions: ["blogs.write"]
26
+ ) do
27
+ let!(:participatory_process) { create(:participatory_process, organization:) }
28
+ let!(:blog_component) { create(:component, participatory_space: participatory_process, manifest_name: "blogs", published_at: Time.zone.now) }
29
+ let(:body) do
30
+ {
31
+ data: {
32
+ component_id: blog_component.id,
33
+ attributes: {
34
+ title: { en: "New post" },
35
+ body: { en: "Content" },
36
+ published_at: 1.day.ago.iso8601
37
+ }
38
+ }
39
+ }
40
+ end
41
+
42
+ response "202", "Job accepted" do
43
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:rest_full_api_job_accepted)
44
+ run_test!(example_name: :accepted) do |response|
45
+ expect(response).to have_http_status(:accepted)
46
+ expect(JSON.parse(response.body)).to include("job_id")
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ path "/blogs/{id}" do
54
+ delete "Delete blog post (async)" do
55
+ tags "Blogs"
56
+ operationId "deleteBlogPostAsync"
57
+ description "Enqueue deletion of a blog post. Requires `blogs.destroy`."
58
+
59
+ parameter name: :id, in: :path, schema: { type: :integer }, required: true
60
+
61
+ describe_api_endpoint(
62
+ controller: Decidim::Api::RestFull::Blogs::BlogsController,
63
+ action: :destroy,
64
+ security_types: [:impersonationFlow],
65
+ scopes: ["blogs"],
66
+ permissions: ["blogs.destroy"]
67
+ ) do
68
+ let!(:participatory_process) { create(:participatory_process, organization:) }
69
+ let!(:blog_component) { create(:component, participatory_space: participatory_process, manifest_name: "blogs", published_at: Time.zone.now) }
70
+ let!(:post) { create(:post, component: blog_component, author: user, published_at: 1.day.ago) }
71
+ let(:id) { post.id }
72
+
73
+ response "202", "Job accepted" do
74
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:rest_full_api_job_accepted)
75
+ run_test!(example_name: :accepted) do |response|
76
+ expect(response).to have_http_status(:accepted)
77
+ expect(JSON.parse(response.body)).to include("job_id")
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+ RSpec.describe Decidim::Api::RestFull::Blogs::BlogsController do
5
+ path "/blogs" do
6
+ get "List posts" do
7
+ tags "Blogs"
8
+ produces "application/json"
9
+ operationId "listBlogPosts"
10
+ description "List posts (Decidim::Blogs::Post)"
11
+
12
+ let(:post_id) { blog_post.id }
13
+ let(:component_id) { component.id }
14
+ let(:space_id) { participatory_process.id }
15
+ let(:space_manifest) { "participatory_processes" }
16
+
17
+ it_behaves_like "localized params"
18
+ it_behaves_like "paginated params"
19
+ it_behaves_like "resource params"
20
+ it_behaves_like "ordered params", columns: %w(rand published_at)
21
+
22
+ describe_api_endpoint(
23
+ controller: Decidim::Api::RestFull::Blogs::BlogsController,
24
+ action: :index,
25
+ security_types: [:impersonationFlow, :credentialFlow],
26
+ scopes: ["blogs"],
27
+ permissions: ["blogs.read"]
28
+ ) do
29
+ let(:"locales[]") { %w(en fr) }
30
+ let!(:blog_posts) { create_list(:post, 3, component:, published_at: 1.day.ago, author: create(:user, :confirmed, organization:)) }
31
+ let!(:blog_post) { create(:post, component:, published_at: 2.days.ago, author: create(:user, :confirmed, organization:)) }
32
+ let!(:component) { create(:component, participatory_space: participatory_process, manifest_name: "blogs", published_at: Time.zone.now) }
33
+ let!(:participatory_process) { create(:participatory_process, organization:) }
34
+ it_behaves_like "localized endpoint"
35
+
36
+ response "200", "Blogs Found" do
37
+ produces "application/json"
38
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:blog_index_response)
39
+
40
+ context "when no posts (empty list)" do
41
+ before do
42
+ Decidim::Blogs::Post.where(component:).destroy_all
43
+ end
44
+
45
+ run_test!(example_name: :ok_empty) do |example|
46
+ body = JSON.parse(example.body)
47
+ expect(body["data"]).to eq([])
48
+ meta = body.fetch("meta")
49
+ expect(meta["page"]).to eq(1)
50
+ expect(meta["per_page"]).to eq(20)
51
+ expect(meta["has_more"]).to be(false)
52
+ expect(meta["next"]).to be_nil
53
+ expect(meta["prev"]).to be_nil
54
+ end
55
+ end
56
+
57
+ on_security(:impersonationFlow) do
58
+ context "when list own drafts" do
59
+ let!(:draft_post) do
60
+ post = create(:post, component:, published_at: nil, decidim_author_id: user.id)
61
+ post.published_at = 1.year.from_now
62
+ post.save!
63
+ post
64
+ end
65
+
66
+ let(:post_id) { draft_post.id }
67
+
68
+ run_test!(example_name: :impersonation_ok_draft) do |example|
69
+ data = JSON.parse(example.body)["data"]
70
+ expect(data.find { |d| d["meta"]["published"] == false }["id"]).to eq(draft_post.id.to_s)
71
+ end
72
+ end
73
+ end
74
+
75
+ context "when filtered by component_id" do
76
+ let!(:other_component) do
77
+ create(:component, participatory_space: participatory_process, manifest_name: "blogs", published_at: Time.zone.now)
78
+ end
79
+ let!(:other_post) do
80
+ create(:post, component: other_component, published_at: 1.day.ago, author: create(:user, :confirmed, organization:))
81
+ end
82
+ let(:component_id) { other_component.id }
83
+
84
+ run_test!(example_name: :filtered_by_component) do |example|
85
+ ids = JSON.parse(example.body)["data"].map { |row| row["id"] }
86
+ expect(ids).to eq([other_post.id.to_s])
87
+ end
88
+ end
89
+
90
+ # Regression: published filter must apply before ordered(); otherwise drafts inflate
91
+ # limit+1 and the last page keeps has_more=true (production: 2 published + drafts).
92
+ context "when two published posts are sorted and paginated" do
93
+ let(:order) { "published_at" }
94
+ let(:order_direction) { "desc" }
95
+ let(:per_page) { 1 }
96
+ let(:author) { create(:user, :confirmed, organization:) }
97
+ let!(:older_post) { create(:post, component:, author:, published_at: 2.days.ago) }
98
+ let!(:newer_post) { create(:post, component:, author:, published_at: 1.day.ago) }
99
+
100
+ before do
101
+ Decidim::Blogs::Post.where(component:).where.not(id: [older_post.id, newer_post.id]).destroy_all
102
+ create(
103
+ :post,
104
+ component:,
105
+ author:,
106
+ published_at: 1.year.from_now
107
+ )
108
+ end
109
+
110
+ context "when page=1" do
111
+ let(:page) { 1 }
112
+
113
+ run_test!(example_name: :ok_sorted_and_paginated) do |example|
114
+ body = JSON.parse(example.body)
115
+ expect(body["data"].map { |row| row["id"] }).to eq([newer_post.id.to_s])
116
+ meta = body.fetch("meta")
117
+ expect(meta["page"]).to eq(1)
118
+ expect(meta["per_page"]).to eq(1)
119
+ expect(meta["has_more"]).to be(true)
120
+ expect(meta["next"]).to be_present
121
+ expect(meta["prev"]).to be_nil
122
+ end
123
+ end
124
+
125
+ context "when page=2 (last page)" do
126
+ let(:page) { 2 }
127
+
128
+ run_test!(example_name: :ok_sorted_and_paginated_last) do |example|
129
+ body = JSON.parse(example.body)
130
+ expect(body["data"].map { |row| row["id"] }).to eq([older_post.id.to_s])
131
+ meta = body.fetch("meta")
132
+ expect(meta["page"]).to eq(2)
133
+ expect(meta["per_page"]).to eq(1)
134
+ expect(meta["has_more"]).to be(false)
135
+ expect(meta["next"]).to be_nil
136
+ expect(meta["prev"]).to be_present
137
+ end
138
+ end
139
+ end
140
+
141
+ it_behaves_like "ordered endpoint", columns: %w(rand published_at) do
142
+ let(:create_resource) { -> { create(:post, component:, author: create(:user, :confirmed, organization:)) } }
143
+ let(:each_resource) do
144
+ lambda { |resource, index|
145
+ resource.published_at = (index + 1).minutes.ago
146
+ resource.save!
147
+ }
148
+ end
149
+
150
+ let(:resources) { Decidim::Blogs::Post.all }
151
+ end
152
+
153
+ it_behaves_like "paginated endpoint" do
154
+ let(:create_resource) { -> { create(:post, component:, author: create(:user, :confirmed, organization:)) } }
155
+ let(:each_resource) do
156
+ lambda { |resource, index|
157
+ resource.published_at = (index + 1).minutes.ago
158
+ resource.save!
159
+ }
160
+ end
161
+
162
+ let(:resources) { Decidim::Blogs::Post.all }
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+
5
+ # Regression: chatbot "Read more" uses GET /blogs/:id with order=published_at&order_direction=desc and
6
+ # follows links.next. Two posts with the same published_at must not produce a non-deterministic LEAD/LAG
7
+ # chain (ResourcesController#order_string adds `id asc` as tie-break).
8
+ #
9
+ # Scenario: 3 posts — one on a newer day, two older posts sharing the exact same published_at.
10
+ # Sort DESC by published_at then id ASC among ties:
11
+ # 1st in chain = newest post (what users see first),
12
+ # 2nd in chain = tied post with lower id,
13
+ # 3rd in chain = tied post with higher id.
14
+ #
15
+ # Expectations:
16
+ # - GET show for the newest → links.next.resource_id = 2nd post (lower id among tied pair),
17
+ # - GET show for that 2nd post → links.next.resource_id = 3rd post,
18
+ # - GET show for the 3rd → links.next is nil.
19
+
20
+ RSpec.describe Decidim::Api::RestFull::Blogs::BlogsController do
21
+ path "/blogs/{id}" do
22
+ get "Show a blog detail (pagination / same published_at)" do
23
+ tags "Blogs"
24
+ produces "application/json"
25
+ security [{ credentialFlowBearer: ["blogs"] }, { resourceOwnerFlowBearer: ["blogs"] }]
26
+ operationId "blog_show_pagination"
27
+ description "Blog show links.next / links.prev when two posts share published_at (DESC + id tie-break)"
28
+ it_behaves_like "localized params"
29
+ it_behaves_like "resource params"
30
+ it_behaves_like "ordered params"
31
+ parameter name: "id", in: :path, schema: { type: :integer, description: "Blog Post Id" }
32
+
33
+ describe_api_endpoint(
34
+ controller: Decidim::Api::RestFull::Blogs::BlogsController,
35
+ action: :show,
36
+ security_types: [:impersonationFlow, :credentialFlow],
37
+ scopes: ["blogs"],
38
+ permissions: ["blogs.read"]
39
+ ) do
40
+ let(:component_id) { pagination_component.id }
41
+ let(:space_id) { participatory_process.id }
42
+ let(:space_manifest) { "participatory_processes" }
43
+ let(:"locales[]") { %w(en fr) }
44
+
45
+ let!(:participatory_process) { create(:participatory_process, organization:) }
46
+ let!(:pagination_component) do
47
+ create(:component, participatory_space: participatory_process, manifest_name: "blogs", published_at: Time.zone.now)
48
+ end
49
+
50
+ let(:shared_published_at) { 7.days.ago.change(usec: 0) }
51
+ let!(:newest_post) do
52
+ create(
53
+ :post,
54
+ component: pagination_component,
55
+ published_at: 1.day.ago,
56
+ author: create(:user, :confirmed, organization:)
57
+ )
58
+ end
59
+ let!(:tied_post_lower_id) do
60
+ create(
61
+ :post,
62
+ component: pagination_component,
63
+ published_at: shared_published_at,
64
+ author: create(:user, :confirmed, organization:)
65
+ )
66
+ end
67
+ let!(:tied_post_higher_id) do
68
+ create(
69
+ :post,
70
+ component: pagination_component,
71
+ published_at: shared_published_at,
72
+ author: create(:user, :confirmed, organization:)
73
+ )
74
+ end
75
+
76
+ let(:second_in_desc_chain_id) { [tied_post_lower_id.id, tied_post_higher_id.id].min }
77
+ let(:third_in_desc_chain_id) { [tied_post_lower_id.id, tied_post_higher_id.id].max }
78
+
79
+ # Default path id for rswag shared examples (403 / 500); pagination examples override.
80
+ let(:id) { newest_post.id }
81
+
82
+ response "200", "Blog Found" do
83
+ produces "application/json"
84
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:blog_item_response)
85
+
86
+ context "when three posts exist with two identical published_at and order is published_at DESC" do
87
+ let(:order) { "published_at" }
88
+ let(:order_direction) { "desc" }
89
+ let(:"filter[state_not_eq]") { "rejected" }
90
+
91
+ context "when the requested post is the newest (next is first tied row by id)" do
92
+ let(:id) { newest_post.id }
93
+
94
+ run_test! do |example|
95
+ data = JSON.parse(example.body)["data"]
96
+ expect(data["id"].to_i).to eq(newest_post.id)
97
+ expect(data["links"]["next"]["meta"]["resource_id"].to_i).to eq(second_in_desc_chain_id)
98
+ end
99
+ end
100
+
101
+ context "when the requested post is second in the chain (next is third)" do
102
+ let(:id) { second_in_desc_chain_id }
103
+
104
+ run_test! do |example|
105
+ data = JSON.parse(example.body)["data"]
106
+ expect(data["id"].to_i).to eq(second_in_desc_chain_id)
107
+ expect(data["links"]["next"]["meta"]["resource_id"].to_i).to eq(third_in_desc_chain_id)
108
+ end
109
+ end
110
+
111
+ context "when the requested post is third in the chain (no next)" do
112
+ let(:id) { third_in_desc_chain_id }
113
+
114
+ run_test! do |example|
115
+ data = JSON.parse(example.body)["data"]
116
+ expect(data["id"].to_i).to eq(third_in_desc_chain_id)
117
+ expect(data["links"]["next"]).to be_nil
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,234 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+ RSpec.describe Decidim::Api::RestFull::Blogs::BlogsController do
5
+ path "/blogs/{id}" do
6
+ get "Show post" do
7
+ tags "Blogs"
8
+ produces "application/json"
9
+ security [{ credentialFlowBearer: ["blogs"] }, { resourceOwnerFlowBearer: ["blogs"] }]
10
+ operationId "getBlogPost"
11
+ description "Get post details (Decidim::Blogs::Post)"
12
+ it_behaves_like "localized params"
13
+ it_behaves_like "resource params"
14
+ it_behaves_like "ordered params"
15
+ parameter name: "id", in: :path, schema: { type: :integer, description: "Post id" }
16
+
17
+ describe_api_endpoint(
18
+ controller: Decidim::Api::RestFull::Blogs::BlogsController,
19
+ action: :show,
20
+ security_types: [:impersonationFlow, :credentialFlow],
21
+ scopes: ["blogs"],
22
+ permissions: ["blogs.read"]
23
+ ) do
24
+ let(:id) { blog_post.id }
25
+ let(:component_id) { component.id }
26
+ let(:space_id) { participatory_process.id }
27
+ let(:space_manifest) { "participatory_processes" }
28
+
29
+ let(:"locales[]") { %w(en fr) }
30
+ let!(:blog_post) { create(:post, component:, published_at: 2.days.ago, author: create(:user, :confirmed, organization:)) }
31
+ let!(:component) { create(:component, participatory_space: participatory_process, manifest_name: "blogs", published_at: Time.zone.now) }
32
+ let!(:participatory_process) { create(:participatory_process, organization:) }
33
+
34
+ response "200", "Blog Found" do
35
+ produces "application/json"
36
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:blog_item_response)
37
+
38
+ context "when the post is alone" do
39
+ run_test! do |example|
40
+ data = JSON.parse(example.body)["data"]
41
+ expect(data["id"].to_i).to eq(blog_post.id.to_i)
42
+ expect(data["links"]["next"]).to be_nil
43
+ expect(data["links"]["prev"]).to be_nil
44
+ end
45
+ end
46
+
47
+ context "when ordering by published_at DESC" do
48
+ context "when selecting the last published blog, next is the 2nd last published blog, prev is nil" do
49
+ let(:order) { "published_at" }
50
+ let(:order_direction) { "desc" }
51
+ let(:last_blog_component) { create(:component, participatory_space: participatory_process, manifest_name: "blogs", published_at: Time.zone.now) }
52
+ let!(:first_blog) { create(:post, component: last_blog_component, published_at: 1.day.ago) }
53
+ let!(:second_blog) { create(:post, component: last_blog_component, published_at: 2.days.ago) }
54
+ let!(:third_blog) { create(:post, component: last_blog_component, published_at: 3.days.ago) }
55
+ let(:"filter[state_not_eq]") { "rejected" }
56
+ let(:component_id) { last_blog_component.id }
57
+ let(:id) { first_blog.id }
58
+
59
+ run_test! do |example|
60
+ data = JSON.parse(example.body)["data"]
61
+ expect(data["id"].to_i).to eq(id.to_i)
62
+ expect(data["meta"]["published"]).to be_truthy
63
+ expect(data["links"]["next"]).to be_present
64
+ expect(data["links"]["next"]["meta"]["resource_id"].to_i).to eq(second_blog.id.to_i)
65
+ expect(data["links"]["prev"]).to be_nil
66
+ end
67
+ end
68
+
69
+ context "when selecting the first published blog, prev is the second published blog, next is nil" do
70
+ let(:order) { "published_at" }
71
+ let(:order_direction) { "desc" }
72
+ let(:last_blog_component) { create(:component, participatory_space: participatory_process, manifest_name: "blogs", published_at: Time.zone.now) }
73
+ let!(:first_blog) { create(:post, component: last_blog_component, published_at: 1.day.ago) }
74
+ let!(:second_blog) { create(:post, component: last_blog_component, published_at: 2.days.ago) }
75
+ let!(:third_blog) { create(:post, component: last_blog_component, published_at: 3.days.ago) }
76
+ let(:component_id) { last_blog_component.id }
77
+ let(:id) { third_blog.id }
78
+
79
+ run_test! do |example|
80
+ data = JSON.parse(example.body)["data"]
81
+ expect(data["id"].to_i).to eq(id.to_i)
82
+ expect(data["meta"]["published"]).to be_truthy
83
+ expect(data["links"]["next"]).to be_nil
84
+ expect(data["links"]["prev"]).to be_present
85
+ expect(data["links"]["prev"]["meta"]["resource_id"].to_i).to eq(second_blog.id.to_i)
86
+ end
87
+ end
88
+ end
89
+
90
+ context "when the post is the last one (published_at ASC)" do
91
+ let(:order) { "published_at" }
92
+ let(:order_direction) { "asc" }
93
+
94
+ let!(:post_list) do
95
+ Array.new(3) { create(:post, component:, author: create(:user, :confirmed, organization:)) }.each_with_index do |post, index|
96
+ post.published_at = (index + 1).minutes.ago
97
+ post.save!
98
+ post
99
+ end.reverse
100
+ end
101
+ let(:id) { Decidim::Blogs::Post.where(component:).order(published_at: :asc).last.id }
102
+
103
+ run_test!(example_name: :ok_no_more) do |example|
104
+ data = JSON.parse(example.body)["data"]
105
+ posts = Decidim::Blogs::Post.where(component:).order(published_at: :asc).ids
106
+ expect(data["id"].to_i).to eq(posts.last.to_i)
107
+ expect(data["links"]["next"]).to be_nil
108
+ expect(data["links"]["prev"]).to be_present
109
+ expect(data["links"]["prev"]["meta"]["resource_id"].to_i).to eq(posts.last(2).first.to_i)
110
+ end
111
+ end
112
+
113
+ context "when the post is first one (published_at ASC)" do
114
+ let(:order) { "published_at" }
115
+ let(:order_direction) { "asc" }
116
+ let!(:posts) do
117
+ Array.new(3) { create(:post, component:, author: create(:user, :confirmed, organization:)) }.each_with_index do |post, index|
118
+ post.published_at = (index + 1).minutes.ago
119
+ post.save!
120
+ post
121
+ end
122
+ end
123
+ let!(:impersonate_token) { create(:oauth_access_token, scopes: "blogs", resource_owner_id: nil, application: api_client) }
124
+ let(:component_id) { component.id }
125
+ let!(:id) { Decidim::Blogs::Post.where(component:).order(published_at: :asc).first.id }
126
+
127
+ run_test!(example_name: :ok) do |example|
128
+ data = JSON.parse(example.body)["data"]
129
+ posts = Decidim::Blogs::Post.where(component:).order(published_at: :asc).ids
130
+ expect(data["id"].to_i).to eq(posts.first.to_i)
131
+ expect(data["links"]["prev"]).to be_nil
132
+ expect(data["links"]["next"]).to be_present
133
+ expect(data["links"]["next"]["meta"]["resource_id"].to_i).to eq(posts.second.to_i)
134
+ end
135
+ end
136
+
137
+ context "when the second post in a collection of 3" do
138
+ let(:order) { "published_at" }
139
+ let(:order_direction) { "asc" }
140
+ let!(:posts) do
141
+ Array.new(3) { create(:post, component:, author: create(:user, :confirmed, organization:)) }.each_with_index do |post, index|
142
+ post.published_at = (index + 1).minutes.ago
143
+ post.save!
144
+ post
145
+ end
146
+ end
147
+ let!(:impersonate_token) { create(:oauth_access_token, scopes: "blogs", resource_owner_id: nil, application: api_client) }
148
+
149
+ let!(:id) { Decidim::Blogs::Post.where(component:).order(published_at: :asc).second.id }
150
+
151
+ run_test!(example_name: :ok) do |example|
152
+ data = JSON.parse(example.body)["data"]
153
+ posts = Decidim::Blogs::Post.where(component:).order(published_at: :asc).ids
154
+ expect(data["id"].to_i).to eq(posts.second.to_i)
155
+ expect(data["links"]).to be_present
156
+ expect(data["links"]["next"]).to be_present
157
+ expect(data["links"]["next"]["meta"]["resource_id"].to_i).to eq(posts.third.to_i)
158
+ expect(data["links"]["prev"]).to be_present
159
+ expect(data["links"]["prev"]["meta"]["resource_id"]).to eq(posts.first.to_s)
160
+ end
161
+ end
162
+
163
+ on_security(:impersonationFlow) do
164
+ context "with draft" do
165
+ let!(:draft_post) do
166
+ post = create(:post, component:, published_at: nil, decidim_author_id: user.id)
167
+ post.published_at = 1.year.from_now
168
+ post.save!
169
+ post
170
+ end
171
+
172
+ let(:id) { draft_post.id }
173
+
174
+ run_test!(example_name: :ok_draft) do |example|
175
+ data = JSON.parse(example.body)["data"]
176
+ expect(data["id"].to_i).to eq(draft_post.id.to_i)
177
+ expect(data["meta"]["published"]).to be(false)
178
+ end
179
+ end
180
+ end
181
+ end
182
+
183
+ response "404", "Blog Not Found" do
184
+ produces "application/json"
185
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:error_response)
186
+
187
+ context "when id=bad_string" do
188
+ let(:id) { "bad_string" }
189
+
190
+ run_test!
191
+ end
192
+
193
+ context "when id=not_found" do
194
+ let(:id) { Decidim::Blogs::Post.last.id + 10 }
195
+
196
+ run_test!(example_name: :not_found)
197
+ end
198
+ end
199
+
200
+ it_behaves_like "localized endpoint"
201
+ end
202
+ end
203
+ end
204
+
205
+ describe "conditional GET" do
206
+ let!(:organization) { create(:organization, available_locales: %w(en fr)) }
207
+ let!(:participatory_process) { create(:participatory_process, organization:) }
208
+ let!(:component) { create(:component, participatory_space: participatory_process, manifest_name: "blogs", published_at: Time.zone.now) }
209
+ let!(:blog_post) { create(:post, component:, published_at: 2.days.ago) }
210
+ let!(:api_client) do
211
+ c = create(:api_client, organization:, scopes: ["blogs"])
212
+ c.permissions = [c.permissions.build(permission: "blogs.read")]
213
+ c.save!
214
+ c
215
+ end
216
+ let!(:token) { create(:oauth_access_token, scopes: "blogs", resource_owner_id: nil, application: api_client) }
217
+
218
+ before { host! organization.host }
219
+
220
+ it "returns 304 when If-None-Match matches ResourceShowFingerprint" do
221
+ path = "/api/rest_full/v#{Decidim::RestFull.major_minor_version}/blogs/#{blog_post.id}"
222
+ qs = "?component_id=#{component.id}&space_id=#{participatory_process.id}&space_manifest=participatory_processes"
223
+ headers = { "Authorization" => "Bearer #{token.token}" }
224
+
225
+ get(path + qs, headers:)
226
+ expect(response).to have_http_status(:ok)
227
+ etag = response.headers["ETag"]
228
+ expect(etag).to be_present
229
+
230
+ get(path + qs, headers: headers.merge("If-None-Match" => etag))
231
+ expect(response).to have_http_status(:not_modified)
232
+ end
233
+ end
234
+ end