Example #1
0
    def test_admin_alert_multiple_meeting_patterns(self):
        """Emails admin if course is scheduled with weird start/end dates."""
        with test_approvals_workflow(app):
            with enabled_job(job_key=AdminEmailsJob.key()):
                term_id = app.config['CURRENT_TERM_ID']
                section_id = 50014
                room_id = Room.find_room('Barker 101').id
                # The course has two instructors.
                instructor_uid = get_instructor_uids(section_id=section_id,
                                                     term_id=term_id)[0]
                approval = Approval.create(
                    approved_by_uid=instructor_uid,
                    approver_type_='instructor',
                    publish_type_='kaltura_my_media',
                    recording_type_='presenter_audio',
                    room_id=room_id,
                    section_id=section_id,
                    term_id=term_id,
                )
                # Uh oh! Only one of them has been scheduled.
                meeting = get_eligible_meeting(section_id=section_id,
                                               term_id=term_id)
                Scheduled.create(
                    instructor_uids=[instructor_uid],
                    kaltura_schedule_id=random.randint(1, 10),
                    meeting_days=meeting['days'],
                    meeting_end_date=get_recording_end_date(meeting),
                    meeting_end_time=meeting['endTime'],
                    meeting_start_date=get_recording_start_date(
                        meeting, return_today_if_past_start=True),
                    meeting_start_time=meeting['startTime'],
                    publish_type_=approval.publish_type,
                    recording_type_=approval.recording_type,
                    room_id=room_id,
                    section_id=section_id,
                    term_id=term_id,
                )
                courses = SisSection.get_courses_scheduled_nonstandard_dates(
                    term_id=term_id)
                course = next(
                    (c for c in courses if c['sectionId'] == section_id), None)
                assert course

                # Message queued but not sent.
                admin_uid = app.config['EMAIL_DIABLO_ADMIN_UID']
                AdminEmailsJob(simply_yield).run()
                queued_messages = QueuedEmail.query.filter_by(
                    section_id=section_id).all()
                assert len(queued_messages) == 1
                for queued_message in queued_messages:
                    assert '2020-08-26 to 2020-10-02' in queued_message.message

                # Message sent.
                QueuedEmailsJob(simply_yield).run()
                emails_sent = SentEmail.get_emails_sent_to(uid=admin_uid)
                assert len(emails_sent) == 1
                assert emails_sent[
                    0].template_type == 'admin_alert_multiple_meeting_patterns'
                assert emails_sent[0].section_id == section_id
 def test_alert_admin_of_instructor_change(self):
     """Emails admin when a scheduled course gets a new instructor."""
     with test_approvals_workflow(app):
         with enabled_job(job_key=AdminEmailsJob.key()):
             term_id = app.config['CURRENT_TERM_ID']
             section_id = 50005
             room_id = Room.find_room('Barker 101').id
             # The course has two instructors.
             instructor_1_uid, instructor_2_uid = get_instructor_uids(
                 section_id=section_id, term_id=term_id)
             approval = Approval.create(
                 approved_by_uid=instructor_1_uid,
                 approver_type_='instructor',
                 course_display_name=
                 f'term_id:{term_id} section_id:{section_id}',
                 publish_type_='kaltura_my_media',
                 recording_type_='presenter_audio',
                 room_id=room_id,
                 section_id=section_id,
                 term_id=term_id,
             )
             # Uh oh! Only one of them has been scheduled.
             meeting = get_eligible_meeting(section_id=section_id,
                                            term_id=term_id)
             Scheduled.create(
                 course_display_name=
                 f'term_id:{term_id} section_id:{section_id}',
                 instructor_uids=[instructor_1_uid],
                 kaltura_schedule_id=random.randint(1, 10),
                 meeting_days=meeting['days'],
                 meeting_end_date=get_recording_end_date(meeting),
                 meeting_end_time=meeting['endTime'],
                 meeting_start_date=get_recording_start_date(
                     meeting, return_today_if_past_start=True),
                 meeting_start_time=meeting['startTime'],
                 publish_type_=approval.publish_type,
                 recording_type_=approval.recording_type,
                 room_id=room_id,
                 section_id=section_id,
                 term_id=term_id,
             )
             admin_uid = app.config['EMAIL_DIABLO_ADMIN_UID']
             email_count = _get_email_count(admin_uid)
             # Message queued but not sent.
             AdminEmailsJob(simply_yield).run()
             assert _get_email_count(admin_uid) == email_count
             queued_messages = QueuedEmail.query.filter_by(
                 template_type='admin_alert_instructor_change').all()
             assert len(queued_messages) == 1
             for snippet in [
                     'LAW 23', 'Old instructor(s) Regan MacNeil',
                     'New instructor(s) Regan MacNeil, Burke Dennings'
             ]:
                 assert snippet in queued_messages[0].message
             # Message sent.
             QueuedEmailsJob(simply_yield).run()
             assert _get_email_count(admin_uid) == email_count + 1
    def test_room_change_no_longer_eligible(self, db_session):
        section_id = 50004
        term_id = app.config['CURRENT_TERM_ID']

        def _move_course(meeting_location):
            db.session.execute(
                text(
                    'UPDATE sis_sections SET meeting_location = :meeting_location WHERE term_id = :term_id AND section_id = :section_id'
                ),
                {
                    'meeting_location': meeting_location,
                    'section_id': section_id,
                    'term_id': term_id,
                },
            )

        with enabled_job(job_key=InstructorEmailsJob.key()):
            with test_approvals_workflow(app):
                course = SisSection.get_course(section_id=section_id,
                                               term_id=term_id)
                eligible_meetings = course.get('meetings',
                                               {}).get('eligible', [])
                assert len(eligible_meetings) == 1
                original_room = eligible_meetings[0]['room']
                assert original_room['location'] == 'Li Ka Shing 145'

                # Schedule
                _schedule(original_room['id'], section_id)
                _run_instructor_emails_job()
                _assert_email_count(0, section_id,
                                    'room_change_no_longer_eligible')

                # Move course to some other eligible room.
                _move_course('Barker 101')
                _run_instructor_emails_job()
                _assert_email_count(0, section_id,
                                    'room_change_no_longer_eligible')

                # Move course to an ineligible room.
                ineligible_room = 'Wheeler 150'
                _move_course(ineligible_room)
                _run_instructor_emails_job()
                _assert_email_count(1, section_id,
                                    'room_change_no_longer_eligible')

                # Move course back to its original location
                _move_course(original_room['location'])

                # Finally, let's pretend the course is scheduled to a room that was previously eligible.
                Scheduled.delete(section_id=section_id, term_id=term_id)
                _schedule(Room.find_room(ineligible_room).id, section_id)
                _run_instructor_emails_job()
                # Expect email.
                _assert_email_count(2, section_id,
                                    'room_change_no_longer_eligible')
                Scheduled.delete(section_id=section_id, term_id=term_id)
