Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking β€œSign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Admin Abilities #3149

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/admin/admins_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
class Admin::AdminsController < Admin::UsersController
before_action :find_resource, except: [:index, :new, :create, :login_as_assessor, :login_as_user]

expose(:collaborators) do
nil
end

def index
params[:search] ||= AdminSearch::DEFAULT_SEARCH
params[:search].permit!
4 changes: 4 additions & 0 deletions app/controllers/admin/assessors_controller.rb
Original file line number Diff line number Diff line change
@@ -15,6 +15,10 @@ class Admin::AssessorsController < Admin::UsersController
:bulk_deactivate_dt,
]

expose(:collaborators) do
nil
end

def index
params[:search] ||= AssessorSearch::DEFAULT_SEARCH
params[:search].permit!
51 changes: 0 additions & 51 deletions app/controllers/admin/form_answers/collaborators_controller.rb

This file was deleted.

4 changes: 4 additions & 0 deletions app/controllers/admin/judges_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class Admin::JudgesController < Admin::UsersController
expose(:collaborators) do
nil
end

def index
params[:search] ||= JudgeSearch::DEFAULT_SEARCH
params[:search].permit!
52 changes: 52 additions & 0 deletions app/controllers/admin/users/collaborators_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Admin::Users::CollaboratorsController < Admin::BaseController
expose(:user) do
User.find(params[:user_id])
end

expose(:collaborator) do
User.find(params[:collaborator_id])
end

expose(:search_users) do
AdminActions::SearchCollaboratorCandidates.new(existing_collaborators: user.account.users, params: search_params)
end

expose(:add_collaborator_interactor) do
AdminActions::AddCollaborator.new(account: user.account, collaborator:, params: create_params)
end

expose(:candidates) do
search_users.candidates
end

def search
authorize user, :can_add_collaborators_to_account?
search_users.run if search_users.valid?
end

def create
authorize user, :can_add_collaborators_to_account?

add_collaborator_interactor.run.tap do |result|
if result.success?
redirect_to edit_admin_user_path(user), notice: "#{collaborator.email} successfully added to Collaborators!"
else
redirect_to edit_admin_user_path(user), notice: "#{collaborator.email} could not be added to Collaborators: #{result.error_messages}"
end
end
end

private

def create_params
params.require(:user).permit(:transfer_form_answers, :new_owner_id, :role).tap do |p|
p[:transfer_form_answers] = ActiveModel::Type::Boolean.new.cast(p[:transfer_form_answers])
end
end

def search_params
return if params[:search].blank?

params.require(:search).permit(:query)
end
end
4 changes: 4 additions & 0 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class Admin::UsersController < Admin::BaseController
before_action :find_resource, except: [:index, :new, :create]

expose(:collaborators) do
@resource.account.collaborators_without(@resource)
end

def index
params[:search] ||= UserSearch::DEFAULT_SEARCH
params[:search].permit!
102 changes: 86 additions & 16 deletions app/interactors/admin_actions/add_collaborator.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
module AdminActions
class AddCollaborator
attr_reader :form_answer,
:user,
:success,
:errors
attr_reader :account, :role, :transfer_form_answers, :new_owner_id, :collaborator, :success, :errors

def initialize(form_answer, user)
@form_answer = form_answer
@user = user
def initialize(account:, collaborator:, params:, existing_account: collaborator.account)
@account = account
@collaborator = collaborator
@existing_account = existing_account
@role = params.fetch(:role, "regular")
@transfer_form_answers = params.fetch(:transfer_form_answers, false)
@new_owner_id = params.fetch(:new_owner_id, nil)
@errors = []
end

def run
if user.can_be_added_to_collaborators_to_another_account?
persist!
if account_will_be_orphaned?
@errors << "User account has active users, ownership of the account must be transferred"
elsif progressed_form_answers_will_be_orphaned?
@errors << "User has applications in progress, and there are no other users on the account to transfer them to"
else
@errors = "can't be added as linked with another account!"
persist!
end

self
@@ -24,17 +28,83 @@ def success?
@success.present?
end

