|
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +describe Metrics::FormCountService do |
| 4 | + subject(:service) { described_class.new } |
| 5 | + |
| 6 | + let(:forms_env) { "test" } |
| 7 | + let(:cloud_watch_client) { Aws::CloudWatch::Client.new(stub_responses: true) } |
| 8 | + let(:organisation) { create(:organisation, name: "Department for Testing") } |
| 9 | + let(:group) { create(:group, organisation:) } |
| 10 | + |
| 11 | + before do |
| 12 | + allow(Settings).to receive(:forms_env).and_return(forms_env) |
| 13 | + allow(Aws::CloudWatch::Client).to receive(:new).and_return(cloud_watch_client) |
| 14 | + end |
| 15 | + |
| 16 | + around do |example| |
| 17 | + travel_to(Time.zone.local(2026, 6, 3, 12, 0, 0)) do |
| 18 | + example.run |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + describe "#publish_form_counts" do |
| 23 | + before do |
| 24 | + Form.destroy_all |
| 25 | + |
| 26 | + create(:form, :with_group, group:, state: :draft) |
| 27 | + create(:form, :live, :with_group, group:) |
| 28 | + create(:form, :live_with_draft, :with_group, group:) |
| 29 | + create(:form, :archived, :with_group, group:) |
| 30 | + create(:form, :archived_with_draft, :with_group, group:) |
| 31 | + create(:form, state: :draft) |
| 32 | + end |
| 33 | + |
| 34 | + it "publishes grouped form counts to CloudWatch" do |
| 35 | + expect(cloud_watch_client).to receive(:put_metric_data).with( |
| 36 | + namespace: "Forms", |
| 37 | + metric_data: contain_exactly( |
| 38 | + metric_datum(org: organisation.name, state: "draft", count: 1), |
| 39 | + metric_datum(org: organisation.name, state: "live", count: 2), |
| 40 | + metric_datum(org: organisation.name, state: "archived", count: 2), |
| 41 | + metric_datum(org: "Unknown", state: "draft", count: 1), |
| 42 | + ), |
| 43 | + ) |
| 44 | + |
| 45 | + service.publish_form_counts |
| 46 | + end |
| 47 | + |
| 48 | + context "when CloudWatch returns an error" do |
| 49 | + before do |
| 50 | + allow(cloud_watch_client).to receive(:put_metric_data) |
| 51 | + .and_raise(Aws::CloudWatch::Errors::ServiceError.new(nil, "CloudWatch error", nil)) |
| 52 | + end |
| 53 | + |
| 54 | + it "captures the exception and re-raises" do |
| 55 | + expect(Sentry).to receive(:capture_exception).with(instance_of(Aws::CloudWatch::Errors::ServiceError)) |
| 56 | + |
| 57 | + expect { service.publish_form_counts }.to raise_error(Aws::CloudWatch::Errors::ServiceError) |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + def metric_datum(org:, state:, count:) |
| 63 | + { |
| 64 | + metric_name: "FormCount", |
| 65 | + dimensions: [ |
| 66 | + { |
| 67 | + name: "Environment", |
| 68 | + value: forms_env, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "Org", |
| 72 | + value: org, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "State", |
| 76 | + value: state, |
| 77 | + }, |
| 78 | + ], |
| 79 | + value: count, |
| 80 | + unit: "Count", |
| 81 | + timestamp: Time.zone.local(2026, 6, 3, 12, 0, 0), |
| 82 | + } |
| 83 | + end |
| 84 | +end |
0 commit comments