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.
- checksums.yaml +7 -0
- data/app/controllers/decidim/api/rest_full/components/proposal_components_controller.rb +91 -0
- data/app/controllers/decidim/api/rest_full/draft_proposals/draft_proposals_controller.rb +116 -0
- data/app/controllers/decidim/api/rest_full/proposals/proposal_extended_data_controller.rb +31 -0
- data/app/controllers/decidim/api/rest_full/proposals/proposals_controller.rb +172 -0
- data/app/controllers/decidim/api/rest_full/vote_proposals/vote_proposals_controller.rb +90 -0
- data/app/jobs/decidim/rest_full/proposals/proposal_webhook_job.rb +67 -0
- data/app/models/decidim/rest_full/proposals/proposal_application_id.rb +14 -0
- data/app/serializers/decidim/api/rest_full/proposals/draft_proposal_serializer.rb +44 -0
- data/app/serializers/decidim/api/rest_full/proposals/proposal_component_serializer.rb +232 -0
- data/app/serializers/decidim/api/rest_full/proposals/proposal_serializer.rb +113 -0
- data/app/serializers/decidim/api/rest_full/proposals/proposal_state_serializer.rb +39 -0
- data/app/serializers/decidim/api/rest_full/proposals/proposal_webhook_serializer.rb +54 -0
- data/app/serializers/decidim/api/rest_full/proposals/vote_proposal_serializer.rb +27 -0
- data/app/services/decidim/rest_full/proposals/draft_proposals_operations.rb +248 -0
- data/app/services/decidim/rest_full/proposals/proposal_vote_eligibility.rb +106 -0
- data/app/services/decidim/rest_full/proposals/proposal_votes_operations.rb +173 -0
- data/app/services/decidim/rest_full/proposals/vote_proposals_operations.rb +114 -0
- data/config/locales/decidim_rest_full_proposals.en.yml +26 -0
- data/lib/decidim/rest_full/http_cache/proposal_show_fingerprint.rb +77 -0
- data/lib/decidim/rest_full/proposals/engine.rb +129 -0
- data/lib/decidim/rest_full/proposals/proposal_client_id_override.rb +18 -0
- data/lib/decidim/rest_full/proposals/proposals_controller_override.rb +28 -0
- data/lib/decidim/rest_full/proposals/ransackers.rb +84 -0
- data/lib/decidim/rest_full/proposals/test_definitions.rb +7 -0
- data/lib/decidim/rest_full/test/definitions/draft_proposal.rb +101 -0
- data/lib/decidim/rest_full/test/definitions/proposal.rb +115 -0
- data/lib/decidim/rest_full/test/definitions/proposal_component_schema.rb +110 -0
- data/lib/decidim/rest_full/test/definitions/proposals.rb +4 -0
- data/lib/decidim/rest_full/test/definitions/tags/draft_proposals_tag.rb +26 -0
- data/lib/decidim/rest_full/test/definitions/tags/proposal_tag.rb +32 -0
- data/lib/decidim/rest_full/test/definitions/vote_proposal.rb +87 -0
- data/lib/decidim-restfull-proposals.rb +17 -0
- data/spec/jobs/decidim/rest_full/proposals/proposal_webhook_job_spec.rb +66 -0
- data/spec/requests/decidim/api/rest_full/components/proposal_components_controller_index_spec.rb +149 -0
- data/spec/requests/decidim/api/rest_full/components/proposal_components_controller_show_spec.rb +140 -0
- data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_create_spec.rb +116 -0
- data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_delete_spec.rb +64 -0
- data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_publish_spec.rb +132 -0
- data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_show_spec.rb +72 -0
- data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_update_spec.rb +307 -0
- data/spec/requests/decidim/api/rest_full/proposals/proposal_extended_data_controller_spec.rb +80 -0
- data/spec/requests/decidim/api/rest_full/proposals/proposals_controller_index_spec.rb +275 -0
- data/spec/requests/decidim/api/rest_full/proposals/proposals_controller_show_spec.rb +187 -0
- data/spec/requests/decidim/api/rest_full/scenarios/draft_publish_async_increments_count_and_lists_newest_spec.rb +144 -0
- data/spec/requests/decidim/api/rest_full/scenarios/participatory_process_phase_change_webhook_and_component_meta_spec.rb +130 -0
- data/spec/requests/decidim/api/rest_full/scenarios/proposals_published_at_desc_prev_next_chain_spec.rb +72 -0
- data/spec/requests/decidim/api/rest_full/scenarios/search_vote_enabled_impersonate_and_vote_spec.rb +88 -0
- data/spec/requests/decidim/api/rest_full/vote_proposals/vote_proposals_controller_spec.rb +231 -0
- data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_chatbot_menu_spec.rb +147 -0
- data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_phases_spec.rb +88 -0
- data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_votes_spec.rb +103 -0
- data/spec/serializers/decidim/api/rest_full/proposals/proposal_serializer_can_vote_spec.rb +86 -0
- data/spec/serializers/decidim/api/rest_full/proposals/proposal_webhook_serializer_spec.rb +63 -0
- data/spec/services/decidim/rest_full/proposals/proposal_vote_eligibility_spec.rb +120 -0
- metadata +132 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
# rubocop:disable RSpec/DescribeClass -- behaviour scenario (multi-step HTTP)
|
|
6
|
+
RSpec.describe "Participatory process phase change drives proposal component meta (+ webhooks)" do
|
|
7
|
+
include ActiveJob::TestHelper
|
|
8
|
+
|
|
9
|
+
let!(:organization) { create(:organization, available_locales: %w(en)) }
|
|
10
|
+
let!(:participatory_process) { create(:participatory_process, organization:) }
|
|
11
|
+
let!(:propose_phase) { create(:participatory_process_step, :active, participatory_process:, position: 0) }
|
|
12
|
+
let!(:vote_phase) { create(:participatory_process_step, active: false, participatory_process:, position: 1) }
|
|
13
|
+
let!(:voter) { create(:user, :confirmed, organization:, nickname: "phase#{SecureRandom.hex(4)}") }
|
|
14
|
+
|
|
15
|
+
let!(:proposal_component) do
|
|
16
|
+
create(
|
|
17
|
+
:proposal_component,
|
|
18
|
+
participatory_space: participatory_process,
|
|
19
|
+
step_settings: {
|
|
20
|
+
propose_phase.id.to_s => { creation_enabled: true, votes_enabled: false },
|
|
21
|
+
vote_phase.id.to_s => { creation_enabled: false, votes_enabled: true }
|
|
22
|
+
},
|
|
23
|
+
settings: { awesome_voting_manifest: :voting_cards }
|
|
24
|
+
)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
let!(:proposal) { create(:proposal, :accepted, component: proposal_component) }
|
|
28
|
+
|
|
29
|
+
let!(:api_client) do
|
|
30
|
+
c = create(:api_client, organization:, scopes: %w(public oauth proposals))
|
|
31
|
+
c.permissions = [
|
|
32
|
+
Decidim::RestFull::Core::Permission.new(permission: "public.component.read"),
|
|
33
|
+
Decidim::RestFull::Core::Permission.new(permission: "oauth.impersonate"),
|
|
34
|
+
Decidim::RestFull::Core::Permission.new(permission: "proposal_creation.succeeded", is_event: true),
|
|
35
|
+
Decidim::RestFull::Core::Permission.new(
|
|
36
|
+
permission: "participatory_process.step_activated.succeeded",
|
|
37
|
+
is_event: true
|
|
38
|
+
)
|
|
39
|
+
]
|
|
40
|
+
c.save!
|
|
41
|
+
c
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
let!(:public_token) do
|
|
45
|
+
create(:oauth_access_token, scopes: "public", resource_owner_id: nil, application: api_client)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
let!(:proposal_webhook) do
|
|
49
|
+
create(:webhook_registration, api_client:, subscriptions: ["proposal_creation.succeeded"])
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
let!(:phase_webhook) do
|
|
53
|
+
create(
|
|
54
|
+
:webhook_registration,
|
|
55
|
+
api_client:,
|
|
56
|
+
subscriptions: ["participatory_process.step_activated.succeeded"]
|
|
57
|
+
)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
let(:api_prefix) { "/api/rest_full/v#{Decidim::RestFull.major_minor_version}" }
|
|
61
|
+
let(:json_headers) { { "CONTENT_TYPE" => "application/json" } }
|
|
62
|
+
|
|
63
|
+
before { host!(organization.host) }
|
|
64
|
+
|
|
65
|
+
def proposal_component_meta(token)
|
|
66
|
+
get(
|
|
67
|
+
"#{api_prefix}/components/proposal_components/#{proposal_component.id}",
|
|
68
|
+
headers: { "Authorization" => "Bearer #{token}" }
|
|
69
|
+
)
|
|
70
|
+
expect(response).to have_http_status(:ok)
|
|
71
|
+
response.parsed_body.dig("data", "meta")
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it "flips votes meta on phase change and enqueues phase + proposal webhooks" do
|
|
75
|
+
meta_propose = proposal_component_meta(public_token.token)
|
|
76
|
+
expect(meta_propose["votes_enabled"]).to be(false)
|
|
77
|
+
expect(meta_propose["can_vote"]).to be(false)
|
|
78
|
+
|
|
79
|
+
propose_phase.update!(active: false)
|
|
80
|
+
vote_phase.update!(active: true)
|
|
81
|
+
participatory_process.reload
|
|
82
|
+
proposal_component.reload
|
|
83
|
+
|
|
84
|
+
post(
|
|
85
|
+
"#{api_prefix}/oauth/token",
|
|
86
|
+
params: {
|
|
87
|
+
grant_type: "password",
|
|
88
|
+
auth_type: "impersonate",
|
|
89
|
+
username: voter.nickname,
|
|
90
|
+
client_id: api_client.client_id,
|
|
91
|
+
client_secret: api_client.client_secret,
|
|
92
|
+
scope: "public"
|
|
93
|
+
}.to_json,
|
|
94
|
+
headers: json_headers
|
|
95
|
+
)
|
|
96
|
+
expect(response).to have_http_status(:ok)
|
|
97
|
+
impersonation_token = response.parsed_body["access_token"]
|
|
98
|
+
|
|
99
|
+
meta_vote = proposal_component_meta(impersonation_token)
|
|
100
|
+
expect(meta_vote["votes_enabled"]).to be(true)
|
|
101
|
+
expect(meta_vote["can_vote"]).to be(true)
|
|
102
|
+
|
|
103
|
+
expect do
|
|
104
|
+
ActiveSupport::Notifications.publish(
|
|
105
|
+
Decidim::RestFull::Core::ParticipatoryProcessStepActivatedWebhookHandler::HANDLED_EVENT,
|
|
106
|
+
{ resource: vote_phase }
|
|
107
|
+
)
|
|
108
|
+
end.to have_enqueued_job(Decidim::RestFull::Core::SpaceWebhookJob).with(
|
|
109
|
+
"participatory_process.step_activated.succeeded",
|
|
110
|
+
participatory_process.id,
|
|
111
|
+
organization.id,
|
|
112
|
+
vote_phase.id
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
expect do
|
|
116
|
+
ActiveSupport::Notifications.publish(
|
|
117
|
+
"decidim.events.proposals.proposal_published",
|
|
118
|
+
{ resource: proposal }
|
|
119
|
+
)
|
|
120
|
+
end.to have_enqueued_job(Decidim::RestFull::Proposals::ProposalWebhookJob).with(
|
|
121
|
+
"proposal_creation.succeeded",
|
|
122
|
+
proposal.id,
|
|
123
|
+
organization.id
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
expect(proposal_webhook.subscriptions).to include("proposal_creation.succeeded")
|
|
127
|
+
expect(phase_webhook.subscriptions).to include("participatory_process.step_activated.succeeded")
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
# rubocop:enable RSpec/DescribeClass
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
# rubocop:disable RSpec/DescribeClass -- behaviour scenario (multi-step HTTP)
|
|
6
|
+
RSpec.describe "Proposals published_at DESC prev/next chain" do
|
|
7
|
+
let!(:organization) { create(:organization, available_locales: ["en"]) }
|
|
8
|
+
let!(:participatory_process) { create(:participatory_process, organization:) }
|
|
9
|
+
let!(:proposal_component) do
|
|
10
|
+
create(:component, participatory_space: participatory_process, manifest_name: "proposals", published_at: Time.zone.now)
|
|
11
|
+
end
|
|
12
|
+
let!(:newest) { create(:proposal, reference: "FIRST", component: proposal_component, published_at: 1.day.ago) }
|
|
13
|
+
let!(:middle) { create(:proposal, reference: "SECOND", component: proposal_component, published_at: 2.days.ago) }
|
|
14
|
+
let!(:oldest) { create(:proposal, reference: "THIRD", component: proposal_component, published_at: 3.days.ago) }
|
|
15
|
+
|
|
16
|
+
let!(:api_client) do
|
|
17
|
+
c = create(:api_client, organization:, scopes: ["proposals"])
|
|
18
|
+
c.permissions = [Decidim::RestFull::Core::Permission.new(permission: "proposals.read")]
|
|
19
|
+
c.save!
|
|
20
|
+
c
|
|
21
|
+
end
|
|
22
|
+
let!(:token) do
|
|
23
|
+
create(:oauth_access_token, scopes: "proposals", resource_owner_id: nil, application: api_client)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
let(:api_prefix) { "/api/rest_full/v#{Decidim::RestFull.major_minor_version}" }
|
|
27
|
+
let(:auth_headers) { { "Authorization" => "Bearer #{token.token}" } }
|
|
28
|
+
let(:chain_params) do
|
|
29
|
+
{
|
|
30
|
+
:component_id => proposal_component.id,
|
|
31
|
+
:space_id => participatory_process.id,
|
|
32
|
+
:space_manifest => "participatory_processes",
|
|
33
|
+
:order => "published_at",
|
|
34
|
+
:order_direction => "desc",
|
|
35
|
+
"filter[state_not_eq]" => "rejected"
|
|
36
|
+
}
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
before { host!(organization.host) }
|
|
40
|
+
|
|
41
|
+
def get_proposal(id)
|
|
42
|
+
get("#{api_prefix}/proposals/#{id}", params: chain_params, headers: auth_headers)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def follow_next!(data)
|
|
46
|
+
next_link = data.dig("links", "next")
|
|
47
|
+
expect(next_link).to be_present
|
|
48
|
+
get_proposal(next_link.dig("meta", "resource_id"))
|
|
49
|
+
expect(response).to have_http_status(:ok)
|
|
50
|
+
response.parsed_body["data"]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "walks newest → middle → oldest with nil prev on first and nil next on last" do
|
|
54
|
+
get_proposal(newest.id)
|
|
55
|
+
expect(response).to have_http_status(:ok)
|
|
56
|
+
first = response.parsed_body["data"]
|
|
57
|
+
expect(first["id"].to_i).to eq(newest.id)
|
|
58
|
+
expect(first.dig("links", "prev")).to be_nil
|
|
59
|
+
expect(first.dig("links", "next", "meta", "resource_id").to_i).to eq(middle.id)
|
|
60
|
+
|
|
61
|
+
second = follow_next!(first)
|
|
62
|
+
expect(second["id"].to_i).to eq(middle.id)
|
|
63
|
+
expect(second.dig("links", "prev", "meta", "resource_id").to_i).to eq(newest.id)
|
|
64
|
+
expect(second.dig("links", "next", "meta", "resource_id").to_i).to eq(oldest.id)
|
|
65
|
+
|
|
66
|
+
third = follow_next!(second)
|
|
67
|
+
expect(third["id"].to_i).to eq(oldest.id)
|
|
68
|
+
expect(third.dig("links", "next")).to be_nil
|
|
69
|
+
expect(third.dig("links", "prev", "meta", "resource_id").to_i).to eq(middle.id)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
# rubocop:enable RSpec/DescribeClass
|
data/spec/requests/decidim/api/rest_full/scenarios/search_vote_enabled_impersonate_and_vote_spec.rb
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
# rubocop:disable RSpec/DescribeClass -- behaviour scenario (multi-step HTTP)
|
|
6
|
+
RSpec.describe "Search vote-enabled component, impersonate, and vote" do
|
|
7
|
+
let!(:organization) { create(:organization, available_locales: ["en"]) }
|
|
8
|
+
let!(:participatory_process) { create(:participatory_process, :with_steps, organization:) }
|
|
9
|
+
let!(:proposal_component) do
|
|
10
|
+
create(:proposal_component, :with_votes_enabled, participatory_space: participatory_process)
|
|
11
|
+
end
|
|
12
|
+
let!(:proposal) { create(:proposal, :accepted, component: proposal_component) }
|
|
13
|
+
let!(:voter) { create(:user, :confirmed, organization:, nickname: "voter#{SecureRandom.hex(4)}") }
|
|
14
|
+
|
|
15
|
+
let!(:api_client) do
|
|
16
|
+
c = create(:api_client, organization:, scopes: %w(public oauth proposals))
|
|
17
|
+
c.permissions = [
|
|
18
|
+
Decidim::RestFull::Core::Permission.new(permission: "public.component.read"),
|
|
19
|
+
Decidim::RestFull::Core::Permission.new(permission: "oauth.impersonate"),
|
|
20
|
+
Decidim::RestFull::Core::Permission.new(permission: "proposals.vote")
|
|
21
|
+
]
|
|
22
|
+
c.save!
|
|
23
|
+
c
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
let!(:public_token) do
|
|
27
|
+
create(:oauth_access_token, scopes: "public", resource_owner_id: nil, application: api_client)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
let(:api_prefix) { "/api/rest_full/v#{Decidim::RestFull.major_minor_version}" }
|
|
31
|
+
let(:json_headers) { { "CONTENT_TYPE" => "application/json" } }
|
|
32
|
+
|
|
33
|
+
before { host!(organization.host) }
|
|
34
|
+
|
|
35
|
+
it "finds the component, asserts voting meta, impersonates, and casts a sync vote" do
|
|
36
|
+
get(
|
|
37
|
+
"#{api_prefix}/components/search",
|
|
38
|
+
params: { "filter[id_in][]" => [proposal_component.id] },
|
|
39
|
+
headers: { "Authorization" => "Bearer #{public_token.token}" }
|
|
40
|
+
)
|
|
41
|
+
expect(response).to have_http_status(:ok)
|
|
42
|
+
search_ids = response.parsed_body["data"].map { |row| row["id"].to_i }
|
|
43
|
+
expect(search_ids).to include(proposal_component.id)
|
|
44
|
+
|
|
45
|
+
get(
|
|
46
|
+
"#{api_prefix}/components/proposal_components/#{proposal_component.id}",
|
|
47
|
+
headers: { "Authorization" => "Bearer #{public_token.token}" }
|
|
48
|
+
)
|
|
49
|
+
expect(response).to have_http_status(:ok)
|
|
50
|
+
meta = response.parsed_body.dig("data", "meta")
|
|
51
|
+
expect(meta["votes_enabled"]).to be(true)
|
|
52
|
+
expect(meta["can_vote"]).to be(true)
|
|
53
|
+
|
|
54
|
+
post(
|
|
55
|
+
"#{api_prefix}/oauth/token",
|
|
56
|
+
params: {
|
|
57
|
+
grant_type: "password",
|
|
58
|
+
auth_type: "impersonate",
|
|
59
|
+
username: voter.nickname,
|
|
60
|
+
client_id: api_client.client_id,
|
|
61
|
+
client_secret: api_client.client_secret,
|
|
62
|
+
scope: "public proposals"
|
|
63
|
+
}.to_json,
|
|
64
|
+
headers: json_headers
|
|
65
|
+
)
|
|
66
|
+
expect(response).to have_http_status(:ok)
|
|
67
|
+
impersonation_token = response.parsed_body["access_token"]
|
|
68
|
+
expect(impersonation_token).to be_present
|
|
69
|
+
|
|
70
|
+
get(
|
|
71
|
+
"#{api_prefix}/components/proposal_components/#{proposal_component.id}",
|
|
72
|
+
headers: { "Authorization" => "Bearer #{impersonation_token}" }
|
|
73
|
+
)
|
|
74
|
+
expect(response).to have_http_status(:ok)
|
|
75
|
+
impersonated_meta = response.parsed_body.dig("data", "meta")
|
|
76
|
+
expect(impersonated_meta["votes_enabled"]).to be(true)
|
|
77
|
+
expect(impersonated_meta["can_vote"]).to be(true)
|
|
78
|
+
|
|
79
|
+
post(
|
|
80
|
+
"#{api_prefix}/vote_proposals/sync",
|
|
81
|
+
params: { data: { weight: 1 }, proposal_id: proposal.id }.to_json,
|
|
82
|
+
headers: json_headers.merge("Authorization" => "Bearer #{impersonation_token}")
|
|
83
|
+
)
|
|
84
|
+
expect(response).to have_http_status(:ok)
|
|
85
|
+
expect(response.parsed_body.dig("data", "type")).to eq("vote_proposals")
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
# rubocop:enable RSpec/DescribeClass
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "swagger_helper"
|
|
4
|
+
RSpec.describe Decidim::Api::RestFull::VoteProposals::VoteProposalsController do
|
|
5
|
+
path "/vote_proposals" do
|
|
6
|
+
get "List vote proposals" do
|
|
7
|
+
tags "Proposals"
|
|
8
|
+
produces "application/json"
|
|
9
|
+
operationId "listProposalVotes"
|
|
10
|
+
description <<~README
|
|
11
|
+
List `Decidim::Proposals::ProposalVote` rows for published proposals in visible spaces.
|
|
12
|
+
|
|
13
|
+
### Filters
|
|
14
|
+
- `filter[creator_id]` (or `filter[author_id]`): voter user id
|
|
15
|
+
- `filter[proposal_id]`: proposal id
|
|
16
|
+
- `filter[component_id]`: proposals component id
|
|
17
|
+
- `filter[participatory_space_id]`: participatory space id
|
|
18
|
+
|
|
19
|
+
Responses support **conditional GET** (`ETag` / `If-None-Match`).
|
|
20
|
+
|
|
21
|
+
### Access
|
|
22
|
+
Requires impersonation (`proposals.vote` or `proposals.read` for listing).
|
|
23
|
+
README
|
|
24
|
+
|
|
25
|
+
it_behaves_like "paginated params"
|
|
26
|
+
it_behaves_like "filtered params", filter: "creator_id", item_schema: { type: :integer }, only: :integer
|
|
27
|
+
it_behaves_like "filtered params", filter: "proposal_id", item_schema: { type: :integer }, only: :integer
|
|
28
|
+
it_behaves_like "filtered params", filter: "component_id", item_schema: { type: :integer }, only: :integer
|
|
29
|
+
it_behaves_like "filtered params", filter: "participatory_space_id", item_schema: { type: :integer }, only: :integer
|
|
30
|
+
|
|
31
|
+
describe_api_endpoint(
|
|
32
|
+
controller: Decidim::Api::RestFull::VoteProposals::VoteProposalsController,
|
|
33
|
+
action: :index,
|
|
34
|
+
security_types: [:impersonationFlow],
|
|
35
|
+
scopes: ["proposals"],
|
|
36
|
+
permissions: ["proposals.read"]
|
|
37
|
+
) do
|
|
38
|
+
let!(:participatory_process) { create(:participatory_process, :with_steps, organization:) }
|
|
39
|
+
let!(:proposal_component) { create(:proposal_component, :with_votes_enabled, participatory_space: participatory_process) }
|
|
40
|
+
let!(:proposal) { create(:proposal, component: proposal_component) }
|
|
41
|
+
let!(:vote) { create(:proposal_vote, proposal:, author: user, weight: 1) }
|
|
42
|
+
|
|
43
|
+
let(:space_manifest) { "participatory_processes" }
|
|
44
|
+
let(:space_id) { participatory_process.id }
|
|
45
|
+
|
|
46
|
+
response "200", "Votes listed" do
|
|
47
|
+
schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:vote_proposals_index_response)
|
|
48
|
+
|
|
49
|
+
run_test!(example_name: :ok) do |response|
|
|
50
|
+
ids = JSON.parse(response.body)["data"].map { |r| r["id"] }
|
|
51
|
+
expect(ids).to include(vote.id.to_s)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
post "Vote on a proposal (async)" do
|
|
58
|
+
tags "Proposals"
|
|
59
|
+
consumes "application/json"
|
|
60
|
+
produces "application/json"
|
|
61
|
+
operationId "castProposalVoteAsync"
|
|
62
|
+
description <<~README
|
|
63
|
+
Enqueue a vote on a **published** proposal. Poll `GET /jobs/:uuid` for the result (slim vote payload by default).
|
|
64
|
+
|
|
65
|
+
Prefer this endpoint under load. Use `POST /vote_proposals/sync` when you need an immediate response.
|
|
66
|
+
README
|
|
67
|
+
|
|
68
|
+
parameter name: :body, in: :body, required: true, schema: {
|
|
69
|
+
"$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:vote_proposal_create_body)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
describe_api_endpoint(
|
|
73
|
+
controller: Decidim::Api::RestFull::VoteProposals::VoteProposalsController,
|
|
74
|
+
action: :create,
|
|
75
|
+
security_types: [:impersonationFlow],
|
|
76
|
+
scopes: ["proposals"],
|
|
77
|
+
permissions: ["proposals.vote"]
|
|
78
|
+
) do
|
|
79
|
+
let!(:participatory_process) { create(:participatory_process, :with_steps, organization:) }
|
|
80
|
+
let!(:proposal_component) { create(:proposal_component, :with_votes_enabled, participatory_space: participatory_process) }
|
|
81
|
+
let!(:proposal) { create(:proposal, component: proposal_component) }
|
|
82
|
+
let!(:body) { { data: { weight: 1 }, proposal_id: proposal.id } }
|
|
83
|
+
|
|
84
|
+
response "202", "Job accepted" do
|
|
85
|
+
schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:rest_full_api_job_accepted)
|
|
86
|
+
|
|
87
|
+
run_test!(example_name: :accepted) do |response|
|
|
88
|
+
expect(response).to have_http_status(:accepted)
|
|
89
|
+
expect(JSON.parse(response.body)).to include("job_id")
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
path "/vote_proposals/sync" do
|
|
97
|
+
post "Vote on a proposal (sync)" do
|
|
98
|
+
tags "Proposals"
|
|
99
|
+
consumes "application/json"
|
|
100
|
+
produces "application/json"
|
|
101
|
+
operationId "castProposalVote"
|
|
102
|
+
description <<~README
|
|
103
|
+
Cast a vote synchronously. Returns a **vote_proposal** resource by default.
|
|
104
|
+
|
|
105
|
+
Set query `include_proposal=true` to return the full **proposal** payload (heavier).
|
|
106
|
+
README
|
|
107
|
+
|
|
108
|
+
parameter name: :body, in: :body, required: true, schema: {
|
|
109
|
+
"$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:vote_proposal_create_body)
|
|
110
|
+
}
|
|
111
|
+
parameter name: :include_proposal, in: :query, schema: { type: :boolean }, required: false,
|
|
112
|
+
description: "When true, return the full proposal instead of the vote resource"
|
|
113
|
+
|
|
114
|
+
describe_api_endpoint(
|
|
115
|
+
controller: Decidim::Api::RestFull::VoteProposals::VoteProposalsController,
|
|
116
|
+
action: :create_sync,
|
|
117
|
+
security_types: [:impersonationFlow],
|
|
118
|
+
scopes: ["proposals"],
|
|
119
|
+
permissions: ["proposals.vote"]
|
|
120
|
+
) do
|
|
121
|
+
let!(:participatory_process) { create(:participatory_process, :with_steps, organization:) }
|
|
122
|
+
let!(:proposal_component) { create(:proposal_component, :with_votes_enabled, participatory_space: participatory_process) }
|
|
123
|
+
let!(:proposal) { create(:proposal, component: proposal_component) }
|
|
124
|
+
let(:"locales[]") { %w(en fr) }
|
|
125
|
+
let!(:body) { { data: { weight: 1 }, proposal_id: proposal.id } }
|
|
126
|
+
|
|
127
|
+
response "400", "Bad Request when user already voted" do
|
|
128
|
+
produces "application/json"
|
|
129
|
+
schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:error_response)
|
|
130
|
+
|
|
131
|
+
before { create(:proposal_vote, proposal:, author: user, weight: 1) }
|
|
132
|
+
|
|
133
|
+
run_test!(example_name: :already_voted)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
response "200", "Vote created" do
|
|
137
|
+
produces "application/json"
|
|
138
|
+
schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:vote_proposal_item_response)
|
|
139
|
+
|
|
140
|
+
run_test!(example_name: :default) do |response|
|
|
141
|
+
data = JSON.parse(response.body)["data"]
|
|
142
|
+
expect(data["type"]).to eq("vote_proposals")
|
|
143
|
+
expect(data["attributes"]["weight"]).to eq(1)
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
response "200", "Vote created with full proposal" do
|
|
148
|
+
produces "application/json"
|
|
149
|
+
schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:proposal_item_response)
|
|
150
|
+
|
|
151
|
+
let(:include_proposal) { true }
|
|
152
|
+
|
|
153
|
+
run_test!(example_name: :with_proposal) do |response|
|
|
154
|
+
data = JSON.parse(response.body)["data"]
|
|
155
|
+
expect(data["id"]).to eq(proposal.id.to_s)
|
|
156
|
+
expect(data["meta"]["voted"]).to eq({ "weight" => 1 })
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
path "/vote_proposals/{id}" do
|
|
164
|
+
get "Show vote proposal" do
|
|
165
|
+
tags "Proposals"
|
|
166
|
+
produces "application/json"
|
|
167
|
+
operationId "getProposalVote"
|
|
168
|
+
description "Fetch a single vote by id. Conditional GET supported."
|
|
169
|
+
|
|
170
|
+
parameter name: :id, in: :path, schema: { type: :integer }, required: true
|
|
171
|
+
|
|
172
|
+
describe_api_endpoint(
|
|
173
|
+
controller: Decidim::Api::RestFull::VoteProposals::VoteProposalsController,
|
|
174
|
+
action: :show,
|
|
175
|
+
security_types: [:impersonationFlow],
|
|
176
|
+
scopes: ["proposals"],
|
|
177
|
+
permissions: ["proposals.read"]
|
|
178
|
+
) do
|
|
179
|
+
let!(:participatory_process) { create(:participatory_process, :with_steps, organization:) }
|
|
180
|
+
let!(:proposal_component) { create(:proposal_component, :with_votes_enabled, participatory_space: participatory_process) }
|
|
181
|
+
let!(:proposal) { create(:proposal, component: proposal_component) }
|
|
182
|
+
let!(:vote) { create(:proposal_vote, proposal:, author: user, weight: 1) }
|
|
183
|
+
let(:id) { vote.id }
|
|
184
|
+
|
|
185
|
+
let(:space_manifest) { "participatory_processes" }
|
|
186
|
+
let(:space_id) { participatory_process.id }
|
|
187
|
+
|
|
188
|
+
response "200", "Vote found" do
|
|
189
|
+
schema type: :object,
|
|
190
|
+
properties: {
|
|
191
|
+
data: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:vote_proposal) }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
run_test!(example_name: :ok)
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
delete "Remove vote on a proposal" do
|
|
200
|
+
tags "Proposals"
|
|
201
|
+
produces "application/json"
|
|
202
|
+
operationId "deleteProposalVote"
|
|
203
|
+
description "Remove the impersonated user's vote (`Decidim::Proposals::UnvoteProposal`). Returns the vote resource."
|
|
204
|
+
|
|
205
|
+
parameter name: :id, in: :path, schema: { type: :integer }, required: true
|
|
206
|
+
|
|
207
|
+
describe_api_endpoint(
|
|
208
|
+
controller: Decidim::Api::RestFull::VoteProposals::VoteProposalsController,
|
|
209
|
+
action: :destroy,
|
|
210
|
+
security_types: [:impersonationFlow],
|
|
211
|
+
scopes: ["proposals"],
|
|
212
|
+
permissions: ["proposals.vote"]
|
|
213
|
+
) do
|
|
214
|
+
let!(:participatory_process) { create(:participatory_process, :with_steps, organization:) }
|
|
215
|
+
let!(:proposal_component) { create(:proposal_component, :with_votes_enabled, participatory_space: participatory_process) }
|
|
216
|
+
let!(:proposal) { create(:proposal, component: proposal_component) }
|
|
217
|
+
let!(:vote) { create(:proposal_vote, proposal:, author: user, weight: 1) }
|
|
218
|
+
let(:id) { vote.id }
|
|
219
|
+
|
|
220
|
+
let(:space_manifest) { "participatory_processes" }
|
|
221
|
+
let(:space_id) { participatory_process.id }
|
|
222
|
+
|
|
223
|
+
response "200", "Vote removed" do
|
|
224
|
+
run_test!(example_name: :ok) do
|
|
225
|
+
expect(Decidim::Proposals::ProposalVote.exists?(vote.id)).to be(false)
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "swagger_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe Decidim::Api::RestFull::Proposals::ProposalComponentSerializer do
|
|
6
|
+
subject(:serialized) do
|
|
7
|
+
described_class.new(
|
|
8
|
+
proposal_component.reload,
|
|
9
|
+
params: {
|
|
10
|
+
only: [],
|
|
11
|
+
locales: %w(en),
|
|
12
|
+
host: organization.host,
|
|
13
|
+
act_as: user,
|
|
14
|
+
client_id: api_client.id
|
|
15
|
+
}
|
|
16
|
+
).serializable_hash
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
let(:organization) { create(:organization, available_locales: %w(en)) }
|
|
20
|
+
let(:data) { serialized.fetch(:data) }
|
|
21
|
+
let(:meta) { data.fetch(:meta).symbolize_keys }
|
|
22
|
+
let(:resource_count) do
|
|
23
|
+
data.dig(:relationships, :resources, :meta, :count)
|
|
24
|
+
end
|
|
25
|
+
let(:participatory_process) { create(:participatory_process, :with_steps, organization:) }
|
|
26
|
+
let(:user) { create(:user, organization:, confirmed_at: Time.zone.now) }
|
|
27
|
+
let(:api_client) { create(:api_client, organization:, scopes: ["public"]) }
|
|
28
|
+
|
|
29
|
+
describe "fields read by chat-platform when building proposal component menu buttons" do
|
|
30
|
+
context "when participants can create proposals and voting is off" do
|
|
31
|
+
let!(:proposal_component) do
|
|
32
|
+
create(
|
|
33
|
+
:proposal_component,
|
|
34
|
+
:with_creation_enabled,
|
|
35
|
+
participatory_space: participatory_process
|
|
36
|
+
)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "reports can_create_proposals, can_vote false, and zero resources" do
|
|
40
|
+
expect(meta[:can_create_proposals]).to be(true)
|
|
41
|
+
expect(meta[:can_vote]).to be(false)
|
|
42
|
+
expect(resource_count).to eq(0)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
context "with published proposals in the component" do
|
|
46
|
+
before do
|
|
47
|
+
create(:proposal, :accepted, component: proposal_component)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "exposes non-zero published resource count" do
|
|
51
|
+
expect(resource_count).to eq(1)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
context "when participants can create and voting is enabled" do
|
|
57
|
+
let!(:proposal_component) do
|
|
58
|
+
step_id = participatory_process.active_step.id
|
|
59
|
+
create(
|
|
60
|
+
:proposal_component,
|
|
61
|
+
participatory_space: participatory_process,
|
|
62
|
+
step_settings: {
|
|
63
|
+
step_id => { creation_enabled: true, votes_enabled: true }
|
|
64
|
+
},
|
|
65
|
+
settings: { awesome_voting_manifest: :voting_cards }
|
|
66
|
+
)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it "reports votes phase on but can_vote false when there is nothing to vote (no pending unvoted proposals)" do
|
|
70
|
+
expect(meta[:can_create_proposals]).to be(true)
|
|
71
|
+
expect(meta[:can_vote]).to be(false)
|
|
72
|
+
expect(resource_count).to eq(0)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
context "with published proposals" do
|
|
76
|
+
let!(:published_proposal) do
|
|
77
|
+
create(:proposal, :accepted, component: proposal_component)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it "exposes positive resource count and can_vote while the user has unvoted proposals" do
|
|
81
|
+
expect(resource_count).to eq(1)
|
|
82
|
+
expect(meta[:can_vote]).to be(true)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it "exposes awesome status restriction keys" do
|
|
86
|
+
expect(meta).to have_key(:awesome_votes_enabled_by_status)
|
|
87
|
+
expect(meta).to have_key(:awesome_votes_enabled_states)
|
|
88
|
+
expect(meta[:awesome_votes_enabled_by_status]).to be(false)
|
|
89
|
+
expect(meta[:awesome_votes_enabled_states]).to eq([])
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
context "when the participant has voted on every voteable proposal" do
|
|
93
|
+
before do
|
|
94
|
+
create(:proposal_vote, proposal: published_proposal, author: user)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it "sets can_vote false" do
|
|
98
|
+
expect(meta[:can_vote]).to be(false)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
context "when voting is restricted by proposal status", if: (
|
|
105
|
+
Decidim::Toggle.gem_present?("decidim-decidim_awesome") &&
|
|
106
|
+
defined?(Decidim::DecidimAwesome) &&
|
|
107
|
+
Decidim::DecidimAwesome.enabled?(:votes_by_proposal_status)
|
|
108
|
+
) do
|
|
109
|
+
let!(:proposal_component) do
|
|
110
|
+
step_id = participatory_process.active_step.id
|
|
111
|
+
create(
|
|
112
|
+
:proposal_component,
|
|
113
|
+
participatory_space: participatory_process,
|
|
114
|
+
step_settings: {
|
|
115
|
+
step_id => {
|
|
116
|
+
creation_enabled: true,
|
|
117
|
+
votes_enabled: true,
|
|
118
|
+
awesome_votes_enabled_by_status: true,
|
|
119
|
+
awesome_votes_enabled_states: %w(accepted)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
it "exposes status restriction meta" do
|
|
126
|
+
expect(meta[:awesome_votes_enabled_by_status]).to be(true)
|
|
127
|
+
expect(meta[:awesome_votes_enabled_states]).to eq(%w(accepted))
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
context "with only disallowed-status proposals" do
|
|
131
|
+
before { create(:proposal, :evaluating, component: proposal_component) }
|
|
132
|
+
|
|
133
|
+
it "sets can_vote false" do
|
|
134
|
+
expect(meta[:can_vote]).to be(false)
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
context "with an allowed unvoted proposal" do
|
|
139
|
+
before { create(:proposal, :accepted, component: proposal_component) }
|
|
140
|
+
|
|
141
|
+
it "sets can_vote true" do
|
|
142
|
+
expect(meta[:can_vote]).to be(true)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|