From 3613dda955617bc3f8ce02c52b84b0a10f5cddef Mon Sep 17 00:00:00 2001 From: Nidhi Sarvaiya Date: Mon, 8 Jul 2024 18:57:35 -0400 Subject: [PATCH] 838 Rename Form to CustomForm::Form and Question to CustomForm::Question (#864) * Added module custom_form for Form and Question Controller and Views * Fix test cases for custom_form module for Form and Question * Nested modules * Modularize Form model and its related test cases * Modularize Question Module with CustomForm * Lint Fix * Fix seed file * Modulaize Question policy code and fix link in question form --- .../staff/custom_form/forms_controller.rb | 73 +++++ .../staff/custom_form/questions_controller.rb | 80 ++++++ .../organizations/staff/forms_controller.rb | 67 ----- .../staff/questions_controller.rb | 74 ------ app/models/{ => custom_form}/form.rb | 12 +- app/models/{ => custom_form}/question.rb | 16 +- app/models/custom_form/submitted_answer.rb | 2 +- app/models/form_profile.rb | 2 +- app/models/organization.rb | 2 +- .../organizations/custom_form/form_policy.rb | 14 + .../custom_form/question_policy.rb | 16 ++ app/policies/organizations/form_policy.rb | 10 - app/policies/organizations/question_policy.rb | 12 - app/views/layouts/dashboard/_sidebar.html.erb | 2 +- .../{ => custom_form}/forms/_form.html.erb | 2 +- .../{ => custom_form}/forms/edit.html.erb | 0 .../{ => custom_form}/forms/index.html.erb | 8 +- .../{ => custom_form}/forms/new.html.erb | 0 .../{ => custom_form}/forms/show.html.erb | 8 +- .../questions/_form.html.erb | 7 +- .../{ => custom_form}/questions/edit.html.erb | 0 .../{ => custom_form}/questions/new.html.erb | 0 config/breadcrumbs.rb | 4 +- config/locales/en.yml | 101 +++---- config/routes.rb | 6 +- db/seeds/01_alta.rb | 8 +- .../custom_form/forms_controller_test.rb | 250 ++++++++++++++++++ .../custom_form/questions_controller_test.rb | 226 ++++++++++++++++ .../staff/forms_controller_test.rb | 244 ----------------- .../staff/questions_controller_test.rb | 220 --------------- test/factories/{ => custom_form}/forms.rb | 2 +- test/factories/{ => custom_form}/questions.rb | 2 +- test/models/custom_form/form_test.rb | 15 ++ test/models/custom_form/question_test.rb | 34 +++ test/models/form_test.rb | 13 - test/models/question_test.rb | 32 --- .../custom_form/form_policy_test.rb | 171 ++++++++++++ .../custom_form/question_policy_test.rb | 171 ++++++++++++ .../organizations/form_policy_test.rb | 167 ------------ .../organizations/question_policy_test.rb | 167 ------------ 40 files changed, 1147 insertions(+), 1093 deletions(-) create mode 100644 app/controllers/organizations/staff/custom_form/forms_controller.rb create mode 100644 app/controllers/organizations/staff/custom_form/questions_controller.rb delete mode 100644 app/controllers/organizations/staff/forms_controller.rb delete mode 100644 app/controllers/organizations/staff/questions_controller.rb rename app/models/{ => custom_form}/form.rb (75%) rename app/models/{ => custom_form}/question.rb (74%) create mode 100644 app/policies/organizations/custom_form/form_policy.rb create mode 100644 app/policies/organizations/custom_form/question_policy.rb delete mode 100644 app/policies/organizations/form_policy.rb delete mode 100644 app/policies/organizations/question_policy.rb rename app/views/organizations/staff/{ => custom_form}/forms/_form.html.erb (88%) rename app/views/organizations/staff/{ => custom_form}/forms/edit.html.erb (100%) rename app/views/organizations/staff/{ => custom_form}/forms/index.html.erb (72%) rename app/views/organizations/staff/{ => custom_form}/forms/new.html.erb (100%) rename app/views/organizations/staff/{ => custom_form}/forms/show.html.erb (71%) rename app/views/organizations/staff/{ => custom_form}/questions/_form.html.erb (74%) rename app/views/organizations/staff/{ => custom_form}/questions/edit.html.erb (100%) rename app/views/organizations/staff/{ => custom_form}/questions/new.html.erb (100%) create mode 100644 test/controllers/organizations/staff/custom_form/forms_controller_test.rb create mode 100644 test/controllers/organizations/staff/custom_form/questions_controller_test.rb delete mode 100644 test/controllers/organizations/staff/forms_controller_test.rb delete mode 100644 test/controllers/organizations/staff/questions_controller_test.rb rename test/factories/{ => custom_form}/forms.rb (78%) rename test/factories/{ => custom_form}/questions.rb (83%) create mode 100644 test/models/custom_form/form_test.rb create mode 100644 test/models/custom_form/question_test.rb delete mode 100644 test/models/form_test.rb delete mode 100644 test/models/question_test.rb create mode 100644 test/policies/organizations/custom_form/form_policy_test.rb create mode 100644 test/policies/organizations/custom_form/question_policy_test.rb delete mode 100644 test/policies/organizations/form_policy_test.rb delete mode 100644 test/policies/organizations/question_policy_test.rb diff --git a/app/controllers/organizations/staff/custom_form/forms_controller.rb b/app/controllers/organizations/staff/custom_form/forms_controller.rb new file mode 100644 index 000000000..7271066fd --- /dev/null +++ b/app/controllers/organizations/staff/custom_form/forms_controller.rb @@ -0,0 +1,73 @@ +module Organizations + module Staff + module CustomForm + class FormsController < ApplicationController + before_action :context_authorize! + before_action :set_form, only: %i[show edit update destroy] + + layout "dashboard" + + def index + @forms = authorized_scope(::CustomForm::Form.all) + end + + def new + @form = ::CustomForm::Form.new + end + + def create + @form = organization.forms.new(form_params) + + if @form.save + redirect_to staff_custom_form_forms_path, notice: t(".success") + else + flash.now[:alert] = t(".error") + + render :new, status: :unprocessable_entity + end + end + + def show + end + + def edit + end + + def update + if @form.update(form_params) + redirect_to staff_custom_form_forms_path, notice: t(".success") + else + render :edit, status: :unprocessable_entity + end + end + + def destroy + @form.destroy + + redirect_to staff_custom_form_forms_path, notice: t(".success") + end + + private + + def organization + current_user.organization + end + + def set_form + @form = organization.forms.find(params[:id]) + authorize! @form + rescue ActiveRecord::RecordNotFound + redirect_to staff_custom_form_forms_path, alert: t(".not_found") + end + + def form_params + params.require(:custom_form_form).permit(:name, :description, :title, :instructions) + end + + def context_authorize! + authorize! ::CustomForm::Form, context: {organization: Current.organization} + end + end + end + end +end diff --git a/app/controllers/organizations/staff/custom_form/questions_controller.rb b/app/controllers/organizations/staff/custom_form/questions_controller.rb new file mode 100644 index 000000000..501310646 --- /dev/null +++ b/app/controllers/organizations/staff/custom_form/questions_controller.rb @@ -0,0 +1,80 @@ +module Organizations + module Staff + module CustomForm + class QuestionsController < ApplicationController + before_action :context_authorize! + before_action :set_form + before_action :set_question, only: %i[show edit update destroy] + + layout "dashboard" + + def index + @questions = authorized_scope(@form.questions) + end + + def new + @question = @form.questions.new + end + + def create + @question = @form.questions.new(question_params) + + if @question.save + redirect_to staff_custom_form_form_path(@form), notice: t(".success") + else + flash.now[:alert] = t(".error") + + render :new, status: :unprocessable_entity + end + end + + def edit + end + + def update + if @question.update(question_params) + redirect_to staff_custom_form_form_path(@form), notice: t(".success") + else + flash.now[:alert] = t(".error") + + render :edit, status: :unprocessable_entity + end + end + + def destroy + @question.destroy + + redirect_to staff_custom_form_form_path(@form), notice: t(".success") + end + + private + + def organization + current_user.organization + end + + def set_form + @form = organization.forms.find(params[:form_id]) + authorize! @form + rescue ActiveRecord::RecordNotFound + redirect_to staff_custom_form_forms_path, alert: t("organizations.staff.custom_form.forms.not_found") + end + + def set_question + @question = @form.questions.find(params[:id]) + authorize! @question + rescue ActiveRecord::RecordNotFound + redirect_to staff_custom_form_form_path(@form), alert: t(".not_found") + end + + def question_params + params.require(:custom_form_question).permit(:name, :description, :label, :help_text, :required, :input_type, :options) + end + + def context_authorize! + authorize! ::CustomForm::Question, context: {organization: Current.organization} + end + end + end + end +end diff --git a/app/controllers/organizations/staff/forms_controller.rb b/app/controllers/organizations/staff/forms_controller.rb deleted file mode 100644 index d061bf667..000000000 --- a/app/controllers/organizations/staff/forms_controller.rb +++ /dev/null @@ -1,67 +0,0 @@ -class Organizations::Staff::FormsController < ApplicationController - before_action :context_authorize! - before_action :set_form, only: %i[show edit update destroy] - - layout "dashboard" - - def index - @forms = authorized_scope(Form.all) - end - - def new - @form = Form.new - end - - def create - @form = organization.forms.new(form_params) - - if @form.save - redirect_to staff_forms_path, notice: t(".success") - else - flash.now[:alert] = t(".error") - - render :new, status: :unprocessable_entity - end - end - - def show - end - - def edit - end - - def update - if @form.update(form_params) - redirect_to staff_forms_path, notice: t(".success") - else - render :edit, status: :unprocessable_entity - end - end - - def destroy - @form.destroy - - redirect_to staff_forms_path, notice: t(".success") - end - - private - - def organization - current_user.organization - end - - def set_form - @form = organization.forms.find(params[:id]) - authorize! @form - rescue ActiveRecord::RecordNotFound - redirect_to staff_forms_path, alert: t(".not_found") - end - - def form_params - params.require(:form).permit(:name, :description, :title, :instructions) - end - - def context_authorize! - authorize! Form, context: {organization: Current.organization} - end -end diff --git a/app/controllers/organizations/staff/questions_controller.rb b/app/controllers/organizations/staff/questions_controller.rb deleted file mode 100644 index 160f34115..000000000 --- a/app/controllers/organizations/staff/questions_controller.rb +++ /dev/null @@ -1,74 +0,0 @@ -class Organizations::Staff::QuestionsController < ApplicationController - before_action :context_authorize! - before_action :set_form - before_action :set_question, only: %i[show edit update destroy] - - layout "dashboard" - - def index - @questions = authorized_scope(@form.questions) - end - - def new - @question = @form.questions.new - end - - def create - @question = @form.questions.new(question_params) - - if @question.save - redirect_to staff_form_path(@form), notice: t(".success") - else - flash.now[:alert] = t(".error") - - render :new, status: :unprocessable_entity - end - end - - def edit - end - - def update - if @question.update(question_params) - redirect_to staff_form_path(@form), notice: t(".success") - else - flash.now[:alert] = t(".error") - - render :edit, status: :unprocessable_entity - end - end - - def destroy - @question.destroy - - redirect_to staff_form_path(@form), notice: t(".success") - end - - private - - def organization - current_user.organization - end - - def set_form - @form = organization.forms.find(params[:form_id]) - authorize! @form - rescue ActiveRecord::RecordNotFound - redirect_to staff_forms_path, alert: t("organizations.staff.forms.not_found") - end - - def set_question - @question = @form.questions.find(params[:id]) - authorize! @question - rescue ActiveRecord::RecordNotFound - redirect_to staff_form_path(@form), alert: t(".not_found") - end - - def question_params - params.require(:question).permit(:name, :description, :label, :help_text, :required, :input_type, :options) - end - - def context_authorize! - authorize! Question, context: {organization: Current.organization} - end -end diff --git a/app/models/form.rb b/app/models/custom_form/form.rb similarity index 75% rename from app/models/form.rb rename to app/models/custom_form/form.rb index aca203b41..735f89998 100644 --- a/app/models/form.rb +++ b/app/models/custom_form/form.rb @@ -22,11 +22,13 @@ # # fk_rails_... (organization_id => organizations.id) # -class Form < ApplicationRecord - acts_as_tenant(:organization) +module CustomForm + class Form < ApplicationRecord + acts_as_tenant(:organization) - has_many :form_profiles, dependent: :destroy - has_many :questions, -> { ordered }, dependent: :destroy + has_many :form_profiles, dependent: :destroy + has_many :questions, -> { ordered }, dependent: :destroy - validates_presence_of :name, :title + validates_presence_of :name, :title + end end diff --git a/app/models/question.rb b/app/models/custom_form/question.rb similarity index 74% rename from app/models/question.rb rename to app/models/custom_form/question.rb index 78a7cdd91..b9f9d7c69 100644 --- a/app/models/question.rb +++ b/app/models/custom_form/question.rb @@ -26,16 +26,18 @@ # # fk_rails_... (form_id => forms.id) # -class Question < ApplicationRecord - belongs_to :form +module CustomForm + class Question < ApplicationRecord + belongs_to :form, class_name: "CustomForm::Form" - has_one :organization, through: :form + has_one :organization, through: :form - scope :ordered, -> { order(:sort_order) } + scope :ordered, -> { order(:sort_order) } - validates_presence_of :name, :label + validates_presence_of :name, :label - def snapshot - {label:, options:} + def snapshot + {label:, options:} + end end end diff --git a/app/models/custom_form/submitted_answer.rb b/app/models/custom_form/submitted_answer.rb index 527d152be..a653bfa51 100644 --- a/app/models/custom_form/submitted_answer.rb +++ b/app/models/custom_form/submitted_answer.rb @@ -25,7 +25,7 @@ class SubmittedAnswer < ApplicationRecord belongs_to :question belongs_to :user - has_one :form, through: :question + has_one :form, class_name: "CustomForm::Form", through: :question has_one :organization, through: :form end end diff --git a/app/models/form_profile.rb b/app/models/form_profile.rb index 74a06f37b..9b7b998a2 100644 --- a/app/models/form_profile.rb +++ b/app/models/form_profile.rb @@ -17,7 +17,7 @@ # fk_rails_... (form_id => forms.id) # class FormProfile < ApplicationRecord - belongs_to :form + belongs_to :form, class_name: "CustomForm::Form" has_one :organization, through: :form end diff --git a/app/models/organization.rb b/app/models/organization.rb index b17148b5d..7b429873d 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -20,7 +20,7 @@ class Organization < ApplicationRecord has_many :users, through: :staff_accounts has_many :pets has_many :default_pet_tasks - has_many :forms, dependent: :destroy + has_many :forms, class_name: "CustomForm::Form", dependent: :destroy has_many :faqs has_one :profile, dependent: :destroy, class_name: "OrganizationProfile", required: true diff --git a/app/policies/organizations/custom_form/form_policy.rb b/app/policies/organizations/custom_form/form_policy.rb new file mode 100644 index 000000000..335536bae --- /dev/null +++ b/app/policies/organizations/custom_form/form_policy.rb @@ -0,0 +1,14 @@ +module Organizations + module CustomForm + class FormPolicy < ApplicationPolicy + pre_check :verify_organization! + pre_check :verify_active_staff! + + alias_rule :new?, :create?, :index?, to: :manage? + + def manage? + permission?(:manage_forms) + end + end + end +end diff --git a/app/policies/organizations/custom_form/question_policy.rb b/app/policies/organizations/custom_form/question_policy.rb new file mode 100644 index 000000000..ad4dbda1b --- /dev/null +++ b/app/policies/organizations/custom_form/question_policy.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Organizations + module CustomForm + class QuestionPolicy < ApplicationPolicy + pre_check :verify_organization! + pre_check :verify_active_staff! + + alias_rule :new?, :create?, :index?, to: :manage? + + def manage? + permission?(:manage_questions) + end + end + end +end diff --git a/app/policies/organizations/form_policy.rb b/app/policies/organizations/form_policy.rb deleted file mode 100644 index 9cc527c4a..000000000 --- a/app/policies/organizations/form_policy.rb +++ /dev/null @@ -1,10 +0,0 @@ -class Organizations::FormPolicy < ApplicationPolicy - pre_check :verify_organization! - pre_check :verify_active_staff! - - alias_rule :new?, :create?, :index?, to: :manage? - - def manage? - permission?(:manage_forms) - end -end diff --git a/app/policies/organizations/question_policy.rb b/app/policies/organizations/question_policy.rb deleted file mode 100644 index e3a5b0378..000000000 --- a/app/policies/organizations/question_policy.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -class Organizations::QuestionPolicy < ApplicationPolicy - pre_check :verify_organization! - pre_check :verify_active_staff! - - alias_rule :new?, :create?, :index?, to: :manage? - - def manage? - permission?(:manage_questions) - end -end diff --git a/app/views/layouts/dashboard/_sidebar.html.erb b/app/views/layouts/dashboard/_sidebar.html.erb index 2b97c8850..e2c6b5a4b 100644 --- a/app/views/layouts/dashboard/_sidebar.html.erb +++ b/app/views/layouts/dashboard/_sidebar.html.erb @@ -61,7 +61,7 @@ <% if current_user.staff_account && current_user.has_role?(:admin, current_user.staff_account.organization) %> diff --git a/app/views/organizations/staff/forms/_form.html.erb b/app/views/organizations/staff/custom_form/forms/_form.html.erb similarity index 88% rename from app/views/organizations/staff/forms/_form.html.erb rename to app/views/organizations/staff/custom_form/forms/_form.html.erb index 35597d6f6..a07edef25 100644 --- a/app/views/organizations/staff/forms/_form.html.erb +++ b/app/views/organizations/staff/custom_form/forms/_form.html.erb @@ -18,7 +18,7 @@ <%= f.submit t('general.save'), class: 'btn btn-primary' %> - <%= link_to t('general.cancel'), staff_forms_path, class: 'btn btn-secondary' %> + <%= link_to t('general.cancel'), staff_custom_form_forms_path, class: 'btn btn-secondary' %> <% end %> diff --git a/app/views/organizations/staff/forms/edit.html.erb b/app/views/organizations/staff/custom_form/forms/edit.html.erb similarity index 100% rename from app/views/organizations/staff/forms/edit.html.erb rename to app/views/organizations/staff/custom_form/forms/edit.html.erb diff --git a/app/views/organizations/staff/forms/index.html.erb b/app/views/organizations/staff/custom_form/forms/index.html.erb similarity index 72% rename from app/views/organizations/staff/forms/index.html.erb rename to app/views/organizations/staff/custom_form/forms/index.html.erb index d1000f449..f76f091e1 100644 --- a/app/views/organizations/staff/forms/index.html.erb +++ b/app/views/organizations/staff/custom_form/forms/index.html.erb @@ -1,7 +1,7 @@ <%= render DashboardPageComponent.new(crumb: :forms_index) do |c| %> <% c.with_header_title { t(".forms") } %> <% c.with_action do %> - <%= link_to t(".create_form"), new_staff_form_path, class: "btn btn-primary" %> + <%= link_to t(".create_form"), new_staff_custom_form_form_path, class: "btn btn-primary" %> <% end %> <% c.with_body do %>
@@ -22,14 +22,14 @@ <% @forms.each do |form| %> - <%= link_to form.name, staff_form_path(form) %> + <%= link_to form.name, staff_custom_form_form_path(form) %> <%= form.description %> <% if current_user.staff_account %>
- <%= link_to 'Edit', edit_staff_form_path(form), class: 'btn btn-warning m-2' %> - <%= link_to 'Delete', staff_form_path(form), class: 'btn btn-danger m-2', data: { turbo_method: "delete", turbo_confirm: t('.confirm_delete') } %> + <%= link_to 'Edit', edit_staff_custom_form_form_path(form), class: 'btn btn-warning m-2' %> + <%= link_to 'Delete', staff_custom_form_form_path(form), class: 'btn btn-danger m-2', data: { turbo_method: "delete", turbo_confirm: t('.confirm_delete') } %>
diff --git a/app/views/organizations/staff/forms/new.html.erb b/app/views/organizations/staff/custom_form/forms/new.html.erb similarity index 100% rename from app/views/organizations/staff/forms/new.html.erb rename to app/views/organizations/staff/custom_form/forms/new.html.erb diff --git a/app/views/organizations/staff/forms/show.html.erb b/app/views/organizations/staff/custom_form/forms/show.html.erb similarity index 71% rename from app/views/organizations/staff/forms/show.html.erb rename to app/views/organizations/staff/custom_form/forms/show.html.erb index 9ed27d6ea..e0f372da5 100644 --- a/app/views/organizations/staff/forms/show.html.erb +++ b/app/views/organizations/staff/custom_form/forms/show.html.erb @@ -3,8 +3,8 @@ <% c.with_action do %>
- <%= link_to t('general.edit'), edit_staff_form_path(@form), class: "btn btn-warning" %> - <%= link_to t('.create_question'), new_staff_form_question_path(@form), class: "btn btn-primary" %> + <%= link_to t('general.edit'), edit_staff_custom_form_form_path(@form), class: "btn btn-warning" %> + <%= link_to t('.create_question'), new_staff_custom_form_form_question_path(@form), class: "btn btn-primary" %>
<% end %> @@ -34,8 +34,8 @@
- <%= link_to 'Edit', edit_staff_form_question_path(@form, question), class: 'btn btn-warning m-2' %> - <%= link_to 'Delete', staff_form_question_path(@form, question), class: 'btn btn-danger m-2', data: { turbo_method: 'delete', turbo_confirm: t('.confirm_delete_question') } %> + <%= link_to 'Edit', edit_staff_custom_form_form_question_path(@form, question), class: 'btn btn-warning m-2' %> + <%= link_to 'Delete', staff_custom_form_form_question_path(@form, question), class: 'btn btn-danger m-2', data: { turbo_method: 'delete', turbo_confirm: t('.confirm_delete_question') } %>
diff --git a/app/views/organizations/staff/questions/_form.html.erb b/app/views/organizations/staff/custom_form/questions/_form.html.erb similarity index 74% rename from app/views/organizations/staff/questions/_form.html.erb rename to app/views/organizations/staff/custom_form/questions/_form.html.erb index b69119c83..a71837756 100644 --- a/app/views/organizations/staff/questions/_form.html.erb +++ b/app/views/organizations/staff/custom_form/questions/_form.html.erb @@ -1,6 +1,9 @@
- <%= bootstrap_form_with model: [:staff, form, question] do |f| %> + <%= bootstrap_form_with( + model: [form, question], + url: question.new_record? ? staff_custom_form_form_questions_path(form) : staff_custom_form_form_question_path(form, question) + ) do |f| %>
<%= f.text_field :name, class: 'form-control' %>
@@ -26,7 +29,7 @@
<%= f.submit t('general.save'), class: 'btn btn-primary' %> - <%= link_to t('general.cancel'), staff_forms_path, class: 'btn btn-secondary' %> + <%= link_to t('general.cancel'), staff_custom_form_forms_path, class: 'btn btn-secondary' %> <% end %>
diff --git a/app/views/organizations/staff/questions/edit.html.erb b/app/views/organizations/staff/custom_form/questions/edit.html.erb similarity index 100% rename from app/views/organizations/staff/questions/edit.html.erb rename to app/views/organizations/staff/custom_form/questions/edit.html.erb diff --git a/app/views/organizations/staff/questions/new.html.erb b/app/views/organizations/staff/custom_form/questions/new.html.erb similarity index 100% rename from app/views/organizations/staff/questions/new.html.erb rename to app/views/organizations/staff/custom_form/questions/new.html.erb diff --git a/config/breadcrumbs.rb b/config/breadcrumbs.rb index 6d2303b1f..04e2fb294 100644 --- a/config/breadcrumbs.rb +++ b/config/breadcrumbs.rb @@ -28,11 +28,11 @@ end crumb :forms_index do - link "Forms", staff_forms_path + link "Forms", staff_custom_form_forms_path end crumb :form do |form| - link form.name, staff_form_path(form) + link form.name, staff_custom_form_form_path(form) parent :forms_index end diff --git a/config/locales/en.yml b/config/locales/en.yml index c7f1f9ce5..94457f87f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -539,57 +539,58 @@ en: paused: "Paused" published: "Published" draft: "Draft" - forms: - set_form: + custom_form: + forms: + set_form: + not_found: "Form not found." not_found: "Form not found." - not_found: "Form not found." - index: - forms: "Forms" - create_form: "Create Form" - confirm_delete: "Are you sure you want to delete this form?" - new: - new_form: "New Form" - create: - success: "Form saved successfully." - error: "Error creating form." - edit: - edit_form: "Edit Form" - show: - confirm_delete_question: "Are you sure you want to delete this question?" - create_question: "Create Question" - update: - success: "Form saved successfully." - destroy: - success: "Form was successfully deleted." - form: - help_text: - title: "What the user sees as the title of the form." - instructions: "Additional instructions to aid the user in filling out the form." - saved: "Form saved successfully." - updated: "Form saved successfully." - destroyed: "Form was successfully deleted." - questions: - set_question: - not_found: "Not Found" - new: - new_question: "New Question" - create: - success: "Question saved successfully." - error: "Error creating question." - update: - success: "Question saved successfully." - error: "Error updating question." - updated: "Question saved successfully." - saved: "Question saved successfully translated." - destroy: - success: "Question was successfully deleted." - not_found: "Question not found." - edit: - edit_question: "Edit Question" - form: - help_text: - label: "Label" - help_text: "help text" + index: + forms: "Forms" + create_form: "Create Form" + confirm_delete: "Are you sure you want to delete this form?" + new: + new_form: "New Form" + create: + success: "Form saved successfully." + error: "Error creating form." + edit: + edit_form: "Edit Form" + show: + confirm_delete_question: "Are you sure you want to delete this question?" + create_question: "Create Question" + update: + success: "Form saved successfully." + destroy: + success: "Form was successfully deleted." + form: + help_text: + title: "What the user sees as the title of the form." + instructions: "Additional instructions to aid the user in filling out the form." + saved: "Form saved successfully." + updated: "Form saved successfully." + destroyed: "Form was successfully deleted." + questions: + set_question: + not_found: "Not Found" + new: + new_question: "New Question" + create: + success: "Question saved successfully." + error: "Error creating question." + update: + success: "Question saved successfully." + error: "Error updating question." + updated: "Question saved successfully." + saved: "Question saved successfully translated." + destroy: + success: "Question was successfully deleted." + not_found: "Question not found." + edit: + edit_question: "Edit Question" + form: + help_text: + label: "Label" + help_text: "help text" staff: deactivate: success: "Staff account deactivated." diff --git a/config/routes.rb b/config/routes.rb index abc090e80..608a4c217 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -46,8 +46,10 @@ resources :staff_invitations, only: %i[new] resources :fosterer_invitations, only: %i[new] - resources :forms do - resources :questions + namespace :custom_form do + resources :forms do + resources :questions + end end post "user_roles/:id/to_staff", to: "user_roles#to_staff", as: "user_to_staff" post "user_roles/:id/to_admin", to: "user_roles#to_admin", as: "user_to_admin" diff --git a/db/seeds/01_alta.rb b/db/seeds/01_alta.rb index e0dd92efa..a5ba08612 100644 --- a/db/seeds/01_alta.rb +++ b/db/seeds/01_alta.rb @@ -269,7 +269,7 @@ ) end - @lifestyle_form = Form.create!( + @lifestyle_form = CustomForm::Form.create!( name: "Lifestyle", description: "Questions regarding the applicant's lifestyle.", title: "Lifestyle", @@ -283,7 +283,7 @@ sort_order: 0 ) - @ideal_pet_question = Question.create!( + @ideal_pet_question = CustomForm::Question.create!( name: "Ideal pet", description: "Brief description of the applicant's ideal pet.", label: "Your ideal pet", @@ -294,7 +294,7 @@ form: @lifestyle_form ) - @lifestyle_question = Question.create!( + @lifestyle_question = CustomForm::Question.create!( name: "General lifestyle", description: "Brief description of the applicant's lifestyle.", label: "Your lifestyle", @@ -305,7 +305,7 @@ form: @lifestyle_form ) - @activities_question = Question.create!( + @activities_question = CustomForm::Question.create!( name: "Activities", description: "Brief description of the applicant's activities.", label: "Your activities", diff --git a/test/controllers/organizations/staff/custom_form/forms_controller_test.rb b/test/controllers/organizations/staff/custom_form/forms_controller_test.rb new file mode 100644 index 000000000..d1a084c43 --- /dev/null +++ b/test/controllers/organizations/staff/custom_form/forms_controller_test.rb @@ -0,0 +1,250 @@ +require "test_helper" +require "action_policy/test_helper" + +module Organizations + module Staff + module CustomForm + class FormsControllerTest < ActionDispatch::IntegrationTest + context "authorization" do + include ActionPolicy::TestHelper + + setup do + @organization = ActsAsTenant.current_tenant + @form = create(:form, organization: @organization) + + user = create(:staff) + sign_in user + end + + context "#new" do + should "be authorized" do + assert_authorized_to( + :manage?, ::CustomForm::Form, + context: {organization: @organization}, + with: Organizations::CustomForm::FormPolicy + ) do + get new_staff_custom_form_form_url + end + end + end + + context "#create" do + setup do + @params = { + custom_form_form: attributes_for(:form) + } + end + + should "be authorized" do + assert_authorized_to( + :manage?, ::CustomForm::Form, + context: {organization: @organization}, + with: Organizations::CustomForm::FormPolicy + ) do + post staff_custom_form_forms_url, params: @params + end + end + end + + context "#index" do + should "be authorized" do + assert_authorized_to( + :manage?, ::CustomForm::Form, + context: {organization: @organization}, + with: Organizations::CustomForm::FormPolicy + ) do + get staff_custom_form_forms_url + end + end + + should "have authorized scope" do + assert_have_authorized_scope( + type: :active_record_relation, + with: Organizations::CustomForm::FormPolicy + ) do + get staff_custom_form_forms_url + end + end + end + + context "#show" do + should "be authorized" do + assert_authorized_to( + :manage?, @form, + with: Organizations::CustomForm::FormPolicy + ) do + get staff_custom_form_form_url(@form) + end + end + end + + context "#edit" do + should "be authorized" do + assert_authorized_to( + :manage?, @form, + with: Organizations::CustomForm::FormPolicy + ) do + get edit_staff_custom_form_form_url(@form) + end + end + end + + context "#update" do + setup do + @params = { + custom_form_form: { + name: "new name" + } + } + end + + should "be authorized" do + assert_authorized_to( + :manage?, @form, + with: Organizations::CustomForm::FormPolicy + ) do + patch staff_custom_form_form_url(@form), + params: @params + end + end + end + + context "#destroy" do + should "be authorized" do + assert_authorized_to( + :manage?, @form, + with: Organizations::CustomForm::FormPolicy + ) do + delete staff_custom_form_form_url(@form) + end + end + end + end + + context "controller" do + setup do + @organization = ActsAsTenant.current_tenant + @form = create(:form) + @user = create(:staff) + sign_in @user + end + + should "get index" do + get staff_custom_form_forms_path + + assert_response :success + assert_select "h2", text: "Forms" + end + + should "should get new" do + get new_staff_custom_form_form_path + + assert_response :success + assert_select "h1", text: "New Form" + end + + context "POST #create" do + should "create new form" do + assert_difference("@organization.forms.count", 1) do + post staff_custom_form_forms_path, params: { + custom_form_form: attributes_for(:form) + } + end + + assert_response :redirect + follow_redirect! + assert_equal "Form saved successfully.", flash.notice + end + + should "not create new form with missing params" do + assert_difference("@organization.forms.count", 0) do + post staff_custom_form_forms_path, params: { + custom_form_form: { + name: "", + title: "" + } + } + end + + assert_template :new + end + end + + context "GET #show" do + should "visit show page" do + get staff_custom_form_form_path(@form) + + assert_response :success + assert_select "h2", text: @form.name + end + + should "not visit show page of non-existent form" do + get staff_custom_form_form_path(id: ::CustomForm::Form.order(:id).last.id + 1) + + assert_response :redirect + follow_redirect! + assert_equal "Form not found.", flash.alert + end + end + + context "GET #edit" do + should "visit edit page" do + get edit_staff_custom_form_form_path(@form) + + assert_response :success + assert_select "h1", text: "Edit Form" + end + + should "not visit edit page of non-existent task" do + get edit_staff_custom_form_form_path(id: ::CustomForm::Form.order(:id).last.id + 1) + + assert_response :redirect + follow_redirect! + assert_equal "Form not found.", flash.alert + end + end + + context "PATCH #update" do + should "update form" do + assert_changes "@form.name" do + patch staff_custom_form_form_path(@form), params: { + custom_form_form: { + name: Faker::Lorem.sentence + } + } + + @form.reload + end + + assert_response :redirect + follow_redirect! + assert_equal "Form saved successfully.", flash.notice + end + + should "not update form with missing param" do + patch staff_custom_form_form_path(@form), params: { + custom_form_form: { + name: "" + } + } + + assert_template :edit + end + end + + context "DELETE #destroy" do + should "destroy a form" do + assert_difference("@organization.forms.count", -1) do + delete staff_custom_form_form_path(@form) + end + + assert_response :redirect + follow_redirect! + assert_equal "Form was successfully deleted.", flash.notice + end + end + end + end + end + end +end diff --git a/test/controllers/organizations/staff/custom_form/questions_controller_test.rb b/test/controllers/organizations/staff/custom_form/questions_controller_test.rb new file mode 100644 index 000000000..f1c2eab76 --- /dev/null +++ b/test/controllers/organizations/staff/custom_form/questions_controller_test.rb @@ -0,0 +1,226 @@ +require "test_helper" +require "action_policy/test_helper" + +module Organizations + module Staff + module CustomForm + class QuestionsControllerTest < ActionDispatch::IntegrationTest + setup do + @organization = ActsAsTenant.current_tenant + @question = create(:question, organization: @organization) + @form = @question.form + + user = create(:staff) + sign_in user + end + + context "authorization" do + include ActionPolicy::TestHelper + + context "new" do + should "be authorized" do + assert_authorized_to( + :manage?, ::CustomForm::Question, + context: {organization: @organization}, + with: Organizations::CustomForm::QuestionPolicy + ) do + get new_staff_custom_form_form_question_url(@form) + end + end + end + + context "create" do + setup do + @params = { + custom_form_question: attributes_for(:question) + } + end + + should "be authorized" do + assert_authorized_to( + :manage?, ::CustomForm::Question, + context: {organization: @organization}, + with: Organizations::CustomForm::QuestionPolicy + ) do + post staff_custom_form_form_questions_url(@form), params: @params + end + end + end + + context "edit" do + should "be authorized" do + assert_authorized_to( + :manage?, @question, + with: Organizations::CustomForm::QuestionPolicy + ) do + get edit_staff_custom_form_form_question_url(@form, @question) + end + end + end + + context "update" do + setup do + @params = { + custom_form_question: { + name: "new name" + } + } + end + + should "be authorized" do + assert_authorized_to( + :manage?, @question, + with: Organizations::CustomForm::QuestionPolicy + ) do + patch staff_custom_form_form_question_url(@form, @question), params: @params + end + end + end + + context "destroy" do + should "be authorized" do + assert_authorized_to( + :manage?, @question, + with: Organizations::CustomForm::QuestionPolicy + ) do + delete staff_custom_form_form_question_url(@form, @question) + end + end + end + end + + test "should get new" do + get new_staff_custom_form_form_question_url(@form) + + assert_response :success + assert_select "h1", text: "New Question" + end + + context "POST #create" do + should "create new question" do + assert_difference("@form.questions.count", 1) do + post staff_custom_form_form_questions_url(@form), params: { + custom_form_question: attributes_for(:question) + } + end + + assert_response :redirect + follow_redirect! + assert_equal "Question saved successfully.", flash.notice + end + + should "not create question with invalid data" do + assert_no_difference("@form.questions.count") do + post staff_custom_form_form_questions_url(@form), params: { + custom_form_question: {name: ""} + } + end + + assert_template :new + end + end + + context "GET #edit" do + should "visit edit page" do + get edit_staff_custom_form_form_question_url(@form, @question) + + assert_response :success + assert_select "h1", text: "Edit Question" + end + + should "not visit edit page of non-existent form" do + get edit_staff_custom_form_form_question_url(0, @question) + + assert_response :redirect + follow_redirect! + assert_equal "Form not found.", flash.alert + end + + should "not visit edit page of non-existent question" do + get edit_staff_custom_form_form_question_url(@form, id: 0) + + assert_response :redirect + follow_redirect! + assert_equal "Question not found.", flash.alert + end + + should "not visit edit page of question outside of form" do + f2 = create(:form, organization: @organization) + q2 = create(:question, form: f2) + + get edit_staff_custom_form_form_question_url(@form, q2) + + assert_response :redirect + follow_redirect! + assert_equal "Question not found.", flash.alert + end + end + + context "PATCH #update" do + should "update question" do + patch staff_custom_form_form_question_url(@form, @question), params: { + custom_form_question: { + name: "new name" + } + } + + assert_response :redirect + follow_redirect! + assert_equal "Question saved successfully.", flash.notice + end + + should "not update question with invalid data" do + patch staff_custom_form_form_question_url(@form, @question), params: { + custom_form_question: { + name: "" + } + } + + assert_template :edit + end + + should "not update question belonging to other form" do + f2 = create(:form, organization: @organization) + q2 = create(:question, form: f2) + + patch staff_custom_form_form_question_url(@form, q2), params: { + custom_form_question: { + name: "new name", + label: "new label" + } + } + + assert_response :redirect + follow_redirect! + assert_equal "Question not found.", flash.alert + end + end + + context "DELETE #destroy" do + should "destroy a question" do + assert_difference("@form.questions.count", -1) do + delete staff_custom_form_form_question_url(@form, @question) + end + + assert_response :redirect + follow_redirect! + assert_equal "Question was successfully deleted.", flash.notice + end + + should "not destroy a question belonging to another form" do + f2 = create(:form, organization: @organization) + q2 = create(:question, form: f2) + + assert_no_difference("::CustomForm::Question.count") do + delete staff_custom_form_form_question_url(@form, q2) + end + + assert_response :redirect + follow_redirect! + assert_equal "Question not found.", flash.alert + end + end + end + end + end +end diff --git a/test/controllers/organizations/staff/forms_controller_test.rb b/test/controllers/organizations/staff/forms_controller_test.rb deleted file mode 100644 index 959c52fb2..000000000 --- a/test/controllers/organizations/staff/forms_controller_test.rb +++ /dev/null @@ -1,244 +0,0 @@ -require "test_helper" -require "action_policy/test_helper" - -class Organizations::Staff::FormsControllerTest < ActionDispatch::IntegrationTest - context "authorization" do - include ActionPolicy::TestHelper - - setup do - @organization = ActsAsTenant.current_tenant - @form = create(:form, organization: @organization) - - user = create(:staff) - sign_in user - end - - context "#new" do - should "be authorized" do - assert_authorized_to( - :manage?, Form, - context: {organization: @organization}, - with: Organizations::FormPolicy - ) do - get new_staff_form_url - end - end - end - - context "#create" do - setup do - @params = { - form: attributes_for(:form) - } - end - - should "be authorized" do - assert_authorized_to( - :manage?, Form, - context: {organization: @organization}, - with: Organizations::FormPolicy - ) do - post staff_forms_url, params: @params - end - end - end - - context "#index" do - should "be authorized" do - assert_authorized_to( - :manage?, Form, - context: {organization: @organization}, - with: Organizations::FormPolicy - ) do - get staff_forms_url - end - end - - should "have authorized scope" do - assert_have_authorized_scope( - type: :active_record_relation, - with: Organizations::FormPolicy - ) do - get staff_forms_url - end - end - end - - context "#show" do - should "be authorized" do - assert_authorized_to( - :manage?, @form, - with: Organizations::FormPolicy - ) do - get staff_form_url(@form) - end - end - end - - context "#edit" do - should "be authorized" do - assert_authorized_to( - :manage?, @form, - with: Organizations::FormPolicy - ) do - get edit_staff_form_url(@form) - end - end - end - - context "#update" do - setup do - @params = { - form: { - name: "new name" - } - } - end - - should "be authorized" do - assert_authorized_to( - :manage?, @form, - with: Organizations::FormPolicy - ) do - patch staff_form_url(@form), - params: @params - end - end - end - - context "#destroy" do - should "be authorized" do - assert_authorized_to( - :manage?, @form, - with: Organizations::FormPolicy - ) do - delete staff_form_url(@form) - end - end - end - end - - context "controller" do - setup do - @organization = ActsAsTenant.current_tenant - @form = create(:form) - @user = create(:staff) - sign_in @user - end - - should "get index" do - get staff_forms_path - - assert_response :success - assert_select "h2", text: "Forms" - end - - should "should get new" do - get new_staff_form_path - - assert_response :success - assert_select "h1", text: "New Form" - end - - context "POST #create" do - should "create new form" do - assert_difference("@organization.forms.count", 1) do - post staff_forms_path, params: { - form: attributes_for(:form) - } - end - - assert_response :redirect - follow_redirect! - assert_equal "Form saved successfully.", flash.notice - end - - should "not create new form with missing params" do - assert_difference("@organization.forms.count", 0) do - post staff_forms_path, params: { - form: { - name: "", - title: "" - } - } - end - - assert_template :new - end - end - - context "GET #show" do - should "visit show page" do - get staff_form_path(@form) - - assert_response :success - assert_select "h2", text: @form.name - end - - should "not visit show page of non-existent form" do - get staff_form_path(id: Form.order(:id).last.id + 1) - - assert_response :redirect - follow_redirect! - assert_equal "Form not found.", flash.alert - end - end - - context "GET #edit" do - should "visit edit page" do - get edit_staff_form_path(@form) - - assert_response :success - assert_select "h1", text: "Edit Form" - end - - should "not visit edit page of non-existent task" do - get edit_staff_form_path(id: Form.order(:id).last.id + 1) - - assert_response :redirect - follow_redirect! - assert_equal "Form not found.", flash.alert - end - end - - context "PATCH #update" do - should "update form" do - assert_changes "@form.name" do - patch staff_form_path(@form), params: { - form: { - name: Faker::Lorem.sentence - } - } - - @form.reload - end - - assert_response :redirect - follow_redirect! - assert_equal "Form saved successfully.", flash.notice - end - - should "not update form with missing param" do - patch staff_form_path(@form), params: { - form: { - name: "" - } - } - - assert_template :edit - end - end - - context "DELETE #destroy" do - should "destroy a form" do - assert_difference("@organization.forms.count", -1) do - delete staff_form_path(@form) - end - - assert_response :redirect - follow_redirect! - assert_equal "Form was successfully deleted.", flash.notice - end - end - end -end diff --git a/test/controllers/organizations/staff/questions_controller_test.rb b/test/controllers/organizations/staff/questions_controller_test.rb deleted file mode 100644 index 0c1b4ff92..000000000 --- a/test/controllers/organizations/staff/questions_controller_test.rb +++ /dev/null @@ -1,220 +0,0 @@ -require "test_helper" -require "action_policy/test_helper" - -class Organizations::Staff::QuestionsControllerTest < ActionDispatch::IntegrationTest - setup do - @organization = ActsAsTenant.current_tenant - @question = create(:question, organization: @organization) - @form = @question.form - - user = create(:staff) - sign_in user - end - - context "authorization" do - include ActionPolicy::TestHelper - - context "new" do - should "be authorized" do - assert_authorized_to( - :manage?, Question, - context: {organization: @organization}, - with: Organizations::QuestionPolicy - ) do - get new_staff_form_question_url(@form) - end - end - end - - context "create" do - setup do - @params = { - question: attributes_for(:question) - } - end - - should "be authorized" do - assert_authorized_to( - :manage?, Question, - context: {organization: @organization}, - with: Organizations::QuestionPolicy - ) do - post staff_form_questions_url(@form), params: @params - end - end - end - - context "edit" do - should "be authorized" do - assert_authorized_to( - :manage?, @question, - with: Organizations::QuestionPolicy - ) do - get edit_staff_form_question_url(@form, @question) - end - end - end - - context "update" do - setup do - @params = { - question: { - name: "new name" - } - } - end - - should "be authorized" do - assert_authorized_to( - :manage?, @question, - with: Organizations::QuestionPolicy - ) do - patch staff_form_question_url(@form, @question), params: @params - end - end - end - - context "destroy" do - should "be authorized" do - assert_authorized_to( - :manage?, @question, - with: Organizations::QuestionPolicy - ) do - delete staff_form_question_url(@form, @question) - end - end - end - end - - test "should get new" do - get new_staff_form_question_url(@form) - - assert_response :success - assert_select "h1", text: "New Question" - end - - context "POST #create" do - should "create new question" do - assert_difference("@form.questions.count", 1) do - post staff_form_questions_url(@form), params: { - question: attributes_for(:question) - } - end - - assert_response :redirect - follow_redirect! - assert_equal "Question saved successfully.", flash.notice - end - - should "not create question with invalid data" do - assert_no_difference("@form.questions.count") do - post staff_form_questions_url(@form), params: { - question: {name: ""} - } - end - - assert_template :new - end - end - - context "GET #edit" do - should "visit edit page" do - get edit_staff_form_question_url(@form, @question) - - assert_response :success - assert_select "h1", text: "Edit Question" - end - - should "not visit edit page of non-existent form" do - get edit_staff_form_question_url(0, @question) - - assert_response :redirect - follow_redirect! - assert_equal "Form not found.", flash.alert - end - - should "not visit edit page of non-existent question" do - get edit_staff_form_question_url(@form, id: 0) - - assert_response :redirect - follow_redirect! - assert_equal "Question not found.", flash.alert - end - - should "not visit edit page of question outside of form" do - f2 = create(:form, organization: @organization) - q2 = create(:question, form: f2) - - get edit_staff_form_question_url(@form, q2) - - assert_response :redirect - follow_redirect! - assert_equal "Question not found.", flash.alert - end - end - - context "PATCH #update" do - should "update question" do - patch staff_form_question_url(@form, @question), params: { - question: { - name: "new name" - } - } - - assert_response :redirect - follow_redirect! - assert_equal "Question saved successfully.", flash.notice - end - - should "not update question with invalid data" do - patch staff_form_question_url(@form, @question), params: { - question: { - name: "" - } - } - - assert_template :edit - end - - should "not update question belonging to other form" do - f2 = create(:form, organization: @organization) - q2 = create(:question, form: f2) - - patch staff_form_question_url(@form, q2), params: { - question: { - name: "new name", - label: "new label" - } - } - - assert_response :redirect - follow_redirect! - assert_equal "Question not found.", flash.alert - end - end - - context "DELETE #destroy" do - should "destroy a question" do - assert_difference("@form.questions.count", -1) do - delete staff_form_question_url(@form, @question) - end - - assert_response :redirect - follow_redirect! - assert_equal "Question was successfully deleted.", flash.notice - end - - should "not destroy a question belonging to another form" do - f2 = create(:form, organization: @organization) - q2 = create(:question, form: f2) - - assert_no_difference("Question.count") do - delete staff_form_question_url(@form, q2) - end - - assert_response :redirect - follow_redirect! - assert_equal "Question not found.", flash.alert - end - end -end diff --git a/test/factories/forms.rb b/test/factories/custom_form/forms.rb similarity index 78% rename from test/factories/forms.rb rename to test/factories/custom_form/forms.rb index 470f853dd..91c947157 100644 --- a/test/factories/forms.rb +++ b/test/factories/custom_form/forms.rb @@ -1,5 +1,5 @@ FactoryBot.define do - factory :form do + factory :form, class: "custom_form/form" do name { Faker::Lorem.sentence } description { Faker::Lorem.sentence } title { name } diff --git a/test/factories/questions.rb b/test/factories/custom_form/questions.rb similarity index 83% rename from test/factories/questions.rb rename to test/factories/custom_form/questions.rb index 8646e356f..5a8dde43e 100644 --- a/test/factories/questions.rb +++ b/test/factories/custom_form/questions.rb @@ -1,5 +1,5 @@ FactoryBot.define do - factory :question do + factory :question, class: "custom_form/question" do name { Faker::Lorem.question } description { Faker::Lorem.sentence } label { name } diff --git a/test/models/custom_form/form_test.rb b/test/models/custom_form/form_test.rb new file mode 100644 index 000000000..488dd6489 --- /dev/null +++ b/test/models/custom_form/form_test.rb @@ -0,0 +1,15 @@ +require "test_helper" + +module CustomForm + class FormTest < ActiveSupport::TestCase + context "associations" do + should have_many(:form_profiles).dependent(:destroy) + should have_many(:questions).dependent(:destroy) + end + + context "validations" do + should validate_presence_of(:name) + should validate_presence_of(:title) + end + end +end diff --git a/test/models/custom_form/question_test.rb b/test/models/custom_form/question_test.rb new file mode 100644 index 000000000..753fda28d --- /dev/null +++ b/test/models/custom_form/question_test.rb @@ -0,0 +1,34 @@ +require "test_helper" + +module CustomForm + class QuestionTest < ActiveSupport::TestCase + context "associations" do + should belong_to(:form) + + should have_one(:organization).through(:form) + end + + context "validations" do + should validate_presence_of(:name) + should validate_presence_of(:label) + end + + context ".ordered" do + should "order questions in a form" do + form = create(:form) + q1 = create(:question, form:, sort_order: 1) + q2 = create(:question, form:, sort_order: 0) + + assert_equal Question.ordered.all, [q2, q1] + end + end + + context "#snapshot" do + should "return the correct snapshot" do + q = create(:question) + + assert_equal q.snapshot, {label: q.label, options: q.options} + end + end + end +end diff --git a/test/models/form_test.rb b/test/models/form_test.rb deleted file mode 100644 index f6dfb7b46..000000000 --- a/test/models/form_test.rb +++ /dev/null @@ -1,13 +0,0 @@ -require "test_helper" - -class FormTest < ActiveSupport::TestCase - context "associations" do - should have_many(:form_profiles).dependent(:destroy) - should have_many(:questions).dependent(:destroy) - end - - context "validations" do - should validate_presence_of(:name) - should validate_presence_of(:title) - end -end diff --git a/test/models/question_test.rb b/test/models/question_test.rb deleted file mode 100644 index ba3735931..000000000 --- a/test/models/question_test.rb +++ /dev/null @@ -1,32 +0,0 @@ -require "test_helper" - -class QuestionTest < ActiveSupport::TestCase - context "associations" do - should belong_to(:form) - - should have_one(:organization).through(:form) - end - - context "validations" do - should validate_presence_of(:name) - should validate_presence_of(:label) - end - - context ".ordered" do - should "order questions in a form" do - form = create(:form) - q1 = create(:question, form:, sort_order: 1) - q2 = create(:question, form:, sort_order: 0) - - assert_equal Question.ordered.all, [q2, q1] - end - end - - context "#snapshot" do - should "return the correct snapshot" do - q = create(:question) - - assert_equal q.snapshot, {label: q.label, options: q.options} - end - end -end diff --git a/test/policies/organizations/custom_form/form_policy_test.rb b/test/policies/organizations/custom_form/form_policy_test.rb new file mode 100644 index 000000000..21892810a --- /dev/null +++ b/test/policies/organizations/custom_form/form_policy_test.rb @@ -0,0 +1,171 @@ +require "test_helper" + +module Organizations + module CustomForm + class FormPolicyTest < ActiveSupport::TestCase + include PetRescue::PolicyAssertions + + context "context only action" do + setup do + @organization = ActsAsTenant.current_tenant + @policy = -> { Organizations::CustomForm::FormPolicy.new(::CustomForm::Form, user: @user, organization: @organization) } + end + + context "#manage?" do + setup do + @action = -> { @policy.call.apply(:manage?) } + end + + context "when user is nil" do + setup do + @user = nil + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is adopter" do + setup do + @user = create(:adopter) + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is fosterer" do + setup do + @user = create(:fosterer) + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is deactivated staff" do + setup do + @user = create(:staff, :deactivated) + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is staff admin" do + setup do + @user = create(:staff_admin) + end + + should "return true" do + assert_equal true, @action.call + end + end + end + + context "#index?" do + should "be an alias to :manage?" do + assert_alias_rule @policy.call, :index?, :manage? + end + end + + context "#new?" do + should "be an alias to :manage?" do + assert_alias_rule @policy.call, :new?, :manage? + end + end + + context "#create?" do + should "be an alias to :manage?" do + assert_alias_rule @policy.call, :create?, :manage? + end + end + end + + context "existing record action" do + setup do + @form = create(:form) + @policy = -> { Organizations::CustomForm::FormPolicy.new(@form, user: @user) } + end + + context "#manage?" do + setup do + @action = -> { @policy.call.apply(:manage?) } + end + + context "when user is nil" do + setup do + @user = nil + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is adopter" do + setup do + @user = create(:adopter) + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is fosterer" do + setup do + @user = create(:fosterer) + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is deactivated staff" do + setup do + @user = create(:staff, :deactivated) + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is staff admin" do + setup do + @user = create(:staff_admin) + end + + should "return true" do + assert_equal true, @action.call + end + end + end + + context "#show?" do + should "be an alias to :manage?" do + assert_alias_rule @policy.call, :show?, :manage? + end + end + + context "#edit?" do + should "be an alias to :manage?" do + assert_alias_rule @policy.call, :edit?, :manage? + end + end + + context "#update?" do + should "be an alias to :manage?" do + assert_alias_rule @policy.call, :update?, :manage? + end + end + end + end + end +end diff --git a/test/policies/organizations/custom_form/question_policy_test.rb b/test/policies/organizations/custom_form/question_policy_test.rb new file mode 100644 index 000000000..f57895099 --- /dev/null +++ b/test/policies/organizations/custom_form/question_policy_test.rb @@ -0,0 +1,171 @@ +require "test_helper" + +module Organizations + module CustomForm + class QuestionPolicyTest < ActiveSupport::TestCase + include PetRescue::PolicyAssertions + + context "context only action" do + setup do + @organization = ActsAsTenant.current_tenant + @policy = -> { Organizations::CustomForm::QuestionPolicy.new(::CustomForm::Question, user: @user, organization: @organization) } + end + + context "#manage?" do + setup do + @action = -> { @policy.call.apply(:manage?) } + end + + context "when user is nil" do + setup do + @user = nil + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is adopter" do + setup do + @user = create(:adopter) + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is fosterer" do + setup do + @user = create(:fosterer) + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is deactivated staff" do + setup do + @user = create(:staff, :deactivated) + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is staff admin" do + setup do + @user = create(:staff_admin) + end + + should "return true" do + assert_equal true, @action.call + end + end + end + + context "#index?" do + should "be an alias to :manage?" do + assert_alias_rule @policy.call, :index?, :manage? + end + end + + context "#new?" do + should "be an alias to :manage?" do + assert_alias_rule @policy.call, :new?, :manage? + end + end + + context "#create?" do + should "be an alias to :manage?" do + assert_alias_rule @policy.call, :create?, :manage? + end + end + end + + context "existing record action" do + setup do + @question = create(:question) + @policy = -> { Organizations::CustomForm::QuestionPolicy.new(@question, user: @user) } + end + + context "#manage?" do + setup do + @action = -> { @policy.call.apply(:manage?) } + end + + context "when user is nil" do + setup do + @user = nil + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is adopter" do + setup do + @user = create(:adopter) + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is fosterer" do + setup do + @user = create(:fosterer) + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is deactivated staff" do + setup do + @user = create(:staff, :deactivated) + end + + should "return false" do + assert_equal false, @action.call + end + end + + context "when user is staff admin" do + setup do + @user = create(:staff_admin) + end + + should "return true" do + assert_equal true, @action.call + end + end + end + + context "#show?" do + should "be an alias to :manage?" do + assert_alias_rule @policy.call, :show?, :manage? + end + end + + context "#edit?" do + should "be an alias to :manage?" do + assert_alias_rule @policy.call, :edit?, :manage? + end + end + + context "#update?" do + should "be an alias to :manage?" do + assert_alias_rule @policy.call, :update?, :manage? + end + end + end + end + end +end diff --git a/test/policies/organizations/form_policy_test.rb b/test/policies/organizations/form_policy_test.rb deleted file mode 100644 index ba73e6dfe..000000000 --- a/test/policies/organizations/form_policy_test.rb +++ /dev/null @@ -1,167 +0,0 @@ -require "test_helper" - -class Organizations::FormPolicyTest < ActiveSupport::TestCase - include PetRescue::PolicyAssertions - - context "context only action" do - setup do - @organization = ActsAsTenant.current_tenant - @policy = -> { Organizations::FormPolicy.new(Form, user: @user, organization: @organization) } - end - - context "#manage?" do - setup do - @action = -> { @policy.call.apply(:manage?) } - end - - context "when user is nil" do - setup do - @user = nil - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is adopter" do - setup do - @user = create(:adopter) - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is fosterer" do - setup do - @user = create(:fosterer) - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is deactivated staff" do - setup do - @user = create(:staff, :deactivated) - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is staff admin" do - setup do - @user = create(:staff_admin) - end - - should "return true" do - assert_equal true, @action.call - end - end - end - - context "#index?" do - should "be an alias to :manage?" do - assert_alias_rule @policy.call, :index?, :manage? - end - end - - context "#new?" do - should "be an alias to :manage?" do - assert_alias_rule @policy.call, :new?, :manage? - end - end - - context "#create?" do - should "be an alias to :manage?" do - assert_alias_rule @policy.call, :create?, :manage? - end - end - end - - context "existing record action" do - setup do - @form = create(:form) - @policy = -> { Organizations::FormPolicy.new(@form, user: @user) } - end - - context "#manage?" do - setup do - @action = -> { @policy.call.apply(:manage?) } - end - - context "when user is nil" do - setup do - @user = nil - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is adopter" do - setup do - @user = create(:adopter) - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is fosterer" do - setup do - @user = create(:fosterer) - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is deactivated staff" do - setup do - @user = create(:staff, :deactivated) - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is staff admin" do - setup do - @user = create(:staff_admin) - end - - should "return true" do - assert_equal true, @action.call - end - end - end - - context "#show?" do - should "be an alias to :manage?" do - assert_alias_rule @policy.call, :show?, :manage? - end - end - - context "#edit?" do - should "be an alias to :manage?" do - assert_alias_rule @policy.call, :edit?, :manage? - end - end - - context "#update?" do - should "be an alias to :manage?" do - assert_alias_rule @policy.call, :update?, :manage? - end - end - end -end diff --git a/test/policies/organizations/question_policy_test.rb b/test/policies/organizations/question_policy_test.rb deleted file mode 100644 index 6b51466f5..000000000 --- a/test/policies/organizations/question_policy_test.rb +++ /dev/null @@ -1,167 +0,0 @@ -require "test_helper" - -class Organizations::QuestionPolicyTest < ActiveSupport::TestCase - include PetRescue::PolicyAssertions - - context "context only action" do - setup do - @organization = ActsAsTenant.current_tenant - @policy = -> { Organizations::QuestionPolicy.new(Question, user: @user, organization: @organization) } - end - - context "#manage?" do - setup do - @action = -> { @policy.call.apply(:manage?) } - end - - context "when user is nil" do - setup do - @user = nil - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is adopter" do - setup do - @user = create(:adopter) - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is fosterer" do - setup do - @user = create(:fosterer) - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is deactivated staff" do - setup do - @user = create(:staff, :deactivated) - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is staff admin" do - setup do - @user = create(:staff_admin) - end - - should "return true" do - assert_equal true, @action.call - end - end - end - - context "#index?" do - should "be an alias to :manage?" do - assert_alias_rule @policy.call, :index?, :manage? - end - end - - context "#new?" do - should "be an alias to :manage?" do - assert_alias_rule @policy.call, :new?, :manage? - end - end - - context "#create?" do - should "be an alias to :manage?" do - assert_alias_rule @policy.call, :create?, :manage? - end - end - end - - context "existing record action" do - setup do - @question = create(:question) - @policy = -> { Organizations::QuestionPolicy.new(@question, user: @user) } - end - - context "#manage?" do - setup do - @action = -> { @policy.call.apply(:manage?) } - end - - context "when user is nil" do - setup do - @user = nil - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is adopter" do - setup do - @user = create(:adopter) - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is fosterer" do - setup do - @user = create(:fosterer) - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is deactivated staff" do - setup do - @user = create(:staff, :deactivated) - end - - should "return false" do - assert_equal false, @action.call - end - end - - context "when user is staff admin" do - setup do - @user = create(:staff_admin) - end - - should "return true" do - assert_equal true, @action.call - end - end - end - - context "#show?" do - should "be an alias to :manage?" do - assert_alias_rule @policy.call, :show?, :manage? - end - end - - context "#edit?" do - should "be an alias to :manage?" do - assert_alias_rule @policy.call, :edit?, :manage? - end - end - - context "#update?" do - should "be an alias to :manage?" do - assert_alias_rule @policy.call, :update?, :manage? - end - end - end -end