Skip to content

Commit

Permalink
Merge pull request #28 from alphagov/tests
Browse files Browse the repository at this point in the history
Move to RSpec
  • Loading branch information
csutter authored Dec 1, 2023
2 parents 2cd9722 + 7dcabb6 commit 1e4e51a
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 67 deletions.
29 changes: 10 additions & 19 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,20 @@ jobs:
uses: alphagov/govuk-infrastructure/.github/workflows/rubocop.yml@main

test-ruby:
name: Test Rails
name: Test Ruby
runs-on: ubuntu-latest
env:
# As we're running the tests through Rake, we need to make sure they are run in the `test`
# Rails env rather than `development`
RAILS_ENV: test
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Ruby
uses: ruby/setup-ruby@v1
- uses: actions/checkout@v4
with:
ref: ${{ inputs.ref || github.ref }}
- uses: ruby/setup-ruby@v1
with:
bundler-cache: true

- name: Precompile assets
uses: alphagov/govuk-infrastructure/.github/actions/precompile-rails-assets@main

- name: Run unit tests
env:
RAILS_ENV: test
run: bin/rails test

- name: Run system tests
env:
RAILS_ENV: test
run: bin/rails test:system
- run: bin/rake

security-analysis:
name: Security Analysis
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@
/config/master.key

/node_modules

/spec/examples.txt
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ gem "sprockets-rails"
group :development, :test do
gem "brakeman", require: false
gem "debug", platforms: %i[mri windows]
gem "rspec-rails"
gem "rubocop-govuk"
end

