decidim-restfull-proposals 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 (56) hide show
  1. checksums.yaml +7 -0
  2. data/app/controllers/decidim/api/rest_full/components/proposal_components_controller.rb +91 -0
  3. data/app/controllers/decidim/api/rest_full/draft_proposals/draft_proposals_controller.rb +116 -0
  4. data/app/controllers/decidim/api/rest_full/proposals/proposal_extended_data_controller.rb +31 -0
  5. data/app/controllers/decidim/api/rest_full/proposals/proposals_controller.rb +172 -0
  6. data/app/controllers/decidim/api/rest_full/vote_proposals/vote_proposals_controller.rb +90 -0
  7. data/app/jobs/decidim/rest_full/proposals/proposal_webhook_job.rb +67 -0
  8. data/app/models/decidim/rest_full/proposals/proposal_application_id.rb +14 -0
  9. data/app/serializers/decidim/api/rest_full/proposals/draft_proposal_serializer.rb +44 -0
  10. data/app/serializers/decidim/api/rest_full/proposals/proposal_component_serializer.rb +232 -0
  11. data/app/serializers/decidim/api/rest_full/proposals/proposal_serializer.rb +113 -0
  12. data/app/serializers/decidim/api/rest_full/proposals/proposal_state_serializer.rb +39 -0
  13. data/app/serializers/decidim/api/rest_full/proposals/proposal_webhook_serializer.rb +54 -0
  14. data/app/serializers/decidim/api/rest_full/proposals/vote_proposal_serializer.rb +27 -0
  15. data/app/services/decidim/rest_full/proposals/draft_proposals_operations.rb +248 -0
  16. data/app/services/decidim/rest_full/proposals/proposal_vote_eligibility.rb +106 -0
  17. data/app/services/decidim/rest_full/proposals/proposal_votes_operations.rb +173 -0
  18. data/app/services/decidim/rest_full/proposals/vote_proposals_operations.rb +114 -0
  19. data/config/locales/decidim_rest_full_proposals.en.yml +26 -0
  20. data/lib/decidim/rest_full/http_cache/proposal_show_fingerprint.rb +77 -0
  21. data/lib/decidim/rest_full/proposals/engine.rb +129 -0
  22. data/lib/decidim/rest_full/proposals/proposal_client_id_override.rb +18 -0
  23. data/lib/decidim/rest_full/proposals/proposals_controller_override.rb +28 -0
  24. data/lib/decidim/rest_full/proposals/ransackers.rb +84 -0
  25. data/lib/decidim/rest_full/proposals/test_definitions.rb +7 -0
  26. data/lib/decidim/rest_full/test/definitions/draft_proposal.rb +101 -0
  27. data/lib/decidim/rest_full/test/definitions/proposal.rb +115 -0
  28. data/lib/decidim/rest_full/test/definitions/proposal_component_schema.rb +110 -0
  29. data/lib/decidim/rest_full/test/definitions/proposals.rb +4 -0
  30. data/lib/decidim/rest_full/test/definitions/tags/draft_proposals_tag.rb +26 -0
  31. data/lib/decidim/rest_full/test/definitions/tags/proposal_tag.rb +32 -0
  32. data/lib/decidim/rest_full/test/definitions/vote_proposal.rb +87 -0
  33. data/lib/decidim-restfull-proposals.rb +17 -0
  34. data/spec/jobs/decidim/rest_full/proposals/proposal_webhook_job_spec.rb +66 -0
  35. data/spec/requests/decidim/api/rest_full/components/proposal_components_controller_index_spec.rb +149 -0
  36. data/spec/requests/decidim/api/rest_full/components/proposal_components_controller_show_spec.rb +140 -0
  37. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_create_spec.rb +116 -0
  38. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_delete_spec.rb +64 -0
  39. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_publish_spec.rb +132 -0
  40. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_show_spec.rb +72 -0
  41. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_update_spec.rb +307 -0
  42. data/spec/requests/decidim/api/rest_full/proposals/proposal_extended_data_controller_spec.rb +80 -0
  43. data/spec/requests/decidim/api/rest_full/proposals/proposals_controller_index_spec.rb +275 -0
  44. data/spec/requests/decidim/api/rest_full/proposals/proposals_controller_show_spec.rb +187 -0
  45. data/spec/requests/decidim/api/rest_full/scenarios/draft_publish_async_increments_count_and_lists_newest_spec.rb +144 -0
  46. data/spec/requests/decidim/api/rest_full/scenarios/participatory_process_phase_change_webhook_and_component_meta_spec.rb +130 -0
  47. data/spec/requests/decidim/api/rest_full/scenarios/proposals_published_at_desc_prev_next_chain_spec.rb +72 -0
  48. data/spec/requests/decidim/api/rest_full/scenarios/search_vote_enabled_impersonate_and_vote_spec.rb +88 -0
  49. data/spec/requests/decidim/api/rest_full/vote_proposals/vote_proposals_controller_spec.rb +231 -0
  50. data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_chatbot_menu_spec.rb +147 -0
  51. data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_phases_spec.rb +88 -0
  52. data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_votes_spec.rb +103 -0
  53. data/spec/serializers/decidim/api/rest_full/proposals/proposal_serializer_can_vote_spec.rb +86 -0
  54. data/spec/serializers/decidim/api/rest_full/proposals/proposal_webhook_serializer_spec.rb +63 -0
  55. data/spec/services/decidim/rest_full/proposals/proposal_vote_eligibility_spec.rb +120 -0
  56. metadata +132 -0
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+ RSpec.describe Decidim::Api::RestFull::DraftProposals::DraftProposalsController do
5
+ path "/draft_proposals/{id}/sync" do
6
+ delete "Withdraw a draft proposal" do
7
+ tags "Draft Proposals"
8
+ produces "application/json"
9
+ operationId "withdrawDraftProposal"
10
+ description "Withdraw (delete) a draft proposal. This action cannot be undone."
11
+ parameter name: "id", in: :path, schema: { type: :integer, description: "Draft Id" }, required: true
12
+
13
+ describe_api_endpoint(
14
+ controller: Decidim::Api::RestFull::DraftProposals::DraftProposalsController,
15
+ action: :destroy_sync,
16
+ security_types: [:impersonationFlow],
17
+ scopes: ["proposals"],
18
+ permissions: ["proposals.draft"]
19
+ ) do
20
+ let!(:organization) { create(:organization, available_locales: ["en"]) }
21
+ let!(:participatory_process) { create(:participatory_process, organization:) }
22
+ let(:proposal_component) { create(:component, participatory_space: participatory_process, manifest_name: "proposals", published_at: Time.zone.now) }
23
+ let(:space_manifest) { "participatory_processes" }
24
+ let(:space_id) { participatory_process.id }
25
+ let(:component_id) { proposal_component.id }
26
+ let!(:proposal) do
27
+ create(:proposal, published_at: nil, component: proposal_component, users: [user])
28
+ end
29
+ let(:id) { proposal.id }
30
+
31
+ response "200", "Draft proposal Removed" do
32
+ produces "application/json"
33
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:draft_proposal_item_response)
34
+
35
+ context "when all fields were valid" do
36
+ let!(:proposal) do
37
+ prop = create(:proposal, published_at: nil, component: proposal_component, users: [user])
38
+ prop.update(rest_full_application: Decidim::RestFull::Proposals::ProposalApplicationId.new(proposal_id: prop.id, api_client_id: api_client.id))
39
+ prop
40
+ end
41
+ let(:id) { proposal.id }
42
+
43
+ run_test!(example_name: :ok) do |example|
44
+ data = JSON.parse(example.body)["data"]
45
+ expect(data["id"]).to eq(proposal.id.to_s)
46
+ end
47
+ end
48
+ end
49
+
50
+ response "404", "Not Found" do
51
+ consumes "application/json"
52
+ produces "application/json"
53
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:error_response)
54
+
55
+ context "with no draft" do
56
+ let(:id) { Decidim::Proposals::Proposal.maximum(:id).to_i + 1 }
57
+
58
+ run_test!(example_name: :not_found)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+ RSpec.describe Decidim::Api::RestFull::DraftProposals::DraftProposalsController do
5
+ path "/draft_proposals/{id}/publish" do
6
+ post "Publish a draft proposal (async)" do
7
+ tags "Draft Proposals"
8
+ produces "application/json"
9
+ security [{ resourceOwnerFlowBearer: ["proposals"] }]
10
+ operationId "publishDraftProposalAsync"
11
+ description <<~README
12
+ Enqueue publication of a draft proposal. Poll `GET /jobs/:uuid` for the published proposal payload.
13
+
14
+ The draft must be **publishable** (`meta.publishable` on the last update). Requires impersonation (`proposals.draft`).
15
+
16
+ For synchronous publication, use `POST /draft_proposals/{id}/publish/sync`.
17
+ README
18
+ parameter name: "id", in: :path, schema: { type: :integer, description: "Draft Id" }, required: true
19
+
20
+ describe_api_endpoint(
21
+ controller: Decidim::Api::RestFull::DraftProposals::DraftProposalsController,
22
+ action: :publish,
23
+ security_types: [:impersonationFlow],
24
+ scopes: ["proposals"],
25
+ permissions: ["proposals.draft"]
26
+ ) do
27
+ let!(:participatory_process) { create(:participatory_process, organization:) }
28
+ let(:proposal_component) { create(:component, participatory_space: participatory_process, manifest_name: "proposals", published_at: Time.zone.now) }
29
+ let!(:proposal) do
30
+ prop = create(:proposal, published_at: nil, component: proposal_component, users: [user], title: { en: "Title" }, body: { en: "Body" })
31
+ prop.update(rest_full_application: Decidim::RestFull::Proposals::ProposalApplicationId.new(proposal_id: prop.id, api_client_id: api_client.id))
32
+ prop
33
+ end
34
+ let(:id) { proposal.id }
35
+
36
+ response "202", "Job accepted" do
37
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:rest_full_api_job_accepted)
38
+ run_test!(example_name: :accepted) do |response|
39
+ expect(response).to have_http_status(:accepted)
40
+ expect(JSON.parse(response.body)).to include("job_id")
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ path "/draft_proposals/{id}/publish/sync" do
48
+ post "Publish a draft proposal (sync)" do
49
+ tags "Draft Proposals"
50
+ produces "application/json"
51
+ security [{ resourceOwnerFlowBearer: ["proposals"] }]
52
+ operationId "publishDraftProposal"
53
+ description <<~README
54
+ Publish a draft proposal inline and return the published proposal resource.
55
+
56
+ Same validation rules as the async publish endpoint. Requires impersonation (`proposals.draft`).
57
+ README
58
+ parameter name: "id", in: :path, schema: { type: :integer, description: "Draft Id" }, required: true
59
+
60
+ describe_api_endpoint(
61
+ controller: Decidim::Api::RestFull::DraftProposals::DraftProposalsController,
62
+ action: :publish_sync,
63
+ security_types: [:impersonationFlow],
64
+ scopes: ["proposals"],
65
+ permissions: ["proposals.draft"]
66
+ ) do
67
+ let!(:participatory_process) { create(:participatory_process, organization:) }
68
+ let(:proposal_component) { create(:component, participatory_space: participatory_process, manifest_name: "proposals", published_at: Time.zone.now) }
69
+ let!(:proposal) { create(:proposal, component: proposal_component) }
70
+
71
+ let(:space_manifest) { "participatory_processes" }
72
+ let(:space_id) { participatory_process.id }
73
+ let(:component_id) { proposal_component.id }
74
+ let(:id) { proposal.id }
75
+ response "200", "Draft Proposal published" do
76
+ produces "application/json"
77
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:proposal_item_response)
78
+
79
+ context "when all fields are valid" do
80
+ let!(:proposal) do
81
+ prop = create(:proposal, published_at: nil, component: proposal_component, users: [user])
82
+ prop.update(rest_full_application: Decidim::RestFull::Proposals::ProposalApplicationId.new(proposal_id: prop.id, api_client_id: api_client.id))
83
+ prop
84
+ end
85
+ let(:id) { proposal.id }
86
+
87
+ run_test!(example_name: :ok) do |example|
88
+ data = JSON.parse(example.body)["data"]
89
+ expect(data["id"]).to eq(proposal.id.to_s)
90
+ expect(data["meta"]["published"]).to be_truthy
91
+ end
92
+ end
93
+ end
94
+
95
+ response "400", "Bad request" do
96
+ produces "application/json"
97
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:error_response)
98
+
99
+ context "when body is invalid" do
100
+ let!(:proposal) do
101
+ prop = create(:proposal, published_at: nil, component: proposal_component, users: [user])
102
+ prop.update(rest_full_application: Decidim::RestFull::Proposals::ProposalApplicationId.new(proposal_id: prop.id, api_client_id: api_client.id))
103
+ prop
104
+ end
105
+ let(:id) { proposal.id }
106
+
107
+ before do
108
+ proposal.body = nil
109
+ proposal.save(validate: false)
110
+ end
111
+
112
+ run_test!(example_name: :bad_request) do |_example|
113
+ expect(response.body).to include("Body cannot be blank")
114
+ end
115
+ end
116
+ end
117
+
118
+ response "404", "Not Found" do
119
+ consumes "application/json"
120
+ produces "application/json"
121
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:error_response)
122
+
123
+ context "with no draft" do
124
+ let(:id) { Decidim::Proposals::Proposal.maximum(:id).to_i + 1 }
125
+
126
+ run_test!(example_name: :not_found)
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+ RSpec.describe Decidim::Api::RestFull::DraftProposals::DraftProposalsController do
5
+ path "/draft_proposals/{id}" do
6
+ get "Display a draft proposal" do
7
+ tags "Draft Proposals"
8
+ produces "application/json"
9
+ operationId "getDraftProposal"
10
+ description "Detail a draft proposal. Raise HTTP 404 error if no draft is created for now."
11
+
12
+ parameter name: "id", in: :path, schema: { type: :integer, description: "Draft Id" }, required: true
13
+ describe_api_endpoint(
14
+ controller: Decidim::Api::RestFull::DraftProposals::DraftProposalsController,
15
+ action: :show,
16
+ security_types: [:impersonationFlow],
17
+ scopes: ["proposals"],
18
+ permissions: ["proposals.draft"]
19
+ ) do
20
+ let!(:participatory_process) { create(:participatory_process, organization:) }
21
+ let(:proposal_component) { create(:component, participatory_space: participatory_process, manifest_name: "proposals", published_at: Time.zone.now) }
22
+ let!(:proposal) { create(:proposal, component: proposal_component) }
23
+ let(:id) { proposal.id }
24
+ let(:space_manifest) { "participatory_processes" }
25
+ let(:space_id) { participatory_process.id }
26
+ let(:component_id) { proposal_component.id }
27
+
28
+ response "200", "Draft Found" do
29
+ produces "application/json"
30
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:draft_proposal_item_response)
31
+
32
+ context "when some fields are invalid" do
33
+ let!(:proposal) do
34
+ prop = create(:proposal, published_at: nil, component: proposal_component, users: [user])
35
+ prop.update(rest_full_application: Decidim::RestFull::Proposals::ProposalApplicationId.new(proposal_id: prop.id, api_client_id: api_client.id))
36
+ prop
37
+ end
38
+ let(:id) { proposal.id }
39
+
40
+ before do
41
+ proposal.body = nil
42
+ proposal.save(validate: false)
43
+ end
44
+
45
+ run_test!(example_name: :ok) do |example|
46
+ data = JSON.parse(example.body)["data"]
47
+ expect(data["id"]).to eq(proposal.id.to_s)
48
+
49
+ expect(data["attributes"]["errors"]).to eq({
50
+ "title" => [],
51
+ "body" => ["Body cannot be blank"]
52
+ })
53
+ expect(data["meta"]["publishable"]).to be_falsy
54
+ end
55
+ end
56
+ end
57
+
58
+ response "404", "Not Found" do
59
+ consumes "application/json"
60
+ produces "application/json"
61
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:error_response)
62
+
63
+ context "with no draft" do
64
+ let(:id) { Decidim::Proposals::Proposal.maximum(:id).to_i + 1 }
65
+
66
+ run_test!(example_name: :not_found)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,307 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+ RSpec.describe Decidim::Api::RestFull::DraftProposals::DraftProposalsController do
5
+ path "/draft_proposals/{id}/sync" do
6
+ put "Update draft proposal" do
7
+ tags "Draft Proposals"
8
+ produces "application/json"
9
+ operationId "updateDraftProposal"
10
+ description <<~README
11
+ Update a draft proposal owned by this API client.
12
+
13
+ Drafts updated here stay hidden on the Decidim front-end. Drafts created in the Decidim UI are not editable through this API.
14
+
15
+ Send `title` and `body` as strings. Do not send a locale. The server stores each field as an object with **one** locale key. The response includes only that key.
16
+
17
+ The locale is the impersonated user's `locale`. When `locale` is null, the server uses the organization `default_locale`.
18
+
19
+ Only an impersonation token (ROPC) is accepted. A service token is rejected.
20
+
21
+ Field errors cover only the fields present in the request. `data.meta.publishable` tells you if the draft can be published. An empty `data` object does not move locales and does not write.
22
+
23
+ ## Update sequence
24
+
25
+ A draft keeps a single locale. Changing the user's locale and sending a new value runs two steps: **move**, then **overlay**.
26
+
27
+ ### 1. Edited while the user locale is `fr`
28
+
29
+ ```json
30
+ {
31
+ "title": {
32
+ "fr": "J'aimerais de nouveaux bancs publiques avec des prises USB-C"
33
+ },
34
+ "body": {
35
+ "fr": "<p>Lorem Ipsum...</p>"
36
+ }
37
+ }
38
+ ```
39
+
40
+ ### 2. The user locale becomes `en`, and the client sends a new title
41
+
42
+ ```json
43
+ {
44
+ "data": {
45
+ "title": "I would like new benches with USB-C charging ports"
46
+ }
47
+ }
48
+ ```
49
+
50
+ **Move.** Strings stay the same. Keys change to the current locale. `fr` is removed.
51
+
52
+ ```json
53
+ {
54
+ "title": {
55
+ "en": "J'aimerais de nouveaux bancs publiques avec des prises USB-C"
56
+ },
57
+ "body": {
58
+ "en": "<p>Lorem Ipsum...</p>"
59
+ }
60
+ }
61
+ ```
62
+
63
+ **Overlay.** Only fields in the request are replaced. `body` was not sent, so it stays on the moved string.
64
+
65
+ ```json
66
+ {
67
+ "title": {
68
+ "en": "I would like new benches with USB-C charging ports"
69
+ },
70
+ "body": {
71
+ "en": "<p>Lorem Ipsum...</p>"
72
+ }
73
+ }
74
+ ```
75
+
76
+ The same two steps apply for `es`, and for a null user locale resolved to the organization `default_locale`.
77
+ README
78
+
79
+ parameter name: "id", in: :path, schema: { type: :integer, description: "Draft Id" }, required: true
80
+ parameter name: :body, in: :body, required: true, schema: {
81
+ type: :object,
82
+ title: "Update Draft Proposal Payload",
83
+ properties: {
84
+ data: {
85
+ type: :object,
86
+ title: "Update Draft Proposal Payload Data",
87
+ properties: {
88
+ title: { type: :string, description: "Title of the draft. Stored under the impersonated user's locale, or the organization default_locale when that locale is null." },
89
+ body: { type: :string, description: "Content of the draft. Stored under the same locale as title." }
90
+ },
91
+ description: "Payload to update in the proposal"
92
+ }
93
+ }, required: [:data]
94
+ }
95
+
96
+ describe_api_endpoint(
97
+ controller: Decidim::Api::RestFull::DraftProposals::DraftProposalsController,
98
+ action: :update_sync,
99
+ security_types: [:impersonationFlow],
100
+ scopes: ["proposals"],
101
+ permissions: ["proposals.draft"]
102
+ ) do
103
+ let!(:organization) { create(:organization, available_locales: %w(en fr es), default_locale: "en") }
104
+ let(:valid_title) { "This is a valid proposal title sample" }
105
+ let!(:participatory_process) { create(:participatory_process, organization:) }
106
+ let(:proposal_component) { create(:component, participatory_space: participatory_process, manifest_name: "proposals", published_at: Time.zone.now) }
107
+ let!(:proposal) do
108
+ prop = create(:proposal, published_at: nil, component: proposal_component, users: [user])
109
+ prop.update(rest_full_application: Decidim::RestFull::Proposals::ProposalApplicationId.new(proposal_id: prop.id, api_client_id: api_client.id))
110
+ prop.body = nil
111
+ prop.title = nil
112
+ prop.save(validate: false)
113
+ prop
114
+ end
115
+ let(:id) { proposal.id }
116
+ let(:space_manifest) { "participatory_processes" }
117
+ let(:space_id) { participatory_process.id }
118
+ let(:component_id) { proposal_component.id }
119
+
120
+ response "200", "Draft updated" do
121
+ produces "application/json"
122
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:draft_proposal_item_response)
123
+
124
+ context "when updating the title in fr" do
125
+ let(:user) { create(:user, locale: "fr", organization:, confirmed_at: Time.zone.now) }
126
+ let(:body) { { data: { title: valid_title } } }
127
+
128
+ run_test!(example_name: :ok_update_title_fr) do |example|
129
+ data = JSON.parse(example.body)["data"]
130
+ expect(data["attributes"]["title"]).to eq({ "fr" => valid_title })
131
+ expect(proposal.reload.title).to eq({ "fr" => valid_title })
132
+ end
133
+ end
134
+
135
+ context "when updating the title in en" do
136
+ let(:user) { create(:user, locale: "en", organization:, confirmed_at: Time.zone.now) }
137
+ let(:body) { { data: { title: valid_title } } }
138
+
139
+ run_test!(example_name: :ok_update_title_en) do |example|
140
+ data = JSON.parse(example.body)["data"]
141
+ expect(data["attributes"]["title"]).to eq({ "en" => valid_title })
142
+ expect(proposal.reload.title).to eq({ "en" => valid_title })
143
+ end
144
+ end
145
+
146
+ context "when updating the title in es" do
147
+ let(:user) { create(:user, locale: "es", organization:, confirmed_at: Time.zone.now) }
148
+ let(:body) { { data: { title: valid_title } } }
149
+
150
+ run_test!(example_name: :ok_update_title_es) do |example|
151
+ data = JSON.parse(example.body)["data"]
152
+ expect(data["attributes"]["title"]).to eq({ "es" => valid_title })
153
+ expect(proposal.reload.title).to eq({ "es" => valid_title })
154
+ end
155
+ end
156
+
157
+ context "when the user locale is nil" do
158
+ let(:user) { create(:user, locale: nil, organization:, confirmed_at: Time.zone.now) }
159
+ let(:body) { { data: { title: valid_title } } }
160
+
161
+ run_test!(example_name: :ok_update_title_default_locale) do |example|
162
+ data = JSON.parse(example.body)["data"]
163
+ expect(data["attributes"]["title"]).to eq({ "en" => valid_title })
164
+ expect(proposal.reload.title).to eq({ "en" => valid_title })
165
+ end
166
+ end
167
+
168
+ context "when updating the body in en" do
169
+ let(:user) { create(:user, locale: "en", organization:, confirmed_at: Time.zone.now) }
170
+ let(:text) { "I am quiet a valid proposal, with one sentence that is long enough to be valid I think." }
171
+ let(:body) { { data: { body: text } } }
172
+
173
+ run_test!(example_name: :ok_update_body_en) do |example|
174
+ data = JSON.parse(example.body)["data"]
175
+ expect(data["attributes"]["body"]).to eq({ "en" => text })
176
+ expect(proposal.reload.body).to eq({ "en" => text })
177
+ end
178
+ end
179
+
180
+ context "when update nothing" do
181
+ let(:user) { create(:user, locale: "en", organization:, confirmed_at: Time.zone.now) }
182
+ let(:body) { { data: {} } }
183
+
184
+ run_test!(example_name: :ok_empty) do |example|
185
+ data = JSON.parse(example.body)["data"]
186
+ expect(data["attributes"]["title"]["en"]).to be_nil
187
+ expect(data["meta"]["publishable"]).to be(false)
188
+ expect(proposal.reload.title).to be_blank
189
+ end
190
+ end
191
+
192
+ context "when the user locale changes from fr to en" do
193
+ let(:french_title) { "J'aimerais de nouveaux bancs publiques avec des prises USB-C" }
194
+ let(:english_title) { "I would like new benches with USB-C charging ports" }
195
+ let(:french_body) { "<p>Lorem Ipsum...</p>" }
196
+ let(:user) { create(:user, locale: "en", organization:, confirmed_at: Time.zone.now) }
197
+ let(:body) { { data: { title: english_title } } }
198
+
199
+ before do
200
+ proposal.title = { "fr" => french_title }
201
+ proposal.body = { "fr" => french_body }
202
+ proposal.save(validate: false)
203
+ end
204
+
205
+ run_test!(example_name: :ok_locale_switch_fr_to_en) do |example|
206
+ data = JSON.parse(example.body)["data"]
207
+ expect(data["attributes"]["title"]).to eq({ "en" => english_title })
208
+ expect(data["attributes"]["body"]).to eq({ "en" => french_body })
209
+ expect(proposal.reload.title).to eq({ "en" => english_title })
210
+ expect(proposal.reload.body).to eq({ "en" => french_body })
211
+ end
212
+ end
213
+ end
214
+
215
+ response "400", "Bad Request" do
216
+ consumes "application/json"
217
+ produces "application/json"
218
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:error_response)
219
+
220
+ context "when title is blank" do
221
+ let(:body) { { data: { title: "" } } }
222
+
223
+ run_test!(example_name: :bad_request_title_blank) do |example|
224
+ data = JSON.parse(example.body)
225
+ expect(response).to have_http_status(:bad_request)
226
+ expect(data["error_details"]).to include(
227
+ a_hash_including("code" => "blank", "field" => "title")
228
+ )
229
+ end
230
+ end
231
+
232
+ context "when title is too short" do
233
+ let(:body) { { data: { title: "Abcdefghijklm" } } } # 13 chars, starts with cap, low caps ratio
234
+
235
+ run_test!(example_name: :bad_request_title_too_short) do |example|
236
+ data = JSON.parse(example.body)
237
+ expect(data["error_details"]).to include(
238
+ a_hash_including("code" => "too_short", "field" => "title")
239
+ )
240
+ end
241
+ end
242
+
243
+ context "when title must start with caps" do
244
+ let(:body) { { data: { title: "this is a long enough title" } } }
245
+
246
+ run_test!(example_name: :bad_request_title_must_start_with_caps) do |example|
247
+ data = JSON.parse(example.body)
248
+ expect(data["error_details"]).to include(
249
+ a_hash_including("code" => "must_start_with_caps", "field" => "title")
250
+ )
251
+ end
252
+ end
253
+
254
+ context "when title has too many caps" do
255
+ let(:body) { { data: { title: "THIS IS ALL CAPS TITLE HERE" } } }
256
+
257
+ run_test!(example_name: :bad_request_title_too_much_caps) do |example|
258
+ data = JSON.parse(example.body)
259
+ expect(data["error_details"]).to include(
260
+ a_hash_including("code" => "too_much_caps", "field" => "title")
261
+ )
262
+ end
263
+ end
264
+
265
+ context "when title is too long" do
266
+ let(:body) { { data: { title: "A#{"b" * 150}" } } } # 151 chars, etiquette-clean
267
+
268
+ run_test!(example_name: :bad_request_title_too_long) do |example|
269
+ data = JSON.parse(example.body)
270
+ expect(data["error_details"]).to include(
271
+ a_hash_including("code" => "too_long", "field" => "title")
272
+ )
273
+ end
274
+ end
275
+
276
+ context "when the token is a service token" do
277
+ let!(:bearer_token) { create(:oauth_access_token, scopes: "proposals", resource_owner_id: nil, application: api_client) }
278
+ let(:body) { { data: { title: valid_title } } }
279
+
280
+ run_test!(example_name: :bad_request_service_token) do |example|
281
+ data = JSON.parse(example.body)
282
+ expect(response).to have_http_status(:bad_request)
283
+ expect(data["error_description"]).to include("User required")
284
+ end
285
+ end
286
+
287
+ context "when title and body are both invalid" do
288
+ let(:body) { { data: { title: "PROPOSAL", body: "proposal 2" } } }
289
+
290
+ run_test!(example_name: :bad_request_title_and_body_multi) do |example|
291
+ data = JSON.parse(example.body)
292
+ details = data["error_details"]
293
+ expect(details.size).to be >= 4
294
+ expect(details).to include(
295
+ a_hash_including("code" => "too_short", "field" => "title"),
296
+ a_hash_including("code" => "too_much_caps", "field" => "title"),
297
+ a_hash_including("code" => "too_short", "field" => "body"),
298
+ a_hash_including("code" => "must_start_with_caps", "field" => "body")
299
+ )
300
+ expect(data["error_description"]).to be_present
301
+ end
302
+ end
303
+ end
304
+ end
305
+ end
306
+ end
307
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+
5
+ RSpec.describe Decidim::Api::RestFull::Proposals::ProposalExtendedDataController do
6
+ path "/proposals/{id}/extended_data" do
7
+ get "Proposal extended data" do
8
+ tags "Proposals"
9
+ produces "application/json"
10
+ operationId "getProposalExtendedData"
11
+ description "See [Extended data](#{Decidim::RestFull.config.docs_url}/integrator/extended-data)."
12
+ parameter name: "object_path", in: :query, required: true, schema: { type: :string }
13
+ parameter name: "id", in: :path, schema: { type: :string }
14
+
15
+ describe_api_endpoint(
16
+ controller: Decidim::Api::RestFull::Proposals::ProposalExtendedDataController,
17
+ action: :index,
18
+ security_types: [:credentialFlow],
19
+ scopes: ["proposals"],
20
+ permissions: ["proposals.read", "proposals.extended_data.read"]
21
+ ) do
22
+ let!(:participatory_process) { create(:participatory_process, :with_steps, organization:) }
23
+ let!(:proposal_component) { create(:proposal_component, participatory_space: participatory_process) }
24
+ let!(:proposal) { create(:proposal, component: proposal_component) }
25
+ let(:id) { proposal.id }
26
+ let(:object_path) { "." }
27
+
28
+ response "200", "Extended data" do
29
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:resource_extended_data)
30
+
31
+ before { proposal.extended_data.update!(data: { "source" => "api" }) }
32
+
33
+ run_test!(example_name: :ok) do |example|
34
+ expect(JSON.parse(example.body)["data"]).to include("source" => "api")
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ path "/proposals/{id}/extended_data/sync" do
42
+ put "Set proposal extended data (sync)" do
43
+ tags "Proposals"
44
+ consumes "application/json"
45
+ produces "application/json"
46
+ operationId "setProposalExtendedData"
47
+ description "See [Extended data](#{Decidim::RestFull.config.docs_url}/integrator/extended-data)."
48
+ parameter name: "object_path", in: :query, required: true, schema: { type: :string }
49
+ parameter name: "id", in: :path, schema: { type: :string }
50
+ parameter name: :body, in: :body, required: true, schema: {
51
+ type: :object,
52
+ properties: { data: { type: :object, additionalProperties: true } },
53
+ required: [:data]
54
+ }
55
+
56
+ describe_api_endpoint(
57
+ controller: Decidim::Api::RestFull::Proposals::ProposalExtendedDataController,
58
+ action: :update_sync,
59
+ security_types: [:credentialFlow],
60
+ scopes: ["proposals"],
61
+ permissions: ["proposals.read", "proposals.extended_data.update"]
62
+ ) do
63
+ let!(:participatory_process) { create(:participatory_process, :with_steps, organization:) }
64
+ let!(:proposal_component) { create(:proposal_component, participatory_space: participatory_process) }
65
+ let!(:proposal) { create(:proposal, component: proposal_component) }
66
+ let(:id) { proposal.id }
67
+ let(:object_path) { "." }
68
+ let(:body) { { data: { "source" => "sync" } } }
69
+
70
+ response "200", "Updated" do
71
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:resource_extended_data)
72
+
73
+ run_test!(example_name: :ok) do |_example|
74
+ expect(proposal.reload.extended_data_hash).to include("source" => "sync")
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end