Example #4
0
 def test_edit_schedule_of_enabled_job(self, client, admin_session):
     """You cannot edit job schedule if the job is enabled."""
     with enabled_job(job_key=AdminEmailsJob.key()):
         self._api_job_update_schedule(
             client,
             expected_status_code=400,
             job_id=Job.get_job_by_key('admin_emails').id,
             schedule_type='minutes',
             schedule_value=3,
         )
Example #5
0
    def test_admin_alert_date_change(self, db_session):
        with enabled_job(job_key=AdminEmailsJob.key()):
            admin_uid = app.config['EMAIL_DIABLO_ADMIN_UID']
            term_id = app.config['CURRENT_TERM_ID']
            section_id = 50004
            meeting = get_eligible_meeting(section_id=section_id,
                                           term_id=term_id)
            with test_approvals_workflow(app):
                with override_config(app, 'CURRENT_TERM_RECORDINGS_BEGIN',
                                     meeting['startDate']):
                    with override_config(app, 'CURRENT_TERM_RECORDINGS_END',
                                         meeting['endDate']):

                        def _run_jobs():
                            AdminEmailsJob(simply_yield).run()
                            QueuedEmailsJob(simply_yield).run()

                        def _schedule():
                            mock_scheduled(
                                meeting=meeting,
                                override_end_time='16:59',
                                override_start_time='08:00',
                                section_id=section_id,
                                term_id=term_id,
                            )
                            course = SisSection.get_course(
                                section_id=section_id, term_id=term_id)
                            scheduled = course['scheduled']
                            assert are_scheduled_dates_obsolete(
                                meeting=meeting, scheduled=scheduled) is False
                            assert are_scheduled_times_obsolete(
                                meeting=meeting, scheduled=scheduled) is True

                        def _assert_alert_count(count):
                            emails_sent = SentEmail.get_emails_sent_to(
                                uid=admin_uid)
                            assert len(emails_sent) == count
                            assert emails_sent[0].section_id == section_id
                            assert emails_sent[
                                0].template_type == 'admin_alert_date_change'

                        # First time scheduled.
                        _schedule()
                        _run_jobs()
                        _assert_alert_count(1)
                        # Unschedule and schedule a second time.
                        Scheduled.delete(section_id=section_id,
                                         term_id=term_id)
                        _schedule()
                        _run_jobs()
                        # Another alert is emailed to admin because it is a new schedule.
                        _assert_alert_count(2)
                        # Run jobs again and expect no alerts.
                        _run_jobs()
                        _assert_alert_count(2)
 def test_email_alert_when_canceled_course(self, db_session):
     term_id = app.config['CURRENT_TERM_ID']
     with enabled_job(job_key=InstructorEmailsJob.key()):
         with test_approvals_workflow(app):
             course = SisSection.get_course(section_id=deleted_section_id,
                                            term_id=term_id,
                                            include_deleted=True)
             room = course.get('meetings', {}).get('eligible',
                                                   [])[0]['room']
             _schedule(room['id'], deleted_section_id)
             _run_instructor_emails_job()
             _assert_email_count(1, deleted_section_id,
                                 'room_change_no_longer_eligible')
