Example #1
0
    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
Example #2
0
    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"])
Example #3
0
    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"])
Example #4
0
 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,
             },
         ],
     }
Example #5
0
 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
Example #6
0
 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,
             },
         ],
     }
Example #7
0
    def test_super_user_access(self, client, user, patient):
        client.force_authenticate(user=user)
        response = client.get(f"/api/v1/patient/{patient.id}/")
        assert response.status_code == 404

        user.is_superuser = True
        user.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,
        }
Example #8
0
    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,
        }