|
5 | 5 |
|
6 | 6 | def test_create_report_succeeds_with_valid_data(mocker, admin_request, sample_service, sample_user):
|
7 | 7 | """Test that creating a report with valid data succeeds"""
|
| 8 | + # Mock the generate_report Celery task to avoid actual task execution |
8 | 9 | generate_report_mock = mocker.patch("app.report.rest.generate_report.apply_async")
|
| 10 | + |
| 11 | + # Use actual database operations instead of mocking create_report |
9 | 12 | data = {"report_type": ReportType.EMAIL.value, "requesting_user_id": str(sample_user.id), "language": "en"}
|
10 | 13 |
|
| 14 | + # Call the endpoint which will create a real report in the database |
| 15 | + response = admin_request.post("report.create_service_report", service_id=sample_service.id, _data=data, _expected_status=201) |
| 16 | + |
| 17 | + # Verify response contains expected data |
| 18 | + assert response["data"]["report_type"] == ReportType.EMAIL.value |
| 19 | + assert response["data"]["service_id"] == str(sample_service.id) |
| 20 | + assert response["data"]["language"] == "en" |
| 21 | + assert response["data"]["status"] == ReportStatus.REQUESTED.value |
| 22 | + |
| 23 | + # Extract the report ID from the response |
| 24 | + report_id = response["data"]["id"] |
| 25 | + |
| 26 | + # Verify generate_report was called with correct parameters |
| 27 | + generate_report_mock.assert_called_once() |
| 28 | + assert str(generate_report_mock.call_args[0][0][0]) == report_id |
| 29 | + assert generate_report_mock.call_args[0][0][1] == [] # Empty notification_statuses |
| 30 | + assert generate_report_mock.call_args[1]["queue"] == "generate-reports" |
| 31 | + |
| 32 | + |
| 33 | +def test_create_report_succeeds_with_notification_statuses(mocker, admin_request, sample_service, sample_user): |
| 34 | + """Test that creating a report with notification_statuses passes the data to generate_report task""" |
| 35 | + # Mock the generate_report Celery task to avoid actual task execution |
| 36 | + generate_report_mock = mocker.patch("app.report.rest.generate_report.apply_async") |
| 37 | + |
| 38 | + notification_statuses = ["delivered", "sending"] |
| 39 | + data = { |
| 40 | + "report_type": ReportType.EMAIL.value, |
| 41 | + "requesting_user_id": str(sample_user.id), |
| 42 | + "notification_statuses": notification_statuses, |
| 43 | + "language": "en", |
| 44 | + } |
| 45 | + |
| 46 | + # Call the endpoint which will create a real report in the database |
| 47 | + response = admin_request.post("report.create_service_report", service_id=sample_service.id, _data=data, _expected_status=201) |
| 48 | + |
| 49 | + # Verify response contains expected data |
| 50 | + assert response["data"]["report_type"] == ReportType.EMAIL.value |
| 51 | + assert str(response["data"]["service_id"]) == str(sample_service.id) |
| 52 | + assert response["data"]["language"] == "en" |
| 53 | + assert response["data"]["status"] == ReportStatus.REQUESTED.value |
| 54 | + |
| 55 | + # Extract the report ID from the response |
| 56 | + report_id = response["data"]["id"] |
| 57 | + |
| 58 | + # Verify generate_report was called with notification_statuses |
| 59 | + generate_report_mock.assert_called_once() |
| 60 | + assert str(generate_report_mock.call_args[0][0][0]) == report_id |
| 61 | + assert generate_report_mock.call_args[0][0][1] == notification_statuses |
| 62 | + assert generate_report_mock.call_args[1]["queue"] == "generate-reports" |
| 63 | + |
| 64 | + |
| 65 | +def test_create_report_succeeds_with_job_id(mocker, admin_request, sample_service, sample_user, sample_job): |
| 66 | + """Test that creating a report with job_id passes the data to generate_report task""" |
| 67 | + # Mock the generate_report Celery task to avoid actual task execution |
| 68 | + generate_report_mock = mocker.patch("app.report.rest.generate_report.apply_async") |
| 69 | + |
| 70 | + data = { |
| 71 | + "report_type": ReportType.SMS.value, |
| 72 | + "requesting_user_id": str(sample_user.id), |
| 73 | + "job_id": str(sample_job.id), |
| 74 | + "language": "en", |
| 75 | + } |
| 76 | + |
| 77 | + # Call the endpoint which will create a real report in the database |
| 78 | + response = admin_request.post("report.create_service_report", service_id=sample_service.id, _data=data, _expected_status=201) |
| 79 | + |
| 80 | + # Verify response contains expected data |
| 81 | + assert response["data"]["report_type"] == ReportType.SMS.value |
| 82 | + assert str(response["data"]["service_id"]) == str(sample_service.id) |
| 83 | + assert response["data"]["job_id"] == str(sample_job.id) |
| 84 | + assert response["data"]["language"] == "en" |
| 85 | + assert response["data"]["status"] == ReportStatus.REQUESTED.value |
| 86 | + |
| 87 | + # Extract the report ID from the response |
| 88 | + report_id = response["data"]["id"] |
| 89 | + |
| 90 | + # Verify generate_report was called with empty notification_statuses |
| 91 | + generate_report_mock.assert_called_once() |
| 92 | + assert str(generate_report_mock.call_args[0][0][0]) == report_id |
| 93 | + assert generate_report_mock.call_args[0][0][1] == [] # Empty notification_statuses |
| 94 | + assert generate_report_mock.call_args[1]["queue"] == "generate-reports" |
| 95 | + |
| 96 | + |
| 97 | +def test_create_report_succeeds_with_both_notification_statuses_and_job_id( |
| 98 | + mocker, admin_request, sample_service, sample_user, sample_job |
| 99 | +): |
| 100 | + """Test that creating a report with both notification_statuses and job_id passes both parameters correctly""" |
| 101 | + # Mock the generate_report Celery task to avoid actual task execution |
| 102 | + generate_report_mock = mocker.patch("app.report.rest.generate_report.apply_async") |
| 103 | + |
| 104 | + notification_statuses = ["delivered", "failed"] |
| 105 | + data = { |
| 106 | + "report_type": ReportType.EMAIL.value, |
| 107 | + "requesting_user_id": str(sample_user.id), |
| 108 | + "job_id": str(sample_job.id), |
| 109 | + "notification_statuses": notification_statuses, |
| 110 | + "language": "en", |
| 111 | + } |
| 112 | + |
| 113 | + # Call the endpoint which will create a real report in the database |
11 | 114 | response = admin_request.post("report.create_service_report", service_id=sample_service.id, _data=data, _expected_status=201)
|
12 | 115 |
|
| 116 | + # Verify response contains expected data |
13 | 117 | assert response["data"]["report_type"] == ReportType.EMAIL.value
|
14 | 118 | assert response["data"]["service_id"] == str(sample_service.id)
|
| 119 | + assert response["data"]["job_id"] == str(sample_job.id) |
15 | 120 | assert response["data"]["language"] == "en"
|
16 | 121 | assert response["data"]["status"] == ReportStatus.REQUESTED.value
|
17 |
| - assert "id" in response["data"] |
18 |
| - assert "requested_at" in response["data"] |
19 | 122 |
|
20 |
| - # assert generate_report_mock called once with the correct report ID |
| 123 | + # Extract the report ID from the response |
| 124 | + report_id = response["data"]["id"] |
| 125 | + |
| 126 | + # Verify generate_report was called with notification_statuses |
21 | 127 | generate_report_mock.assert_called_once()
|
22 |
| - assert str(generate_report_mock.call_args[0][0][0]) == response["data"]["id"] |
23 |
| - # check the queue name |
| 128 | + assert str(generate_report_mock.call_args[0][0][0]) == report_id |
| 129 | + assert generate_report_mock.call_args[0][0][1] == notification_statuses |
24 | 130 | assert generate_report_mock.call_args[1]["queue"] == "generate-reports"
|
25 | 131 |
|
26 | 132 |
|
|
0 commit comments