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,275 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "swagger_helper"
|
|
4
|
+
RSpec.describe Decidim::Api::RestFull::Proposals::ProposalsController do
|
|
5
|
+
path "/proposals" do
|
|
6
|
+
get "Proposals" do
|
|
7
|
+
tags "Proposals"
|
|
8
|
+
produces "application/json"
|
|
9
|
+
operationId "listProposals"
|
|
10
|
+
description "Search proposals"
|
|
11
|
+
it_behaves_like "localized params"
|
|
12
|
+
it_behaves_like "paginated params"
|
|
13
|
+
it_behaves_like "resource params"
|
|
14
|
+
it_behaves_like "ordered params", columns: %w(published_at rand)
|
|
15
|
+
it_behaves_like "filtered params", filter: "voted_weight", item_schema: { type: :string }, only: :integer, security: [:impersonationFlow]
|
|
16
|
+
it_behaves_like "filtered params", filter: "state", item_schema: { type: :string }, only: :string
|
|
17
|
+
parameter name: :"filter[extended_data_cont]",
|
|
18
|
+
in: :query,
|
|
19
|
+
schema: { type: :string },
|
|
20
|
+
required: false,
|
|
21
|
+
description: "Search on proposal extended_data. Requires `proposals.extended_data.read`. See [Extended data](#{Decidim::RestFull.config.docs_url}/integrator/extended-data)."
|
|
22
|
+
|
|
23
|
+
describe_api_endpoint(
|
|
24
|
+
controller: Decidim::Api::RestFull::Proposals::ProposalsController,
|
|
25
|
+
action: :index,
|
|
26
|
+
security_types: [:credentialFlow, :impersonationFlow],
|
|
27
|
+
scopes: ["proposals"],
|
|
28
|
+
permissions: ["proposals.read"]
|
|
29
|
+
) do
|
|
30
|
+
it_behaves_like "localized endpoint"
|
|
31
|
+
let(:proposal_id) { proposal.id }
|
|
32
|
+
let(:component_id) { proposal_component.id }
|
|
33
|
+
let(:space_id) { participatory_process.id }
|
|
34
|
+
let(:space_manifest) { "participatory_processes" }
|
|
35
|
+
let(:user) { create(:user, organization:) }
|
|
36
|
+
let(:"locales[]") { %w(en fr) }
|
|
37
|
+
let!(:proposal) { create(:proposal, component: proposal_component) }
|
|
38
|
+
let!(:proposal_component) { create(:proposal_component, participatory_space: participatory_process) }
|
|
39
|
+
let!(:participatory_process) { create(:participatory_process, :with_steps, organization:) }
|
|
40
|
+
let!(:organization) { create(:organization, available_locales: ["en"]) }
|
|
41
|
+
let!(:per_page) { 50 }
|
|
42
|
+
let!(:page) { 1 }
|
|
43
|
+
|
|
44
|
+
response "200", "Proposal List" do
|
|
45
|
+
produces "application/json"
|
|
46
|
+
schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:proposal_index_response)
|
|
47
|
+
it_behaves_like "paginated endpoint" do
|
|
48
|
+
let(:create_resource) { -> { create(:proposal, :accepted, component: proposal_component, published_at: 1.day.ago) } }
|
|
49
|
+
let(:each_resource) { ->(_resource, _index) {} }
|
|
50
|
+
let(:resources) { Decidim::Proposals::Proposal.where(component: proposal_component) }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it_behaves_like "ordered endpoint", columns: %w(rand published_at) do
|
|
54
|
+
let(:create_resource) { -> { create(:proposal, :accepted, component: proposal_component, published_at: 1.day.ago) } }
|
|
55
|
+
let(:each_resource) do
|
|
56
|
+
lambda { |resource, index|
|
|
57
|
+
resource.update!(published_at: (index + 1).minutes.ago)
|
|
58
|
+
}
|
|
59
|
+
end
|
|
60
|
+
let(:resources) { Decidim::Proposals::Proposal.where(component: proposal_component) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
context "when voting_cards is enabled" do
|
|
64
|
+
let!(:participatory_process) { create(:participatory_process, :with_steps, organization:) }
|
|
65
|
+
let(:proposal_component) do
|
|
66
|
+
component = create(
|
|
67
|
+
:proposal_component,
|
|
68
|
+
:with_votes_enabled,
|
|
69
|
+
participatory_space: participatory_process,
|
|
70
|
+
settings: { awesome_voting_manifest: :voting_cards, voting_cards_show_abstain: true }
|
|
71
|
+
)
|
|
72
|
+
component
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
let!(:proposals) do
|
|
76
|
+
create(:proposal, :accepted, component: proposal_component)
|
|
77
|
+
create(:proposal, :rejected, component: proposal_component)
|
|
78
|
+
create_list(:proposal, 2, component: proposal_component)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
context "with filter state_eq accepted, filter only published accepted proposal" do
|
|
82
|
+
let(:"filter[state_eq]") { "accepted" }
|
|
83
|
+
|
|
84
|
+
run_test!(example_name: :filtered_by_state) do |example|
|
|
85
|
+
data = JSON.parse(example.body)["data"]
|
|
86
|
+
data.each do |d|
|
|
87
|
+
expect(d["relationships"]["state"]["meta"]["token"]).to eq("accepted")
|
|
88
|
+
expect(d["meta"]["published"]).to be_truthy
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
context "with filter state_not_eq rejected, filter only non-rejected publisehd proposal" do
|
|
94
|
+
let(:"filter[state_not_eq]") { "rejected" }
|
|
95
|
+
|
|
96
|
+
run_test!(example_name: :state_non_rejected) do |example|
|
|
97
|
+
data = JSON.parse(example.body)["data"]
|
|
98
|
+
data.each do |d|
|
|
99
|
+
expect(d["meta"]["published"]).to be_truthy
|
|
100
|
+
if d["relationships"]["state"]
|
|
101
|
+
expect(d["relationships"]["state"]["meta"]["token"]).not_to eq("rejected")
|
|
102
|
+
else
|
|
103
|
+
expect(d["relationships"]["state"]).to be_nil
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
on_security(:impersonationFlow) do
|
|
110
|
+
before do
|
|
111
|
+
accepted_proposal = create(:proposal, :accepted, component: proposal_component)
|
|
112
|
+
create(:proposal_vote, proposal: accepted_proposal, author: user).update(weight: 1)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
context "when list own drafts" do
|
|
116
|
+
let!(:draft_proposal) do
|
|
117
|
+
proposal = create(:proposal, component: proposal_component, published_at: nil, users: [user])
|
|
118
|
+
proposal.save!
|
|
119
|
+
proposal
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
let(:proposal_id) { draft_proposal.id }
|
|
123
|
+
|
|
124
|
+
run_test!(example_name: :ok_drafts) do |example|
|
|
125
|
+
data = JSON.parse(example.body)["data"]
|
|
126
|
+
draft = data.find { |d| d["meta"]["published"] == false }
|
|
127
|
+
expect(draft).to be_truthy
|
|
128
|
+
expect(draft["id"]).to eq(draft_proposal.id.to_s)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
context "with filter voted_weight" do
|
|
133
|
+
context "when filter voted_weight_eq 1, filter only the vote_weight=1" do
|
|
134
|
+
let(:"filter[voted_weight_eq]") { 1.to_s }
|
|
135
|
+
|
|
136
|
+
before do
|
|
137
|
+
create(:proposal_vote, proposal: create(:proposal, component: proposal_component), author: user).update(weight: 1)
|
|
138
|
+
create(:proposal_vote, proposal: create(:proposal, component: proposal_component), author: user).update(weight: 2)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
run_test!(example_name: :voted) do |example|
|
|
142
|
+
data = JSON.parse(example.body)["data"]
|
|
143
|
+
data.each do |d|
|
|
144
|
+
expect(d["meta"]["voted"]).to eq({ "weight" => 1 })
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
context "when filter voted_weight_eq 0, filter only the abstention" do
|
|
150
|
+
let(:"filter[voted_weight_eq]") { 0.to_s }
|
|
151
|
+
|
|
152
|
+
before do
|
|
153
|
+
create(:proposal_vote, proposal: create(:proposal, component: proposal_component), author: user).update(weight: 0)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
run_test!(example_name: :abstentions) do |example|
|
|
157
|
+
data = JSON.parse(example.body)["data"]
|
|
158
|
+
data.each do |d|
|
|
159
|
+
expect(d["meta"]["voted"]).to eq({ "weight" => 0 })
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
context "when filter voted_weight_eq 1 and state_eq rejected, return an empty list" do
|
|
165
|
+
let(:"filter[state_eq]") { "rejected" }
|
|
166
|
+
let(:"filter[voted_weight_eq]") { 0.to_s }
|
|
167
|
+
|
|
168
|
+
run_test! do |example|
|
|
169
|
+
data = JSON.parse(example.body)["data"]
|
|
170
|
+
expect(data).to be_empty
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
context "when filter voted_weight_blank, filter only the non-voted proposals" do
|
|
175
|
+
let(:"filter[voted_weight_blank]") { "true" }
|
|
176
|
+
|
|
177
|
+
before do
|
|
178
|
+
create(:proposal, component: proposal_component)
|
|
179
|
+
voted = create(:proposal, component: proposal_component)
|
|
180
|
+
create(:proposal_vote, proposal: voted, author: user).update(weight: 1)
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
run_test!(example_name: :unvoted) do |example|
|
|
184
|
+
data = JSON.parse(example.body)["data"]
|
|
185
|
+
data.each do |d|
|
|
186
|
+
expect(d["meta"]).not_to have_key("voted")
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
context "when filter voted_weight_blank, and per_page=1, order=rand get one non-voted proposal" do
|
|
192
|
+
let(:"filter[voted_weight_blank]") { true }
|
|
193
|
+
let(:per_page) { 1 }
|
|
194
|
+
let(:order) { "rand" }
|
|
195
|
+
|
|
196
|
+
before do
|
|
197
|
+
create(:proposal, component: proposal_component)
|
|
198
|
+
voted = create(:proposal, component: proposal_component)
|
|
199
|
+
create(:proposal_vote, proposal: voted, author: user).update(weight: 1)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
run_test!(example_name: :unvoted_unordered_list) do |example|
|
|
203
|
+
data = JSON.parse(example.body)["data"]
|
|
204
|
+
expect(data.size).to eq(1)
|
|
205
|
+
expect(data.first["meta"]["voted"]).to be_blank
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
context "with filter[extended_data_cont] and permission" do
|
|
213
|
+
let(:api_client) do
|
|
214
|
+
client = create(:api_client, organization:, scopes: %w(proposals))
|
|
215
|
+
client.permissions = [
|
|
216
|
+
client.permissions.build(permission: "proposals.read"),
|
|
217
|
+
client.permissions.build(permission: "proposals.extended_data.read")
|
|
218
|
+
]
|
|
219
|
+
client.save!
|
|
220
|
+
client
|
|
221
|
+
end
|
|
222
|
+
let(:"filter[extended_data_cont]") { '"ext": "1"' }
|
|
223
|
+
|
|
224
|
+
before do
|
|
225
|
+
proposal.extended_data.update!(data: { "ext" => "1" })
|
|
226
|
+
create(:proposal, component: proposal_component)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
run_test!(example_name: :filtered_by_extended_data) do |example|
|
|
230
|
+
data = JSON.parse(example.body)["data"]
|
|
231
|
+
expect(data.map { |d| d["id"] }).to eq([proposal.id.to_s])
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
context "with filter[extended_data_cont] no hit" do
|
|
236
|
+
let(:api_client) do
|
|
237
|
+
client = create(:api_client, organization:, scopes: %w(proposals))
|
|
238
|
+
client.permissions = [
|
|
239
|
+
client.permissions.build(permission: "proposals.read"),
|
|
240
|
+
client.permissions.build(permission: "proposals.extended_data.read")
|
|
241
|
+
]
|
|
242
|
+
client.save!
|
|
243
|
+
client
|
|
244
|
+
end
|
|
245
|
+
let(:"filter[extended_data_cont]") { '"ext": "missing"' }
|
|
246
|
+
|
|
247
|
+
before do
|
|
248
|
+
proposal.extended_data.update!(data: { "ext" => "1" })
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
run_test!(example_name: :filtered_by_extended_data_miss) do |example|
|
|
252
|
+
data = JSON.parse(example.body)["data"]
|
|
253
|
+
expect(data).to be_empty
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
response "403", "Forbidden when filtering extended_data without permission" do
|
|
259
|
+
produces "application/json"
|
|
260
|
+
let(:"filter[extended_data_cont]") { '"ext": "1"' }
|
|
261
|
+
|
|
262
|
+
before do
|
|
263
|
+
proposal.extended_data.update!(data: { "ext" => "1" })
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
run_test!(example_name: :filter_extended_data_forbidden) do |example|
|
|
267
|
+
expect(example.status).to eq(403)
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
it_behaves_like "unauthorized when no Bearer token"
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
end
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "swagger_helper"
|
|
4
|
+
RSpec.describe Decidim::Api::RestFull::Proposals::ProposalsController do
|
|
5
|
+
path "/proposals/{id}" do
|
|
6
|
+
get "Proposal Details" do
|
|
7
|
+
tags "Proposals"
|
|
8
|
+
produces "application/json"
|
|
9
|
+
security [{ credentialFlowBearer: ["proposals"] }, { resourceOwnerFlowBearer: ["proposals"] }]
|
|
10
|
+
operationId "getProposal"
|
|
11
|
+
description "Proposal detail"
|
|
12
|
+
it_behaves_like "localized params"
|
|
13
|
+
it_behaves_like "resource params"
|
|
14
|
+
it_behaves_like "filtered params", filter: "voted_weight", item_schema: { type: :string }, only: :string
|
|
15
|
+
it_behaves_like "filtered params", filter: "state", item_schema: { type: :string }, only: :string
|
|
16
|
+
it_behaves_like "ordered params"
|
|
17
|
+
|
|
18
|
+
parameter name: "id", in: :path, schema: { type: :integer, description: "Proposal Id" }, required: true
|
|
19
|
+
|
|
20
|
+
describe_api_endpoint(
|
|
21
|
+
controller: Decidim::Api::RestFull::Proposals::ProposalsController,
|
|
22
|
+
action: :show,
|
|
23
|
+
security_types: [:credentialFlow, :impersonationFlow],
|
|
24
|
+
scopes: ["proposals"],
|
|
25
|
+
permissions: ["proposals.read"]
|
|
26
|
+
) do
|
|
27
|
+
it_behaves_like "localized endpoint"
|
|
28
|
+
let(:id) { proposal.id }
|
|
29
|
+
let(:component_id) { proposal_component.id }
|
|
30
|
+
let(:space_id) { participatory_process.id }
|
|
31
|
+
let(:space_manifest) { "participatory_processes" }
|
|
32
|
+
|
|
33
|
+
let(:"locales[]") { %w(en fr) }
|
|
34
|
+
let!(:proposal) { create(:proposal, component: proposal_component) }
|
|
35
|
+
let(:proposal_component) { create(:component, participatory_space: participatory_process, manifest_name: "proposals", published_at: Time.zone.now) }
|
|
36
|
+
let!(:participatory_process) { create(:participatory_process, organization:) }
|
|
37
|
+
|
|
38
|
+
response "200", "Proposal Found" do
|
|
39
|
+
produces "application/json"
|
|
40
|
+
schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:proposal_item_response)
|
|
41
|
+
|
|
42
|
+
context "when published" do
|
|
43
|
+
run_test!(example_name: :ok) do |example|
|
|
44
|
+
data = JSON.parse(example.body)["data"]
|
|
45
|
+
expect(data["id"]).to eq(id.to_s)
|
|
46
|
+
expect(data["meta"]["published"]).to be_truthy
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
context "when paginating" do
|
|
51
|
+
context "when ordering by published_at DESC" do
|
|
52
|
+
context "when selecting the last published proposal, next is the 2nd last published proposal, prev is nil" do
|
|
53
|
+
let(:order) { "published_at" }
|
|
54
|
+
let(:order_direction) { "desc" }
|
|
55
|
+
let(:last_proposal_component) { create(:component, participatory_space: participatory_process, manifest_name: "proposals", published_at: Time.zone.now) }
|
|
56
|
+
let!(:first_proposal) { create(:proposal, reference: "FIRST", component: last_proposal_component, published_at: 1.day.ago) }
|
|
57
|
+
let!(:second_proposal) { create(:proposal, reference: "SECOND", component: last_proposal_component, published_at: 2.days.ago) }
|
|
58
|
+
let!(:third_proposal) { create(:proposal, reference: "THIRD", component: last_proposal_component, published_at: 3.days.ago) }
|
|
59
|
+
let(:"filter[state_not_eq]") { "rejected" }
|
|
60
|
+
let(:component_id) { last_proposal_component.id }
|
|
61
|
+
let(:id) { first_proposal.id }
|
|
62
|
+
|
|
63
|
+
run_test! do |example|
|
|
64
|
+
data = JSON.parse(example.body)["data"]
|
|
65
|
+
expect(data["id"].to_i).to eq(id.to_i)
|
|
66
|
+
expect(data["meta"]["published"]).to be_truthy
|
|
67
|
+
expect(data["links"]["next"]).to be_present
|
|
68
|
+
expect(data["links"]["next"]["meta"]["resource_id"].to_i).to eq(second_proposal.id.to_i)
|
|
69
|
+
expect(data["links"]["prev"]).to be_nil
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
context "when selecting the first published proposal, prev is the second published proposal, next is nil" do
|
|
74
|
+
let(:order) { "published_at" }
|
|
75
|
+
let(:order_direction) { "desc" }
|
|
76
|
+
let(:last_proposal_component) { create(:component, participatory_space: participatory_process, manifest_name: "proposals", published_at: Time.zone.now) }
|
|
77
|
+
let!(:first_proposal) { create(:proposal, reference: "FIRST", component: last_proposal_component, published_at: 1.day.ago) }
|
|
78
|
+
let!(:second_proposal) { create(:proposal, reference: "SECOND", component: last_proposal_component, published_at: 2.days.ago) }
|
|
79
|
+
let!(:third_proposal) { create(:proposal, reference: "THIRD", component: last_proposal_component, published_at: 3.days.ago) }
|
|
80
|
+
let(:"filter[state_not_eq]") { "rejected" }
|
|
81
|
+
let(:component_id) { last_proposal_component.id }
|
|
82
|
+
let(:id) { third_proposal.id }
|
|
83
|
+
|
|
84
|
+
run_test! do |example|
|
|
85
|
+
data = JSON.parse(example.body)["data"]
|
|
86
|
+
expect(data["id"].to_i).to eq(id.to_i)
|
|
87
|
+
expect(data["meta"]["published"]).to be_truthy
|
|
88
|
+
expect(data["links"]["next"]).to be_nil
|
|
89
|
+
expect(data["links"]["prev"]).to be_present
|
|
90
|
+
expect(data["links"]["prev"]["meta"]["resource_id"].to_i).to eq(second_proposal.id)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
context "when looking for the next not-rejected proposal" do
|
|
96
|
+
let(:order) { "published_at" }
|
|
97
|
+
let(:order_direction) { "desc" }
|
|
98
|
+
let!(:newer_proposal) { create(:proposal, :accepted, component: proposal_component, published_at: 1.day.ago) }
|
|
99
|
+
let!(:rejected_proposal) { create(:proposal, :rejected, component: proposal_component, published_at: 1.5.days.ago) }
|
|
100
|
+
let!(:older_proposal) { create(:proposal, component: proposal_component, published_at: 2.days.ago) }
|
|
101
|
+
let(:"filter[state_not_eq]") { "rejected" }
|
|
102
|
+
let(:id) { newer_proposal.id }
|
|
103
|
+
|
|
104
|
+
run_test!(example_name: :navigation_non_rejected) do |example|
|
|
105
|
+
data = JSON.parse(example.body)["data"]
|
|
106
|
+
expect(data["id"].to_i).to eq(id.to_i)
|
|
107
|
+
expect(data["meta"]["published"]).to be_truthy
|
|
108
|
+
expect(data["links"]["next"]).to be_present
|
|
109
|
+
expect(data["links"]["next"]["meta"]["resource_id"].to_i).to eq(older_proposal.id.to_i)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
context "when looking at the last accepted proposal" do
|
|
114
|
+
let(:order) { "published_at" }
|
|
115
|
+
let(:order_direction) { "desc" }
|
|
116
|
+
let!(:newer_accepted_proposal) { create(:proposal, :accepted, component: proposal_component, published_at: 1.day.ago) }
|
|
117
|
+
let!(:older_accepted_proposal) { create(:proposal, :accepted, component: proposal_component, published_at: 2.days.ago) }
|
|
118
|
+
let!(:rejected_proposal) { create(:proposal, :rejected, component: proposal_component, published_at: 1.5.days.ago) }
|
|
119
|
+
let(:"filter[state_eq]") { "accepted" }
|
|
120
|
+
let(:id) { older_accepted_proposal.id }
|
|
121
|
+
|
|
122
|
+
run_test!(example_name: :navigation_last_accepted) do |example|
|
|
123
|
+
data = JSON.parse(example.body)["data"]
|
|
124
|
+
expect(data["id"].to_i).to eq(id.to_i)
|
|
125
|
+
expect(data["meta"]["published"]).to be_truthy
|
|
126
|
+
expect(data["links"]["next"]).to be_nil
|
|
127
|
+
expect(data["links"]["prev"]).to be_present
|
|
128
|
+
expect(data["links"]["prev"]["meta"]["resource_id"].to_i).to eq(newer_accepted_proposal.id.to_i)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
on_security(:impersonationFlow) do
|
|
134
|
+
context "when own drafts" do
|
|
135
|
+
let!(:draft_proposal) do
|
|
136
|
+
create(:proposal, component: proposal_component, published_at: nil, users: [user])
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
let(:id) { draft_proposal.id }
|
|
140
|
+
|
|
141
|
+
run_test!(example_name: :ok_drafts) do |example|
|
|
142
|
+
data = JSON.parse(example.body)["data"]
|
|
143
|
+
expect(data).to be_truthy
|
|
144
|
+
expect(data["id"].to_i).to eq(draft_proposal.id)
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
context "when answered proposal" do
|
|
150
|
+
let!(:accepted_proposal) { create(:proposal, :accepted, component: proposal_component) }
|
|
151
|
+
|
|
152
|
+
context "when accepted" do
|
|
153
|
+
let(:id) { accepted_proposal.id }
|
|
154
|
+
|
|
155
|
+
run_test!(example_name: :accepted_proposal) do |example|
|
|
156
|
+
data = JSON.parse(example.body)["data"]
|
|
157
|
+
expect(data).to be_truthy
|
|
158
|
+
expect(data["id"].to_i).to eq(accepted_proposal.id.to_i)
|
|
159
|
+
expect(data["relationships"]["state"]["meta"]).to eq({ "token" => "accepted" })
|
|
160
|
+
expect(data["relationships"]["state"]["data"]).to eq({ "id" => accepted_proposal.proposal_state.id.to_s, "type" => "proposal_state" })
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
response "404", "Not Found" do
|
|
167
|
+
consumes "application/json"
|
|
168
|
+
produces "application/json"
|
|
169
|
+
schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:error_response)
|
|
170
|
+
on_security(:impersonationFlow) do
|
|
171
|
+
context "with draft that did not co-authored" do
|
|
172
|
+
let!(:draft_proposal) do
|
|
173
|
+
proposal = create(:proposal, component: proposal_component, published_at: nil, users: [create(:user, :confirmed, organization:)])
|
|
174
|
+
proposal.save!
|
|
175
|
+
proposal
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
let(:id) { draft_proposal.id }
|
|
179
|
+
|
|
180
|
+
run_test!(example_name: :not_found)
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
end
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
# rubocop:disable RSpec/DescribeClass -- behaviour scenario (multi-step HTTP)
|
|
6
|
+
RSpec.describe "Draft publish async increments component count and lists newest" do
|
|
7
|
+
include ActiveJob::TestHelper
|
|
8
|
+
|
|
9
|
+
let!(:organization) { create(:organization, available_locales: ["en"], default_locale: "en") }
|
|
10
|
+
let!(:participatory_process) { create(:participatory_process, organization:) }
|
|
11
|
+
let!(:proposal_component) do
|
|
12
|
+
create(:component, participatory_space: participatory_process, manifest_name: "proposals", published_at: Time.zone.now)
|
|
13
|
+
end
|
|
14
|
+
let!(:existing_proposal) do
|
|
15
|
+
create(:proposal, component: proposal_component, published_at: 2.days.ago)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
let!(:api_client) do
|
|
19
|
+
c = create(:api_client, organization:, scopes: %w(oauth public proposals))
|
|
20
|
+
c.permissions = [
|
|
21
|
+
Decidim::RestFull::Core::Permission.new(permission: "oauth.impersonate"),
|
|
22
|
+
Decidim::RestFull::Core::Permission.new(permission: "public.component.read"),
|
|
23
|
+
Decidim::RestFull::Core::Permission.new(permission: "proposals.draft"),
|
|
24
|
+
Decidim::RestFull::Core::Permission.new(permission: "proposals.read")
|
|
25
|
+
]
|
|
26
|
+
c.save!
|
|
27
|
+
c
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
let!(:credential_token) do
|
|
31
|
+
create(:oauth_access_token, scopes: "public", resource_owner_id: nil, application: api_client)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
let(:api_prefix) { "/api/rest_full/v#{Decidim::RestFull.major_minor_version}" }
|
|
35
|
+
let(:json_headers) { { "CONTENT_TYPE" => "application/json" } }
|
|
36
|
+
let(:nickname) { "author#{SecureRandom.hex(4)}" }
|
|
37
|
+
let(:email) { "#{nickname}@example.org" }
|
|
38
|
+
|
|
39
|
+
before { host!(organization.host) }
|
|
40
|
+
|
|
41
|
+
def bearer(token)
|
|
42
|
+
{ "Authorization" => "Bearer #{token}" }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def component_resources_count(token)
|
|
46
|
+
get(
|
|
47
|
+
"#{api_prefix}/components/proposal_components/#{proposal_component.id}",
|
|
48
|
+
headers: bearer(token)
|
|
49
|
+
)
|
|
50
|
+
expect(response).to have_http_status(:ok)
|
|
51
|
+
response.parsed_body.dig("data", "relationships", "resources", "meta", "count")
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# rubocop:disable RSpec/ExampleLength -- end-to-end publish scenario
|
|
55
|
+
it "upserts user, fills draft, publishes async, then count and newest list update" do
|
|
56
|
+
post(
|
|
57
|
+
"#{api_prefix}/oauth/token",
|
|
58
|
+
params: {
|
|
59
|
+
grant_type: "password",
|
|
60
|
+
auth_type: "impersonate",
|
|
61
|
+
username: nickname,
|
|
62
|
+
client_id: api_client.client_id,
|
|
63
|
+
client_secret: api_client.client_secret,
|
|
64
|
+
scope: "proposals",
|
|
65
|
+
meta: {
|
|
66
|
+
register_on_missing: true,
|
|
67
|
+
email:,
|
|
68
|
+
skip_confirmation_on_register: true
|
|
69
|
+
}
|
|
70
|
+
}.to_json,
|
|
71
|
+
headers: json_headers
|
|
72
|
+
)
|
|
73
|
+
expect(response).to have_http_status(:ok)
|
|
74
|
+
user_token = response.parsed_body["access_token"]
|
|
75
|
+
expect(user_token).to be_present
|
|
76
|
+
|
|
77
|
+
post(
|
|
78
|
+
"#{api_prefix}/draft_proposals/sync",
|
|
79
|
+
params: { data: { component_id: proposal_component.id } }.to_json,
|
|
80
|
+
headers: json_headers.merge(bearer(user_token))
|
|
81
|
+
)
|
|
82
|
+
expect(response).to have_http_status(:ok)
|
|
83
|
+
draft_id = response.parsed_body.dig("data", "id")
|
|
84
|
+
expect(draft_id).to be_present
|
|
85
|
+
expect(response.parsed_body.dig("data", "meta", "publishable")).to be(false)
|
|
86
|
+
|
|
87
|
+
put(
|
|
88
|
+
"#{api_prefix}/draft_proposals/#{draft_id}/sync",
|
|
89
|
+
params: {
|
|
90
|
+
data: {
|
|
91
|
+
title: "This is a valid proposal title sample",
|
|
92
|
+
body: "I am quite a valid proposal, with one sentence that is long enough to be valid I think."
|
|
93
|
+
}
|
|
94
|
+
}.to_json,
|
|
95
|
+
headers: json_headers.merge(bearer(user_token))
|
|
96
|
+
)
|
|
97
|
+
expect(response).to have_http_status(:ok)
|
|
98
|
+
expect(response.parsed_body.dig("data", "meta", "publishable")).to be(true)
|
|
99
|
+
|
|
100
|
+
count_before = component_resources_count(credential_token.token)
|
|
101
|
+
|
|
102
|
+
post(
|
|
103
|
+
"#{api_prefix}/draft_proposals/#{draft_id}/publish",
|
|
104
|
+
headers: bearer(user_token)
|
|
105
|
+
)
|
|
106
|
+
expect(response).to have_http_status(:accepted)
|
|
107
|
+
job_id = response.parsed_body["job_id"]
|
|
108
|
+
expect(job_id).to be_present
|
|
109
|
+
|
|
110
|
+
perform_enqueued_jobs
|
|
111
|
+
|
|
112
|
+
get("#{api_prefix}/jobs/#{job_id}")
|
|
113
|
+
expect(response).to have_http_status(:ok)
|
|
114
|
+
expect(response.parsed_body["status"]).to eq("completed")
|
|
115
|
+
|
|
116
|
+
expect(component_resources_count(credential_token.token)).to eq(count_before + 1)
|
|
117
|
+
|
|
118
|
+
proposals_token = create(
|
|
119
|
+
:oauth_access_token,
|
|
120
|
+
scopes: "proposals",
|
|
121
|
+
resource_owner_id: nil,
|
|
122
|
+
application: api_client
|
|
123
|
+
)
|
|
124
|
+
get(
|
|
125
|
+
"#{api_prefix}/proposals",
|
|
126
|
+
params: {
|
|
127
|
+
order: "published_at",
|
|
128
|
+
order_direction: "desc",
|
|
129
|
+
per_page: 1,
|
|
130
|
+
component_id: proposal_component.id,
|
|
131
|
+
space_id: participatory_process.id,
|
|
132
|
+
space_manifest: "participatory_processes"
|
|
133
|
+
},
|
|
134
|
+
headers: bearer(proposals_token.token)
|
|
135
|
+
)
|
|
136
|
+
expect(response).to have_http_status(:ok)
|
|
137
|
+
listed = response.parsed_body["data"]
|
|
138
|
+
expect(listed.size).to eq(1)
|
|
139
|
+
expect(listed.first["id"].to_s).to eq(draft_id.to_s)
|
|
140
|
+
expect(listed.first.dig("meta", "published")).to be_truthy
|
|
141
|
+
end
|
|
142
|
+
# rubocop:enable RSpec/ExampleLength
|
|
143
|
+
end
|
|
144
|
+
# rubocop:enable RSpec/DescribeClass
|