def put(self, args): """Update a reminder.""" reminder_id = args['reminder_id'] anticipation_minutes = args['anticipation_minutes'] with session_scope() as session: # Get the user id from the token token = flask.request.headers.environ['HTTP_AUTHORIZATION'][7:] user_id = authentication.get_token_field(token.encode(), 'user') success, msg = reminders.update_reminder(session, reminder_id, anticipation_minutes, user_id) if success: return flask.make_response( flask.jsonify({ 'reminder_list': auxiliary.list_to_json( reminders.get_reminders(session, user_id)) }), 201) else: if msg == 'Not found': return flask.make_response('', 404) else: return flask.make_response(msg, 400)
def test_update_reminder_ok(self) -> None: """ Test the function update_reminder with success. """ # The expected result expected_result = True, None # Prepare the mocks reminder = models.Reminder(60, 15, 1) reminder.id = 123 db_calls_mock.get_reminder_id_user.return_value = reminder show_session = models.ShowSession(None, None, datetime.datetime.utcnow() + datetime.timedelta(hours=5), 1, 1) db_calls_mock.get_show_session.return_value = show_session db_calls_mock.update_reminder.return_value = True # Call the function actual_result = reminders.update_reminder(self.session, 1, 120, 1) # Verify the result self.assertEqual(expected_result, actual_result) # Verify the calls to the mocks db_calls_mock.get_reminder_id_user.assert_called_with(self.session, 1, 1) db_calls_mock.get_show_session.assert_called_with(self.session, 15) db_calls_mock.update_reminder.assert_called()
def test_update_reminder_error_01(self) -> None: """ Test the function update_reminder with an reminder id or user id. """ # The expected result expected_result = False, 'Not found' # Prepare the mocks db_calls_mock.get_reminder_id_user.return_value = None # Call the function actual_result = reminders.update_reminder(self.session, -1, 60, 1) # Verify the result self.assertEqual(expected_result, actual_result) # Verify the calls to the mocks db_calls_mock.get_reminder_id_user.assert_called_with(self.session, -1, 1)
def test_update_reminder_error_02(self) -> None: """ Test the function update_reminder with anticipation in less than two hours from airing. """ # The expected result expected_result = False, 'Invalid anticipation time' # Prepare the mocks reminder = models.Reminder(60, 15, 1) reminder.id = 123 db_calls_mock.get_reminder_id_user.return_value = reminder show_session = models.ShowSession(None, None, datetime.datetime.utcnow() + datetime.timedelta(hours=1), 1, 1) db_calls_mock.get_show_session.return_value = show_session # Call the function actual_result = reminders.update_reminder(self.session, 1, 60, 1) # Verify the result self.assertEqual(expected_result, actual_result) # Verify the calls to the mocks db_calls_mock.get_reminder_id_user.assert_called_with(self.session, 1, 1) db_calls_mock.get_show_session.assert_called_with(self.session, 15)