def generate_auth_user():
    user = buildUserAuth()
    usersAuthForDbToDelete.append(user)

    tableName = current_app.config['TABLE_NAMES']['USER_AUTH_TABLE']
    dynamo.get_table(tableName).put_item(Item=user.generateDict())
    return user
예제 #2
0
def test_refresh_token(test_client):
    userAuth = buildUserAuth()
    strava_client_id = 'TESTCLIENTID'
    strava_client_secret = 'TESTSECRET'

    strava_response = {
        'token_type': 'Bearer',
        'access_token': random_utils.randomString(10),
        'expires_at': random_utils.randint(0, 10000),
        'expires_in': random_utils.randint(0, 100),
        'refresh_token': random_utils.randomString(10)
    }
    expectedPostBody = {
        'client_id': strava_client_id,
        'client_secret': strava_client_secret,
        'grant_type': 'refresh_token',
        'refresh_token': userAuth.strava_refresh_token
    }

    with requests_mock.Mocker() as m:
        stravaUrl = 'https://www.strava.com/api/v3/oauth/token'
        adapter = m.post(stravaUrl, text=json.dumps(strava_response))

        assert unit.refresh_auth_token(userAuth) == strava_response
        adapter.call_count == 1
        assert "client_id=" + expectedPostBody[
            'client_id'] in adapter.last_request.text
        assert "client_secret=" + expectedPostBody[
            'client_secret'] in adapter.last_request.text
        assert "grant_type=" + expectedPostBody[
            'grant_type'] in adapter.last_request.text
        assert "refresh_token=" + expectedPostBody[
            'refresh_token'] in adapter.last_request.text
예제 #3
0
def test_collect_runs_returns_the_runs_of_interest():
    userAuth = buildUserAuth()
    runIds = []
    for i in range(10):
        id = random_utils.randint(0, 10000)
        run = buildRun(overridenValues={'id': str(id), 'userId': userAuth.id})
        runIds.append(id)

    with requests_mock.Mocker() as m:
        strava_api.generate_mock_strava_api(runIds, 30, m)
        runs = unit.collectNewRuns(userAuth, last_update=5)

    assert len(runs) == 10
예제 #4
0
def test_update_user_only_inserts_runs_up_to_max_into_db():
    userAuth = buildUserAuth()
    runIds = []
    for i in range(40):
        id = random_utils.randint(0, 10000)
        run = buildRun(overridenValues={'id': str(id), 'userId': userAuth.id})
        runIds.append(id)

    with requests_mock.Mocker() as m:
        strava_api.generate_mock_strava_api(runIds, 0, m)
        unit.MAX_RUNS_TO_COLLECT = 10
        runs = unit.collectNewRuns(userAuth)

    assert len(runs) == 30
예제 #5
0
def test_get_activity_by_id():
    userAuth = buildUserAuth()
    activityId = random_utils.randint(0, 100)
    strava_response = {'fakedata': random_utils.randomString(6)}

    with requests_mock.Mocker() as m:
        stravaUrl = 'https://www.strava.com/api/v3/activities/' \
                    + str(activityId)
        headers = {'Authorization': 'Bearer ' + userAuth.strava_auth_token}
        print(stravaUrl)
        m.get(stravaUrl,
              text=json.dumps(strava_response),
              request_headers=headers)

        assert unit.get_activity_by_id(userAuth, activityId) == strava_response
예제 #6
0
def test_list_activities_passes_in_required_header_and_reqest():

    userAuth = buildUserAuth()
    strava_response = []
    for i in range(5):
        strava_response.append({'fakedata': random_utils.randomString(6)})

    with requests_mock.Mocker() as m:
        stravaUrl = 'https://www.strava.com/api/v3/athlete/activities'
        stravaUrlParmas = '?page=1&per_page=30'
        headers = {'Authorization': 'Bearer ' + userAuth.strava_auth_token}
        m.get(stravaUrl + stravaUrlParmas,
              text=json.dumps(strava_response),
              request_headers=headers)

        assert unit.list_activities(userAuth, 1) == strava_response
def test_exchange_token_with_bad_scope_returns_error(test_client):
    userAuth = buildUserAuth()
    usersAuthForDbToDelete.append(userAuth)

    with requests_mock.Mocker() as m:
        athleteCode = "asdfasdfds"
        stravaUrl = 'https://www.strava.com/oauth/token'
        stravaUrlParmas = '?client_id=TESTCLIENTID&client_secret=TESTSECRET' \
            + '&code=' + athleteCode + '&grant_type=authorization_code'
        m.post(stravaUrl + stravaUrlParmas, text="{}")

        reqUrl = 'user/' + userAuth.id + '/auth/exchange_token?state=&code=' \
            + athleteCode + '&scope=read'

        response = test_client.get(reqUrl)
        assert response.status_code == 401

        tableName = current_app.config['TABLE_NAMES']['USER_AUTH_TABLE']
        dbRes = dynamo.get_table(tableName).get_item(Key={'id': userAuth.id})
        assert 'Item' not in dbRes
def test_exchange_token_calls_strava_api_and_sets_token(test_client):
    userAuth = buildUserAuth()
    usersAuthForDbToDelete.append(userAuth)

    strava_response = {
        "token_type": "Bearer",
        "expires_at": userAuth.strava_expiration_time,
        "expires_in": 21600,
        "refresh_token": userAuth.strava_refresh_token,
        "access_token": userAuth.strava_auth_token,
        "athlete": {
            "id": userAuth.strava_athlete_id,
            "username": userAuth.strava_username,
            "resource_state": 2
        }
    }

    with requests_mock.Mocker() as m:
        athleteCode = "asdfasdfds"
        stravaUrl = 'https://www.strava.com/oauth/token'
        stravaUrlParmas = '?client_id=TESTCLIENTID&client_secret=TESTSECRET' \
            + '&code=' + athleteCode + '&grant_type=authorization_code'
        m.post(stravaUrl + stravaUrlParmas, text=json.dumps(strava_response))

        reqUrl = 'user/' + userAuth.id + '/auth/exchange_token?state=&code=' \
            + athleteCode + '&scope=read,activity:read'

        response = test_client.get(reqUrl)
        assert response.status_code == 200

        tableName = current_app.config['TABLE_NAMES']['USER_AUTH_TABLE']
        dbRes = dynamo.get_table(tableName).get_item(Key={'id': userAuth.id})
        userAuth = UserAuth(dbRes['Item'])
        assert userAuth == UserAuth(dbRes['Item'])

    # Ensure that a user entry has been created
    userTableName = current_app.config['TABLE_NAMES']['USER_TABLE']
    userRes = dynamo.get_table(userTableName).get_item(Key={'id': userAuth.id})
    assert User(id=userAuth.id).generateDict() == userRes['Item']
def generate_user_auth(init_value={}):
    userAuth = buildUserAuth(overridenValues=init_value)
    userAuthsForDbToDelete.append(userAuth)
    unit.createUserAuth(userAuth)
    return userAuth