Expand Down
19 changes: 19 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ GEM
irb (>= 1.5.0)
reline (>= 0.3.1)
declarative (0.0.20)
diff-lcs (1.5.0)
domain_name (0.6.20231109)
drb (2.2.0)
ruby2_keywords
Expand Down Expand Up @@ -498,6 +499,23 @@ GEM
retriable (3.1.2)
rexml (3.2.6)
rouge (4.2.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-rails (6.1.0)
actionpack (>= 6.1)
activesupport (>= 6.1)
railties (>= 6.1)
rspec-core (~> 3.12)
rspec-expectations (~> 3.12)
rspec-mocks (~> 3.12)
rspec-support (~> 3.12)
rspec-support (3.12.1)
rubocop (1.55.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
Expand Down Expand Up @@ -601,6 +619,7 @@ DEPENDENCIES
govuk_publishing_components
importmap-rails
rails (~> 7.1.2)
rspec-rails
rubocop-govuk
sassc-rails
selenium-webdriver
Expand Down
43 changes: 43 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "spec_helper"
ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
abort("The Rails environment is running in production mode!") if Rails.env.production?
require "rspec/rails"

# We're using rack-test for simplicity, but we need to be able to find fields in <details> elements
Capybara.ignore_hidden_elements = false

RSpec.configure do |config|
config.before(:each, type: :system) do
# We don't have JS, so rack_test will suffice
driven_by :rack_test
end

config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.shared_context_metadata_behavior = :apply_to_host_groups

config.filter_run_when_matching :focus
config.disable_monkey_patching!
config.example_status_persistence_file_path = "spec/examples.txt"
config.order = :random
Kernel.srand config.seed

config.use_active_record = false

config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!

if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = "doc"
end
end
155 changes: 155 additions & 0 deletions spec/system/feedback_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
require "google/cloud/bigquery"

RSpec.describe "Submitting feedback", type: :system do
let(:search_service) { double("Search service", search:) }
let(:search) do
{ "results" => [
{ "content_id" => "3fc2d1a1-bc9b-4875-899a-54a1fd968175",
"title" => "GDS blog",
"link" => "https://gds.blog.gov.uk/",
"content_purpose_supergroup" => "other",
"_id" => "3fc2d1a1-bc9b-4875-899a-54a1fd968175",
"description_with_highlighting" =>
"The Government Digital Service (GDS) blog about how the government is transforming digital public services.",
"format" => "external_content",
"public_timestamp" => "2017-12-21T09:45:01+00:00",
"content_store_document_type" => "external_content",
"is_historic" => false },
{ "content_id" => "af07d5a5-df63-4ddc-9383-6a666845ebe9",
"title" => "Government Digital Service",
"link" => "/government/organisations/government-digital-service",
"content_purpose_supergroup" => "other",
"_id" => "/government/organisations/government-digital-service",
"description_with_highlighting" =>
"We are here to make digital government simpler, clearer and faster for everyone. GDS is part of the Cabinet Office .",
"format" => "organisation",
"public_timestamp" => "2023-08-08T17:10:39+01:00",
"content_store_document_type" => "organisation",
"is_historic" => false },
{ "content_id" => "27f58621-a0d0-44fc-8c3a-4548245c6079",
"title" => "Government Digital Service IT platform privacy notice",
"link" => "/government/publications/government-digital-service-it-platform-privacy-notice",
"content_purpose_supergroup" => "guidance_and_regulation",
"government_name" => "2015 Conservative government",
"_id" => "/government/publications/government-digital-service-it-platform-privacy-notice",
"description_with_highlighting" =>
"Find details here about the privacy policy for the Government Digital Service's internal IT platform, and how we collect and process your data. ",
"format" => "guidance",
"public_timestamp" => "2022-11-18T12:00:04+00:00",
"content_store_document_type" => "guidance",
"is_historic" => false },
],
"total" => 28_766,
"start" => 0 }
end

let(:bigquery) { double("BigQuery client", dataset: bigquery_dataset) }
let(:bigquery_dataset) { double("BigQuery dataset") }
let(:bigquery_table) { double("BigQuery table", insert: nil) }

before do
allow(Plek).to receive(:find).with("search-api-v2").and_return("https://example.com/v2")
allow(GdsApi::SearchApiV2).to receive(:new).with("https://example.com/v2")
.and_return(search_service)
allow(Google::Cloud::Bigquery).to receive(:new).and_return(bigquery)
allow(bigquery).to receive(:dataset).with("search_feedback").and_return(bigquery_dataset)
allow(bigquery_dataset).to receive(:table).with("evaluator_ratings").and_return(bigquery_table)
end

scenario "performing a basic search and submitting result-level feedback" do
perform_search

# Makes the right query to the search API v2
expect(search_service).to have_received(:search).with(count: 5, q: "government digital service")

# Renders the proper results
expect(page).to have_content("Showing top 3 results (out of 28,766)")

# Renders external and internal links properly
expect(page).to have_link("GDS blog", href: "https://gds.blog.gov.uk/")
expect(page).to have_link("Government Digital Service", href: "http://www.dev.gov.uk/government/organisations/government-digital-service")
end

scenario "submitting feedback" do
perform_search

# Allows the user to submit feedback for results (and leave some blank)
within(all(".search-results__ranking")[0]) { choose "Perfect" }
within(all(".search-results__ranking")[2]) { choose "Bad" }
click_on "Submit feedback"

# Sends the right data to Bigquery
expect(bigquery_table).to have_received(:insert).with(
{
anonymised_user_id: "not-yet-available",
comments: "",
query_id: anything,
result_ratings: [{ content_id: "3fc2d1a1-bc9b-4875-899a-54a1fd968175",
position: 1,
rating: 3,
url: "https://gds.blog.gov.uk/" },
{ content_id: "af07d5a5-df63-4ddc-9383-6a666845ebe9",
position: 2,
rating: nil,
url: "http://www.dev.gov.uk/government/organisations/government-digital-service" },
{ content_id: "27f58621-a0d0-44fc-8c3a-4548245c6079",
position: 3,
rating: 0,
url: "http://www.dev.gov.uk/government/publications/government-digital-service-it-platform-privacy-notice" }],
search_query: "government digital service",
suggested_url: "",
timestamp: a_string_matching(Time.zone.now.strftime("%Y-%m-%d")),
},
)

# Shows a nice message
expect(page).to have_content("Thank you for your feedback")
end

scenario "submitting feedback with a suggested URL and comments" do
perform_search

# Allows the user to give a suggested URL
fill_in "URL for the best possible result", with: "https://www.gov.uk/"

# Allows the user to give comments
fill_in "Your comments about these results", with: "Looking good!"

click_on "Submit feedback"

expect(bigquery_table).to have_received(:insert).with(
hash_including(
{
comments: "Looking good!",
suggested_url: "https://www.gov.uk/",
},
),
)
end

scenario "submitting feedback with an invalid suggested URL fails but doesn't lose input" do
perform_search

fill_in "URL for the best possible result", with: "bad url"
within(all(".search-results__ranking")[1]) { choose "Good" }
click_on "Submit feedback"

# Doesn't send the data to Bigquery
expect(bigquery_table).not_to have_received(:insert)

# Shows an error message
expect(page).to have_content("URL for the best possible result must be a valid URL")

# Doesn't lose the input
within(all(".search-results__ranking")[1]) do
expect(page).to have_checked_field("Good")
end
expect(page).to have_field("URL for the best possible result", with: "bad url")
end

def perform_search
visit "/"
fill_in "Search GOV.UK using the new search engine", with: "government digital service"
click_on "Search"
end
end
5 changes: 0 additions & 5 deletions test/application_system_test_case.rb

This file was deleted.

Empty file removed test/fixtures/files/.keep
Empty file.
Empty file removed test/integration/.keep
Empty file.
Empty file removed test/system/.keep
Empty file.
20 changes: 0 additions & 20 deletions test/system/submitting_feedback_test.rb

This file was deleted.

23 changes: 0 additions & 23 deletions test/test_helper.rb

This file was deleted.

0 comments on commit 1e4e51a

Please sign in to comment.