def error_messages
@errors.join(", ")
end

private

def persist!
user.role = "regular"
user.account = form_answer.account

if user.save
ActiveRecord::Base.transaction do
transfer_collaborator!
transfer_ownership!
handle_form_answers!
delete_existing_account!
@success = true
else
@errors = user.errors.full_messages.join(", ")
end
rescue ActiveRecord::RecordInvalid => e
@errors << e.message
end

def existing_account_has_other_collaborators?
return unless @existing_account

@existing_account.collaborators_without(collaborator).any?
end

def account_will_be_orphaned?
collaborator_is_owner? && existing_account_has_other_collaborators? && new_owner_id.blank?
end

def collaborator_is_owner?
return unless @existing_account

@existing_account.owner == collaborator
end

def progressed_form_answers_will_be_orphaned?
keep_form_answers_on_original_account? && progressed_form_answers? && !existing_account_has_other_collaborators?
end

def progressed_form_answers?
collaborator.form_answers.touched.any?
end

def transfer_form_answers?
transfer_form_answers
end

def keep_form_answers_on_original_account?
!transfer_form_answers?
end

def handle_form_answers!
return unless @collaborator.form_answers.any?

if transfer_form_answers?
@collaborator.form_answers.each { |f| f.update!(account: @account) }
elsif existing_account_has_other_collaborators?
@collaborator.form_answers.each { |f| f.update!(user: @existing_account.owner) }
elsif !progressed_form_answers?
@collaborator.form_answers.each(&:destroy!)
end
end

def transfer_ownership!
return unless @new_owner_id

@existing_account.update!(owner_id: @new_owner_id)
end

def transfer_collaborator!
collaborator.update!(role: role, account: account)
end

def delete_existing_account!
return unless @existing_account
return if existing_account_has_other_collaborators?

@existing_account.destroy!
end
end
end
15 changes: 4 additions & 11 deletions app/interactors/admin_actions/search_collaborator_candidates.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
module AdminActions
class SearchCollaboratorCandidates
attr_accessor :form_answer,
:account,
:existing_collaborators,
:candidates,
:query,
:error
attr_accessor :account, :existing_collaborators, :candidates, :query, :error

def initialize(form_answer, query = nil)
@query = query[:query]
@form_answer = form_answer
@account = form_answer.account
@existing_collaborators = account.users
def initialize(existing_collaborators:, params: {})
@query = params[:query]
@existing_collaborators = existing_collaborators
end

def run
11 changes: 10 additions & 1 deletion app/models/form_answer.rb
Original file line number Diff line number Diff line change
@@ -190,6 +190,12 @@ def secondary
scope :vocf_free, -> { where(award_type: %w[mobility development]) }
scope :provided_estimates, -> { where("document #>> '{product_estimated_figures}' = 'yes'") }

scope :touched, -> {
joins("LEFT JOIN eligibilities ON eligibilities.form_answer_id = form_answers.id")
.where("eligibilities.id IS NOT NULL AND eligibilities.type = form_answers.award_type AND eligibilities.answers::jsonb <> '{}'::jsonb")
.or(where("(document::jsonb - 'organization_name' - 'company_name') <> '{}'::jsonb"))
}

# callbacks
before_save :set_award_year, unless: :award_year
before_save :set_urn
@@ -326,13 +332,16 @@ def head_of_business
end

def company_or_nominee_from_document
comp_attr = promotion? ? "organization_name" : "company_name"
name = document[comp_attr]
name = nominee_full_name_from_document if promotion? && name.blank?
name = name.try(:strip)
name.presence
end

def comp_attr
promotion? ? "organization_name" : "company_name"
end

def nominee_full_name_from_document
"#{document["nominee_info_first_name"]} #{document["nominee_info_last_name"]}".strip
end
8 changes: 0 additions & 8 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -142,14 +142,6 @@ def reset_password_period_valid?
end
end

def can_be_added_to_collaborators_to_another_account?
account.blank? || (
account.present? &&
form_answers.blank? &&
account.form_answers.blank?
)
end

