def test_save_completed_activity_strava_integration_off( test_client_csrf, init_database, activate_strava_sync, add_strava_athlete): """ mock the strava APA and check it is not called""" assert flask.current_app.config["CALL_STRAVA_API"] is True u = User.query.filter_by(username=conftest.TEST_USER_USERNAME).first() load_activity = Activity.query.filter_by(user_id=u.id).first() assert load_activity is None strava_athlete = StravaAthlete.query.filter_by(user_id=u.id).first() strava_athlete.is_active = 0 db.session.commit() with patch('flask_login.utils._get_user') as current_user: current_user.return_value.id = u.id current_user.return_value.get_id.return_value = u.id with patch('app.services.strava.create_activity') as mock_strava: mock_strava.return_value = True activity_date = datetime.utcnow() - timedelta(days=1, hours=7) activity_date_str = activity_date.strftime('%d/%m/%Y %H:%M') activity_utc_time = utils.get_utc_from_local_time( activity_date, 'UTC') activity = Activity(type=1, title='title', duration=20, timestamp=activity_utc_time, user_id=u.id) routes.save_completed_activity(activity, activity_date, 'UTC') load_activity = Activity.query.filter_by(user_id=u.id).first() assert load_activity.user_id == u.id assert load_activity.type == 1 assert load_activity.title == 'title' assert load_activity.duration == 20 assert mock_strava.called is False
def test_get_utc_from_local_time(): # convert a string to a date/time that is in a local timezone and # check its UTC date/time is correct iso_date = '2020-06-18T21:58:33' my_date = datetime.strptime(iso_date, '%Y-%m-%dT%H:%M:%S') my_date_utc = utils.get_utc_from_local_time(my_date, 'America/Los_Angeles') assert my_date_utc.day == 19 assert my_date_utc.month == 6 assert my_date_utc.minute == 58 assert my_date_utc.second == 33
def test_construct_strava_activity_data_no_distance(test_client, init_database): activity_date = datetime.utcnow() - timedelta(days=1, hours=7) activity_date_str = activity_date.strftime('%d/%m/%Y %H:%M') activity_utc_time = utils.get_utc_from_local_time(activity_date, 'UTC') activity = Activity(id=1, type=3, title='some title', description='a description', duration=35, local_timestamp=activity_utc_time) strava_data = ss.construct_strava_activity_data(activity) assert strava_data is not None assert len(strava_data) == 5 assert strava_data['name'] == 'some title' assert strava_data['type'] == 'Ride' assert strava_data[ 'start_date_local'] == activity.local_timestamp.isoformat() assert strava_data['elapsed_time'] == activity.duration * 60 assert strava_data['description'] == activity.description
def index(): """ shows the user's regular activities in order for them to log it :return: :rtype: """ activities = RegularActivity.query.filter_by( user_id=current_user.get_id()).all() form = CompletedActivity() if form.validate_on_submit(): # user submits date and time in local time so need to convert to UTC activity_utc_time = utils.get_utc_from_local_time( form.timestamp.data, form.user_tz.data) activity = Activity(title=form.title.data, description=form.description.data, type=int(form.activity_type.data), duration=form.duration.data, timestamp=activity_utc_time, distance=form.distance.data, user_id=current_user.get_id()) # want to ensure local time and timezone information is saved also save_completed_activity(activity, form.timestamp.data, form.user_tz.data) flash('Well done on completing {} today'.format(activity.title)) return redirect(url_for('main.index')) form.timestamp.data = datetime.utcnow() return render_template('index.html', title='Home', form=form, regular_activities=activities, icons=ICONS_LOOKUP, colors=ACTIVITY_COLOR_LOOKUP, form_url=url_for('main.index'))