def test_client():
    app = buildTestApp()

    # Establish an application context before running the tests.
    ctx = app.app_context()
    ctx.push()
    tableName = current_app.config['TABLE_NAMES']['USER_AUTH_TABLE']

    yield app.test_client()  # this is where the testing happens!

    # After
    exceptionsToRaise = []
    for user in userAuthsForDbToDelete:
        try:
            dynamo.get_table(tableName).delete_item(Key={'id': user.id})
        except ClientError as e:
            exceptionsToRaise.append(e)

    for e in exceptionsToRaise:
        print(e.response['Error']['Message'])

    if not exceptionsToRaise == []:
        raise Exception("Error while trying to delete items from db")

    ctx.pop()
Esempio n. 2
0
def cleanupUsers(exceptions):
    tableName = current_app.config['TABLE_NAMES']['USER_TABLE']
    for user in userForDbToDelete:
        try:
            dynamo.get_table(tableName).delete_item(Key={'id': user.id})
        except ClientError as e:
            exceptions.append(e)
def generate_user():
    user = buildUser()
    usersForDbToDelete.append(user)

    tableName = current_app.config['TABLE_NAMES']['USER_TABLE']
    dynamo.get_table(tableName).put_item(Item=user.generateDict())
    return user
Esempio n. 4
0
def generate_run(initValues={}):
    run = buildRun(initValues)
    runForDbToDelete.append(run)

    tableName = current_app.config['TABLE_NAMES']['RUN_TABLE']
    dynamo.get_table(tableName).put_item(Item=run.generateDict())
    return run
def cleanupRunMaps(exceptions):
    tableName = current_app.config['TABLE_NAMES']['RUN_MAP_TABLE']
    for runMap in runMapForDbToDelete:
        try:
            dynamo.get_table(tableName).delete_item(
                Key={'id': runMap.id, 'userId': runMap.userId})
        except ClientError as e:
            exceptions.append(e)
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 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 runMapTable():
    tableName = current_app.config['TABLE_NAMES']['RUN_MAP_TABLE']
    return dynamo.get_table(tableName)
Esempio n. 9
0
def userAuthTable():
    tableName = current_app.config['TABLE_NAMES']['USER_AUTH_TABLE']
    return dynamo.get_table(tableName)