def new_member?
created_at > 3.days.ago
end
4 changes: 0 additions & 4 deletions app/policies/form_answer_policy.rb
Original file line number Diff line number Diff line change
@@ -135,10 +135,6 @@ def can_download_original_pdf_of_application_before_deadline?
record.pdf_version.present?
end

def can_add_collaborators_to_application?
admin?
end

private

def audit_certificate_available?
2 changes: 1 addition & 1 deletion app/policies/user_policy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class UserPolicy < ApplicationPolicy
%w[index? update? create? show? new?].each do |method|
%w[index? update? create? show? new? can_add_collaborators_to_account?].each do |method|
define_method method do
admin?
end

This file was deleted.

6 changes: 0 additions & 6 deletions app/views/admin/form_answers/collaborators/create.js.slim

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
.form-group
.form-container
label.form-label User accounts

= render "admin/form_answers/collaborators/list"

- if policy(resource).can_add_collaborators_to_application?
= render "admin/form_answers/collaborators/form"
5 changes: 3 additions & 2 deletions app/views/admin/users/_fields_collaborators.html.slim
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/ TODO collaborators new/delete, also the collaborators variable
- if action_name == "edit"
- collaborators = resource.account.collaborators_without(resource)
- if collaborators.any?
= render "admin/collaborators/list", collaborators: collaborators
- else
@@ -11,3 +9,6 @@
br
p.p-empty This user has not added any collaborators.
br

- if policy(resource).can_add_collaborators_to_account?
= render "admin/users/collaborators/search_form", resource: resource
10 changes: 0 additions & 10 deletions app/views/admin/users/_form.html.slim
Original file line number Diff line number Diff line change
@@ -28,16 +28,6 @@
.panel-body
= render "fields_contact_preferences", f: f

- unless action_name == "new"
.panel.panel-default[data-controller="element-focus"]
.panel-heading id="section-collaborators-header"
h2.panel-title
a.collapsed data-toggle="collapse" data-parent="#user-form-panel" href="#section-collaborators" aria-expanded="false" aria-controls="section-collaborators" data-element-focus-target="reveal"
' Collaborators
#section-collaborators.section-collaborators.panel-collapse.collapse[aria-labelledby="section-collaborators-header" data-element-scroll-target="accordion"]
.panel-body
= render "fields_collaborators", f: f, resource: resource

br
.clearfix
.pull-right
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
= simple_form_for :search,
url: search_admin_form_answer_collaborators_url(resource),
url: search_admin_user_collaborators_url(resource),
remote: true,
method: :get,
as: nil,
@@ -10,8 +10,6 @@

.alert.alert-danger.hidden.js-admin-search-collaborators-error-box role="alert"

ul.list-unstyled.list-actions.hidden.js-admin-search-collaborators-results-box

.form-block
.row
.col-md-12
@@ -25,3 +23,5 @@
.text-right
= f.submit "Search", class: "btn btn-primary pull-right"
.clear

ul.list-unstyled.list-actions.hidden.js-admin-search-collaborators-results-box
9 changes: 9 additions & 0 deletions app/views/admin/users/collaborators/_search_results.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- if candidates.present?
- candidates.each do |candidate|
li.list-group-item id="user_#{candidate.id}"
p.pull-right
= link_to "#{candidate.full_name} (#{candidate.email})", edit_admin_user_path(candidate)

br

p= render "admin/users/collaborators/transfer_form", collaborator: candidate
32 changes: 32 additions & 0 deletions app/views/admin/users/collaborators/_transfer_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
= simple_form_for :user, url: admin_user_collaborators_url(collaborator_id: collaborator.id), method: :post do |f|
- if collaborator&.account.collaborators_without(collaborator).any? && collaborator.account.owner == collaborator
.question-group
h3= f.label :new_owner_id, { label: "Owner", for: "new_owner_id_#{collaborator.id}" }
.row
.col-sm-6
= f.select :new_owner_id,
collaborator.account.collaborators_without(collaborator).map { |u| [u.email, u.id] },
{ include_blank: 'Select New Owner…' },
{ class: "form-control", id: "new_owner_id_#{collaborator.id}" }

