From 882a1de227a04607043d42d6dbb6b121355c09e6 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Fri, 15 Oct 2021 11:52:50 +0100 Subject: [PATCH] Test case log state machine --- app/models/case_log.rb | 4 ++-- spec/models/case_log_spec.rb | 17 +++++++++++++++++ spec/requests/case_log_controller_spec.rb | 13 +++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index efcf09f92..30a2b3980 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -51,11 +51,11 @@ class CaseLog < ApplicationRecord end def not_started? - status == "not started" + status == "not_started" end def in_progress? - status == "in progress" + status == "in_progress" end private diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 86f4e9d9b..b456e6988 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -14,4 +14,21 @@ RSpec.describe Form, type: :model do expect { CaseLog.create!(tenant_age: 0) }.to raise_error(ActiveRecord::RecordInvalid) end end + + describe "status" do + let!(:empty_case_log) { FactoryBot.create(:case_log) } + let!(:in_progress_case_log) { FactoryBot.create(:case_log, :in_progress) } + + it "is set to not started for an empty case log" do + expect(empty_case_log.not_started?).to be(true) + expect(empty_case_log.in_progress?).to be(false) + expect(empty_case_log.completed?).to be(false) + end + + it "is set to not started for an empty case log" do + expect(in_progress_case_log.in_progress?).to be(true) + expect(in_progress_case_log.not_started?).to be(false) + expect(in_progress_case_log.completed?).to be(false) + end + end end diff --git a/spec/requests/case_log_controller_spec.rb b/spec/requests/case_log_controller_spec.rb index 0cdf5d791..a1c552a52 100644 --- a/spec/requests/case_log_controller_spec.rb +++ b/spec/requests/case_log_controller_spec.rb @@ -127,6 +127,19 @@ RSpec.describe CaseLogsController, type: :request do end end + context "invalid case log params" do + let(:params) { {tenant_age: 200} } + + it "returns 422" do + expect(response).to have_http_status(:unprocessable_entity) + end + + it "returns an error message" do + json_response = JSON.parse(response.body) + expect(json_response["errors"]).to eq(["Tenant age must be between 0 and 120"]) + end + end + context "request with invalid credentials" do let(:basic_credentials) do ActionController::HttpAuthentication::Basic.encode_credentials(api_username, "Oops")