Beispiel #1
0
def generate_detailed_activity(id=random_utils.randint(0, 1000000)):
    return {
        "id": id,
        "resource_state": random_utils.randint(0, 10000),
        "athlete": {
            "id": random_utils.randint(0, 10000),
            "resource_state": random_utils.randint(0, 10000)
        },
        "name": random_utils.randomString(6),
        "distance": random_utils.randint(0, 10000),
        "moving_time": random_utils.randint(0, 10000),
        "elapsed_time": random_utils.randint(0, 10000),
        "type": "Ride",
        "start_latlng": random_utils.randomCord(),
        "end_latlng": random_utils.randomCord(),
        "start_latitude": random_utils.randomCord()[0],
        "start_longitude": random_utils.randomCord()[1],
        "achievement_count": random_utils.randint(0, 10000),
        "kudos_count": random_utils.randint(0, 10000),
        "comment_count": random_utils.randint(0, 10000),
        "map": {
            "id": random_utils.randomString(6),
            "polyline": random_utils.randomString(20),
            "resource_state": random_utils.randint(0, 10000),
            "summary_polyline": random_utils.randomString(6)
        },
        "trainer": False,
        "commute": False,
        "manual": False,
    }
Beispiel #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
Beispiel #3
0
def test_get_run_map_with_id_and_user_gets_correct_map(test_client):
    mapId = random_utils.randomString(10)
    userId = random_utils.randomString(5)

    runMap = generate_run_map({'id': mapId, 'userId': userId})
    generate_run_map({'id': mapId})
    generate_run_map({'userId': userId})

    assert runMap == unit.getRunMapByIdAndUserId(runMap.id, runMap.userId)
Beispiel #4
0
def test_add_run_maps_adds_runs_to_given_map(test_client):
    runs = []
    for i in range(5):
        runs.append(random_utils.randomString(10))

    runMap = generate_run_map({'runs': runs})

    runsToAdd = []
    for i in range(6):
        runsToAdd.append(random_utils.randomString(10))

    unit.addRunsToRunMap(runMap.id, runMap.userId, runsToAdd)
    assert (runsToAdd + runs) == \
        unit.getRunMapByIdAndUserId(runMap.id, runMap.userId).runs
def buildMarker(*overridenValues):
    data = {
        "mapId": random_utils.randomString(10),
        "text": random_utils.randomString(14),
        "cord": random_utils.randomCord()
    }

    if not overridenValues:
        overridenValues = {}

    for (key, value) in overridenValues:
        data[key] = value

    return Marker(data)
def buildRun(overridenValues={}):
    data = {
        'id': random_utils.randomString(4),
        'userId': random_utils.randomString(10),
        'polyline': random_utils.randomString(14),
        'start': random_utils.randomCord(),
        'name': random_utils.randomString(10),
        'type': random_utils.randomString(6)
    }

    for (key, value) in overridenValues.items():
        data[key] = value

    return Run(data)
def test_get_updated_user_auth_when_valid_does_nothing():
    userAuth = generate_user_auth(
        {'strava_expiration_time': int(time.time()) + 1000})
    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)
    }
    with requests_mock.Mocker() as m:
        stravaUrl = 'https://www.strava.com/api/v3/oauth/token'
        adapter = m.post(stravaUrl, text=json.dumps(strava_response))
        newUserAuth = unit.getUpdatedUserAuth(userAuth.id)
        assert userAuth.strava_auth_token == newUserAuth.strava_auth_token
        assert adapter.call_count == 0
Beispiel #8
0
def buildRunMap(overridenValues={}):
    data = {
        'id': random_utils.randomString(4),
        'mapName': random_utils.randomString(14),
        'userId': random_utils.randomString(10),
        'center': random_utils.randomCord(),
        'zoom': random_utils.randint(0, 12),
        'runs': []
    }

    for i in range(random_utils.randint(0, 5)):
        data['runs'].append(random_utils.randomString(5))

    for (key, value) in overridenValues.items():
        data[key] = value

    return RunMap(data)
