def test_list(self, client, user, facility): client.force_authenticate(user=user) facility.created_by = user facility.save() response = client.get(f"/api/v1/facility/") assert response.status_code == 200 assert response.data == { "count": 1, "next": None, "previous": None, "results": [ { "id": facility.id, "name": facility.name, "district": "Kannur", "facility_type": "Educational Inst", "address": facility.address, "location": { "latitude": facility.location.tuple[1], "longitude": facility.location.tuple[0], }, "oxygen_capacity": facility.oxygen_capacity, "phone_number": facility.phone_number, }, ], }
def test_update(self, client, user, facility): client.force_authenticate(user=user) facility.created_by = user facility.save() response = client.put( f"/api/v1/facility/{facility.id}/", { "name": "Another name", "district": facility.district, "facility_type": facility.facility_type, "address": facility.address, }, ) assert response.status_code == 200 assert response.data == { "id": facility.id, "name": "Another name", "district": "Kannur", "facility_type": "Educational Inst", "address": facility.address, "location": { "latitude": facility.location.tuple[1], "longitude": facility.location.tuple[0], }, "oxygen_capacity": facility.oxygen_capacity, "phone_number": facility.phone_number, } facility.refresh_from_db() assert facility.name == "Another name"
def test_create(self, client, user, facility_data): client.force_authenticate(user=user) response = client.post( "/api/v1/facility/", facility_data, ) assert response.status_code == 201 response.data.pop("id") assert response.data == { **facility_data, "district": "Kannur", "facility_type": "Educational Inst", } facility = Facility.objects.get( name=facility_data["name"], district=facility_data["district"], facility_type=facility_data["facility_type"], address=facility_data["address"], oxygen_capacity=facility_data["oxygen_capacity"], phone_number=facility_data["phone_number"], created_by=user, ) assert facility assert facility.location.tuple == ( facility_data["location"]["longitude"], facility_data["location"]["latitude"], )
def test_active_check(self, client, user, facility): client.force_authenticate(user=user) facility.created_by = user facility.is_active = False facility.save() response = client.get(f"/api/v1/facility/{facility.id}/") assert response.status_code == 404
def test_list(self, client, user, patient): client.force_authenticate(user=user) patient.created_by = user patient.save() response = client.get(f"/api/v1/patient/") assert response.status_code == 200 assert response.data == { "count": 1, "next": None, "previous": None, "results": [ { "id": patient.id, "name": patient.name, "age": patient.age, "gender": patient.gender, "phone_number": patient.phone_number, "contact_with_carrier": patient.contact_with_carrier, "medical_history": {2, 4}, "medical_history_details": patient.medical_history_details, "is_active": True, }, ], }
def test_create(self, client, user, patient_data): client.force_authenticate(user=user) response = client.post( "/api/v1/patient/", patient_data, ) assert response.status_code == 201 response.data.pop("id") assert response.data == { **patient_data, "medical_history_details": None, "is_active": True, } assert PatientRegistration.objects.get( name=patient_data["name"], age=patient_data["age"], gender=patient_data["gender"], phone_number=patient_data["phone_number"], contact_with_carrier=patient_data["contact_with_carrier"], medical_history=patient_data["medical_history"], medical_history_details=None, created_by=user, is_active=True, )
def test_update(self, client, user, patient): client.force_authenticate(user=user) patient.created_by = user patient.save() new_phone_number = "9999997775" response = client.put( f"/api/v1/patient/{patient.id}/", { "name": patient.name, "age": patient.age, "gender": patient.gender, "phone_number": new_phone_number, "contact_with_carrier": patient.contact_with_carrier, "medical_history": {2, 4}, "medical_history_details": patient.medical_history_details, }, ) assert response.status_code == 200 assert response.data == { "id": patient.id, "name": patient.name, "age": patient.age, "gender": patient.gender, "phone_number": new_phone_number, "contact_with_carrier": patient.contact_with_carrier, "medical_history": {2, 4}, "medical_history_details": patient.medical_history_details, "is_active": True, } patient.refresh_from_db() assert patient.phone_number == new_phone_number
def test_user_can_read_cant_modify_delete_others(self, client, user, data): response = client.post( "/api/v1/users/", data, ) assert response.status_code == 201 client.force_authenticate(user=user) response = client.get("/api/v1/users/") assert response.status_code == 200 assert response.json()["count"] == 2 assert user.username in { r["username"] for r in response.json()["results"] } assert data["username"] in { r["username"] for r in response.json()["results"] } response = client.get(f"/api/v1/users/{data['username']}/") assert response.status_code == 200 response = client.put(f"/api/v1/users/{data['username']}/", data) assert response.status_code == 404 response = client.delete(f"/api/v1/users/{data['username']}/") assert response.status_code == 404
def test_user_can_read_modify_delete_himself(self, client, data): response = client.post( "/api/v1/users/", data, ) assert response.status_code == 201 user = User.objects.get(username=data["username"]) client.force_authenticate(user=user) response = client.get("/api/v1/users/") assert response.status_code == 200 assert response.json()["count"] == 1 assert user.username in { r["username"] for r in response.json()["results"] } response = client.get(f"/api/v1/users/{user.username}/") assert response.status_code == 200 response = client.put(f"/api/v1/users/{user.username}/", { **data, "age": 31, }) assert response.status_code == 200 assert response.json()["age"] == 31 assert User.objects.only('age').get( username=data["username"]).age == 31 response = client.delete(f"/api/v1/users/{user.username}/") assert response.status_code == 204 with pytest.raises(User.DoesNotExist): User.objects.get(username=data["username"])
def test_others_cant_update_ones_facility(self, client, user, facility): client.force_authenticate(user=user) response = client.post( "/api/v1/facility/bulk_upsert/", data=[ { "name": facility.name, "district": facility.district, "facility_type": 3, "address": facility.address, "capacity": [{ "room_type": 1, "total_capacity": 100, "current_capacity": 48, }], }, ], ) assert response.status_code == 403 assert response.json( )["detail"] == "Foo, Kannur is owned by another user"
def test_admins_can_update_ones_facility(self, client, user, facility): client.force_authenticate(user=user) user.is_superuser = True user.save() response = client.post( "/api/v1/facility/bulk_upsert/", data=[ { "name": facility.name, "district": facility.district, "facility_type": 3, "address": facility.address, "capacity": [{ "room_type": 1, "total_capacity": 100, "current_capacity": 48, }], }, ], ) assert response.status_code == 204
def test_destroy(self, client, user, facility): client.force_authenticate(user=user) facility.created_by = user facility.save() response = client.delete(f"/api/v1/facility/{facility.id}/", ) assert response.status_code == 204 with pytest.raises(Facility.DoesNotExist): Facility.objects.get(id=facility.id)
def test_destroy(self, client, user, patient): client.force_authenticate(user=user) patient.created_by = user patient.save() response = client.delete(f"/api/v1/patient/{patient.id}/", ) assert response.status_code == 204 with pytest.raises(PatientRegistration.DoesNotExist): PatientRegistration.objects.get(id=patient.id)
def test_superuser_can_read_modify_delete_all(self, client, user, data): user.is_superuser = True user.save() response = client.post( "/api/v1/users/", data, ) assert response.status_code == 201 client.force_authenticate(user=user) response = client.get("/api/v1/users/") assert response.status_code == 200 assert response.json()["count"] == 2 assert user.username in { r["username"] for r in response.json()["results"] } assert data["username"] in { r["username"] for r in response.json()["results"] } response = client.get(f"/api/v1/users/{data['username']}/") assert response.status_code == 200 res_data = response.json() res_data.pop("id") password = data.pop("password") assert res_data == { **data, 'district': 'Kozhikode', 'gender': 'Male', 'user_type': 'Doctor', 'first_name': '', 'last_name': '', 'skill': None } response = client.put(f"/api/v1/users/{data['username']}/", { **data, "age": 31, "password": password, }) assert response.status_code == 200 assert response.json()["age"] == 31 assert User.objects.only('age').get( username=data["username"]).age == 31 response = client.delete(f"/api/v1/users/{data['username']}/") assert response.status_code == 204 with pytest.raises(User.DoesNotExist): User.objects.get(username=data["username"])
def test_update_doesnt_change_creator(self, client, user, user_data, facility, facility_data): facility.created_by = User.objects.create(**user_data) facility.save() original_creator = facility.created_by user.is_superuser = True user.save() client.force_authenticate(user=user) response = client.put(f"/api/v1/facility/{facility.id}/", facility_data) assert response.status_code == 200 facility.refresh_from_db() assert facility.created_by == original_creator
def test_retrieve(self, client, user, patient): client.force_authenticate(user=user) patient.created_by = user patient.save() response = client.get(f"/api/v1/patient/{patient.id}/") assert response.status_code == 200 assert response.data == { "id": patient.id, "name": patient.name, "age": patient.age, "gender": patient.gender, "phone_number": patient.phone_number, "contact_with_carrier": patient.contact_with_carrier, "medical_history": {2, 4}, "medical_history_details": patient.medical_history_details, "tele_consultation_history": [], "is_active": True, }
def test_super_user_access(self, client, user, facility): client.force_authenticate(user=user) response = client.get(f"/api/v1/facility/{facility.id}/") assert response.status_code == 404 user.is_superuser = True user.save() response = client.get(f"/api/v1/facility/{facility.id}/") assert response.status_code == 200 assert response.data == { "id": facility.id, "name": facility.name, "district": "Kannur", "facility_type": "Educational Inst", "address": facility.address, "location": { "latitude": facility.location.tuple[1], "longitude": facility.location.tuple[0], }, "oxygen_capacity": facility.oxygen_capacity, "phone_number": facility.phone_number, }
def test_success(self, client, user, facility): capacity = FacilityCapacity.objects.create(facility=facility, room_type=1, total_capacity=50, current_capacity=0) facility.created_by = user facility.save() client.force_authenticate(user=user) name = "Another" address = "Dasappan's House, Washington Jn, OolaMukk P.O." phone_number = "7776665554" response = client.post( "/api/v1/facility/bulk_upsert/", data=[ { "name": facility.name, "district": facility.district, "facility_type": 3, "address": facility.address, "capacity": [{ "room_type": 1, "total_capacity": 100, "current_capacity": 48, }], }, { "name": name, "district": 1, "facility_type": 2, "address": address, "phone_number": phone_number, "capacity": [ { "room_type": 0, "total_capacity": 350, "current_capacity": 150, }, { "room_type": 1, "total_capacity": 200, "current_capacity": 100, }, ], }, ], ) assert response.status_code == 204 facility.refresh_from_db() assert facility.facility_type == 3 capacities = facility.facilitycapacity_set.all() assert capacities[0].id == capacity.id assert capacities[0].room_type == 1 assert capacities[0].total_capacity == 100 assert capacities[0].current_capacity == 48 assert len(capacities) == 1 new_facility = Facility.objects.get( created_by=user, name=name, district=1, facility_type=2, address=address, phone_number=phone_number, ) assert new_facility capacities = new_facility.facilitycapacity_set.all() assert capacities[0].room_type == 0 assert capacities[0].total_capacity == 350 assert capacities[0].current_capacity == 150 assert capacities[1].room_type == 1 assert capacities[1].total_capacity == 200 assert capacities[1].current_capacity == 100 assert len(capacities) == 2