def test_nurse_successful(self):
        patient = User.objects.get(username=PATIENT_USERNAME).patient
        request = self.factory.post(
            reverse('hospital:admit_patient', args=[patient.id]), {})
        request.user = User.objects.get(username=NURSE_USERNAME)
        views.admit_patient(request, patient.id)

        self.assertTrue(TreatmentSession.objects.exists(),
                        'Expected a new treatment session to be created.')
        treatment_session = TreatmentSession.objects.first()
        self.assertEqual(
            treatment_session.patient, patient,
            'Expected the new treatment session to be associated with the given patient.'
        )
        self.assertEqual(treatment_session.discharge_timestamp, None,
                         'Unexpected discharge for patient.')
    def test_failed_already_admitted(self):
        patient = User.objects.get(username=PATIENT_USERNAME).patient
        # Create & send the first request
        request = self.factory.post(
            reverse('hospital:admit_patient', args=[patient.id]), {})
        request.user = User.objects.get(username=DOCTOR_USERNAME)
        views.admit_patient(request, patient.id)
        # Create & send the second request
        request = self.factory.post(
            reverse('hospital:admit_patient', args=[patient.id]), {})
        request.user = User.objects.get(username=DOCTOR_USERNAME)
        views.admit_patient(request, patient.id)

        self.assertEqual(
            TreatmentSession.objects.count(), 1,
            'Expected skipping treatment session creation since patient is already admitted.'
        )
    def test_failed_non_post_request(self):
        patient = User.objects.get(username=PATIENT_USERNAME).patient
        request = self.factory.get(
            reverse('hospital:admit_patient', args=[patient.id]))
        request.user = User.objects.get(username=NURSE_USERNAME)
        response = views.admit_patient(request, patient.id)

        self.assertEqual(response.status_code, 302,
                         'Expected to be redirected.')
        self.assertFalse(
            TreatmentSession.objects.exists(),
            'Expected skipping treatment session creation due to incorrect HTTP request method.'
        )
    def test_failed_permission(self):
        patient_user = User.objects.get(username=PATIENT_USERNAME)
        patient = patient_user.patient
        request = self.factory.post(
            reverse('hospital:admit_patient', args=[patient.id]), {})
        request.user = AnonymousUser()
        response = views.admit_patient(request, patient.id)

        self.assertEqual(
            response.status_code, 302,
            'Expected to be redirected due to insufficient permission.')
        self.assertFalse(TreatmentSession.objects.exists(),
                         'Expected failing to create new treatment session.')

        request = self.factory.post(
            reverse('hospital:admit_patient', args=[patient.id]), {})
        request.user = patient_user
        response = views.admit_patient(request, patient.id)

        self.assertEqual(
            response.status_code, 302,
            'Expected to be redirected due to insufficient permission.')
        self.assertFalse(TreatmentSession.objects.exists(),
                         'Expected failing to create new treatment session.')