.question-group
h3= f.label :role, { label: "Role", for: "role_#{collaborator.id}" }
.row
.col-sm-4
= f.select :role,
User::POSSIBLE_ROLES.map { |r| [r.humanize, r] },
{ selected: collaborator.role },
{ class: "form-control", id: "role_#{collaborator.id}" }

- if collaborator.form_answers.any?
.question-group
h3= f.label :transfer_form_answers, { label: "Transfer existing Application/s", for: "transfer_form_answers_#{collaborator.id}" }
.row
.col-sm-1
= f.check_box :transfer_form_answers, value: true, id: "transfer_form_answers_#{collaborator.id}"
.col-sm-11
.alert.alert-warning
p Ticking this box will transfer all applications to the new account. All collaborators on the new account will have access to these applications and the associated information.

br
= f.submit "Transfer", class: "btn btn-secondary"
10 changes: 10 additions & 0 deletions app/views/admin/users/edit.html.slim
Original file line number Diff line number Diff line change
@@ -3,3 +3,13 @@
= "Edit #{controller_name == "users" ? "applicant" : controller_name.singularize}"

= render 'form', resource: @resource

- if collaborators
.panel.panel-default[data-controller="element-focus"]
.panel-heading id="section-collaborators-header"
h2.panel-title
a.collapsed data-toggle="collapse" data-parent="#user-form-panel" href="#section-collaborators" aria-expanded="false" aria-controls="section-collaborators" data-element-focus-target="reveal"
' Collaborators
#section-collaborators.section-collaborators.panel-collapse.collapse[aria-labelledby="section-collaborators-header" data-element-scroll-target="accordion"]
.panel-body
= render "fields_collaborators", resource: @resource
7 changes: 4 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -263,6 +263,10 @@
patch :unlock
post :scan_via_debounce_api
end

resources :collaborators, only: [:create], module: :users do
get :search, on: :collection
end
end

resources :collaborator_deletion, only: [:destroy]
@@ -351,9 +355,6 @@
resources :case_summaries, only: [:index]
resources :draft_notes, only: [:create, :update]
resources :review_corp_responsibility, only: [:create]
resources :collaborators, only: [:create], module: "form_answers" do
get :search, on: :collection
end
end

resource :settings, only: [:show] do
81 changes: 0 additions & 81 deletions spec/features/admin/form_answers/collaborators_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@

it "can see the edit buttons" do
within ".company-details-forms" do
expect(page).to have_selector("input[type='submit']", count: 13)
expect(page).to have_selector("input[type='submit']", count: 12)
end
end
end
155 changes: 155 additions & 0 deletions spec/features/admin/users/collaborators_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
require "rails_helper"

describe "Collaborators", '
As a an Admin
I want to be able to add collaborators to any account
So that they can collaborate applications
', js: true do
include ActiveJob::TestHelper

let!(:admin) { create(:admin) }
let(:existing_user) { create(:user, :completed_profile) }

before do
login_admin admin
visit edit_admin_user_path(existing_user)
end

describe "Add Collaborator" do
let(:email) { generate(:email) }
let!(:user) { create(:user, email: email) }
let(:form_answers) { [] }

context "with the Account Admin role" do
let(:role) { "Account admin" }

before do
search_for_user(email)
transfer_user(role)
end

it "should transfer the user with the correct role" do
expect(page).to have_content("#{email} successfully added to Collaborators!")
expect(page).to have_content("#{email} successfully added to Collaborators!")
login_as existing_user
click_link "Collaborators"
expect_to_see_user(user, role: "Admin and collaborator")
end
end

context "with the regular role" do
let(:role) { "Regular" }

before do
search_for_user(email)
transfer_user(role)
end

it "should transfer the user with the correct role" do
expect(page).to have_content("#{email} successfully added to Collaborators!")
expect(page).to have_content("#{email} successfully added to Collaborators!")
login_as existing_user
click_link "Collaborators"
expect_to_see_user(user, role: "Collaborator only")
end
end

