Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Commit

Permalink
Add validation for Display Id format
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgtpluck committed Nov 10, 2021
1 parent 53f8d72 commit 7b55b59
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 11 deletions.
8 changes: 8 additions & 0 deletions app/models/tta_activity_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class TtaActivityReport
include ActiveModel::Validations
validate :check_display_id_format
validate :api_call_succeeded

attr_accessor :access_token, :display_id
Expand Down Expand Up @@ -38,6 +39,7 @@ def api_error_message
private

def api_call_succeeded
return unless errors.blank?
errors.add(:base, tta_link_error_message) if activity_data.failed?
end

Expand All @@ -57,6 +59,12 @@ def api
ApiDelegator.use("tta", "activity_report", display_id: display_id, access_token: access_token)
end

def check_display_id_format
unless display_id.match?(/^R\d{2}-AR-/)
errors.add(:base, "This doesn't look like a TTA Activity Report Display ID. Please double check the format.")
end
end

def tta_link_error_message
case activity_data.code
when 403
Expand Down
2 changes: 1 addition & 1 deletion spec/features/complaints/tta_activity_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
expires: true
}
}
let(:test_display_id) { "Test-Display-ID" }
let(:test_display_id) { "R12-AR-1234" }

let!(:complaint) { Complaint.new(FakeIssues.instance.data.first) }

Expand Down
2 changes: 1 addition & 1 deletion spec/models/api/fake_data/activity_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Api::FakeData::ActivityReport do
describe "ActivityReport" do
let(:display_id) { "RO4-VQ-14661" }
let(:display_id) { "RO4-AR-14661" }
let(:report) { Api::FakeData::ActivityReport.new(display_id: display_id) }

describe "#init" do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/api/fake_data/tta_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

describe "#request" do
context "successful request" do
let(:display_id) { "RO4-VQ-14661" }
let(:display_id) { "RO4-AR-14661" }

it "returns an activity report with the right display_id" do
expect(subject.request.data[:attributes][:displayId]).to eq display_id
Expand Down
2 changes: 1 addition & 1 deletion spec/models/timeline/tta_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Timeline::TtaEvent do
let(:date) { 1.day.ago.strftime("%F") }
let(:display_id) { "Test-Display-Name" }
let(:display_id) { "R12-AR-532413" }
# just setting the start_date manually because we aren't triggering the before_validation callback
let(:event_param) { IssueTtaReport.new tta_report_display_id: display_id, start_date: date }
let(:tta_activity_report) { event_param.tta_activity_report }
Expand Down
4 changes: 2 additions & 2 deletions spec/models/timeline_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "rails_helper"

RSpec.describe Timeline do
let(:tta_display_id) { "First-Display-ID" }
let(:tta_display_id_2) { "Second-Display-ID" }
let(:tta_display_id) { "R09-AR-First-ID" }
let(:tta_display_id_2) { "R09-AR-Second-ID" }
let(:review_id) { "First-Review-ID" }
let(:review_id_2) { "Second-Review-ID" }
let(:tta_activity_report) { IssueTtaReport.create tta_report_display_id: tta_display_id, issue_id: "1" }
Expand Down
11 changes: 6 additions & 5 deletions spec/requests/issue_tta_reports_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

RSpec.describe "IssueTtaReports", type: :request do
let(:complaint_id) { FakeIssues.instance.data.first[:id] }
let(:tta_display_id) { "RO4-VQ-14661" }
let(:tta_display_id) { "R04-AR-14661" }
let(:user) {
{
name: "Request Spec",
Expand Down Expand Up @@ -79,6 +79,7 @@
end

context "with an authorized user" do
let(:new_display_id) { "R04-AR-909090" }
it "sends an API request to the tta system" do
expect(ApiDelegator).to receive(:use)
.with("tta", "activity_report", {display_id: tta_display_id, access_token: kind_of(HsesAccessToken)})
Expand All @@ -88,17 +89,17 @@

it "updates the IssueTtaReport object" do
put issue_tta_report_path(issue_tta_report),
params: {tta_report_display_id: "new_display_id", issue_id_tta: complaint_id, format: :js}
params: {tta_report_display_id: new_display_id, issue_id_tta: complaint_id, format: :js}

updated_report = IssueTtaReport.find(issue_tta_report.id)
expect(updated_report.tta_report_display_id).to eq "new_display_id"
expect(updated_report.tta_report_display_id).to eq new_display_id
end
end
end

describe "DELETE /issue_tta_report/unlink_report/:issue_id" do
context "with an authorized user" do
let(:display_report_id) { "DisplayID " }
let(:display_report_id) { "R04-AR-909090" }
let(:complaint_id) { FakeIssues.instance.data.first[:id] }
let!(:issue_tta_report) do
IssueTtaReport.create(
Expand All @@ -119,7 +120,7 @@
end

describe "if a complaint has multiple TTA records" do
let(:second_display_id) { "SecondDisplayId" }
let(:second_display_id) { "R04-AR-11111" }

before do
post issue_tta_reports_path,
Expand Down

0 comments on commit 7b55b59

Please sign in to comment.