<commit_before>from django.core import mail

from accelerator.tests.factories import (
    MentorProgramOfficeHourFactory,
)

from impact.tests.api_test_case import APITestCase


class TestCancelOfficeHourReservationView(APITestCase):
    def mentee_cancels_their_own_office_hour_reservation(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=office_hour.finalist)
        self.assert_reservation_cancelled(office_hour)

    def mentee_cancels_office_hour_reservation_and_gets_notification(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=office_hour.finalist)
        self.assert_notified(office_hour.finalist)

    def mentee_cancels_office_hour_reservation_mentor_gets_notification(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=office_hour.finalist)
        self.assert_notified(office_hour.mentor)

    def staff_user_cancels_their_own_office_hour_reservation(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=self.staff_user())
        self.assert_reservation_cancelled(office_hour)

    def staff_user_cancels_office_hour_reservation_and_gets_notification(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=self.staff_user())
        self.assert_notified(self.office_hour.finalist)

    def staff_user_cancels_office_hour_reservation_mentor_gets_notification(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=self.staff_user())
        self.assert_notified(office_hour.mentor)
        
    
    def non_staff_user_attempts_to_cancel_reservation_not_theirs(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=self.basic_user())
        self.assert_reservation_not_cancelled(office_hour)

    def non_staff_user_attempts_to_cancel_reservation_not_theirs(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=self.basic_user())
        self.assert_not_notified(office_hour.mentor)                

    def user_attempts_to_cancel_non_existent_reservation(self):
        office_hour = MentorProgramOfficeHourFactory(finalist=None)
        self._submit_cancellation(office_hour, user=self.basic_user())
        # assert what?
        
    def user_attempts_to_cancel_non_existent_reservation_no_notification(self):
        office_hour = MentorProgramOfficeHourFactory(finalist=None)
        self._submit_cancellation(office_hour, user=self.basic_user())
        self.assert_not_notified(office_hour.mentor)                
        <commit_msg>[AC-7562] Add tests for mail handler<commit_after>from django.core import mail

from accelerator.tests.factories import (
    MentorProgramOfficeHourFactory,
)

from impact.tests.api_test_case import APITestCase
from impact.v1.views import CancelOfficeHourReservationView

class TestCancelOfficeHourReservationView(APITestCase):
    def mentee_cancels_their_own_office_hour_reservation(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=office_hour.finalist)
        self.assert_reservation_cancelled(office_hour)

    def mentee_cancels_office_hour_reservation_and_gets_notification(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=office_hour.finalist)
        self.assert_notified(office_hour.finalist)

    def mentee_cancels_office_hour_reservation_mentor_gets_notification(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=office_hour.finalist)
        self.assert_notified(office_hour.mentor)

    def notification_includes_submitted_message(self):
        cancellation_message = "I'm sorry I had to cancel"
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour,
                                  user=office_hour.finalist,
                                  message=cancellation_message))
        self.assert_notified(office_hour.mentor,
                             message=cancellation_message)
        
    def staff_user_cancels_their_own_office_hour_reservation(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=self.staff_user())
        self.assert_reservation_cancelled(office_hour)

    def staff_user_cancels_office_hour_reservation_and_gets_notification(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=self.staff_user())
        self.assert_notified(self.office_hour.finalist)

    def staff_user_cancels_office_hour_reservation_mentor_gets_notification(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=self.staff_user())
        self.assert_notified(office_hour.mentor)
        
    
    def non_staff_user_attempts_to_cancel_reservation_not_theirs(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=self.basic_user())
        self.assert_reservation_not_cancelled(office_hour)

    def non_staff_user_attempts_to_cancel_reservation_not_theirs(self):
        office_hour = MentorProgramOfficeHourFactory()
        self._submit_cancellation(office_hour, user=self.basic_user())
        self.assert_not_notified(office_hour.mentor)                

    def user_attempts_to_cancel_non_existent_reservation(self):
        office_hour = MentorProgramOfficeHourFactory(finalist=None)
        self._submit_cancellation(office_hour, user=self.basic_user())
        # assert what?
        
    def user_attempts_to_cancel_non_existent_reservation_no_notification(self):
        office_hour = MentorProgramOfficeHourFactory(finalist=None)
        self._submit_cancellation(office_hour, user=self.basic_user())
        self.assert_not_notified(office_hour.mentor)                
        

    def _submit_cancellation(self, office_hour, user, message=""):
        with self.login(email=user.email):
            url = reverse(CancelOfficeHourReservationView.view_name)
            data = {"office_hour_id": office_hour.id,
                    "message": message}
            response = self.client.post(url, data=data)
            return response

    def assert_notified(user, message=""):
        email = mail.outbox[-1]
        self.assertContains(email.to, user.email)
        if message:
            self.assertContains(email.body, message)
            
{'fraction_non_alphanumeric': 0.06791843919612733, 'fraction_numerical': 0.000880152559777028, 'mean_word_length': 4.042899408284024, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7486615', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2022, 'n_words': 225}