docbrown/spec/requests/api/v1/health_checks_spec.rb
Anna Buianova 7d053471f8
Rubocop fixes in spec/requests (#18946)
* Rubocop fixes in spec/requests

* Fixed spec/requests/api/v0/articles_spec.rb

Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>

Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>
2023-01-12 19:10:16 +03:00

59 lines
2.3 KiB
Ruby

require "rails_helper"
RSpec.describe "HealthCheck" do
let(:token) { "secret" }
let(:headers) { { "health-check-token" => token, "Accept" => "application/vnd.forem.api-v1+json" } }
before { allow(Settings::General).to receive(:health_check_token).and_return(token) }
context "without a token" do
it "returns an unauthorized request" do
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_addr).and_return("0.0.0.0") # rubocop:disable RSpec/AnyInstance
get app_api_health_checks_path
expect(response).to have_http_status(:unauthorized)
end
end
describe "GET /api/health_checks/app" do
it "returns json success" do
get app_api_health_checks_path, headers: headers
expect(response).to have_http_status(:ok)
expect(response.parsed_body["message"]).to eq("App is up!")
end
end
describe "GET /api/health_checks/database" do
it "returns json success if connection check succeeds" do
get database_api_health_checks_path, headers: headers
expect(response).to have_http_status(:ok)
expect(response.parsed_body["message"]).to eq("Database connected")
end
it "returns json failure if connection check fails" do
allow(ActiveRecord::Base).to receive(:connected?).and_return(false)
get database_api_health_checks_path, headers: headers
expect(response).to have_http_status(:internal_server_error)
expect(response.parsed_body["message"]).to eq("Database NOT connected!")
end
end
describe "GET /api/health_checks/cache" do
it "returns json success if connection check succeeds" do
get cache_api_health_checks_path, headers: headers
expect(response).to have_http_status(:ok)
expect(response.parsed_body["message"]).to eq("Redis connected")
end
it "returns json failure if connection check fails" do
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with("REDIS_SESSIONS_URL").and_return("redis://redis:6379")
redis_obj = Redis.new
allow(Redis).to receive(:new).and_return(redis_obj)
allow(redis_obj).to receive(:ping).and_return("fail")
get cache_api_health_checks_path, headers: headers
expect(response).to have_http_status(:internal_server_error)
expect(response.parsed_body["message"]).to eq("Redis NOT connected!")
end
end
end