context "with form_answers" do
let!(:form_answers) { create_list(:form_answer, 3, user: user, account: user.account) }
let(:role) { "Account admin" }
context "when transferring form_answers" do
before do
search_for_user(email)
transfer_user(role, transfer_form_answers: true)
end

it "should transfer the form_answers and user with the correct role" do
expect(page).to have_content("#{email} successfully added to Collaborators!")
login_as existing_user, scope: :user
click_link "Collaborators"
expect_to_see_user(user, role: "Admin and collaborator")
expect(existing_user.account.reload.form_answers).to eq(form_answers)
expect(user.reload.form_answers).to eq(form_answers)
end
end

context "when not transferring form_answers" do
context "with no other users" do
before do
search_for_user(email)
transfer_user(role, transfer_form_answers: false)
end

it "should transfer the user with the correct role" do
expect(page).to have_content("#{email} successfully added to Collaborators!")
login_as existing_user
click_link "Collaborators"
expect_to_see_user(user, role: "Admin and collaborator")
expect(existing_user.account.reload.form_answers).to eq([])
expect(user.reload.form_answers).to eq([])
end
end

context "with other users" do
let!(:other_user) { create(:user, :completed_profile, account: user.account) }

before do
search_for_user(email)
transfer_user(role, transfer_form_answers: false, new_owner: other_user)
end

it "should transfer the user with the correct role" do
expect(page).to have_content("#{email} successfully added to Collaborators!")
login_as existing_user
click_link "Collaborators"
expect_to_see_user(user, role: "Admin and collaborator")
expect(existing_user.account.reload.form_answers).to eq([])
expect(user.reload.form_answers).to eq([])
expect(other_user.reload.form_answers).to eq(form_answers)
end
end

context "when the form_answers are in progress" do
let!(:form_answers) { create_list(:form_answer, 3, :development, user: user, account: user.account) }

before do
search_for_user(email)
transfer_user(role, transfer_form_answers: false)
end

it "should display an error" do
expect(page).to have_content(
"#{user.email} could not be added to Collaborators: User has applications in progress, and there are no other users on the account to transfer them to",
)
end
end
end
end
end

def expect_to_see_user(user, role:)
within "#user_#{user.id}" do
expect(page).to have_content(user.full_name)
expect(page).to have_content(role)
end
end

def transfer_user(role, transfer_form_answers: false, new_owner: nil)
within(".js-admin-search-collaborators-results-box") do
select role, from: "user[role]"
select new_owner.email, from: "user[new_owner_id]" if new_owner
check "user[transfer_form_answers]" if transfer_form_answers

click_button "Transfer"
end
end

def search_for_user(email)
find("a[aria-controls='section-collaborators']").click

within(".admin-search-collaborators-form") do
fill_in "search[query]", with: email.to_s[2..-2]
first("input[type='submit']").click
end
end
end
197 changes: 197 additions & 0 deletions spec/interactors/admin_actions/add_collaborator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
require "rails_helper"

describe AdminActions::AddCollaborator do
describe "#run" do
let(:result) { described_class.new(account:, collaborator:, params:, existing_account:).run }
let(:account) { create(:account) }
let(:collaborator) { create(:user, form_answers: form_answers, account: collaborator_account, role: "regular") }
let(:existing_account) { collaborator.account }
let(:collaborator_account) { create(:account) }
let(:collaborator_account_other_users) { [] }
let(:form_answers) { [] }
let(:params) { { role: role, transfer_form_answers: transfer_form_answers, new_owner_id: new_owner_id } }
let(:role) { "account_admin" }
let(:new_owner_id) { nil }

context "when transfer_form_answers is true" do
let(:transfer_form_answers) { true }

context "when the collaborator has form_answers" do
let(:form_answers) { create_list(:form_answer, 3) }

context "when the collaborator_account has no other users" do
it "transfers the collaborator and form_answers, and the collaborator account is deleted" do
expect(result).to be_success
expect(collaborator.account).to eq(account)
expect(collaborator.role).to eq(role)
expect { collaborator_account.reload }.to raise_error(ActiveRecord::RecordNotFound)
form_answers.each { |f| expect(f.account).to eq(account) }
end
end

