Beispiel #1
0
def test_strava_athlete_model(test_client, init_database):
    # create a regular activity and use this to
    # create the new activity
    # link to the test user created
    u = User.query.filter_by(username=conftest.TEST_USER_USERNAME).first()
    strava_athlete = StravaAthlete.query.filter_by(user_id=u.id).first()
    assert strava_athlete is None
    authorize_details = json.loads(conftest.STRAVA_RESPONSE_EXAMPLE)
    scope = 'activity:write'
    strava_athlete = ss.create_strava_athlete(authorize_details, u.id, scope)

    db.session.add(strava_athlete)
    db.session.commit()

    load_strava_athlete = StravaAthlete.query.filter_by(user_id=u.id).first()
    assert strava_athlete.user_id == load_strava_athlete.user_id
    assert strava_athlete.athlete_id == load_strava_athlete.athlete_id
    assert strava_athlete.scope == load_strava_athlete.scope
    assert strava_athlete.access_token == load_strava_athlete.access_token
    assert strava_athlete.access_token_expires_at == load_strava_athlete.access_token_expires_at
    assert strava_athlete.access_token_expires_in == load_strava_athlete.access_token_expires_in
    assert strava_athlete.refresh_token == load_strava_athlete.refresh_token
    assert strava_athlete.last_updated == load_strava_athlete.last_updated

    assert "StravaAthlete" in repr(strava_athlete)
Beispiel #2
0
def add_strava_athlete():
    u = User.query.filter_by(username=TEST_USER_USERNAME).first()

    authorize_details = json.loads(STRAVA_RESPONSE_EXAMPLE)
    scope = 'activity:write'
    strava_athlete = ss.create_strava_athlete(authorize_details, u.id, scope)

    db.session.add(strava_athlete)
    db.session.commit()
Beispiel #3
0
def test_strava_athlete_create(test_client, init_database):
    u = User.query.filter_by(username=conftest.TEST_USER_USERNAME).first()

    authorize_details = json.loads(conftest.STRAVA_RESPONSE_EXAMPLE)
    scope = 'activity:write'
    strava_athlete = ss.create_strava_athlete(authorize_details, u.id, scope)

    assert strava_athlete.athlete_id == 123456
    assert strava_athlete.scope == scope
    assert strava_athlete.access_token == 'd188074a'
    assert strava_athlete.access_token_expires_at == conftest.EXPIRES_AT_SECS
    assert strava_athlete.access_token_expires_in == 21390
    assert strava_athlete.refresh_token == '767bdc6899'
Beispiel #4
0
def test_refresh_access_token_none(test_client, init_database):
    u = User.query.filter_by(username=conftest.TEST_USER_USERNAME).first()

    with patch('app.services.strava.OAuth2Session') as mock_oauth:
        authorize_details = json.loads(conftest.STRAVA_RESPONSE_EXAMPLE)
        scope = 'activity:write'
        strava_athlete = ss.create_strava_athlete(authorize_details, u.id,
                                                  scope)

        db.session.add(strava_athlete)
        db.session.commit()

        # mock the refresh_token call
        mock_oauth.return_value = Mock()
        mock_oauth.return_value.refresh_token.return_value = None
        new_token = ss.refresh_access_token(u.id)

        assert new_token is None
Beispiel #5
0
def strava_callback():
    """
    is called back by strava when the user has either provided or rejected access
    by this application to their strava profile
    :return:
    :rtype:
    """
    # need to get the scope details to check the user has given correct permissions
    # to this app
    # resp = oauth.strava.get('scope')
    # will be called back with a scope query string parameter
    scope = request.args.get('scope')
    code = request.args.get('code')
    error = request.args.get('error')

    if error == 'access_denied':
        flash('Strava: Access Denied')
        return redirect(url_for('main.user'))

    if len(code) == 0:
        # error as strava should return a code
        flash('Strava: Invalid response')
        return redirect(url_for('main.user'))

    if 'activity:read' in scope and 'activity:write' in scope:

        access_token = oauth.strava.authorize_access_token(
            code=code,
            grant_type='authorization_code',
            client_id=oauth.strava.client_id,
            client_secret=oauth.strava.client_secret)
        # authorize_details = access_token.json()
        # will need to save this access_token against the user
        # will then need to refresh it when it needs to be used against the API
        # see if we already have a strava record for this user
        # if so, then we can update it, otherwise, we create it
        strava_athlete = StravaAthlete.query.filter_by(
            user_id=current_user.get_id()).first()
        if not strava_athlete:
            # create the record
            strava_athlete = ss.create_strava_athlete(access_token,
                                                      current_user.get_id(),
                                                      scope)
            db.session.add(strava_athlete)
            db.session.commit()
        else:
            # update the record with the new details
            new_strava_athlete = ss.create_strava_athlete(
                access_token, current_user.get_id(), scope)
            strava_athlete.scope = new_strava_athlete.scope
            strava_athlete.access_token = new_strava_athlete.access_token
            strava_athlete.access_token_expires_at = new_strava_athlete.access_token_expires_at
            strava_athlete.access_token_expires_in = new_strava_athlete.access_token_expires_in
            strava_athlete.refresh_token = new_strava_athlete.refresh_token
            strava_athlete.last_updated = new_strava_athlete.last_updated
            strava_athlete.is_active = 1
            db.session.commit()
    else:
        flash(
            'Please ensure you agree to sharing your data with LogMyExercise.')
        return redirect(url_for('main.user'))

    flash('Thank you for granting access to your Strava details.')
    ss.log_strava_event(strava_athlete.athlete_id, 'Authorize')
    return redirect(url_for('main.user'))