Beispiel #9
0
def test_get_run_map_by_user_returns_all_run_maps_for_user(test_client):
    userId = random_utils.randomString(5)

    runMaps = []
    runMaps.append(generate_run_map({'userId': userId}))
    runMaps.append(generate_run_map({'userId': userId}))
    generate_run_map()

    assert sorted(runMaps) == sorted(unit.getRunMapByUser(userId))
Beispiel #10
0
def test_add_runs_adds_runs_and_removes_runs_more_than_max():
    runs = []
    for i in range(unit.MAX_RUNS - 5):
        runs.append(random_utils.randomString(10))

    runMap = generate_run_map({'runs': runs})

    runsToAdd = []
    for i in range(10):
        runsToAdd.append(random_utils.randomString(10))

    unit.addRunsToRunMap(runMap.id, runMap.userId, runsToAdd)

    expectedRuns = []
    expectedRuns += runsToAdd
    for i in range(unit.MAX_RUNS - 10):
        expectedRuns.append(runs[i])

    assert expectedRuns == \
        unit.getRunMapByIdAndUserId(runMap.id, runMap.userId).runs
def buildUser(overridenValues={}):
    data = {
        "id": str(random_utils.randint(0, 1000)),
        "email": random_utils.randomString(13),
        "basemap": buildBasemap(),
        "is_admin": False,
        "last_update": random_utils.randint(0, 100000)
    }

    for (key, value) in overridenValues.items():
        data[key] = value

    return User(data)
def test_get_updated_user_auth_when_expired_updates(test_client):
    userAuth = generate_user_auth(
        {'strava_expiration_time': int(time.time()) - 1000})

    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)
    }

    with requests_mock.Mocker() as m:
        stravaUrl = 'https://www.strava.com/api/v3/oauth/token'
        m.post(stravaUrl, text=json.dumps(strava_response))
        unit.getUpdatedUserAuth(userAuth.id)
        # ensure that it have been saved to the db.
        updatedUser = unit.getUserAuthById(userAuth.id)
        assert updatedUser.strava_expiration_time == Decimal(
            strava_response['expires_at'])
        assert updatedUser.strava_refresh_token == strava_response[
            'refresh_token']
        assert updatedUser.strava_auth_token == strava_response['access_token']
Beispiel #13
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
Beispiel #14
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_create_run_from_detailed_activity():
    userId = random_utils.randomString(5)
    activity = generate_detailed_activity()

    run = unit.create_run_from_detailed_activity(userId, activity)

    start = [
        Decimal(activity['start_latlng'][0]).quantize(
            unit.ROUNDING_RESOLUTION),
        Decimal(activity['start_latlng'][1]).quantize(unit.ROUNDING_RESOLUTION)
    ]
    dictOfInterest = {
        'id': activity['id'],
        'userId': userId,
        'polyline': activity['map']['polyline'],
        'start': start,
        'name': activity['name'],
        'type': activity['type']
    }
    expectedRun = Run(dictOfInterest)

    #TODO deal with the problem of decimal equality
    assert str(expectedRun.id) == run.id
    assert expectedRun.polyline == run.polyline
Beispiel #16
0
def generate_summary_activity(type, commuteFlag, id=None, hasGPS=True):
    return {
        "resource_state": random_utils.randint(0, 5),
        "athlete": {
            "id": random_utils.randint(0, 10000),
            "resource_state": random_utils.randint(0, 100)
        },
        "name": random_utils.randomString(5),
        "distance": random_utils.randomDecimal(0, 100, 3),
        "type": type,
        "workout_type": random_utils.randomString(15),
        "id": id if id else random_utils.randint(0, 1000000),
        "commute": commuteFlag,
        "external_id": random_utils.randomString(5),
        "utc_offset": random_utils.randint(-10000, 10000),
        "start_latlng": random_utils.randomCord() if hasGPS else None,
        "end_latlng": random_utils.randomCord() if hasGPS else None,
        "location_city": random_utils.randomString(6),
        "location_state": random_utils.randomString(6),
        "location_country": random_utils.randomString(4),
        "start_latitude": [random_utils.randomCord()[0]],
        "start_longitude": [random_utils.randomCord()[1]],
        "achievement_count": random_utils.randint(0, 10)
    }