context "when the collaborator_account has other users" do
let!(:collaborator_account_other_users) { create_list(:user, 2, account: collaborator_account) }
let(:new_owner_id) { collaborator_account_other_users.first.id }

it "transfers the collaborator and form_answers, and ownership of the collaborator account is transferred" do
expect(result).to be_success
expect(collaborator.account).to eq(account)
expect(collaborator.role).to eq(role)
expect(collaborator_account.owner).to eq(collaborator_account_other_users.first)
expect(collaborator_account.users).not_to include(collaborator)
form_answers.each { |f| expect(f.account).to eq(account) }
end
end
end
end

context "when transfer_form_answers is false" do
let(:transfer_form_answers) { false }

context "when the existing_account is nil" do
let(:existing_account) { nil }

it "transfers the collaborator" do
expect(result).to be_success
expect(collaborator.account).to eq(account)
expect(collaborator.role).to eq(role)
end
end

context "when the collaborator does not have form_answers" do
let(:form_answers) { [] }

it "transfers the collaborator and the collaborator account is deleted" do
expect(result).to be_success
expect(collaborator.account).to eq(account)
expect(collaborator.role).to eq(role)
expect { collaborator_account.reload }.to raise_error(ActiveRecord::RecordNotFound)
end

context "when the collaborator is the owner of the account" do
before do
collaborator_account.owner = collaborator
collaborator_account.save
end

