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,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "proposal"
4
+ require_relative "draft_proposal"
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Definitions
6
+ module Tags
7
+ DRAFT_PROPOSALS = {
8
+ name: "Draft Proposals",
9
+ description: <<~TXT.strip
10
+ Unpublished proposals (`published_at` is `null`) tied to your OAuth application and impersonated participant.
11
+
12
+ **One draft per component**: for a given proposals component and user, only a single draft may exist. Creating again returns `400` if a draft is already present.
13
+
14
+ **Isolation from the Decidim UI**: drafts created or updated through this API are not editable in the web app, and drafts created in the UI are not visible here.
15
+
16
+ **Lifecycle**: create → update (`meta.publishable`) → **publish** (`POST /draft_proposals/:id/publish`, async by default; `…/publish/sync` for inline publication) → the resource becomes a published proposal.
17
+
18
+ Requires **impersonation** token (`proposals.draft`); service (client credentials) tokens cannot hold drafts.
19
+ TXT
20
+ }.freeze
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ Decidim::RestFull::Test::OpenApiTagRegistry.register_tag(Decidim::RestFull::Definitions::Tags::DRAFT_PROPOSALS)
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Definitions
6
+ module Tags
7
+ PROPOSAL = {
8
+ name: "Proposals",
9
+ description: <<~TXT.strip,
10
+ Published proposals (`Decidim::Proposals::Proposal` with `published_at` set) within visible participatory spaces.
11
+
12
+ **Read** (`proposals.read`): list and show proposals; filter by component, space, scope, and vote-related facets.
13
+
14
+ **Vote proposals** (`proposals.vote`, impersonation): async `POST /vote_proposals` (202 + job poll) or sync `POST /vote_proposals/sync` (slim vote payload; `?include_proposal=true` for full proposal). One vote per author per proposal.
15
+
16
+ **Components**: `GET /components/proposal_components` exposes proposals-component settings (votes enabled, limits, phases).
17
+
18
+ **Extended data**: `GET/PUT /proposals/{id}/extended_data` (+ `/sync`); filter with `filter[extended_data_cont]` on index. See [Extended data](#{Decidim::RestFull.config.docs_url}/integrator/extended-data).
19
+
20
+ Draft authoring lives under the **Draft Proposals** tag.
21
+ TXT
22
+ externalDocs: {
23
+ description: "Extended data integrator guide",
24
+ url: "#{Decidim::RestFull.config.docs_url}/integrator/extended-data"
25
+ }
26
+ }.freeze
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ Decidim::RestFull::Test::OpenApiTagRegistry.register_tag(Decidim::RestFull::Definitions::Tags::PROPOSAL)
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ Decidim::RestFull::Core::DefinitionRegistry.register_resource(:vote_proposal) do
4
+ {
5
+ type: :object,
6
+ title: "Vote proposal",
7
+ description: "A user's vote on a published proposal (Decidim::Proposals::ProposalVote).",
8
+ properties: {
9
+ id: { type: :string },
10
+ type: { type: :string, enum: ["vote_proposals"] },
11
+ attributes: {
12
+ type: :object,
13
+ properties: {
14
+ weight: { type: :integer, description: "Vote weight (0 = abstention when enabled)" },
15
+ created_at: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:creation_date) },
16
+ updated_at: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:edition_date) }
17
+ },
18
+ required: %w(weight created_at updated_at),
19
+ additionalProperties: false
20
+ },
21
+ relationships: {
22
+ type: :object,
23
+ properties: {
24
+ proposal: {
25
+ type: :object,
26
+ properties: {
27
+ data: {
28
+ type: :object,
29
+ properties: {
30
+ type: { type: :string, enum: ["proposals"] },
31
+ id: { type: :string }
32
+ }
33
+ }
34
+ }
35
+ },
36
+ author: {
37
+ type: :object,
38
+ properties: {
39
+ data: {
40
+ type: :object,
41
+ properties: {
42
+ type: { type: :string, enum: ["users"] },
43
+ id: { type: :string }
44
+ }
45
+ }
46
+ }
47
+ }
48
+ }
49
+ }
50
+ },
51
+ required: %w(id type)
52
+ }
53
+ end
54
+
55
+ Decidim::RestFull::Core::DefinitionRegistry.register_object(:vote_proposal_create_body) do
56
+ {
57
+ type: :object,
58
+ required: %w(proposal_id data),
59
+ properties: {
60
+ proposal_id: { type: :integer, description: "Published proposal id" },
61
+ data: {
62
+ type: :object,
63
+ required: [:weight],
64
+ properties: {
65
+ weight: { type: :integer, description: "Vote weight" }
66
+ },
67
+ additionalProperties: false
68
+ }
69
+ }
70
+ }
71
+ end
72
+
73
+ Decidim::RestFull::Core::DefinitionRegistry.register_object(:vote_proposals_index_response) do
74
+ {
75
+ type: :object,
76
+ properties: {
77
+ data: {
78
+ type: :array,
79
+ items: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:vote_proposal) }
80
+ },
81
+ meta: {
82
+ "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:collection_meta)
83
+ }
84
+ },
85
+ required: [:data, :meta]
86
+ }
87
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "decidim-restfull-core"
4
+
5
+ module Decidim
6
+ module RestFull
7
+ module Proposals
8
+ ENGINE_ROOT = File.expand_path("..", __dir__).freeze
9
+ end
10
+ end
11
+ end
12
+
13
+ require "decidim/rest_full/http_cache/proposal_show_fingerprint"
14
+ require "decidim/rest_full/proposals/proposal_client_id_override"
15
+ require "decidim/rest_full/proposals/proposals_controller_override"
16
+ require "decidim/rest_full/proposals/ransackers"
17
+ require "decidim/rest_full/proposals/engine"
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe Decidim::RestFull::Proposals::ProposalWebhookJob do
6
+ let(:proposal) { create(:proposal) }
7
+ let(:organization) { proposal.organization }
8
+ let(:proposal_id) { proposal.id }
9
+ let(:organization_id) { organization.id }
10
+ let(:api_client) { create(:api_client, organization:, scopes: ["proposals"]) }
11
+
12
+ before do
13
+ allow(Decidim::RestFull::Core::WebhookJob).to receive(:perform_later).and_return(true)
14
+ end
15
+
16
+ context "when sending a proposal_creation.succeeded event" do
17
+ let(:event_name) { "proposal_creation.succeeded" }
18
+ let!(:webhooks) { create_list(:webhook_registration, 3, api_client:, subscriptions: [event_name]) }
19
+ let!(:webhook_other) { create(:webhook_registration, api_client:, subscriptions: ["whatever"]) }
20
+
21
+ before do
22
+ # Define permissions for the webohook api_client
23
+ api_client.permissions.create(permission: event_name)
24
+ api_client.save!
25
+ api_client.reload
26
+ end
27
+
28
+ it "call subscribed webhooks" do
29
+ expect(Decidim::RestFull::Core::WebhookJob).to receive(:perform_later) do |webhook_registration, _payload, _timestamp|
30
+ expect(webhook_registration).to be_in(webhooks)
31
+ end
32
+ described_class.perform_now(event_name, proposal_id, organization_id)
33
+ end
34
+
35
+ it "does not call other webhooks" do
36
+ expect(Decidim::RestFull::Core::WebhookJob).not_to receive(:perform_later).with(webhook_other)
37
+ described_class.perform_now(event_name, proposal_id, organization_id)
38
+ end
39
+
40
+ it "logs when the event payload is invalid" do
41
+ form = instance_double(
42
+ Decidim::RestFull::Core::WebhookEventForm,
43
+ valid?: false,
44
+ errors: instance_double(ActiveModel::Errors, full_messages: ["bad"])
45
+ )
46
+ allow(Decidim::RestFull::Core::WebhookEventForm).to receive(:new).and_return(form)
47
+ allow(form).to receive(:with_context).and_return(form)
48
+
49
+ expect(Decidim::RestFull::Core::WebhookJob).not_to receive(:perform_later)
50
+ expect(Rails.logger).to receive(:warn).with(/Invalid event name/)
51
+ described_class.perform_now(event_name, proposal_id, organization_id)
52
+ end
53
+
54
+ it "serialize proposal with DraftProposalSerializer if it is a draft" do
55
+ proposal.update(published_at: nil)
56
+
57
+ expect(Decidim::Api::RestFull::Proposals::DraftProposalSerializer).to receive(:new).with(proposal, kind_of(Hash)).and_call_original
58
+ described_class.perform_now(event_name, proposal_id, organization_id)
59
+ end
60
+
61
+ it "serialize proposal with ProposalSerializer if it is published" do
62
+ expect(Decidim::Api::RestFull::Proposals::ProposalSerializer).to receive(:new).with(proposal, kind_of(Hash)).and_call_original
63
+ described_class.perform_now(event_name, proposal_id, organization_id)
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+ RSpec.describe Decidim::Api::RestFull::Components::ProposalComponentsController do
5
+ path "/components/proposal_components" do
6
+ get "Proposal Components" do
7
+ tags "Components"
8
+ produces "application/json"
9
+ operationId "listProposalComponents"
10
+ description "List or search proposal components of the organization"
11
+ it_behaves_like "filtered params", filter: "name", item_schema: { type: :string }, only: :string
12
+ it_behaves_like "filtered params", filter: "participatory_space_type", item_schema: { type: :string, example: "Decidim::Assembly" }, only: :string
13
+ it_behaves_like "filtered params", filter: "participatory_space_id", item_schema: { type: :string }, only: :integer
14
+ it_behaves_like "paginated params"
15
+ it_behaves_like "localized params"
16
+
17
+ describe_api_endpoint(
18
+ controller: Decidim::Api::RestFull::Components::ProposalComponentsController,
19
+ action: :index,
20
+ security_types: [:impersonationFlow, :credentialFlow],
21
+ scopes: ["public"],
22
+ permissions: ["public.component.read"]
23
+ ) do
24
+ let(:user) { create(:user, locale: "fr", organization:) }
25
+ let(:component) { create(:proposal_component) }
26
+ let!(:assembly) { create(:assembly, organization:) }
27
+ let!(:participatory_process) { create(:participatory_process, :with_steps, organization:) }
28
+ let!(:organization) { create(:organization, available_locales: ["en"]) }
29
+
30
+ let!(:simple_no_abstain) do
31
+ create(
32
+ :proposal_component,
33
+ :with_votes_enabled,
34
+ participatory_space: participatory_process,
35
+ name: { en: "Simple vote no abstain" },
36
+ settings: { voting_cards_show_abstain: false }
37
+ )
38
+ end
39
+ let!(:simple_with_abstain) do
40
+ create(
41
+ :proposal_component,
42
+ :with_votes_enabled,
43
+ participatory_space: participatory_process,
44
+ name: { en: "Simple vote with abstain" },
45
+ settings: { voting_cards_show_abstain: true }
46
+ )
47
+ end
48
+ let!(:cards_no_abstain) do
49
+ create(
50
+ :proposal_component,
51
+ :with_votes_enabled,
52
+ participatory_space: participatory_process,
53
+ name: { en: "Cards vote no abstain" },
54
+ settings: { awesome_voting_manifest: :voting_cards, voting_cards_show_abstain: false }
55
+ )
56
+ end
57
+ let!(:cards_with_abstain) do
58
+ create(
59
+ :proposal_component,
60
+ :with_votes_enabled,
61
+ participatory_space: participatory_process,
62
+ name: { en: "Cards vote with abstain" },
63
+ settings: { awesome_voting_manifest: :voting_cards, voting_cards_show_abstain: true }
64
+ )
65
+ end
66
+
67
+ before do
68
+ [simple_no_abstain, simple_with_abstain, cards_no_abstain, cards_with_abstain].each do |comp|
69
+ create(:proposal, :accepted, component: comp)
70
+ end
71
+ end
72
+
73
+ it_behaves_like "localized endpoint"
74
+
75
+ response "200", "List of proposal components" do
76
+ produces "application/json"
77
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:proposal_component_index_response)
78
+
79
+ context "with no filter params" do
80
+ let(:"locales[]") { %w(en fr) }
81
+ let(:page) { 1 }
82
+ let(:per_page) { 10 }
83
+
84
+ run_test!(example_name: :ok) do |example|
85
+ data = JSON.parse(example.body)["data"]
86
+ by_id = data.index_by { |row| row["id"] }
87
+
88
+ expect(by_id.fetch(simple_no_abstain.id.to_s).dig("meta", "votes").map { |v| v["weight"] }).to eq([1])
89
+ expect(by_id.fetch(simple_with_abstain.id.to_s).dig("meta", "votes").map { |v| v["weight"] }).to eq([0, 1])
90
+ expect(by_id.fetch(cards_no_abstain.id.to_s).dig("meta", "votes").map { |v| v["weight"] }).to eq([1, 2, 3])
91
+ expect(by_id.fetch(cards_with_abstain.id.to_s).dig("meta", "votes").map { |v| v["weight"] }).to eq([0, 1, 2, 3])
92
+
93
+ data.each do |row|
94
+ expect(row["meta"]["votes"]).to be_an(Array)
95
+ end
96
+ end
97
+ end
98
+
99
+ context "with filter[participatory_space_type_eq]=Decidim::ParticipatoryProcess and filter[participatory_space_type_id_eq]=processId filters" do
100
+ let(:"filter[participatory_space_id_eq]") { participatory_process.id.to_s }
101
+ let(:"filter[participatory_space_type_eq]") { "Decidim::ParticipatoryProcess" }
102
+ let(:"locales[]") { %w(en fr) }
103
+ let(:page) { 1 }
104
+ let(:per_page) { 10 }
105
+
106
+ run_test!(example_name: :components_in_Process) do |example|
107
+ data = JSON.parse(example.body)["data"]
108
+ not_in_process = data.select { |component| component["attributes"]["participatory_space_type"] != "Decidim::ParticipatoryProcess" && component["attributes"]["participatory_space_id"] != participatory_process.id.to_s }
109
+ expect(not_in_process).to be_empty
110
+ end
111
+ end
112
+
113
+ context "with impersonation and an active draft" do
114
+ let!(:bearer_token) { create(:oauth_access_token, scopes: "public", resource_owner_id: user.id, application: api_client) }
115
+ let(:Authorization) { "Bearer #{bearer_token.token}" }
116
+ let!(:draft) do
117
+ proposal_component = create(:component, participatory_space: participatory_process, manifest_name: "proposals", published_at: Time.zone.now)
118
+ prop = create(:proposal, published_at: nil, component: proposal_component, users: [user])
119
+ prop.update(rest_full_application: Decidim::RestFull::Proposals::ProposalApplicationId.new(proposal_id: prop.id, api_client_id: api_client.id))
120
+ prop
121
+ end
122
+
123
+ run_test!(example_name: :ok_with_draft) do |example|
124
+ json_response = JSON.parse(example.body)
125
+ comp = json_response["data"].find { |d| d["id"] == draft.decidim_component_id.to_s }
126
+ expect(comp["links"]["draft"]).to be_present
127
+ expect(comp["links"]["draft"]["meta"]).to eq(
128
+ {
129
+ "component_id" => draft.decidim_component_id.to_s,
130
+ "component_manifest" => "proposals",
131
+ "space_id" => draft.component.participatory_space_id.to_s,
132
+ "space_manifest" => "participatory_processes",
133
+ "resource_id" => draft.id.to_s,
134
+ "action_method" => "GET"
135
+ }
136
+ )
137
+ end
138
+ end
139
+
140
+ it_behaves_like "paginated endpoint" do
141
+ let(:create_resource) { -> { create(:component, participatory_space: assembly, manifest_name: "proposals", published_at: Time.zone.now) } }
142
+ let(:each_resource) { ->(_resource, _index) {} }
143
+ let(:resources) { Decidim::Component.where(manifest_name: "proposals") }
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+ RSpec.describe Decidim::Api::RestFull::Components::ProposalComponentsController do
5
+ path "/components/proposal_components/{id}" do
6
+ get "Proposal Component Details" do
7
+ tags "Components"
8
+ produces "application/json"
9
+ operationId "showProposalComponent"
10
+
11
+ description "Find on proposal"
12
+ it_behaves_like "localized params"
13
+ it_behaves_like "paginated params"
14
+ it_behaves_like "filtered params", filter: "id", item_schema: { type: :integer }, only: :integer
15
+ parameter name: "id", in: :path, schema: { type: :integer }
16
+
17
+ describe_api_endpoint(
18
+ controller: Decidim::Api::RestFull::Components::ProposalComponentsController,
19
+ action: :show,
20
+ security_types: [:impersonationFlow, :credentialFlow],
21
+ scopes: ["public"],
22
+ permissions: ["public.component.read"]
23
+ ) do
24
+ let(:id) { component.id }
25
+
26
+ let(:user) { create(:user, locale: "fr", organization:) }
27
+ let(:component) { create(:component, participatory_space: participatory_process, manifest_name: "proposals", published_at: Time.zone.now) }
28
+ let!(:assembly) { create(:assembly, organization:) }
29
+ let!(:participatory_process) { create(:participatory_process, :with_steps, organization:) }
30
+ let!(:organization) { create(:organization, available_locales: ["en"]) }
31
+ it_behaves_like "localized endpoint"
32
+
33
+ response "200", "Proposal Component" do
34
+ produces "application/json"
35
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:proposal_component_item_response)
36
+
37
+ context "with no filter params" do
38
+ let(:"locales[]") { %w(en fr) }
39
+ let(:page) { 1 }
40
+ let(:per_page) { 10 }
41
+
42
+ run_test!(example_name: :ok)
43
+ end
44
+
45
+ context "when voting is restricted by proposal status", if: (
46
+ Decidim::Toggle.gem_present?("decidim-decidim_awesome") &&
47
+ defined?(Decidim::DecidimAwesome) &&
48
+ Decidim::DecidimAwesome.enabled?(:votes_by_proposal_status)
49
+ ) do
50
+ let(:"locales[]") { %w(en fr) }
51
+ let(:page) { 1 }
52
+ let(:per_page) { 10 }
53
+ let(:component) do
54
+ step_id = participatory_process.active_step.id
55
+ create(
56
+ :proposal_component,
57
+ participatory_space: participatory_process,
58
+ published_at: Time.zone.now,
59
+ step_settings: {
60
+ step_id => {
61
+ votes_enabled: true,
62
+ awesome_votes_enabled_by_status: true,
63
+ awesome_votes_enabled_states: %w(accepted)
64
+ }
65
+ }
66
+ )
67
+ end
68
+ let!(:accepted_proposal) { create(:proposal, :accepted, component:) }
69
+
70
+ run_test!(example_name: :restricted_by_status) do |example|
71
+ meta = JSON.parse(example.body).dig("data", "meta")
72
+ expect(meta["votes_enabled"]).to be(true)
73
+ expect(meta["can_vote"]).to be(true)
74
+ expect(meta["awesome_votes_enabled_by_status"]).to be(true)
75
+ expect(meta["awesome_votes_enabled_states"]).to eq(%w(accepted))
76
+ end
77
+ end
78
+
79
+ on_security(:impersonationFlow) do
80
+ context "without an active draft" do
81
+ let(:user) { create(:user, locale: "fr", organization:) }
82
+ let!(:unrelated_participatory_process) { create(:participatory_process, :with_steps, organization:) }
83
+
84
+ let(:unrelated_component) { create(:component, participatory_space: unrelated_participatory_process, manifest_name: "proposals", published_at: Time.zone.now) }
85
+ let!(:unrelated_draft) do
86
+ prop = create(:proposal, published_at: nil, component: unrelated_component, users: [user])
87
+ prop.update(rest_full_application: Decidim::RestFull::Proposals::ProposalApplicationId.new(proposal_id: prop.id, api_client_id: api_client.id))
88
+ prop
89
+ end
90
+
91
+ let(:bearer_token) { create(:oauth_access_token, scopes: "public", resource_owner_id: user.id, application: api_client) }
92
+ let(:Authorization) { "Bearer #{bearer_token.token}" }
93
+
94
+ run_test! do |example|
95
+ json_response = JSON.parse(example.body)
96
+ comp = json_response["data"]
97
+ expect(comp["links"]["draft"]).to be_nil
98
+ end
99
+ end
100
+
101
+ context "with an active draft" do
102
+ let(:user) { create(:user, locale: "fr", organization:) }
103
+ let!(:unrelated_participatory_process) { create(:participatory_process, :with_steps, organization:) }
104
+
105
+ let(:unrelated_component) { create(:component, participatory_space: unrelated_participatory_process, manifest_name: "proposals", published_at: Time.zone.now) }
106
+ let!(:unrelated_draft) do
107
+ prop = create(:proposal, published_at: nil, component: unrelated_component, users: [user])
108
+ prop.update(rest_full_application: Decidim::RestFull::Proposals::ProposalApplicationId.new(proposal_id: prop.id, api_client_id: api_client.id))
109
+ prop
110
+ end
111
+ let!(:draft) do
112
+ prop = create(:proposal, published_at: nil, component:, users: [user])
113
+ prop.update(rest_full_application: Decidim::RestFull::Proposals::ProposalApplicationId.new(proposal_id: prop.id, api_client_id: api_client.id))
114
+ prop
115
+ end
116
+ let(:bearer_token) { create(:oauth_access_token, scopes: "public", resource_owner_id: user.id, application: api_client) }
117
+ let(:Authorization) { "Bearer #{bearer_token.token}" }
118
+
119
+ run_test!(example_name: :ok_with_draft) do |example|
120
+ json_response = JSON.parse(example.body)
121
+ comp = json_response["data"]
122
+ expect(comp["links"]["draft"]).to be_present
123
+ expect(comp["links"]["draft"]["meta"]).to eq(
124
+ {
125
+ "component_id" => draft.decidim_component_id.to_s,
126
+ "component_manifest" => "proposals",
127
+ "space_id" => draft.component.participatory_space_id.to_s,
128
+ "space_manifest" => "participatory_processes",
129
+ "resource_id" => draft.id.to_s,
130
+ "action_method" => "GET"
131
+ }
132
+ )
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+ RSpec.describe Decidim::Api::RestFull::DraftProposals::DraftProposalsController do
5
+ path "/draft_proposals/sync" do
6
+ post "Create draft proposal" do
7
+ tags "Draft Proposals"
8
+ produces "application/json"
9
+ operationId "createDraftProposal"
10
+ description <<~README
11
+ Create a draft
12
+ README
13
+
14
+ parameter name: :body, in: :body, required: true, schema: {
15
+ type: :object,
16
+ title: "Create Draft Proposal Payload",
17
+ properties: {
18
+ data: {
19
+ type: :object,
20
+ title: "Draft Proposal Data",
21
+ properties: {
22
+ component_id: { type: :integer, description: "Component ID" }
23
+ },
24
+ required: [:component_id],
25
+ description: "Payload to update in the proposal"
26
+ }
27
+ }, required: [:data]
28
+ }
29
+ describe_api_endpoint(
30
+ controller: Decidim::Api::RestFull::DraftProposals::DraftProposalsController,
31
+ action: :create_sync,
32
+ security_types: [:impersonationFlow],
33
+ scopes: ["proposals"],
34
+ permissions: ["proposals.draft"]
35
+ ) do
36
+ let!(:participatory_process) { create(:participatory_process, organization:) }
37
+ let(:proposal_component) { create(:component, participatory_space: participatory_process, manifest_name: "proposals", published_at: Time.zone.now) }
38
+ let!(:proposal) do
39
+ prop = create(:proposal, published_at: nil, component: proposal_component, users: [user])
40
+ prop.update(rest_full_application: Decidim::RestFull::Proposals::ProposalApplicationId.new(proposal_id: prop.id, api_client_id: api_client.id))
41
+ prop.body = nil
42
+ prop.title = nil
43
+ prop.save(validate: false)
44
+ prop
45
+ end
46
+ let(:id) { proposal.id }
47
+ let(:space_manifest) { "participatory_processes" }
48
+ let(:space_id) { participatory_process.id }
49
+ let(:component_id) { proposal_component.id }
50
+
51
+ response "200", "Draft updated" do
52
+ produces "application/json"
53
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:draft_proposal_item_response)
54
+
55
+ context "when create empty" do
56
+ before do
57
+ Decidim::Proposals::Proposal.where(
58
+ decidim_component_id: component_id
59
+ ).each(&:destroy)
60
+ end
61
+
62
+ let(:body) { { data: { component_id: proposal_component.id } } }
63
+
64
+ run_test!(example_name: :ok_empty) do |example|
65
+ data = JSON.parse(example.body)["data"]
66
+ expect(data["attributes"]["title"]["fr"]).to be_nil
67
+ expect(data["meta"]["publishable"]).to be(false)
68
+ end
69
+ end
70
+ end
71
+
72
+ response "404", "Bad Request" do
73
+ consumes "application/json"
74
+ produces "application/json"
75
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:error_response)
76
+
77
+ context "with invalid component ID" do
78
+ let(:body) { { data: { component_id: Decidim::Component.maximum(:id).to_i + 1 } } }
79
+
80
+ after do
81
+ Decidim::Proposals::Proposal.where(
82
+ decidim_component_id: component_id
83
+ ).each(&:destroy)
84
+ end
85
+
86
+ run_test!(:component_not_found)
87
+ end
88
+ end
89
+
90
+ response "400", "Bad Request" do
91
+ consumes "application/json"
92
+ produces "application/json"
93
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:error_response)
94
+
95
+ context "when posted too much proposals" do
96
+ let(:proposal_component) { create(:component, participatory_space: participatory_process, manifest_name: "proposals", published_at: Time.zone.now, settings: { proposal_limit: 2 }) }
97
+ let(:body) { { data: { title: "This is a valid title, but unfortunatly, I already posted too much stuff.", component_id: proposal_component.id } } }
98
+
99
+ before do
100
+ Decidim::Proposals::Proposal.where(
101
+ decidim_component_id: component_id
102
+ ).each(&:destroy)
103
+ create(:proposal, component: proposal_component, published_at: Time.now.utc, users: [user])
104
+ create(:proposal, component: proposal_component, published_at: Time.now.utc, users: [user])
105
+ end
106
+
107
+ run_test!(example_name: :bad_request_limit_reached) do |example|
108
+ error_description = JSON.parse(example.body)["error_description"]
109
+ expect(error_description).to include("you have exceeded the limit.")
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end