|
1 | 1 | require "rails_helper" |
2 | 2 |
|
3 | 3 | RSpec.describe Admin::DeduplicatePatientsController, type: :controller do |
4 | | - context "#show" do |
| 4 | + describe "#show" do |
5 | 5 | it "shows patients accessible by the user" do |
6 | 6 | patient = create(:patient, full_name: "Patient one") |
7 | 7 | patient_passport_id = patient.business_identifiers.first.identifier |
|
40 | 40 | expect(response.status).to eq(302) |
41 | 41 | expect(flash[:alert]).to eq("You are not authorized to perform this action.") |
42 | 42 | end |
| 43 | + |
| 44 | + context "when patient_deduplication_filter feature flag is enabled" do |
| 45 | + let(:organization) { create(:organization) } |
| 46 | + let(:facility_group) { create(:facility_group, organization: organization) } |
| 47 | + let(:district) { facility_group.region } |
| 48 | + let(:facility1) { create(:facility, facility_group: facility_group) } |
| 49 | + let(:facility2) { create(:facility, facility_group: facility_group) } |
| 50 | + let(:admin) { create(:admin, :manager, :with_access, resource: organization) } |
| 51 | + |
| 52 | + before do |
| 53 | + sign_in(admin.email_authentication) |
| 54 | + Flipper.enable(:patient_deduplication_filter, admin) |
| 55 | + end |
| 56 | + |
| 57 | + it "sets filter options when feature flag is enabled" do |
| 58 | + facility1 |
| 59 | + get :show |
| 60 | + |
| 61 | + expect(assigns(:districts)).to be_present |
| 62 | + expect(assigns(:selected_district)).to be_present |
| 63 | + end |
| 64 | + |
| 65 | + it "populates accessible districts" do |
| 66 | + facility1 |
| 67 | + get :show |
| 68 | + |
| 69 | + expect(assigns(:districts)).to include(district) |
| 70 | + end |
| 71 | + |
| 72 | + it "filters patients by selected district" do |
| 73 | + patient1 = create(:patient, full_name: "Patient one", assigned_facility: facility1) |
| 74 | + patient1_passport_id = patient1.business_identifiers.first.identifier |
| 75 | + |
| 76 | + patient1_dup = create(:patient, full_name: "Patient one dup", assigned_facility: facility1) |
| 77 | + patient1_dup.business_identifiers.first.update(identifier: patient1_passport_id) |
| 78 | + |
| 79 | + other_facility_group = create(:facility_group, organization: organization) |
| 80 | + other_facility = create(:facility, facility_group: other_facility_group) |
| 81 | + |
| 82 | + patient2 = create(:patient, full_name: "Patient two", assigned_facility: other_facility) |
| 83 | + patient2_passport_id = patient2.business_identifiers.first.identifier |
| 84 | + |
| 85 | + patient2_dup = create(:patient, full_name: "Patient two dup", assigned_facility: other_facility) |
| 86 | + patient2_dup.business_identifiers.first.update(identifier: patient2_passport_id) |
| 87 | + |
| 88 | + get :show, params: {district_slug: district.slug} |
| 89 | + |
| 90 | + expect(assigns(:patients)).to include(patient1, patient1_dup) |
| 91 | + expect(assigns(:patients)).not_to include(patient2, patient2_dup) |
| 92 | + end |
| 93 | + |
| 94 | + it "filters patients by selected facility" do |
| 95 | + patient1 = create(:patient, full_name: "Patient one", assigned_facility: facility1) |
| 96 | + patient1_passport_id = patient1.business_identifiers.first.identifier |
| 97 | + |
| 98 | + patient1_dup = create(:patient, full_name: "Patient one dup", assigned_facility: facility1) |
| 99 | + patient1_dup.business_identifiers.first.update(identifier: patient1_passport_id) |
| 100 | + |
| 101 | + patient2 = create(:patient, full_name: "Patient two", assigned_facility: facility2) |
| 102 | + patient2_passport_id = patient2.business_identifiers.first.identifier |
| 103 | + |
| 104 | + patient2_dup = create(:patient, full_name: "Patient two dup", assigned_facility: facility2) |
| 105 | + patient2_dup.business_identifiers.first.update(identifier: patient2_passport_id) |
| 106 | + |
| 107 | + get :show, params: {district_slug: district.slug, facility_id: facility1.id} |
| 108 | + |
| 109 | + expect(assigns(:patients)).to include(patient1, patient1_dup) |
| 110 | + expect(assigns(:patients)).not_to include(patient2, patient2_dup) |
| 111 | + end |
| 112 | + |
| 113 | + it "populates facilities for the selected district" do |
| 114 | + facility1 |
| 115 | + facility2 |
| 116 | + get :show, params: {district_slug: district.slug} |
| 117 | + |
| 118 | + expect(assigns(:facilities)).to include(facility1, facility2) |
| 119 | + end |
| 120 | + |
| 121 | + it "sets the selected facility when facility_id param is present" do |
| 122 | + facility1 |
| 123 | + get :show, params: {district_slug: district.slug, facility_id: facility1.id} |
| 124 | + |
| 125 | + expect(assigns(:selected_facility)).to eq(facility1) |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + context "when patient_deduplication_filter feature flag is disabled" do |
| 130 | + let(:admin) { create(:admin, :manager, :with_access, resource: create(:facility)) } |
| 131 | + |
| 132 | + before do |
| 133 | + sign_in(admin.email_authentication) |
| 134 | + Flipper.disable(:patient_deduplication_filter) |
| 135 | + end |
| 136 | + |
| 137 | + it "does not set filter options" do |
| 138 | + get :show |
| 139 | + |
| 140 | + expect(assigns(:districts)).to be_nil |
| 141 | + expect(assigns(:selected_district)).to be_nil |
| 142 | + expect(assigns(:facilities)).to be_nil |
| 143 | + end |
| 144 | + |
| 145 | + it "shows all accessible duplicate patients without filtering" do |
| 146 | + facility = admin.accessible_facilities(:manage).first |
| 147 | + patient = create(:patient, full_name: "Patient one", assigned_facility: facility) |
| 148 | + patient_passport_id = patient.business_identifiers.first.identifier |
| 149 | + |
| 150 | + patient_dup = create(:patient, full_name: "Patient one dup", assigned_facility: facility) |
| 151 | + patient_dup.business_identifiers.first.update(identifier: patient_passport_id) |
| 152 | + |
| 153 | + get :show |
| 154 | + |
| 155 | + expect(assigns(:patients)).to contain_exactly(patient, patient_dup) |
| 156 | + end |
| 157 | + end |
43 | 158 | end |
44 | 159 |
|
45 | | - context "#merge" do |
| 160 | + describe "#merge" do |
46 | 161 | it "returns unauthorized when none of the patient IDs is accessible by the user" do |
47 | 162 | patients = [create(:patient, full_name: "Patient one"), create(:patient, full_name: "Patient two")] |
48 | 163 | admin = create(:admin, :manager, :with_access, resource: create(:facility)) |
|
0 commit comments