it "transfers the collaborator and the collaborator account is deleted" do
expect(result).to be_success
expect(collaborator.account).to eq(account)
expect(collaborator.role).to eq(role)
expect { collaborator_account.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end

context "when the collaborator_account has other users" do
let!(:collaborator_account_other_users) { create_list(:user, 2, account: collaborator_account) }

it "transfers the collaborator and the collaborator account is not deleted" do
expect(result).to be_success
expect(collaborator.account).to eq(account)
expect(collaborator.role).to eq(role)
expect(collaborator_account.users).to eq(collaborator_account_other_users)
end

context "when the collaborator is the owner of the account" do
let(:new_owner_id) { collaborator_account_other_users.first.id }

before do
collaborator_account.owner = collaborator
collaborator_account.save
end

it "transfers the collaborator and ownership of the collaborator_account" do
expect(result).to be_success
expect(collaborator.account).to eq(account)
expect(collaborator.role).to eq(role)
expect(collaborator_account.owner).to eq(collaborator_account_other_users.first)
expect(collaborator_account.users).not_to include(collaborator)
end

context "when the new_owner_id is not specified" do
let(:new_owner_id) { nil }

it "is not a success" do
expect(result).not_to be_success
expect(result.error_messages)
.to eq("User account has active users, ownership of the account must be transferred")
expect(collaborator.account).to eq(collaborator_account)
end
end

context "when an invalid new_owner_id is specified" do
let(:new_owner_id) { 1000 }

it "is not a success" do
expect(result).not_to be_success
expect(result.error_messages)
.to eq("Validation failed: Owner Owner is empty - it is a required field and must be filled in")
expect(collaborator.reload.account).to eq(collaborator_account)
end
end
end
end
end
context "when the collaborator has form_answers" do
let(:form_answers) { create_list(:form_answer, 3) }

context "when the collaborator_account has no other users" do
it "transfers the collaborator, and the collaborator account and form_answers are deleted" do
expect(result).to be_success
expect(collaborator.account).to eq(account)
expect(collaborator.role).to eq(role)
expect { collaborator_account.reload }.to raise_error(ActiveRecord::RecordNotFound)
form_answers.each { |f| expect { f.reload }.to raise_error(ActiveRecord::RecordNotFound) }
end
end

context "when the form_answers are in progress" do
let(:form_answers) { create_list(:form_answer, 3, :development) }

context "when the collaborator_account has other users" do
let!(:collaborator_account_other_users) { create_list(:user, 2, account: collaborator_account) }

context "when the new_owner_id is specified" do
let(:new_owner_id) { collaborator_account_other_users.first.id }

it "transfers the collaborator and ownership of the form_answers to the new owner" do
expect(result).to be_success
expect(collaborator.account).to eq(account)
expect(collaborator.role).to eq(role)
expect(collaborator_account.owner).to eq(collaborator_account_other_users.first)
form_answers.each { |f| expect(f.user).to eq(collaborator_account_other_users.first) }
expect(collaborator_account.users).not_to include(collaborator)
end
end

context "when the new_owner_id is not specified" do
let(:new_owner_id) { nil }

before do
collaborator_account.owner = collaborator_account_other_users.first
collaborator_account.save
end

it "transfers the collaborator and ownership of the form_answers to the existing owner" do
expect(result).to be_success
expect(collaborator.account).to eq(account)
expect(collaborator.role).to eq(role)
expect(collaborator_account.users).not_to include(collaborator)
form_answers.each { |f| expect(f.user).to eq(collaborator_account_other_users.first) }
end
end
end

context "when the collaborator_account has no other users" do
it "is not a success" do
expect(result).not_to be_success
expect(result.error_messages)
.to eq("User has applications in progress, and there are no other users on the account to transfer them to")
expect(collaborator.account).to eq(collaborator_account)
end
end
end
end
end
end
end
21 changes: 21 additions & 0 deletions spec/models/form_answer_spec.rb
Original file line number Diff line number Diff line change
@@ -9,6 +9,27 @@
target = FormAnswer.submitted.where("feedback_hard_copy_generated" => true).to_sql
expect(target).to eq FormAnswer.hard_copy_generated("feedback").to_sql
end

describe ".touched" do
let!(:eligibility_with_answers) { create(:eligibility, answers: { anything: :something }, type: :trade) }
let!(:form_answer_with_touched_document) { create(:form_answer, document: { anything: :something }) }

before do
create(:form_answer, document: {})
create(:form_answer, document: { organization_name: :anything })
create(:form_answer, document: { company_name: :anything })
create(:eligibility, answers: {}, type: :trade)
end

it "returns only touched form_answers" do
expect(FormAnswer.touched).to match_array(
[
eligibility_with_answers.form_answer,
form_answer_with_touched_document,
],
)
end
end
end

describe "#unsuccessful?" do

Unchanged files with check annotations Beta

And I should see international trade application link
And I should see sustainable development application link
Scenario: I'm able to create innovation form

Check failure on line 12 in spec/acceptance/application_form_creation.feature

GitHub Actions / test

Creating application forms I'm able to create innovation form Given settings with submission deadlines exists -> And I am eligible user -> When I create innovation form -> Then I should see qae form -> And I should see application edit link on dashboard Failure/Error: Date.new(AwardYear.current.year - 1 - to, month + 1, 1) Date::Error: invalid date
When I create innovation form
Then I should see qae form
And I should see application edit link on dashboard
described_class.skip_before_action :check_basic_eligibility, :check_award_eligibility, :check_account_completion, raise: false
end
it "sends email after submission" do

Check failure on line 25 in spec/controllers/form_controller_spec.rb

GitHub Actions / test

FormController sends email after submission Failure/Error: Date.new(AwardYear.current.year - 1 - to, month + 1, 1) Date::Error: invalid date
notifier = double
expect(notifier).to receive(:run)
expect(Notifiers::Submission::SuccessNotifier).to receive(:new).with(form_answer) { notifier }
end
describe "#new_innovation_form" do
it "allows to open innovation form" do

Check failure on line 66 in spec/controllers/form_controller_spec.rb

GitHub Actions / test

FormController#new_innovation_form allows to open innovation form Failure/Error: Date.new(AwardYear.current.year - 1 - to, month + 1, 1) Date::Error: invalid date
response = post(:new_innovation_form, params: { form_answer: { nickname: "Innovation" } })
expect(response).not_to have_http_status(:unprocessable_entity)
end
describe "#new_innovation_form" do
it "allows to create an application if innovation start deadline has past" do

Check failure on line 113 in spec/controllers/form_controller_spec.rb

GitHub Actions / test

FormController individual deadlines #new_innovation_form allows to create an application if innovation start deadline has past Failure/Error: Date.new(AwardYear.current.year - 1 - to, month + 1, 1) Date::Error: invalid date
response = post(:new_innovation_form, params: { form_answer: { nickname: "Innovation" } })
expect(response).to redirect_to(edit_form_url(FormAnswer.where(award_type: "innovation").last))
describe "#add_attachment" do
let(:file) { Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/cat.jpg"), "image/jpeg") }
it "adds attachment to the form answer" do

Check failure on line 129 in spec/controllers/form_controller_spec.rb

GitHub Actions / test

FormController#add_attachment adds attachment to the form answer Failure/Error: Date.new(AwardYear.current.year - 1 - to, month + 1, 1) Date::Error: invalid date
expect {
post :add_attachment, params: {
form: {
end
context "innovation" do
it "process the eligibility form" do

Check failure on line 43 in spec/features/users/eligibility_form_fulfillment_spec.rb

GitHub Actions / test

Eligibility forms innovation process the eligibility form Failure/Error: Date.new(AwardYear.current.year - 1 - to, month + 1, 1) Date::Error: invalid date
visit dashboard_path
new_application("Innovation Award")
fill_in("award-reference", with: "innovation nick")
visit users_form_answer_audit_certificate_url(form_answer, format: :pdf)
end
xit "should generate pdf file" do

Check warning on line 63 in spec/features/users/form_answers/audit_certificate_request_spec.rb

GitHub Actions / test

Verification of Commercial Figures As a User I want to be able to download an Verification of Commercial Figures So that I can check, complete it and then upload it to application Download Verification of Commercial Figures prefilled with my financial data should generate pdf file Skipped: Temporarily skipped with xit
expect(page.status_code).to eq(200)
expect(page.response_headers["Content-Disposition"]).to include(
"attachment; filename=\"#{CGI.escape(audit_certificate_filename)}\"",
require "rails_helper"
RSpec.describe AuditLog, type: :model do
pending "add some examples to (or delete) #{__FILE__}"

Check warning on line 4 in spec/models/audit_log_spec.rb

GitHub Actions / test

AuditLog add some examples to (or delete) /home/runner/work/qae/qae/spec/models/audit_log_spec.rb Skipped: Not yet implemented
end
end
describe "PDF generation" do
it "should include main header information" do

Check failure on line 58 in spec/support/shared_contexts/pdf_file_checks.rb

GitHub Actions / test

QaePdfForms::Awards2016::Innovation::Base PDF generation should include main header information Failure/Error: Date.new(AwardYear.current.year - 1 - to, month + 1, 1) Date::Error: invalid date Shared Example Group: "pdf file checks" called from ./spec/unit/pdf_generators/qae_pdf_forms/awards2014/innovation/base_spec.rb:5
expect(pdf_content.join(" ")).to match(award_application_title)
expect(pdf_content).to include(match_name_condition)
expect(pdf_content).to include(form_urn)
end
it "should include steps headers" do

Check failure on line 64 in spec/support/shared_contexts/pdf_file_checks.rb

GitHub Actions / test

QaePdfForms::Awards2016::Innovation::Base PDF generation should include steps headers Failure/Error: Date.new(AwardYear.current.year - 1 - to, month + 1, 1) Date::Error: invalid date Shared Example Group: "pdf file checks" called from ./spec/unit/pdf_generators/qae_pdf_forms/awards2014/innovation/base_spec.rb:5
steps.each do |step|
title = "#{step.title.upcase}:"
end
end
it "should include answers with question titles" do

Check failure on line 80 in spec/support/shared_contexts/pdf_file_checks.rb

GitHub Actions / test

QaePdfForms::Awards2016::Innovation::Base PDF generation should include answers with question titles Failure/Error: Date.new(AwardYear.current.year - 1 - to, month + 1, 1) Date::Error: invalid date Shared Example Group: "pdf file checks" called from ./spec/unit/pdf_generators/qae_pdf_forms/awards2014/innovation/base_spec.rb:5
step1_question_answers.each do |question_key, question_answer|
question = fetch_question_by_question_key(step1.questions, question_key)