Skip to content

Commit

Permalink
health_spec test includes coverage for negative scenarios, modified t…
Browse files Browse the repository at this point in the history
…he index of health_controller
  • Loading branch information
tomasdrga committed Jan 23, 2024
1 parent e9f0089 commit 9a6dce8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
10 changes: 5 additions & 5 deletions app/controllers/health_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ class HealthController < ApplicationController
def index
database_check_results = check_db_connections
if all_database_healthy?(database_check_results)
render json: {status: "ok", databases: database_check_results}, status: :ok
render json: {status: "ok"}, status: :ok
else
render json: {status: "error", databases: database_check_results}, status: :internal_server_error
render json: {status: "fail"}, status: :internal_server_error
end
rescue StandardError
render json: {status: "fail"}, status: :internal_server_error
end

private
Expand Down Expand Up @@ -36,9 +38,7 @@ def perform_health_check(connection)
connection.execute('SELECT 1')
{status: 'ok'}
else
{status: 'error', message: 'Connection not established'}
{status: 'fail'}
end
rescue => e
{status: 'error', message: e.message}
end
end
49 changes: 42 additions & 7 deletions spec/features/health_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
require 'rails_helper'

RSpec.feature "Healthcheck", type: :feature do
scenario 'As a healthcheck bot I want to check if everything is ok' do
visit health_path
context 'when healthcheck passes' do
scenario 'With healthcheck I want to check if everything is ok' do
visit health_path

expect(page.status_code).to be(200)
expect(page.status_code).to be(200)

returned_data = JSON.parse(page.body)
returned_data = JSON.parse(page.body)

expect(returned_data["status"]).to eq("ok")
expect(returned_data["databases"]["primary"]).to include("status" => "ok")
expect(returned_data["databases"]["datahub"]).to include("status" => "ok")
expect(returned_data["status"]).to eq("ok")
end
end

context 'when healthcheck fails' do
scenario 'unexpected error in health check' do
allow_any_instance_of(HealthController).to receive(:all_database_healthy?).and_raise(StandardError, 'Unexpected error')

visit health_path

expect(page.status_code).to be(500)
returned_data = JSON.parse(page.body)

expect(returned_data["status"]).to eq("fail")
end

scenario 'primary database fails' do
allow(ApplicationRecord.connection).to receive(:execute).and_raise(StandardError, 'Something went wrong in primary db')

visit health_path

expect(page.status_code).to be(500)
returned_data = JSON.parse(page.body)

expect(returned_data["status"]).to eq("fail")
end

scenario 'datahub database fails' do
allow(DatahubRecord.connection).to receive(:execute).and_raise(StandardError, 'Something went wrong in datahub db')

visit health_path

expect(page.status_code).to be(500)
returned_data = JSON.parse(page.body)

expect(returned_data["status"]).to eq("fail")
end
end
end

0 comments on commit 9a6dce8

Please sign in to comment.