Skip to content

Commit

Permalink
added retired_at exclusion from search query; refactored spec and add…
Browse files Browse the repository at this point in the history
…ed test
  • Loading branch information
tahir-khalid committed Sep 24, 2024
1 parent d8ac8e1 commit 85a616f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 22 deletions.
18 changes: 11 additions & 7 deletions app/controllers/notifications/create_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,27 @@ def show
}[params[:sort_by]] || { created_at: :desc }

products = if @page_name == "your_products"
Product.includes(investigations: %i[owner_user owner_team])
Product.not_retired
.includes(investigations: %i[owner_user owner_team])
.where(users: { id: current_user.id })
.order(sort_by)
elsif @page_name == "team_products"
team = current_user.team
Product.includes(investigations: %i[owner_user owner_team])
Product.not_retired
.includes(investigations: %i[owner_user owner_team])
.where(users: { id: team.users.map(&:id) }, teams: { id: team.id })
.order(sort_by)
elsif @search_query
@search_query.strip!
Product.where("products.name ILIKE ?", "%#{@search_query}%")
.or(Product.where("products.description ILIKE ?", "%#{@search_query}%"))
.or(Product.where("CONCAT('psd-', products.id) = LOWER(?)", @search_query))
.or(Product.where(id: @search_query))
Product.not_retired
.where("products.name ILIKE ?", "%#{@search_query}%")
.or(Product.not_retired.where("products.description ILIKE ?", "%#{@search_query}%"))
.or(Product.not_retired.where("CONCAT('psd-', products.id) = LOWER(?)", @search_query))
.or(Product.not_retired.where(id: @search_query))
.order(sort_by)
else
Product.all.order(sort_by)
Product.not_retired
.all.order(sort_by)
end

@records_count = products.size
Expand Down
91 changes: 76 additions & 15 deletions spec/features/search_for_or_add_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
require "rails_helper"

RSpec.feature "Add notification with search", :with_opensearch, :with_product_form_helper, :with_stubbed_antivirus, :with_stubbed_mailer do
RSpec.feature "Search for or add a product", :with_opensearch, :with_product_form_helper, :with_stubbed_antivirus, :with_stubbed_mailer do
let(:user) { create(:user, :opss_user, :activated, has_viewed_introduction: true, roles: %w[notification_task_list_user]) }
let(:existing_product) { create(:product) }
let(:new_product_attributes) do
attributes_for(:product_iphone, authenticity: Product.authenticities.keys.without("missing", "unsure").sample)
end
let(:image_file) { Rails.root.join "test/fixtures/files/testImage.png" }
let(:text_file) { Rails.root.join "test/fixtures/files/attachment_filename.txt" }
let(:product_one) { create(:product) }
let(:product_two) { create(:product) }
let(:retired_product) { create(:product) }

before do
sign_in(user)

existing_product
product_one
product_two
retired_product
end

scenario "Creating a notification with the normal flow search for and add a product" do
Expand All @@ -35,9 +33,11 @@
click_button "Continue"
end

scenario "Creating a notification with the normal flow search for and add multiple products" do
scenario "Creating a notification add existing product and a product that does not exist" do
visit "/notifications/create"

set_retired(retired_product)

expect(page).to have_current_path(/\/notifications\/\d{4}-\d{4}\/create/)
expect(page).to have_content("Create a product safety notification")
expect(page).to have_selector(:id, "task-list-0-0-status", text: "Not yet started")
Expand All @@ -52,17 +52,78 @@
click_button "Continue"

expect(page).to have_content("Choose the product for your notification")

expect(page).to have_content("Search by product name, description or PSD reference")

expect(page).to have_content("Product details")

find_field("q-field").fill_in(with: "ABC123")

find('button.govuk-button[type="submit"][formnovalidate="formnovalidate"]').click
first('button.govuk-button[type="submit"][formnovalidate="formnovalidate"]').click

expect(page).to have_content("There are no product records")

# TODO: figure out how to go back to the previous screen "Search for or add a product"
visit "/notifications/your-notifications"

expect(page).to have_content("Draft notifications")

click_link "Make changes"

expect(page).to have_content("Create a product safety notification")

expect_page_to_have_product_name
end

scenario "Creating a notification add existing product with one retired product" do
set_retired(retired_product)

visit "/notifications/create"

expect(page).to have_current_path(/\/notifications\/\d{4}-\d{4}\/create/)
expect(page).to have_content("Create a product safety notification")
expect(page).to have_selector(:id, "task-list-0-0-status", text: "Not yet started")

click_link "Search for or add a product"
click_button "Select", match: :first

within_fieldset "Do you need to add another product?" do
choose "Yes"
end

click_button "Continue"

expect(page).to have_content("Choose the product for your notification")
expect(page).to have_content("Search by product name, description or PSD reference")
expect(page).to have_content("Product details")

find_field("q-field").fill_in(with: retired_product.id)
first('button.govuk-button[type="submit"][formnovalidate="formnovalidate"]').click

expect(page).to have_content("There are no product records for \"#{retired_product.id}\"")

visit "/notifications/your-notifications"

expect(page).to have_content("Draft notifications")

click_link "Make changes"

expect(page).to have_content("Create a product safety notification")

expect_page_to_have_product_name
end

private

def set_retired(product)
if product.present?
product.retired_at = Time.zone.now
product.save!
end
end

def expect_page_to_have_product_name
if page.has_text?(product_one.name)
expect(page).to have_content(product.name)
else
page.has_text?(product_two.name)
expect(page).to have_content(product_two.name)
end
end
end

0 comments on commit 85a616f

Please sign in to comment.