|
170 | 170 | }.not_to change(CasaCase, :count) |
171 | 171 |
|
172 | 172 | expect(request.session[:import_error]).to include("Not all rows were imported.") |
173 | | - expect(request.session[:exported_rows]).to include("Case CINA-00-0000 already exists, but is inactive. Reactivate the CASA case instead.") |
174 | 173 | expect(response).to redirect_to(imports_url(import_type: "casa_case")) |
| 174 | + |
| 175 | + failed_csv_path = FailedImportCsvService.new(import_type: :casa_case, user: casa_admin).csv_filepath |
| 176 | + expect(File.exist?(failed_csv_path)).to be true |
| 177 | + |
| 178 | + file_contents = File.read(failed_csv_path) |
| 179 | + expect(file_contents).to include("Case CINA-00-0000 already exists, but is inactive. Reactivate the CASA case instead.") |
| 180 | + |
| 181 | + FileUtils.rm_f(failed_csv_path) |
| 182 | + end |
| 183 | + |
| 184 | + it "calls FailedImportCsvService#store when there are failed rows from the import" do |
| 185 | + sign_in casa_admin |
| 186 | + |
| 187 | + csv_service_double = instance_double(FailedImportCsvService) |
| 188 | + allow(csv_service_double).to receive(:failed_rows=) |
| 189 | + allow(csv_service_double).to receive(:store) |
| 190 | + allow(csv_service_double).to receive(:cleanup) |
| 191 | + allow(FailedImportCsvService).to receive(:new).and_return(csv_service_double) |
| 192 | + |
| 193 | + allow(CaseImporter).to receive(:import_cases).and_return({ |
| 194 | + message: "Some cases were not imported.", |
| 195 | + exported_rows: "Case CINA-00-0000 already exists, but is inactive. Reactivate the CASA case instead.", |
| 196 | + type: :error |
| 197 | + }) |
| 198 | + |
| 199 | + expect(csv_service_double).to receive(:failed_rows=).with("Case CINA-00-0000 already exists, but is inactive. Reactivate the CASA case instead.") |
| 200 | + expect(csv_service_double).to receive(:store) |
| 201 | + expect(csv_service_double).to receive(:cleanup) |
| 202 | + |
| 203 | + post imports_url, |
| 204 | + params: { |
| 205 | + import_type: "casa_case", |
| 206 | + file: fixture_file_upload("existing_casa_case.csv", "text/csv") |
| 207 | + } |
| 208 | + |
| 209 | + expect(request.session[:import_error]).to include("Click here to download failed rows.") |
| 210 | + expect(response).to redirect_to(imports_url(import_type: "casa_case")) |
| 211 | + end |
| 212 | + |
| 213 | + it "writes a fallback message when exported rows exceed max size" do |
| 214 | + sign_in casa_admin |
| 215 | + |
| 216 | + large_exported_content = "a" * (FailedImportCsvService::MAX_FILE_SIZE_BYTES + 1) |
| 217 | + |
| 218 | + allow(CaseImporter).to receive(:import_cases).and_return({ |
| 219 | + message: "Some rows were too large.", |
| 220 | + exported_rows: large_exported_content, |
| 221 | + type: :error |
| 222 | + }) |
| 223 | + |
| 224 | + post imports_url, |
| 225 | + params: { |
| 226 | + import_type: "casa_case", |
| 227 | + file: case_file |
| 228 | + } |
| 229 | + |
| 230 | + failed_csv_path = FailedImportCsvService.new(import_type: :casa_case, user: casa_admin).csv_filepath |
| 231 | + expect(File.exist?(failed_csv_path)).to be true |
| 232 | + |
| 233 | + written_content = File.read(failed_csv_path) |
| 234 | + expect(written_content).to include("The file was too large to save") |
| 235 | + |
| 236 | + expect(request.session[:import_error]).to include("Click here to download failed rows.") |
| 237 | + |
| 238 | + FileUtils.rm_f(failed_csv_path) |
| 239 | + end |
| 240 | + |
| 241 | + it "shows a fallback error message when the failed import CSV was never created" do |
| 242 | + sign_in casa_admin |
| 243 | + |
| 244 | + get download_failed_imports_path(import_type: "casa_case") |
| 245 | + |
| 246 | + expect(response.body).to include("Please upload a CASA Case CSV") |
| 247 | + expect(response.headers["Content-Disposition"]).to include("attachment; filename=\"failed_rows.csv\"") |
| 248 | + end |
| 249 | + |
| 250 | + it "shows a fallback error message when the failed import CSV expired" do |
| 251 | + sign_in casa_admin |
| 252 | + |
| 253 | + service = FailedImportCsvService.new( |
| 254 | + import_type: :casa_case, |
| 255 | + user: casa_admin, |
| 256 | + failed_rows: "some content", |
| 257 | + filepath: path |
| 258 | + ) |
| 259 | + service.store |
| 260 | + |
| 261 | + File.utime(2.days.ago.to_time, 2.days.ago.to_time, service.csv_filepath) |
| 262 | + |
| 263 | + get download_failed_imports_path(import_type: "casa_case") |
| 264 | + |
| 265 | + expect(response.body).to include("Please upload a CASA Case CSV") |
| 266 | + expect(response.headers["Content-Disposition"]).to include("attachment; filename=\"failed_rows.csv\"") |
| 267 | + expect(File.exist?(path)).to be_falsey |
175 | 268 | end |
176 | 269 | end |
177 | 270 | end |
0 commit comments