decidim-restfull-forms 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 (45) hide show
  1. checksums.yaml +7 -0
  2. data/app/concerns/decidim/api/rest_full/forms/controller_helpers.rb +66 -0
  3. data/app/controllers/decidim/api/rest_full/forms/answer_options_controller.rb +76 -0
  4. data/app/controllers/decidim/api/rest_full/forms/answers_controller.rb +85 -0
  5. data/app/controllers/decidim/api/rest_full/forms/questionnaire_responses_controller.rb +51 -0
  6. data/app/controllers/decidim/api/rest_full/forms/questionnaires_controller.rb +64 -0
  7. data/app/controllers/decidim/api/rest_full/forms/questions_controller.rb +90 -0
  8. data/app/controllers/decidim/api/rest_full/forms/submission_requests_controller.rb +39 -0
  9. data/app/serializers/decidim/api/rest_full/forms/answer_option_serializer.rb +17 -0
  10. data/app/serializers/decidim/api/rest_full/forms/answer_serializer.rb +21 -0
  11. data/app/serializers/decidim/api/rest_full/forms/application_serializer.rb +22 -0
  12. data/app/serializers/decidim/api/rest_full/forms/question_serializer.rb +21 -0
  13. data/app/serializers/decidim/api/rest_full/forms/questionnaire_response_serializer.rb +37 -0
  14. data/app/serializers/decidim/api/rest_full/forms/questionnaire_serializer.rb +75 -0
  15. data/app/serializers/decidim/api/rest_full/forms/submission_request_serializer.rb +37 -0
  16. data/app/services/decidim/rest_full/forms/answers_operations.rb +180 -0
  17. data/app/services/decidim/rest_full/forms/authoring_operations.rb +153 -0
  18. data/app/services/decidim/rest_full/forms/error_payload_builder.rb +40 -0
  19. data/app/services/decidim/rest_full/forms/locale_resolver.rb +86 -0
  20. data/app/services/decidim/rest_full/forms/questionnaire_json_forms_builder.rb +136 -0
  21. data/app/services/decidim/rest_full/forms/questionnaire_scope.rb +140 -0
  22. data/app/services/decidim/rest_full/forms/response_builder.rb +69 -0
  23. data/app/services/decidim/rest_full/forms/submission_bundle.rb +75 -0
  24. data/config/locales/decidim_rest_full_forms.en.yml +18 -0
  25. data/lib/decidim/rest_full/forms/decidim_032_compat.rb +62 -0
  26. data/lib/decidim/rest_full/forms/engine.rb +97 -0
  27. data/lib/decidim/rest_full/forms/test/definitions/forms_errors.rb +32 -0
  28. data/lib/decidim/rest_full/forms/test/definitions/locale_meta.rb +50 -0
  29. data/lib/decidim/rest_full/forms/test/definitions/question.rb +83 -0
  30. data/lib/decidim/rest_full/forms/test/definitions/question_create_request.rb +94 -0
  31. data/lib/decidim/rest_full/forms/test/definitions/questionnaire.rb +47 -0
  32. data/lib/decidim/rest_full/forms/test/definitions/questionnaire_response.rb +41 -0
  33. data/lib/decidim/rest_full/forms/test/definitions/request_bodies.rb +88 -0
  34. data/lib/decidim/rest_full/forms/test/definitions/submission_request.rb +44 -0
  35. data/lib/decidim/rest_full/forms/test/definitions/tags/forms_tag.rb +30 -0
  36. data/lib/decidim/rest_full/forms/test_definitions.rb +11 -0
  37. data/lib/decidim-restfull-forms.rb +14 -0
  38. data/spec/requests/decidim/api/rest_full/forms/answers_controller_create_spec.rb +68 -0
  39. data/spec/requests/decidim/api/rest_full/forms/answers_controller_sync_spec.rb +67 -0
  40. data/spec/requests/decidim/api/rest_full/forms/questionnaire_responses_controller_show_spec.rb +55 -0
  41. data/spec/requests/decidim/api/rest_full/forms/questionnaires_controller_index_spec.rb +43 -0
  42. data/spec/requests/decidim/api/rest_full/forms/questionnaires_controller_show_spec.rb +46 -0
  43. data/spec/requests/decidim/api/rest_full/forms/questionnaires_controller_update_spec.rb +107 -0
  44. data/spec/requests/decidim/api/rest_full/forms/questions_controller_spec.rb +166 -0
  45. metadata +141 -0
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Forms
6
+ module Definitions
7
+ module QuestionCreateRequest
8
+ BASE_ATTRS = {
9
+ position: { type: :integer, description: "Order within the questionnaire" },
10
+ mandatory: { type: :boolean },
11
+ body: {
12
+ type: :object,
13
+ additionalProperties: { type: :string },
14
+ description: "Translated question label"
15
+ },
16
+ description: {
17
+ type: :object,
18
+ additionalProperties: { type: :string },
19
+ nullable: true
20
+ }
21
+ }.freeze
22
+
23
+ QUESTION_CREATE_BODY = {
24
+ type: :object,
25
+ required: [:data],
26
+ properties: {
27
+ data: {
28
+ type: :object,
29
+ required: %w(type attributes relationships),
30
+ properties: {
31
+ type: { type: :string, enum: ["questions"] },
32
+ attributes: {
33
+ oneOf: [
34
+ {
35
+ type: :object,
36
+ required: %w(position mandatory question_type body),
37
+ properties: BASE_ATTRS.merge(
38
+ question_type: { type: :string, enum: %w(single_option multiple_option sorting matrix_single matrix_multiple) },
39
+ max_choices: { type: :integer, nullable: true }
40
+ ),
41
+ additionalProperties: false
42
+ },
43
+ {
44
+ type: :object,
45
+ required: %w(position mandatory question_type body),
46
+ properties: BASE_ATTRS.merge(
47
+ question_type: { type: :string, enum: ["long_answer"] },
48
+ max_characters: { type: :integer, nullable: true }
49
+ ),
50
+ additionalProperties: false
51
+ },
52
+ {
53
+ type: :object,
54
+ required: %w(position mandatory question_type body),
55
+ properties: BASE_ATTRS.merge(
56
+ question_type: { type: :string, enum: %w(short_answer files) }
57
+ ),
58
+ additionalProperties: false
59
+ }
60
+ ]
61
+ },
62
+ relationships: {
63
+ type: :object,
64
+ required: ["questionnaire"],
65
+ properties: {
66
+ questionnaire: {
67
+ type: :object,
68
+ required: ["data"],
69
+ properties: {
70
+ data: {
71
+ type: :object,
72
+ required: %w(type id),
73
+ properties: {
74
+ type: { type: :string, enum: ["questionnaires"] },
75
+ id: { type: :string }
76
+ }
77
+ }
78
+ }
79
+ }
80
+ }
81
+ }
82
+ }
83
+ }
84
+ }
85
+ }.freeze
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ Decidim::RestFull::Core::DefinitionRegistry.register_object(:question_create_body) do
93
+ Decidim::RestFull::Forms::Definitions::QuestionCreateRequest::QUESTION_CREATE_BODY
94
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ Decidim::RestFull::Core::DefinitionRegistry.register_resource(:questionnaire) do
4
+ {
5
+ type: :object,
6
+ title: "Questionnaire",
7
+ description: "Decidim::Forms::Questionnaire with JSON Schema and JSON Forms UI for answer submission.",
8
+ properties: {
9
+ id: { type: :string },
10
+ type: { type: :string, enum: ["questionnaires"] },
11
+ attributes: {
12
+ type: :object,
13
+ properties: {
14
+ title: { type: :string, nullable: true, description: "Localized title for the active locale" },
15
+ description: { type: :string, nullable: true, description: "Localized description for the active locale" },
16
+ schema: { type: :object, description: "JSON Schema (structural only)" },
17
+ ui: { type: :object, description: "JSON Forms UI schema" },
18
+ updated_at: { type: :string, format: :"date-time", nullable: true }
19
+ },
20
+ additionalProperties: true
21
+ },
22
+ meta: {
23
+ type: :object,
24
+ properties: {
25
+ locale: { type: :string },
26
+ requested_locale: { type: :string },
27
+ fallback_from: { type: :string, nullable: true },
28
+ submission: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:forms_submission_policy_meta) }
29
+ },
30
+ additionalProperties: true
31
+ },
32
+ links: {
33
+ type: :object,
34
+ properties: {
35
+ self: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:rest_full_hypermedia_link) },
36
+ submit: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:rest_full_hypermedia_link) },
37
+ submit_sync: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:rest_full_hypermedia_link) },
38
+ questions: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:rest_full_hypermedia_link) }
39
+ },
40
+ additionalProperties: {
41
+ "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:rest_full_hypermedia_link)
42
+ }
43
+ }
44
+ },
45
+ required: [:id, :type]
46
+ }
47
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ Decidim::RestFull::Core::DefinitionRegistry.register_resource(:questionnaire_response) do
4
+ {
5
+ type: :object,
6
+ title: "Questionnaire response (submission bundle)",
7
+ properties: {
8
+ id: { type: :string },
9
+ type: { type: :string, enum: ["questionnaire_response"] },
10
+ attributes: {
11
+ type: :object,
12
+ properties: {
13
+ answers: {
14
+ type: :object,
15
+ additionalProperties: true,
16
+ description: "Map of question_id to answer value"
17
+ },
18
+ client_ip: { type: :string, nullable: true, description: "Opaque ip_hash digest" }
19
+ },
20
+ required: [:answers]
21
+ },
22
+ meta: { type: :object, additionalProperties: true },
23
+ links: {
24
+ type: :object,
25
+ properties: {
26
+ self: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:rest_full_hypermedia_link) }
27
+ },
28
+ additionalProperties: {
29
+ "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:rest_full_hypermedia_link)
30
+ }
31
+ },
32
+ relationships: {
33
+ type: :object,
34
+ properties: {
35
+ questionnaire: Decidim::RestFull::Core::DefinitionRegistry.belongs_to("questionnaires", title: "Questionnaire")
36
+ }
37
+ }
38
+ },
39
+ required: [:id, :type, :attributes]
40
+ }
41
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ Decidim::RestFull::Core::DefinitionRegistry.register_object(:questionnaire_update_body) do
4
+ {
5
+ type: :object,
6
+ title: "Questionnaire metadata update payload",
7
+ required: [:data],
8
+ properties: {
9
+ data: {
10
+ type: :object,
11
+ required: [:type, :id, :attributes],
12
+ properties: {
13
+ type: { type: :string, enum: ["questionnaires"] },
14
+ id: { type: :string },
15
+ attributes: {
16
+ type: :object,
17
+ properties: {
18
+ title: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop) },
19
+ description: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop) },
20
+ tos: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop) }
21
+ },
22
+ additionalProperties: false
23
+ }
24
+ },
25
+ additionalProperties: false
26
+ }
27
+ },
28
+ additionalProperties: false
29
+ }.freeze
30
+ end
31
+
32
+ Decidim::RestFull::Core::DefinitionRegistry.register_object(:questionnaire_answers_create_body) do
33
+ {
34
+ type: :object,
35
+ title: "Questionnaire answers submission payload",
36
+ properties: {
37
+ meta: {
38
+ type: :object,
39
+ properties: {
40
+ locale: { type: :string },
41
+ anonymous: { type: :boolean },
42
+ client_ip: { type: :string }
43
+ },
44
+ additionalProperties: true
45
+ },
46
+ data: {
47
+ type: :object,
48
+ required: [:type, :attributes, :relationships],
49
+ properties: {
50
+ type: { type: :string, enum: ["questionnaire_response"] },
51
+ attributes: {
52
+ type: :object,
53
+ properties: {
54
+ answers: {
55
+ type: :object,
56
+ additionalProperties: true,
57
+ description: "Map of question id (string) to answer value"
58
+ }
59
+ },
60
+ required: [:answers]
61
+ },
62
+ relationships: {
63
+ type: :object,
64
+ properties: {
65
+ questionnaire: {
66
+ type: :object,
67
+ properties: {
68
+ data: {
69
+ type: :object,
70
+ properties: {
71
+ type: { type: :string, enum: ["questionnaires"] },
72
+ id: { type: :string }
73
+ },
74
+ required: [:type, :id]
75
+ }
76
+ },
77
+ required: [:data]
78
+ }
79
+ },
80
+ required: [:questionnaire]
81
+ }
82
+ }
83
+ }
84
+ },
85
+ required: [:data],
86
+ additionalProperties: false
87
+ }.freeze
88
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ Decidim::RestFull::Core::DefinitionRegistry.register_resource(:submission_request) do
4
+ {
5
+ type: :object,
6
+ title: "Submission request (async answer submit)",
7
+ properties: {
8
+ id: { type: :string, format: :uuid },
9
+ type: { type: :string, enum: ["submission_request"] },
10
+ attributes: {
11
+ type: :object,
12
+ properties: {
13
+ status: { type: :string, enum: %w(pending processing completed failed) }
14
+ },
15
+ required: [:status]
16
+ },
17
+ meta: {
18
+ type: :object,
19
+ properties: {
20
+ status: { type: :string }
21
+ }
22
+ },
23
+ links: {
24
+ type: :object,
25
+ properties: {
26
+ self: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:rest_full_hypermedia_link) },
27
+ result: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:rest_full_hypermedia_link) }
28
+ }
29
+ }
30
+ },
31
+ required: [:id, :type, :attributes, :links]
32
+ }
33
+ end
34
+
35
+ Decidim::RestFull::Core::DefinitionRegistry.register_object(:submission_request_accepted_response) do
36
+ {
37
+ type: :object,
38
+ title: "Submission request accepted (202)",
39
+ properties: {
40
+ data: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:submission_request) }
41
+ },
42
+ required: [:data]
43
+ }
44
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Forms
6
+ module Definitions
7
+ module Tags
8
+ FORMS = {
9
+ name: "Forms",
10
+ description: <<~TXT.strip
11
+ Survey and questionnaire authoring backed by Decidim::Forms.
12
+
13
+ **Questionnaire** (`Decidim::Forms::Questionnaire`) is the container: title, description, and terms of service. It belongs to a survey resource (e.g. `Decidim::Surveys::Survey`) via `questionnaire_for`.
14
+
15
+ **Questions** (`Decidim::Forms::Question`) define rows in the questionnaire (`question_type`, `body`, `mandatory`, …). **Answer options** (`Decidim::Forms::AnswerOption`) belong to choice-style questions.
16
+
17
+ **Participant flow**: answers are submitted as a bundle (`POST /answers` → `submission_requests/:id` for validation state). **Questionnaire responses** (`GET /questionnaire_responses/:id`) expose a read model of stored answers; admins can delete a response bundle.
18
+
19
+ **Authoring** (credential token, `surveys.questions.manage`): mutate questionnaires, questions, and answer options. Default writes are **async** (`HTTP 202` + `GET /jobs/:uuid`); use `…/sync` routes for inline `200`/`201`.
20
+
21
+ **Listing**: `GET /questionnaires` returns a JSON Forms projection for rendering; `GET /questions` and `GET /answers` require `filter[questionnaire_id]`.
22
+ TXT
23
+ }.freeze
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ Decidim::RestFull::Test::OpenApiTagRegistry.register_tag(Decidim::RestFull::Forms::Definitions::Tags::FORMS)
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "test/definitions/locale_meta"
4
+ require_relative "test/definitions/forms_errors"
5
+ require_relative "test/definitions/questionnaire"
6
+ require_relative "test/definitions/questionnaire_response"
7
+ require_relative "test/definitions/submission_request"
8
+ require_relative "test/definitions/question"
9
+ require_relative "test/definitions/question_create_request"
10
+ require_relative "test/definitions/request_bodies"
11
+ require_relative "test/definitions/tags/forms_tag"
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "decidim-restfull-core"
4
+
5
+ module Decidim
6
+ module RestFull
7
+ module Forms
8
+ ENGINE_ROOT = File.expand_path("..", __dir__).freeze
9
+ end
10
+ end
11
+ end
12
+
13
+ require "decidim/rest_full/forms/decidim_032_compat"
14
+ require "decidim/rest_full/forms/engine"
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+ require "decidim/surveys/test/factories"
5
+
6
+ RSpec.describe Decidim::Api::RestFull::Forms::AnswersController do
7
+ path "/answers" do
8
+ post "Submit answers (async)" do
9
+ tags "Forms"
10
+ consumes "application/json"
11
+ produces "application/json"
12
+ security [{ credentialFlowBearer: ["surveys"] }]
13
+ operationId "createAnswersAsync"
14
+ description "Enqueue answer submission; poll GET /submission_requests/:id (202 Accepted)."
15
+ parameter name: :body, in: :body, required: true,
16
+ schema: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:questionnaire_answers_create_body) }
17
+
18
+ describe_api_endpoint(
19
+ controller: Decidim::Api::RestFull::Forms::AnswersController,
20
+ action: :create,
21
+ security_types: [:credentialFlow],
22
+ scopes: ["surveys"],
23
+ permissions: %w(surveys.answers.submit surveys.read)
24
+ ) do
25
+ let!(:organization) { create(:organization, available_locales: %w(en)) }
26
+ let!(:participatory_process) { create(:participatory_process, organization:) }
27
+ let!(:surveys_component) do
28
+ create(:surveys_component, participatory_space: participatory_process, published_at: Time.zone.now)
29
+ end
30
+ let!(:survey) { create(:survey, component: surveys_component) }
31
+ let(:questionnaire) { survey.questionnaire }
32
+ let(:question) { questionnaire.questions.not_separator.first }
33
+
34
+ before { host! organization.host }
35
+
36
+ let(:body) do
37
+ {
38
+ meta: { locale: "en", anonymous: true, client_ip: "203.0.113.42" },
39
+ data: {
40
+ type: "questionnaire_response",
41
+ attributes: {
42
+ answers: {
43
+ question.id.to_s => "Async answer"
44
+ }
45
+ },
46
+ relationships: {
47
+ questionnaire: {
48
+ data: { type: "questionnaires", id: questionnaire.id.to_s }
49
+ }
50
+ }
51
+ }
52
+ }
53
+ end
54
+
55
+ response "202", "Submission request accepted" do
56
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:submission_request_accepted_response)
57
+
58
+ run_test!(example_name: :accepted) do |response|
59
+ expect(response).to have_http_status(:accepted)
60
+ parsed = JSON.parse(response.body)
61
+ expect(parsed.dig("data", "type")).to eq("submission_request")
62
+ expect(parsed.dig("data", "links", "self", "href")).to include("/submission_requests/")
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+ require "decidim/surveys/test/factories"
5
+
6
+ RSpec.describe Decidim::Api::RestFull::Forms::AnswersController do
7
+ path "/answers/sync" do
8
+ post "Submit answers synchronously" do
9
+ tags "Forms"
10
+ consumes "application/json"
11
+ produces "application/json"
12
+ security [{ credentialFlowBearer: ["surveys"] }]
13
+ operationId "createAnswers"
14
+ parameter name: :body, in: :body, required: true,
15
+ schema: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:questionnaire_answers_create_body) }
16
+
17
+ describe_api_endpoint(
18
+ controller: Decidim::Api::RestFull::Forms::AnswersController,
19
+ action: :create_sync,
20
+ security_types: [:credentialFlow],
21
+ scopes: ["surveys"],
22
+ permissions: %w(surveys.answers.submit surveys.read)
23
+ ) do
24
+ let!(:organization) { create(:organization, available_locales: %w(en)) }
25
+ let!(:participatory_process) { create(:participatory_process, organization:) }
26
+ let!(:surveys_component) do
27
+ create(:surveys_component, participatory_space: participatory_process, published_at: Time.zone.now)
28
+ end
29
+ let!(:survey) { create(:survey, component: surveys_component) }
30
+ let(:questionnaire) { survey.questionnaire }
31
+ let(:question) { questionnaire.questions.not_separator.first }
32
+
33
+ before { host! organization.host }
34
+
35
+ let(:body) do
36
+ {
37
+ meta: { locale: "en", anonymous: true, client_ip: "203.0.113.42" },
38
+ data: {
39
+ type: "questionnaire_response",
40
+ attributes: {
41
+ answers: {
42
+ question.id.to_s => "Test answer"
43
+ }
44
+ },
45
+ relationships: {
46
+ questionnaire: {
47
+ data: { type: "questionnaires", id: questionnaire.id.to_s }
48
+ }
49
+ }
50
+ }
51
+ }
52
+ end
53
+
54
+ response "201", "Created questionnaire response" do
55
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:questionnaire_response_item_response)
56
+
57
+ run_test!(example_name: :created) do |response|
58
+ expect(response).to have_http_status(:created)
59
+ parsed = JSON.parse(response.body)
60
+ expect(parsed.dig("data", "type")).to eq("questionnaire_response")
61
+ expect(parsed.dig("data", "attributes", "answers")).to be_present
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+ require "decidim/surveys/test/factories"
5
+
6
+ RSpec.describe Decidim::Api::RestFull::Forms::QuestionnaireResponsesController do
7
+ path "/questionnaire_responses/{id}" do
8
+ get "Show questionnaire response" do
9
+ tags "Forms"
10
+ produces "application/json"
11
+ security [{ credentialFlowBearer: ["surveys"] }]
12
+ operationId "getQuestionnaireResponse"
13
+ description "Read a submission bundle (aggregate of Decidim::Forms::Answer rows)."
14
+ parameter name: "id", in: :path, schema: { type: :integer }, required: true
15
+
16
+ describe_api_endpoint(
17
+ controller: Decidim::Api::RestFull::Forms::QuestionnaireResponsesController,
18
+ action: :show,
19
+ security_types: [:credentialFlow],
20
+ scopes: ["surveys"],
21
+ permissions: %w(surveys.answers.read surveys.read)
22
+ ) do
23
+ let!(:organization) { create(:organization, available_locales: %w(en)) }
24
+ let!(:participatory_process) { create(:participatory_process, organization:) }
25
+ let!(:surveys_component) do
26
+ create(:surveys_component, participatory_space: participatory_process, published_at: Time.zone.now)
27
+ end
28
+ let!(:survey) { create(:survey, component: surveys_component) }
29
+ let(:questionnaire) { survey.questionnaire }
30
+ let(:question) { questionnaire.questions.not_separator.first }
31
+ let!(:answer) do
32
+ Decidim::Forms::Answer.create!(
33
+ questionnaire:,
34
+ question:,
35
+ body: "Stored answer",
36
+ user: nil
37
+ )
38
+ end
39
+ let(:id) { answer.id }
40
+
41
+ before { host! organization.host }
42
+
43
+ response "200", "Questionnaire response" do
44
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:questionnaire_response_item_response)
45
+
46
+ run_test!(example_name: :ok) do |response|
47
+ parsed = JSON.parse(response.body)
48
+ expect(parsed.dig("data", "type")).to eq("questionnaire_response")
49
+ expect(parsed.dig("data", "attributes", "answers")).to be_present
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+ require "decidim/surveys/test/factories"
5
+
6
+ RSpec.describe Decidim::Api::RestFull::Forms::QuestionnairesController do
7
+ path "/questionnaires" do
8
+ get "Questionnaires index" do
9
+ tags "Forms"
10
+ produces "application/json"
11
+ security [{ credentialFlowBearer: ["surveys"] }]
12
+ operationId "listQuestionnaires"
13
+ it_behaves_like "paginated params"
14
+
15
+ describe_api_endpoint(
16
+ controller: Decidim::Api::RestFull::Forms::QuestionnairesController,
17
+ action: :index,
18
+ security_types: [:credentialFlow],
19
+ scopes: ["surveys"],
20
+ permissions: %w(surveys.questionnaires.read surveys.read)
21
+ ) do
22
+ let!(:organization) { create(:organization, available_locales: %w(en)) }
23
+ let!(:participatory_process) { create(:participatory_process, organization:) }
24
+ let!(:surveys_component) do
25
+ create(:surveys_component, participatory_space: participatory_process, published_at: Time.zone.now)
26
+ end
27
+ let!(:survey) { create(:survey, component: surveys_component) }
28
+
29
+ before { host! organization.host }
30
+
31
+ response "200", "Questionnaires list" do
32
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:questionnaire_index_response)
33
+
34
+ run_test!(example_name: :ok) do |response|
35
+ body = JSON.parse(response.body)
36
+ expect(body["data"]).to be_an(Array)
37
+ expect(body["data"].map { |r| r["id"] }).to include(survey.questionnaire.id.to_s)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "swagger_helper"
4
+ require "decidim/surveys/test/factories"
5
+
6
+ RSpec.describe Decidim::Api::RestFull::Forms::QuestionnairesController do
7
+ path "/questionnaires/{id}" do
8
+ get "Questionnaire" do
9
+ tags "Forms"
10
+ produces "application/json"
11
+ security [{ credentialFlowBearer: ["surveys"] }]
12
+ operationId "getQuestionnaire"
13
+ parameter name: "id", in: :path, schema: { type: :integer }, required: true
14
+
15
+ describe_api_endpoint(
16
+ controller: Decidim::Api::RestFull::Forms::QuestionnairesController,
17
+ action: :show,
18
+ security_types: [:credentialFlow],
19
+ scopes: ["surveys"],
20
+ permissions: %w(surveys.questionnaires.read surveys.read)
21
+ ) do
22
+ let!(:organization) { create(:organization, available_locales: %w(en fr)) }
23
+ let!(:participatory_process) { create(:participatory_process, organization:) }
24
+ let!(:surveys_component) do
25
+ create(:surveys_component, participatory_space: participatory_process, published_at: Time.zone.now)
26
+ end
27
+ let!(:survey) { create(:survey, component: surveys_component) }
28
+ let(:id) { survey.questionnaire.id }
29
+
30
+ before { host! organization.host }
31
+
32
+ response "200", "Questionnaire with schema and ui" do
33
+ schema "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:questionnaire_item_response)
34
+
35
+ run_test!(example_name: :ok) do |response|
36
+ body = JSON.parse(response.body)
37
+ expect(body.dig("data", "attributes", "schema")).to be_present
38
+ expect(body.dig("data", "attributes", "ui", "type")).to eq("VerticalLayout")
39
+ expect(body.dig("data", "meta", "locale")).to be_present
40
+ expect(body.dig("data", "links", "submit", "href")).to include("/answers")
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end