Example #7
0
    def test_alert_admin_of_room_change(self, db_session):
        """Emails admin when a scheduled course gets a room change."""
        with test_approvals_workflow(app):
            with enabled_job(job_key=AdminEmailsJob.key()):
                term_id = app.config['CURRENT_TERM_ID']
                section_id = 50004
                approved_by_uid = '10004'
                the_old_room = 'Wheeler 150'
                scheduled_in_room = Room.find_room(the_old_room)
                approval = Approval.create(
                    approved_by_uid=approved_by_uid,
                    approver_type_='instructor',
                    publish_type_='kaltura_media_gallery',
                    recording_type_='presenter_audio',
                    room_id=scheduled_in_room.id,
                    section_id=section_id,
                    term_id=term_id,
                )
                meeting = get_eligible_meeting(section_id=section_id,
                                               term_id=term_id)
                Scheduled.create(
                    instructor_uids=get_instructor_uids(term_id=term_id,
                                                        section_id=section_id),
                    kaltura_schedule_id=random.randint(1, 10),
                    meeting_days=meeting['days'],
                    meeting_end_date=get_recording_end_date(meeting),
                    meeting_end_time=meeting['endTime'],
                    meeting_start_date=get_recording_start_date(
                        meeting, return_today_if_past_start=True),
                    meeting_start_time=meeting['startTime'],
                    publish_type_=approval.publish_type,
                    recording_type_=approval.recording_type,
                    room_id=scheduled_in_room.id,
                    section_id=section_id,
                    term_id=term_id,
                )

                admin_uid = app.config['EMAIL_DIABLO_ADMIN_UID']
                # Message queued, then sent.
                AdminEmailsJob(simply_yield).run()
                QueuedEmailsJob(simply_yield).run()
                emails_sent = SentEmail.get_emails_sent_to(uid=admin_uid)
                assert len(emails_sent) == 1
                assert emails_sent[0].section_id == section_id
                assert emails_sent[
                    0].template_type == 'admin_alert_room_change'
Example #8
0
    def test_room_change_no_longer_eligible(self, db_session):
        with enabled_job(job_key=InstructorEmailsJob.key()):
            term_id = app.config['CURRENT_TERM_ID']
            section_id = 50004
            with test_approvals_workflow(app):
                def _run_jobs():
                    InstructorEmailsJob(simply_yield).run()
                    QueuedEmailsJob(simply_yield).run()

                def _schedule():
                    mock_scheduled(
                        override_room_id=Room.find_room('Barker 101').id,
                        section_id=section_id,
                        term_id=term_id,
                    )
                    course = SisSection.get_course(section_id=section_id, term_id=term_id)
                    assert course['scheduled']['hasObsoleteRoom'] is True

                def _assert_alert_count(count):
                    emails_sent = SentEmail.get_emails_of_type(
                        section_ids=[section_id],
                        template_type='room_change_no_longer_eligible',
                        term_id=term_id,
                    )
                    assert len(emails_sent) == count

                # First time scheduled.
                _schedule()
                _run_jobs()
                _assert_alert_count(1)
                # Unschedule and schedule a second time.
                Scheduled.delete(section_id=section_id, term_id=term_id)
                _schedule()
                _run_jobs()
                # Another alert is emailed to admin because it is a new schedule.
                _assert_alert_count(2)
                # Run jobs again and expect no alerts.
                _run_jobs()
                _assert_alert_count(2)