|
|
@ -34,84 +34,104 @@ RSpec.describe CaseLogsController, type: :request do |
|
|
|
let(:in_progress) { "in_progress" } |
|
|
|
let(:in_progress) { "in_progress" } |
|
|
|
let(:completed) { "completed" } |
|
|
|
let(:completed) { "completed" } |
|
|
|
|
|
|
|
|
|
|
|
let(:params) do |
|
|
|
context "when API" do |
|
|
|
{ |
|
|
|
let(:params) do |
|
|
|
"owning_organisation_id": owning_organisation.id, |
|
|
|
{ |
|
|
|
"managing_organisation_id": managing_organisation.id, |
|
|
|
"owning_organisation_id": owning_organisation.id, |
|
|
|
"tenant_code": tenant_code, |
|
|
|
"managing_organisation_id": managing_organisation.id, |
|
|
|
"age1": age1, |
|
|
|
"tenant_code": tenant_code, |
|
|
|
"property_postcode": property_postcode, |
|
|
|
"age1": age1, |
|
|
|
"offered": offered, |
|
|
|
"property_postcode": property_postcode, |
|
|
|
} |
|
|
|
"offered": offered, |
|
|
|
end |
|
|
|
} |
|
|
|
|
|
|
|
end |
|
|
|
before do |
|
|
|
|
|
|
|
post "/logs", headers: headers, params: params.to_json |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it "returns http success" do |
|
|
|
|
|
|
|
expect(response).to have_http_status(:success) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it "returns a serialized Case Log" do |
|
|
|
|
|
|
|
json_response = JSON.parse(response.body) |
|
|
|
|
|
|
|
expect(json_response.keys).to match_array(CaseLog.new.attributes.keys) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it "creates a case log with the values passed" do |
|
|
|
before do |
|
|
|
json_response = JSON.parse(response.body) |
|
|
|
post "/logs", headers: headers, params: params.to_json |
|
|
|
expect(json_response["tenant_code"]).to eq(tenant_code) |
|
|
|
end |
|
|
|
expect(json_response["age1"]).to eq(age1) |
|
|
|
|
|
|
|
expect(json_response["property_postcode"]).to eq(property_postcode) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context "with invalid json parameters" do |
|
|
|
it "returns http success" do |
|
|
|
let(:age1) { 2000 } |
|
|
|
expect(response).to have_http_status(:success) |
|
|
|
let(:offered) { 21 } |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
it "validates case log parameters" do |
|
|
|
it "returns a serialized Case Log" do |
|
|
|
json_response = JSON.parse(response.body) |
|
|
|
json_response = JSON.parse(response.body) |
|
|
|
expect(response).to have_http_status(:unprocessable_entity) |
|
|
|
expect(json_response.keys).to match_array(CaseLog.new.attributes.keys) |
|
|
|
expect(json_response["errors"]).to match_array([["offered", [I18n.t("validations.property.offered.relet_number")]], ["age1", [I18n.t("validations.household.age.must_be_valid", lower_bound: 16)]]]) |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context "with a partial case log submission" do |
|
|
|
it "creates a case log with the values passed" do |
|
|
|
it "marks the record as in_progress" do |
|
|
|
|
|
|
|
json_response = JSON.parse(response.body) |
|
|
|
json_response = JSON.parse(response.body) |
|
|
|
expect(json_response["status"]).to eq(in_progress) |
|
|
|
expect(json_response["tenant_code"]).to eq(tenant_code) |
|
|
|
|
|
|
|
expect(json_response["age1"]).to eq(age1) |
|
|
|
|
|
|
|
expect(json_response["property_postcode"]).to eq(property_postcode) |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context "with a complete case log submission" do |
|
|
|
context "with invalid json parameters" do |
|
|
|
let(:org_params) do |
|
|
|
let(:age1) { 2000 } |
|
|
|
{ |
|
|
|
let(:offered) { 21 } |
|
|
|
"case_log" => { |
|
|
|
|
|
|
|
"owning_organisation_id" => owning_organisation.id, |
|
|
|
it "validates case log parameters" do |
|
|
|
"managing_organisation_id" => managing_organisation.id, |
|
|
|
json_response = JSON.parse(response.body) |
|
|
|
}, |
|
|
|
expect(response).to have_http_status(:unprocessable_entity) |
|
|
|
} |
|
|
|
expect(json_response["errors"]).to match_array([["offered", [I18n.t("validations.property.offered.relet_number")]], ["age1", [I18n.t("validations.household.age.must_be_valid", lower_bound: 16)]]]) |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
let(:case_log_params) { JSON.parse(File.open("spec/fixtures/complete_case_log.json").read) } |
|
|
|
|
|
|
|
let(:params) do |
|
|
|
context "with a partial case log submission" do |
|
|
|
case_log_params.merge(org_params) { |_k, a_val, b_val| a_val.merge(b_val) } |
|
|
|
it "marks the record as in_progress" do |
|
|
|
|
|
|
|
json_response = JSON.parse(response.body) |
|
|
|
|
|
|
|
expect(json_response["status"]).to eq(in_progress) |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
it "marks the record as completed" do |
|
|
|
context "with a complete case log submission" do |
|
|
|
json_response = JSON.parse(response.body) |
|
|
|
let(:org_params) do |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
"case_log" => { |
|
|
|
|
|
|
|
"owning_organisation_id" => owning_organisation.id, |
|
|
|
|
|
|
|
"managing_organisation_id" => managing_organisation.id, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
let(:case_log_params) { JSON.parse(File.open("spec/fixtures/complete_case_log.json").read) } |
|
|
|
|
|
|
|
let(:params) do |
|
|
|
|
|
|
|
case_log_params.merge(org_params) { |_k, a_val, b_val| a_val.merge(b_val) } |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it "marks the record as completed" do |
|
|
|
|
|
|
|
json_response = JSON.parse(response.body) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expect(json_response).not_to have_key("errors") |
|
|
|
|
|
|
|
expect(json_response["status"]).to eq(completed) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context "with a request containing invalid credentials" do |
|
|
|
|
|
|
|
let(:basic_credentials) do |
|
|
|
|
|
|
|
ActionController::HttpAuthentication::Basic.encode_credentials(api_username, "Oops") |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
expect(json_response).not_to have_key("errors") |
|
|
|
it "returns 401" do |
|
|
|
expect(json_response["status"]).to eq(completed) |
|
|
|
expect(response).to have_http_status(:unauthorized) |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
context "with a request containing invalid credentials" do |
|
|
|
context "when UI" do |
|
|
|
let(:basic_credentials) do |
|
|
|
let(:user) { FactoryBot.create(:user) } |
|
|
|
ActionController::HttpAuthentication::Basic.encode_credentials(api_username, "Oops") |
|
|
|
let(:headers) { { "Accept" => "text/html" } } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
before do |
|
|
|
|
|
|
|
RequestHelper.stub_http_requests |
|
|
|
|
|
|
|
sign_in user |
|
|
|
|
|
|
|
post "/logs", headers: headers |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
it "returns 401" do |
|
|
|
it "tracks who created the record" do |
|
|
|
expect(response).to have_http_status(:unauthorized) |
|
|
|
created_id = response.location.match(/[0-9]+/)[0] |
|
|
|
|
|
|
|
whodunnit_actor = CaseLog.find_by(id: created_id).versions.last.actor |
|
|
|
|
|
|
|
expect(whodunnit_actor).to be_a(User) |
|
|
|
|
|
|
|
expect(whodunnit_actor.id).to eq(user.id) |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
@ -401,9 +421,8 @@ RSpec.describe CaseLogsController, type: :request do |
|
|
|
expect(response).to have_http_status(:success) |
|
|
|
expect(response).to have_http_status(:success) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
it "soft deletes the case log" do |
|
|
|
it "deletes the case log" do |
|
|
|
expect { CaseLog.find(id) }.to raise_error(ActiveRecord::RecordNotFound) |
|
|
|
expect { CaseLog.find(id) }.to raise_error(ActiveRecord::RecordNotFound) |
|
|
|
expect(CaseLog.with_discarded.find(id)).to be_a(CaseLog) |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
context "with an invalid case log id" do |
|
|
|
context "with an invalid case log id" do |
|
|
@ -428,7 +447,7 @@ RSpec.describe CaseLogsController, type: :request do |
|
|
|
context "when a case log deletion fails" do |
|
|
|
context "when a case log deletion fails" do |
|
|
|
before do |
|
|
|
before do |
|
|
|
allow(CaseLog).to receive(:find_by).and_return(case_log) |
|
|
|
allow(CaseLog).to receive(:find_by).and_return(case_log) |
|
|
|
allow(case_log).to receive(:discard).and_return(false) |
|
|
|
allow(case_log).to receive(:delete).and_return(false) |
|
|
|
delete "/logs/#{id}", headers: headers |
|
|
|
delete "/logs/#{id}", headers: headers |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|