API - remove intermediate directory and rename api directory
This commit is contained in:
0
fittrackee/tests/__init__.py
Normal file
0
fittrackee/tests/__init__.py
Normal file
683
fittrackee/tests/conftest.py
Normal file
683
fittrackee/tests/conftest.py
Normal file
@ -0,0 +1,683 @@
|
||||
import datetime
|
||||
import os
|
||||
|
||||
import pytest
|
||||
from fittrackee import create_app, db
|
||||
from fittrackee.activities.models import Activity, ActivitySegment, Sport
|
||||
from fittrackee.application.models import AppConfig
|
||||
from fittrackee.application.utils import update_app_config_from_database
|
||||
from fittrackee.users.models import User
|
||||
|
||||
os.environ['FLASK_ENV'] = 'testing'
|
||||
os.environ['APP_SETTINGS'] = 'fittrackee.config.TestingConfig'
|
||||
# to avoid resetting dev database during tests
|
||||
os.environ['DATABASE_URL'] = os.getenv('DATABASE_TEST_URL')
|
||||
|
||||
|
||||
def get_app_config(with_config=False):
|
||||
if with_config:
|
||||
config = AppConfig()
|
||||
config.gpx_limit_import = 10
|
||||
config.max_single_file_size = 1 * 1024 * 1024
|
||||
config.max_zip_file_size = 1 * 1024 * 1024 * 10
|
||||
config.max_users = 100
|
||||
db.session.add(config)
|
||||
db.session.commit()
|
||||
return config
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def get_app(with_config=False):
|
||||
app = create_app()
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
app_db_config = get_app_config(with_config)
|
||||
if app_db_config:
|
||||
update_app_config_from_database(app, app_db_config)
|
||||
yield app
|
||||
db.session.remove()
|
||||
db.drop_all()
|
||||
# close unused idle connections => avoid the following error:
|
||||
# FATAL: remaining connection slots are reserved for non-replication
|
||||
# superuser connections
|
||||
db.engine.dispose()
|
||||
return app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app(monkeypatch):
|
||||
monkeypatch.setenv('EMAIL_URL', 'smtp://none:none@0.0.0.0:1025')
|
||||
if os.getenv('TILE_SERVER_URL'):
|
||||
monkeypatch.delenv('TILE_SERVER_URL')
|
||||
if os.getenv('MAP_ATTRIBUTION'):
|
||||
monkeypatch.delenv('MAP_ATTRIBUTION')
|
||||
yield from get_app(with_config=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app_no_config():
|
||||
yield from get_app(with_config=False)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app_ssl(monkeypatch):
|
||||
monkeypatch.setenv('EMAIL_URL', 'smtp://none:none@0.0.0.0:1025?ssl=True')
|
||||
yield from get_app(with_config=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app_tls(monkeypatch):
|
||||
monkeypatch.setenv('EMAIL_URL', 'smtp://none:none@0.0.0.0:1025?tls=True')
|
||||
yield from get_app(with_config=True)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def app_config():
|
||||
config = AppConfig()
|
||||
config.gpx_limit_import = 10
|
||||
config.max_single_file_size = 1048576
|
||||
config.max_zip_file_size = 10485760
|
||||
config.max_users = 0
|
||||
db.session.add(config)
|
||||
db.session.commit()
|
||||
return config
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def user_1():
|
||||
user = User(username='test', email='test@test.com', password='12345678')
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def user_1_admin():
|
||||
admin = User(
|
||||
username='admin', email='admin@example.com', password='12345678'
|
||||
)
|
||||
admin.admin = True
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
return admin
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def user_1_full():
|
||||
user = User(username='test', email='test@test.com', password='12345678')
|
||||
user.first_name = 'John'
|
||||
user.last_name = 'Doe'
|
||||
user.bio = 'just a random guy'
|
||||
user.location = 'somewhere'
|
||||
user.language = 'en'
|
||||
user.timezone = 'America/New_York'
|
||||
user.birth_date = datetime.datetime.strptime('01/01/1980', '%d/%m/%Y')
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def user_1_paris():
|
||||
user = User(username='test', email='test@test.com', password='12345678')
|
||||
user.timezone = 'Europe/Paris'
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def user_2():
|
||||
user = User(username='toto', email='toto@toto.com', password='87654321')
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def user_2_admin():
|
||||
user = User(username='toto', email='toto@toto.com', password='87654321')
|
||||
user.admin = True
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def user_3():
|
||||
user = User(username='sam', email='sam@test.com', password='12345678')
|
||||
user.weekm = True
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def sport_1_cycling():
|
||||
sport = Sport(label='Cycling')
|
||||
db.session.add(sport)
|
||||
db.session.commit()
|
||||
return sport
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def sport_1_cycling_inactive():
|
||||
sport = Sport(label='Cycling')
|
||||
sport.is_active = False
|
||||
db.session.add(sport)
|
||||
db.session.commit()
|
||||
return sport
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def sport_2_running():
|
||||
sport = Sport(label='Running')
|
||||
db.session.add(sport)
|
||||
db.session.commit()
|
||||
return sport
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def activity_cycling_user_1():
|
||||
activity = Activity(
|
||||
user_id=1,
|
||||
sport_id=1,
|
||||
activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'),
|
||||
distance=10,
|
||||
duration=datetime.timedelta(seconds=3600),
|
||||
)
|
||||
activity.max_speed = 10
|
||||
activity.ave_speed = 10
|
||||
activity.moving = activity.duration
|
||||
db.session.add(activity)
|
||||
db.session.commit()
|
||||
return activity
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def activity_cycling_user_1_segment():
|
||||
activity_segment = ActivitySegment(activity_id=1, segment_id=0)
|
||||
activity_segment.duration = datetime.timedelta(seconds=6000)
|
||||
activity_segment.moving = activity_segment.duration
|
||||
activity_segment.distance = 5
|
||||
db.session.add(activity_segment)
|
||||
db.session.commit()
|
||||
return activity_segment
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def activity_running_user_1():
|
||||
activity = Activity(
|
||||
user_id=1,
|
||||
sport_id=2,
|
||||
activity_date=datetime.datetime.strptime('01/04/2018', '%d/%m/%Y'),
|
||||
distance=12,
|
||||
duration=datetime.timedelta(seconds=6000),
|
||||
)
|
||||
activity.moving = activity.duration
|
||||
db.session.add(activity)
|
||||
db.session.commit()
|
||||
return activity
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def seven_activities_user_1():
|
||||
activity = Activity(
|
||||
user_id=1,
|
||||
sport_id=1,
|
||||
activity_date=datetime.datetime.strptime('20/03/2017', '%d/%m/%Y'),
|
||||
distance=5,
|
||||
duration=datetime.timedelta(seconds=1024),
|
||||
)
|
||||
activity.ave_speed = float(activity.distance) / (1024 / 3600)
|
||||
activity.moving = activity.duration
|
||||
db.session.add(activity)
|
||||
db.session.flush()
|
||||
activity = Activity(
|
||||
user_id=1,
|
||||
sport_id=1,
|
||||
activity_date=datetime.datetime.strptime('01/06/2017', '%d/%m/%Y'),
|
||||
distance=10,
|
||||
duration=datetime.timedelta(seconds=3456),
|
||||
)
|
||||
activity.ave_speed = float(activity.distance) / (3456 / 3600)
|
||||
activity.moving = activity.duration
|
||||
db.session.add(activity)
|
||||
db.session.flush()
|
||||
activity = Activity(
|
||||
user_id=1,
|
||||
sport_id=1,
|
||||
activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'),
|
||||
distance=10,
|
||||
duration=datetime.timedelta(seconds=1024),
|
||||
)
|
||||
activity.ave_speed = float(activity.distance) / (1024 / 3600)
|
||||
activity.moving = activity.duration
|
||||
db.session.add(activity)
|
||||
db.session.flush()
|
||||
activity = Activity(
|
||||
user_id=1,
|
||||
sport_id=1,
|
||||
activity_date=datetime.datetime.strptime('23/02/2018', '%d/%m/%Y'),
|
||||
distance=1,
|
||||
duration=datetime.timedelta(seconds=600),
|
||||
)
|
||||
activity.ave_speed = float(activity.distance) / (600 / 3600)
|
||||
activity.moving = activity.duration
|
||||
db.session.add(activity)
|
||||
db.session.flush()
|
||||
activity = Activity(
|
||||
user_id=1,
|
||||
sport_id=1,
|
||||
activity_date=datetime.datetime.strptime('23/02/2018', '%d/%m/%Y'),
|
||||
distance=10,
|
||||
duration=datetime.timedelta(seconds=1000),
|
||||
)
|
||||
activity.ave_speed = float(activity.distance) / (1000 / 3600)
|
||||
activity.moving = activity.duration
|
||||
db.session.add(activity)
|
||||
db.session.flush()
|
||||
activity = Activity(
|
||||
user_id=1,
|
||||
sport_id=1,
|
||||
activity_date=datetime.datetime.strptime('01/04/2018', '%d/%m/%Y'),
|
||||
distance=8,
|
||||
duration=datetime.timedelta(seconds=6000),
|
||||
)
|
||||
activity.ave_speed = float(activity.distance) / (6000 / 3600)
|
||||
activity.moving = activity.duration
|
||||
db.session.add(activity)
|
||||
db.session.flush()
|
||||
activity = Activity(
|
||||
user_id=1,
|
||||
sport_id=1,
|
||||
activity_date=datetime.datetime.strptime('09/05/2018', '%d/%m/%Y'),
|
||||
distance=10,
|
||||
duration=datetime.timedelta(seconds=3000),
|
||||
)
|
||||
activity.ave_speed = float(activity.distance) / (3000 / 3600)
|
||||
activity.moving = activity.duration
|
||||
db.session.add(activity)
|
||||
db.session.commit()
|
||||
return activity
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def activity_cycling_user_2():
|
||||
activity = Activity(
|
||||
user_id=2,
|
||||
sport_id=1,
|
||||
activity_date=datetime.datetime.strptime('23/01/2018', '%d/%m/%Y'),
|
||||
distance=15,
|
||||
duration=datetime.timedelta(seconds=3600),
|
||||
)
|
||||
activity.moving = activity.duration
|
||||
db.session.add(activity)
|
||||
db.session.commit()
|
||||
return activity
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def gpx_file():
|
||||
return (
|
||||
'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'
|
||||
'<gpx xmlns:gpxdata="http://www.cluetrust.com/XML/GPXDATA/1/0" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxext="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns="http://www.topografix.com/GPX/1/1">' # noqa
|
||||
' <metadata/>'
|
||||
' <trk>'
|
||||
' <name>just an activity</name>'
|
||||
' <trkseg>'
|
||||
' <trkpt lat="44.68095" lon="6.07367">'
|
||||
' <ele>998</ele>'
|
||||
' <time>2018-03-13T12:44:45Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68091" lon="6.07367">'
|
||||
' <ele>998</ele>'
|
||||
' <time>2018-03-13T12:44:50Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.6808" lon="6.07364">'
|
||||
' <ele>994</ele>'
|
||||
' <time>2018-03-13T12:45:00Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68075" lon="6.07364">'
|
||||
' <ele>994</ele>'
|
||||
' <time>2018-03-13T12:45:05Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68071" lon="6.07364">'
|
||||
' <ele>994</ele>'
|
||||
' <time>2018-03-13T12:45:10Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68049" lon="6.07361">'
|
||||
' <ele>993</ele>'
|
||||
' <time>2018-03-13T12:45:30Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68019" lon="6.07356">'
|
||||
' <ele>992</ele>'
|
||||
' <time>2018-03-13T12:45:55Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68014" lon="6.07355">'
|
||||
' <ele>992</ele>'
|
||||
' <time>2018-03-13T12:46:00Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67995" lon="6.07358">'
|
||||
' <ele>987</ele>'
|
||||
' <time>2018-03-13T12:46:15Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67977" lon="6.07364">'
|
||||
' <ele>987</ele>'
|
||||
' <time>2018-03-13T12:46:30Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67972" lon="6.07367">'
|
||||
' <ele>987</ele>'
|
||||
' <time>2018-03-13T12:46:35Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67966" lon="6.07368">'
|
||||
' <ele>987</ele>'
|
||||
' <time>2018-03-13T12:46:40Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67961" lon="6.0737">'
|
||||
' <ele>986</ele>'
|
||||
' <time>2018-03-13T12:46:45Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67938" lon="6.07377">'
|
||||
' <ele>986</ele>'
|
||||
' <time>2018-03-13T12:47:05Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67933" lon="6.07381">'
|
||||
' <ele>986</ele>'
|
||||
' <time>2018-03-13T12:47:10Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67922" lon="6.07385">'
|
||||
' <ele>985</ele>'
|
||||
' <time>2018-03-13T12:47:20Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67911" lon="6.0739">'
|
||||
' <ele>980</ele>'
|
||||
' <time>2018-03-13T12:47:30Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.679" lon="6.07399">'
|
||||
' <ele>980</ele>'
|
||||
' <time>2018-03-13T12:47:40Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67896" lon="6.07402">'
|
||||
' <ele>980</ele>'
|
||||
' <time>2018-03-13T12:47:45Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67884" lon="6.07408">'
|
||||
' <ele>979</ele>'
|
||||
' <time>2018-03-13T12:47:55Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67863" lon="6.07423">'
|
||||
' <ele>981</ele>'
|
||||
' <time>2018-03-13T12:48:15Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67858" lon="6.07425">'
|
||||
' <ele>980</ele>'
|
||||
' <time>2018-03-13T12:48:20Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67842" lon="6.07434">'
|
||||
' <ele>979</ele>'
|
||||
' <time>2018-03-13T12:48:35Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67837" lon="6.07435">'
|
||||
' <ele>979</ele>'
|
||||
' <time>2018-03-13T12:48:40Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67822" lon="6.07442">'
|
||||
' <ele>975</ele>'
|
||||
' <time>2018-03-13T12:48:55Z</time>'
|
||||
' </trkpt>'
|
||||
' </trkseg>'
|
||||
' </trk>'
|
||||
'</gpx>'
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def gpx_file_wo_name():
|
||||
return (
|
||||
'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'
|
||||
'<gpx xmlns:gpxdata="http://www.cluetrust.com/XML/GPXDATA/1/0" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxext="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns="http://www.topografix.com/GPX/1/1">' # noqa
|
||||
' <metadata/>'
|
||||
' <trk>'
|
||||
' <trkseg>'
|
||||
' <trkpt lat="44.68095" lon="6.07367">'
|
||||
' <ele>998</ele>'
|
||||
' <time>2018-03-13T12:44:45Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68091" lon="6.07367">'
|
||||
' <ele>998</ele>'
|
||||
' <time>2018-03-13T12:44:50Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.6808" lon="6.07364">'
|
||||
' <ele>994</ele>'
|
||||
' <time>2018-03-13T12:45:00Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68075" lon="6.07364">'
|
||||
' <ele>994</ele>'
|
||||
' <time>2018-03-13T12:45:05Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68071" lon="6.07364">'
|
||||
' <ele>994</ele>'
|
||||
' <time>2018-03-13T12:45:10Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68049" lon="6.07361">'
|
||||
' <ele>993</ele>'
|
||||
' <time>2018-03-13T12:45:30Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68019" lon="6.07356">'
|
||||
' <ele>992</ele>'
|
||||
' <time>2018-03-13T12:45:55Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68014" lon="6.07355">'
|
||||
' <ele>992</ele>'
|
||||
' <time>2018-03-13T12:46:00Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67995" lon="6.07358">'
|
||||
' <ele>987</ele>'
|
||||
' <time>2018-03-13T12:46:15Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67977" lon="6.07364">'
|
||||
' <ele>987</ele>'
|
||||
' <time>2018-03-13T12:46:30Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67972" lon="6.07367">'
|
||||
' <ele>987</ele>'
|
||||
' <time>2018-03-13T12:46:35Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67966" lon="6.07368">'
|
||||
' <ele>987</ele>'
|
||||
' <time>2018-03-13T12:46:40Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67961" lon="6.0737">'
|
||||
' <ele>986</ele>'
|
||||
' <time>2018-03-13T12:46:45Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67938" lon="6.07377">'
|
||||
' <ele>986</ele>'
|
||||
' <time>2018-03-13T12:47:05Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67933" lon="6.07381">'
|
||||
' <ele>986</ele>'
|
||||
' <time>2018-03-13T12:47:10Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67922" lon="6.07385">'
|
||||
' <ele>985</ele>'
|
||||
' <time>2018-03-13T12:47:20Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67911" lon="6.0739">'
|
||||
' <ele>980</ele>'
|
||||
' <time>2018-03-13T12:47:30Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.679" lon="6.07399">'
|
||||
' <ele>980</ele>'
|
||||
' <time>2018-03-13T12:47:40Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67896" lon="6.07402">'
|
||||
' <ele>980</ele>'
|
||||
' <time>2018-03-13T12:47:45Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67884" lon="6.07408">'
|
||||
' <ele>979</ele>'
|
||||
' <time>2018-03-13T12:47:55Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67863" lon="6.07423">'
|
||||
' <ele>981</ele>'
|
||||
' <time>2018-03-13T12:48:15Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67858" lon="6.07425">'
|
||||
' <ele>980</ele>'
|
||||
' <time>2018-03-13T12:48:20Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67842" lon="6.07434">'
|
||||
' <ele>979</ele>'
|
||||
' <time>2018-03-13T12:48:35Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67837" lon="6.07435">'
|
||||
' <ele>979</ele>'
|
||||
' <time>2018-03-13T12:48:40Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67822" lon="6.07442">'
|
||||
' <ele>975</ele>'
|
||||
' <time>2018-03-13T12:48:55Z</time>'
|
||||
' </trkpt>'
|
||||
' </trkseg>'
|
||||
' </trk>'
|
||||
'</gpx>'
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def gpx_file_wo_track():
|
||||
return (
|
||||
'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'
|
||||
'<gpx xmlns:gpxdata="http://www.cluetrust.com/XML/GPXDATA/1/0" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxext="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns="http://www.topografix.com/GPX/1/1">' # noqa
|
||||
' <metadata/>'
|
||||
'</gpx>'
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def gpx_file_invalid_xml():
|
||||
return (
|
||||
'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'
|
||||
'<gpx xmlns:gpxdata="http://www.cluetrust.com/XML/GPXDATA/1/0" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxext="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns="http://www.topografix.com/GPX/1/1">' # noqa
|
||||
' <metadata/>'
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def gpx_file_with_segments():
|
||||
return (
|
||||
'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'
|
||||
'<gpx xmlns:gpxdata="http://www.cluetrust.com/XML/GPXDATA/1/0" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxext="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns="http://www.topografix.com/GPX/1/1">' # noqa
|
||||
' <metadata/>'
|
||||
' <trk>'
|
||||
' <name>just an activity</name>'
|
||||
' <trkseg>'
|
||||
' <trkpt lat="44.68095" lon="6.07367">'
|
||||
' <ele>998</ele>'
|
||||
' <time>2018-03-13T12:44:45Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68091" lon="6.07367">'
|
||||
' <ele>998</ele>'
|
||||
' <time>2018-03-13T12:44:50Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.6808" lon="6.07364">'
|
||||
' <ele>994</ele>'
|
||||
' <time>2018-03-13T12:45:00Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68075" lon="6.07364">'
|
||||
' <ele>994</ele>'
|
||||
' <time>2018-03-13T12:45:05Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68071" lon="6.07364">'
|
||||
' <ele>994</ele>'
|
||||
' <time>2018-03-13T12:45:10Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68049" lon="6.07361">'
|
||||
' <ele>993</ele>'
|
||||
' <time>2018-03-13T12:45:30Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68019" lon="6.07356">'
|
||||
' <ele>992</ele>'
|
||||
' <time>2018-03-13T12:45:55Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.68014" lon="6.07355">'
|
||||
' <ele>992</ele>'
|
||||
' <time>2018-03-13T12:46:00Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67995" lon="6.07358">'
|
||||
' <ele>987</ele>'
|
||||
' <time>2018-03-13T12:46:15Z</time>'
|
||||
' </trkpt>'
|
||||
' </trkseg>'
|
||||
' <trkseg>'
|
||||
' <trkpt lat="44.67977" lon="6.07364">'
|
||||
' <ele>987</ele>'
|
||||
' <time>2018-03-13T12:46:30Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67972" lon="6.07367">'
|
||||
' <ele>987</ele>'
|
||||
' <time>2018-03-13T12:46:35Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67966" lon="6.07368">'
|
||||
' <ele>987</ele>'
|
||||
' <time>2018-03-13T12:46:40Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67961" lon="6.0737">'
|
||||
' <ele>986</ele>'
|
||||
' <time>2018-03-13T12:46:45Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67938" lon="6.07377">'
|
||||
' <ele>986</ele>'
|
||||
' <time>2018-03-13T12:47:05Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67933" lon="6.07381">'
|
||||
' <ele>986</ele>'
|
||||
' <time>2018-03-13T12:47:10Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67922" lon="6.07385">'
|
||||
' <ele>985</ele>'
|
||||
' <time>2018-03-13T12:47:20Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67911" lon="6.0739">'
|
||||
' <ele>980</ele>'
|
||||
' <time>2018-03-13T12:47:30Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.679" lon="6.07399">'
|
||||
' <ele>980</ele>'
|
||||
' <time>2018-03-13T12:47:40Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67896" lon="6.07402">'
|
||||
' <ele>980</ele>'
|
||||
' <time>2018-03-13T12:47:45Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67884" lon="6.07408">'
|
||||
' <ele>979</ele>'
|
||||
' <time>2018-03-13T12:47:55Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67863" lon="6.07423">'
|
||||
' <ele>981</ele>'
|
||||
' <time>2018-03-13T12:48:15Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67858" lon="6.07425">'
|
||||
' <ele>980</ele>'
|
||||
' <time>2018-03-13T12:48:20Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67842" lon="6.07434">'
|
||||
' <ele>979</ele>'
|
||||
' <time>2018-03-13T12:48:35Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67837" lon="6.07435">'
|
||||
' <ele>979</ele>'
|
||||
' <time>2018-03-13T12:48:40Z</time>'
|
||||
' </trkpt>'
|
||||
' <trkpt lat="44.67822" lon="6.07442">'
|
||||
' <ele>975</ele>'
|
||||
' <time>2018-03-13T12:48:55Z</time>'
|
||||
' </trkpt>'
|
||||
' </trkseg>'
|
||||
' </trk>'
|
||||
'</gpx>'
|
||||
)
|
BIN
fittrackee/tests/files/gpx_test.zip
Normal file
BIN
fittrackee/tests/files/gpx_test.zip
Normal file
Binary file not shown.
BIN
fittrackee/tests/files/gpx_test_folder.zip
Normal file
BIN
fittrackee/tests/files/gpx_test_folder.zip
Normal file
Binary file not shown.
BIN
fittrackee/tests/files/gpx_test_incorrect.zip
Normal file
BIN
fittrackee/tests/files/gpx_test_incorrect.zip
Normal file
Binary file not shown.
0
fittrackee/tests/template_results/__init__.py
Normal file
0
fittrackee/tests/template_results/__init__.py
Normal file
174
fittrackee/tests/template_results/password_reset_request.py
Normal file
174
fittrackee/tests/template_results/password_reset_request.py
Normal file
@ -0,0 +1,174 @@
|
||||
# flake8: noqa
|
||||
|
||||
expected_en_text_body = """Hi test,
|
||||
|
||||
You recently requested to reset your password for your FitTrackee account. Use the button below to reset it. This password reset link is only valid for 3 seconds.
|
||||
|
||||
Reset your password ( http://localhost/password-reset?token=xxx )
|
||||
|
||||
For security, this request was received from a Linux device using Firefox. If you did not request a password reset, please ignore this email.
|
||||
|
||||
Thanks,
|
||||
The FitTrackee Team"""
|
||||
|
||||
expected_fr_text_body = """Bonjour test,
|
||||
|
||||
Vous avez récemment demander la réinitilisation du mot de passe de votre compte sur FitTrackee.
|
||||
Cliquez sur le lien ci-dessous pour le réinitialiser. Ce lien n'est valide que pendant 3 secondes.
|
||||
|
||||
Réinitialiser le mot de passe: ( http://localhost/password-reset?token=xxx )
|
||||
|
||||
Pour vérification, cette demande a été reçue à partir d'un appareil sous Linux, utilisant le navigateur Firefox.
|
||||
Si vous n'avez pas demandé de réinitalisation, vous pouvez ignorer cet e-mail.
|
||||
|
||||
Merci,
|
||||
L'équipe FitTrackee"""
|
||||
|
||||
expected_en_html_body = """ <body>
|
||||
<span class="preheader">Use this link to reset your password. The link is only valid for 3 seconds.</span>
|
||||
<table class="email-wrapper" width="100%" cellpadding="0" cellspacing="0" role="presentation">
|
||||
<tr>
|
||||
<td align="center">
|
||||
<table class="email-content" width="100%" cellpadding="0" cellspacing="0" role="presentation">
|
||||
<tr>
|
||||
<td class="email-masthead">
|
||||
<a href="https://example.com" class="f-fallback email-masthead-name">
|
||||
FitTrackee
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="email-body" width="100%" cellpadding="0" cellspacing="0">
|
||||
<table class="email-body-inner" align="center" width="570" cellpadding="0" cellspacing="0" role="presentation">
|
||||
<tr>
|
||||
<td class="content-cell">
|
||||
<div class="f-fallback">
|
||||
<h1>Hi test,</h1>
|
||||
<p>You recently requested to reset your password for your account. Use the button below to reset it.
|
||||
<strong>This password reset link is only valid for 3 seconds.</strong>
|
||||
</p>
|
||||
<table class="body-action" align="center" width="100%" cellpadding="0" cellspacing="0" role="presentation">
|
||||
<tr>
|
||||
<td align="center">
|
||||
<table width="100%" border="0" cellspacing="0" cellpadding="0" role="presentation">
|
||||
<tr>
|
||||
<td align="center">
|
||||
<a href="http://localhost/password-reset?token=xxx" class="f-fallback button button--green" target="_blank">Reset your password</a>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p>
|
||||
For security, this request was received from a Linux device using Firefox.
|
||||
If you did not request a password reset, please ignore this email.
|
||||
</p>
|
||||
<p>Thanks,
|
||||
<br>The FitTrackee Team</p>
|
||||
<table class="body-sub" role="presentation">
|
||||
<tr>
|
||||
<td>
|
||||
<p class="f-fallback sub">If you’re having trouble with the button above, copy and paste the URL below into your web browser.</p>
|
||||
<p class="f-fallback sub">http://localhost/password-reset?token=xxx</p>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<table class="email-footer" align="center" width="570" cellpadding="0" cellspacing="0" role="presentation">
|
||||
<tr>
|
||||
<td class="content-cell" align="center">
|
||||
<p class="f-fallback sub align-center">© FitTrackee.</p>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>"""
|
||||
|
||||
expected_fr_html_body = """ <body>
|
||||
<span class="preheader">Utiliser ce lien pour réinitialiser le mot de passe. Ce lien n'est valide que pendant 3 secondes.</span>
|
||||
<table class="email-wrapper" width="100%" cellpadding="0" cellspacing="0" role="presentation">
|
||||
<tr>
|
||||
<td align="center">
|
||||
<table class="email-content" width="100%" cellpadding="0" cellspacing="0" role="presentation">
|
||||
<tr>
|
||||
<td class="email-masthead">
|
||||
<a href="https://example.com" class="f-fallback email-masthead-name">
|
||||
FitTrackee
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="email-body" width="100%" cellpadding="0" cellspacing="0">
|
||||
<table class="email-body-inner" align="center" width="570" cellpadding="0" cellspacing="0" role="presentation">
|
||||
<tr>
|
||||
<td class="content-cell">
|
||||
<div class="f-fallback">
|
||||
<h1>Bonjour test,</h1>
|
||||
<p>Vous avez récemment demander la réinitilisation du mot de passe de votre compte sur FitTrackee.
|
||||
Cliquez sur le bouton ci-dessous pour le réinitialiser.
|
||||
<strong>Cette réinitialisation n'est valide que pendant 3 secondes.</strong>
|
||||
</p>
|
||||
<table class="body-action" align="center" width="100%" cellpadding="0" cellspacing="0" role="presentation">
|
||||
<tr>
|
||||
<td align="center">
|
||||
<table width="100%" border="0" cellspacing="0" cellpadding="0" role="presentation">
|
||||
<tr>
|
||||
<td align="center">
|
||||
<a href="http://localhost/password-reset?token=xxx" class="f-fallback button button--green" target="_blank">Réinitialiser le mot de passe</a>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p>
|
||||
Pour vérification, cette demande a été reçue à partir d'un appareil sous Linux, utilisant le navigateur Firefox.
|
||||
Si vous n'avez pas demandé de réinitalisation, vous pouvez ignorer cet e-mail.
|
||||
</p>
|
||||
<p>Merci,
|
||||
<br>L'équipe FitTrackee</p>
|
||||
<table class="body-sub" role="presentation">
|
||||
<tr>
|
||||
<td>
|
||||
<p class="f-fallback sub">Si vous avez des problèmes avec le bouton, vous pouvez copier et coller le lien suivant dans votre navigateur</p>
|
||||
<p class="f-fallback sub">http://localhost/password-reset?token=xxx</p>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<table class="email-footer" align="center" width="570" cellpadding="0" cellspacing="0" role="presentation">
|
||||
<tr>
|
||||
<td class="content-cell" align="center">
|
||||
<p class="f-fallback sub align-center">© FitTrackee.</p>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>"""
|
924
fittrackee/tests/test_activities_api_0_get.py
Normal file
924
fittrackee/tests/test_activities_api_0_get.py
Normal file
@ -0,0 +1,924 @@
|
||||
import json
|
||||
|
||||
|
||||
class TestGetActivities:
|
||||
def test_it_gets_all_activities_for_authenticated_user(
|
||||
self,
|
||||
app,
|
||||
user_1,
|
||||
user_2,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
activity_cycling_user_1,
|
||||
activity_cycling_user_2,
|
||||
activity_running_user_1,
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 2
|
||||
assert 'creation_date' in data['data']['activities'][0]
|
||||
assert (
|
||||
'Sun, 01 Apr 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert 'test' == data['data']['activities'][0]['user']
|
||||
assert 2 == data['data']['activities'][0]['sport_id']
|
||||
assert 12.0 == data['data']['activities'][0]['distance']
|
||||
assert '1:40:00' == data['data']['activities'][0]['duration']
|
||||
|
||||
assert 'creation_date' in data['data']['activities'][1]
|
||||
assert (
|
||||
'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][1]['activity_date']
|
||||
)
|
||||
assert 'test' == data['data']['activities'][1]['user']
|
||||
assert 1 == data['data']['activities'][1]['sport_id']
|
||||
assert 10.0 == data['data']['activities'][1]['distance']
|
||||
assert '1:00:00' == data['data']['activities'][1]['duration']
|
||||
|
||||
def test_it_gets_no_activities_for_authenticated_user_with_no_activities(
|
||||
self,
|
||||
app,
|
||||
user_1,
|
||||
user_2,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
activity_cycling_user_1,
|
||||
activity_running_user_1,
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='toto@toto.com', password='87654321')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 0
|
||||
|
||||
def test_it_returns_401_if_user_is_not_authenticated(self, app):
|
||||
client = app.test_client()
|
||||
|
||||
response = client.get('/api/activities')
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 401
|
||||
assert 'error' in data['status']
|
||||
assert 'Provide a valid auth token.' in data['message']
|
||||
|
||||
|
||||
class TestGetActivitiesWithPagination:
|
||||
def test_it_gets_activities_with_default_pagination(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 5
|
||||
assert 'creation_date' in data['data']['activities'][0]
|
||||
assert (
|
||||
'Wed, 09 May 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert '0:50:00' == data['data']['activities'][0]['duration']
|
||||
assert 'creation_date' in data['data']['activities'][4]
|
||||
assert (
|
||||
'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][4]['activity_date']
|
||||
)
|
||||
assert '0:17:04' == data['data']['activities'][4]['duration']
|
||||
|
||||
def test_it_gets_first_page(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?page=1',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 5
|
||||
assert 'creation_date' in data['data']['activities'][0]
|
||||
assert (
|
||||
'Wed, 09 May 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert '0:50:00' == data['data']['activities'][0]['duration']
|
||||
assert 'creation_date' in data['data']['activities'][4]
|
||||
assert (
|
||||
'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][4]['activity_date']
|
||||
)
|
||||
assert '0:17:04' == data['data']['activities'][4]['duration']
|
||||
|
||||
def test_it_gets_second_page(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?page=2',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 2
|
||||
assert 'creation_date' in data['data']['activities'][0]
|
||||
assert (
|
||||
'Thu, 01 Jun 2017 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert '0:57:36' == data['data']['activities'][0]['duration']
|
||||
assert 'creation_date' in data['data']['activities'][1]
|
||||
assert (
|
||||
'Mon, 20 Mar 2017 00:00:00 GMT'
|
||||
== data['data']['activities'][1]['activity_date']
|
||||
)
|
||||
assert '0:17:04' == data['data']['activities'][1]['duration']
|
||||
|
||||
def test_it_gets_empty_third_page(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?page=3',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 0
|
||||
|
||||
def test_it_returns_error_on_invalid_page_value(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?page=A',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 500
|
||||
assert 'error' in data['status']
|
||||
assert (
|
||||
'Error. Please try again or contact the administrator.'
|
||||
in data['message']
|
||||
)
|
||||
|
||||
def test_it_gets_5_activities_per_page(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?per_page=10',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 7
|
||||
assert (
|
||||
'Wed, 09 May 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert (
|
||||
'Mon, 20 Mar 2017 00:00:00 GMT'
|
||||
== data['data']['activities'][6]['activity_date']
|
||||
)
|
||||
|
||||
def test_it_gets_3_activities_per_page(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?per_page=3',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 3
|
||||
assert (
|
||||
'Wed, 09 May 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert (
|
||||
'Fri, 23 Feb 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][2]['activity_date']
|
||||
)
|
||||
|
||||
|
||||
class TestGetActivitiesWithFilters:
|
||||
def test_it_gets_activities_with_date_filter(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?from=2018-02-01&to=2018-02-28',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 2
|
||||
assert 'creation_date' in data['data']['activities'][0]
|
||||
assert (
|
||||
'Fri, 23 Feb 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert '0:10:00' == data['data']['activities'][0]['duration']
|
||||
assert 'creation_date' in data['data']['activities'][1]
|
||||
assert (
|
||||
'Fri, 23 Feb 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][1]['activity_date']
|
||||
)
|
||||
assert '0:16:40' == data['data']['activities'][1]['duration']
|
||||
|
||||
def test_it_gets_no_activities_with_date_filter(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?from=2018-03-01&to=2018-03-30',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 0
|
||||
|
||||
def test_if_gets_activities_with_date_filter_from(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?from=2018-04-01',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 2
|
||||
assert 'creation_date' in data['data']['activities'][0]
|
||||
assert (
|
||||
'Wed, 09 May 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert (
|
||||
'Sun, 01 Apr 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][1]['activity_date']
|
||||
)
|
||||
|
||||
def test_it_gets_activities_with_date_filter_to(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?to=2017-12-31',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 2
|
||||
assert (
|
||||
'Thu, 01 Jun 2017 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert (
|
||||
'Mon, 20 Mar 2017 00:00:00 GMT'
|
||||
== data['data']['activities'][1]['activity_date']
|
||||
)
|
||||
|
||||
def test_it_gets_activities_with_ascending_order(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?order=asc',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 5
|
||||
assert (
|
||||
'Mon, 20 Mar 2017 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert (
|
||||
'Fri, 23 Feb 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][4]['activity_date']
|
||||
)
|
||||
|
||||
def test_it_gets_activities_with_distance_filter(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?distance_from=5&distance_to=8',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 2
|
||||
assert (
|
||||
'Sun, 01 Apr 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert (
|
||||
'Mon, 20 Mar 2017 00:00:00 GMT'
|
||||
== data['data']['activities'][1]['activity_date']
|
||||
)
|
||||
|
||||
def test_it_gets_activities_with_duration_filter(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?duration_from=00:52&duration_to=01:20',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 1
|
||||
assert (
|
||||
'Thu, 01 Jun 2017 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
|
||||
def test_it_gets_activities_with_average_speed_filter(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?ave_speed_from=5&ave_speed_to=10',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 1
|
||||
assert (
|
||||
'Fri, 23 Feb 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
|
||||
def test_it_gets_activities_with_max_speed_filter(
|
||||
self,
|
||||
app,
|
||||
user_1,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
activity_cycling_user_1,
|
||||
activity_running_user_1,
|
||||
):
|
||||
activity_cycling_user_1.max_speed = 25
|
||||
activity_running_user_1.max_speed = 11
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?max_speed_from=10&max_speed_to=20',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 1
|
||||
assert (
|
||||
'Sun, 01 Apr 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
|
||||
def test_it_gets_activities_with_sport_filter(
|
||||
self,
|
||||
app,
|
||||
user_1,
|
||||
sport_1_cycling,
|
||||
seven_activities_user_1,
|
||||
sport_2_running,
|
||||
activity_running_user_1,
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?sport_id=2',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 1
|
||||
assert (
|
||||
'Sun, 01 Apr 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
|
||||
|
||||
class TestGetActivitiesWithFiltersAndPagination:
|
||||
def test_it_gets_page_2_with_date_filter(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?from=2017-01-01&page=2',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 2
|
||||
assert (
|
||||
'Thu, 01 Jun 2017 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert (
|
||||
'Mon, 20 Mar 2017 00:00:00 GMT'
|
||||
== data['data']['activities'][1]['activity_date']
|
||||
)
|
||||
|
||||
def test_it_get_page_2_with_date_filter_and_ascending_order(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities?from=2017-01-01&page=2&order=asc',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 2
|
||||
assert (
|
||||
'Sun, 01 Apr 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert (
|
||||
'Wed, 09 May 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][1]['activity_date']
|
||||
)
|
||||
|
||||
|
||||
class TestGetActivity:
|
||||
def test_it_gets_an_activity(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities/1',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 1
|
||||
assert 'creation_date' in data['data']['activities'][0]
|
||||
assert (
|
||||
'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert 'test' == data['data']['activities'][0]['user']
|
||||
assert 1 == data['data']['activities'][0]['sport_id']
|
||||
assert 10.0 == data['data']['activities'][0]['distance']
|
||||
assert '1:00:00' == data['data']['activities'][0]['duration']
|
||||
|
||||
def test_it_returns_403_if_activity_belongs_to_a_different_user(
|
||||
self, app, user_1, user_2, sport_1_cycling, activity_cycling_user_2
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities/1',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 403
|
||||
assert 'error' in data['status']
|
||||
assert 'You do not have permissions.' in data['message']
|
||||
|
||||
def test_it_returns_404_if_activity_does_not_exist(self, app, user_1):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities/11',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 404
|
||||
assert 'not found' in data['status']
|
||||
assert len(data['data']['activities']) == 0
|
||||
|
||||
def test_it_returns_404_on_getting_gpx_if_activity_does_not_exist(
|
||||
self, app, user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities/11/gpx',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 404
|
||||
assert 'not found' in data['status']
|
||||
assert 'Activity not found (id: 11)' in data['message']
|
||||
assert data['data']['gpx'] == ''
|
||||
|
||||
def test_it_returns_404_on_getting_chart_data_if_activity_does_not_exist(
|
||||
self, app, user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities/11/chart_data',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 404
|
||||
assert 'not found' in data['status']
|
||||
assert 'Activity not found (id: 11)' in data['message']
|
||||
assert data['data']['chart_data'] == ''
|
||||
|
||||
def test_it_returns_404_on_getting_gpx_if_activity_have_no_gpx(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities/1/gpx',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 404
|
||||
assert 'error' in data['status']
|
||||
assert 'No gpx file for this activity (id: 1)' in data['message']
|
||||
|
||||
def test_it_returns_404_if_activity_have_no_chart_data(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities/1/chart_data',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 404
|
||||
assert 'error' in data['status']
|
||||
assert 'No gpx file for this activity (id: 1)' in data['message']
|
||||
|
||||
def test_it_returns_500_on_getting_gpx_if_an_activity_has_invalid_gpx_pathname( # noqa
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
activity_cycling_user_1.gpx = "some path"
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities/1/gpx',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 500
|
||||
assert 'error' in data['status']
|
||||
assert 'internal error' in data['message']
|
||||
assert 'data' not in data
|
||||
|
||||
def test_it_returns_500_on_getting_chart_data_if_an_activity_has_invalid_gpx_pathname( # noqa
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
activity_cycling_user_1.gpx = "some path"
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/activities/1/chart_data',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 500
|
||||
assert 'error' in data['status']
|
||||
assert 'internal error' in data['message']
|
||||
assert 'data' not in data
|
||||
|
||||
def test_it_returns_404_if_activity_has_no_map(self, app, user_1):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.get(
|
||||
'/api/activities/map/123',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 404
|
||||
assert 'error' in data['status']
|
||||
assert 'Map does not exist' in data['message']
|
1322
fittrackee/tests/test_activities_api_1_post.py
Normal file
1322
fittrackee/tests/test_activities_api_1_post.py
Normal file
File diff suppressed because it is too large
Load Diff
768
fittrackee/tests/test_activities_api_2_patch.py
Normal file
768
fittrackee/tests/test_activities_api_2_patch.py
Normal file
@ -0,0 +1,768 @@
|
||||
import json
|
||||
from io import BytesIO
|
||||
|
||||
from fittrackee.activities.models import Activity
|
||||
|
||||
|
||||
def assert_activity_data_with_gpx(data):
|
||||
assert 'creation_date' in data['data']['activities'][0]
|
||||
assert (
|
||||
'Tue, 13 Mar 2018 12:44:45 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert 'test' == data['data']['activities'][0]['user']
|
||||
assert '0:04:10' == data['data']['activities'][0]['duration']
|
||||
assert data['data']['activities'][0]['ascent'] == 0.4
|
||||
assert data['data']['activities'][0]['ave_speed'] == 4.61
|
||||
assert data['data']['activities'][0]['descent'] == 23.4
|
||||
assert data['data']['activities'][0]['distance'] == 0.32
|
||||
assert data['data']['activities'][0]['max_alt'] == 998.0
|
||||
assert data['data']['activities'][0]['max_speed'] == 5.12
|
||||
assert data['data']['activities'][0]['min_alt'] == 975.0
|
||||
assert data['data']['activities'][0]['moving'] == '0:04:10'
|
||||
assert data['data']['activities'][0]['pauses'] is None
|
||||
assert data['data']['activities'][0]['with_gpx'] is True
|
||||
|
||||
records = data['data']['activities'][0]['records']
|
||||
assert len(records) == 4
|
||||
assert records[0]['sport_id'] == 2
|
||||
assert records[0]['activity_id'] == 1
|
||||
assert records[0]['record_type'] == 'MS'
|
||||
assert records[0]['activity_date'] == 'Tue, 13 Mar 2018 12:44:45 GMT'
|
||||
assert records[0]['value'] == 5.12
|
||||
assert records[1]['sport_id'] == 2
|
||||
assert records[1]['activity_id'] == 1
|
||||
assert records[1]['record_type'] == 'LD'
|
||||
assert records[1]['activity_date'] == 'Tue, 13 Mar 2018 12:44:45 GMT'
|
||||
assert records[1]['value'] == '0:04:10'
|
||||
assert records[2]['sport_id'] == 2
|
||||
assert records[2]['activity_id'] == 1
|
||||
assert records[2]['record_type'] == 'FD'
|
||||
assert records[2]['activity_date'] == 'Tue, 13 Mar 2018 12:44:45 GMT'
|
||||
assert records[2]['value'] == 0.32
|
||||
assert records[3]['sport_id'] == 2
|
||||
assert records[3]['activity_id'] == 1
|
||||
assert records[3]['record_type'] == 'AS'
|
||||
assert records[3]['activity_date'] == 'Tue, 13 Mar 2018 12:44:45 GMT'
|
||||
assert records[3]['value'] == 4.61
|
||||
|
||||
|
||||
class TestEditActivityWithGpx:
|
||||
def test_it_updates_title_for_an_activity_with_gpx(
|
||||
self, app, user_1, sport_1_cycling, sport_2_running, gpx_file
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(sport_id=2, title="Activity test")),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 1
|
||||
assert 2 == data['data']['activities'][0]['sport_id']
|
||||
assert data['data']['activities'][0]['title'] == 'Activity test'
|
||||
assert_activity_data_with_gpx(data)
|
||||
|
||||
def test_it_adds_notes_for_an_activity_with_gpx(
|
||||
self, app, user_1, sport_1_cycling, sport_2_running, gpx_file
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(notes="test notes")),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 1
|
||||
assert data['data']['activities'][0]['title'] == 'just an activity'
|
||||
assert data['data']['activities'][0]['notes'] == 'test notes'
|
||||
|
||||
def test_it_raises_403_when_editing_an_activity_from_different_user(
|
||||
self, app, user_1, user_2, sport_1_cycling, sport_2_running, gpx_file
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='toto@toto.com', password='87654321')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(sport_id=2, title="Activity test")),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 403
|
||||
assert 'error' in data['status']
|
||||
assert 'You do not have permissions.' in data['message']
|
||||
|
||||
def test_it_updates_sport(
|
||||
self, app, user_1, sport_1_cycling, sport_2_running, gpx_file
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(sport_id=2)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 1
|
||||
assert 2 == data['data']['activities'][0]['sport_id']
|
||||
assert data['data']['activities'][0]['title'] == 'just an activity'
|
||||
assert_activity_data_with_gpx(data)
|
||||
|
||||
def test_it_returns_400_if_payload_is_empty(
|
||||
self, app, user_1, sport_1_cycling, gpx_file
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict()),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 400
|
||||
assert 'error' in data['status']
|
||||
assert 'Invalid payload.' in data['message']
|
||||
|
||||
def test_it_raises_500_if_sport_does_not_exists(
|
||||
self, app, user_1, sport_1_cycling, gpx_file
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(sport_id=2)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 500
|
||||
assert 'error' in data['status']
|
||||
assert (
|
||||
'Error. Please try again or contact the administrator.'
|
||||
in data['message']
|
||||
)
|
||||
|
||||
|
||||
class TestEditActivityWithoutGpx:
|
||||
def test_it_updates_an_activity_wo_gpx(
|
||||
self,
|
||||
app,
|
||||
user_1,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
activity_cycling_user_1,
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=2,
|
||||
duration=3600,
|
||||
activity_date='2018-05-15 15:05',
|
||||
distance=8,
|
||||
title='Activity test',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 1
|
||||
assert 'creation_date' in data['data']['activities'][0]
|
||||
assert (
|
||||
data['data']['activities'][0]['activity_date']
|
||||
== 'Tue, 15 May 2018 15:05:00 GMT'
|
||||
)
|
||||
assert data['data']['activities'][0]['user'] == 'test'
|
||||
assert data['data']['activities'][0]['sport_id'] == 2
|
||||
assert data['data']['activities'][0]['duration'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['title'] == 'Activity test'
|
||||
assert data['data']['activities'][0]['ascent'] is None
|
||||
assert data['data']['activities'][0]['ave_speed'] == 8.0
|
||||
assert data['data']['activities'][0]['descent'] is None
|
||||
assert data['data']['activities'][0]['distance'] == 8.0
|
||||
assert data['data']['activities'][0]['max_alt'] is None
|
||||
assert data['data']['activities'][0]['max_speed'] == 8.0
|
||||
assert data['data']['activities'][0]['min_alt'] is None
|
||||
assert data['data']['activities'][0]['moving'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['pauses'] is None
|
||||
assert data['data']['activities'][0]['with_gpx'] is False
|
||||
assert data['data']['activities'][0]['map'] is None
|
||||
assert data['data']['activities'][0]['weather_start'] is None
|
||||
assert data['data']['activities'][0]['weather_end'] is None
|
||||
assert data['data']['activities'][0]['notes'] is None
|
||||
|
||||
records = data['data']['activities'][0]['records']
|
||||
assert len(records) == 4
|
||||
assert records[0]['sport_id'] == 2
|
||||
assert records[0]['activity_id'] == 1
|
||||
assert records[0]['record_type'] == 'MS'
|
||||
assert records[0]['activity_date'] == 'Tue, 15 May 2018 15:05:00 GMT'
|
||||
assert records[0]['value'] == 8.0
|
||||
assert records[1]['sport_id'] == 2
|
||||
assert records[1]['activity_id'] == 1
|
||||
assert records[1]['record_type'] == 'LD'
|
||||
assert records[1]['activity_date'] == 'Tue, 15 May 2018 15:05:00 GMT'
|
||||
assert records[1]['value'] == '1:00:00'
|
||||
assert records[2]['sport_id'] == 2
|
||||
assert records[2]['activity_id'] == 1
|
||||
assert records[2]['record_type'] == 'FD'
|
||||
assert records[2]['activity_date'] == 'Tue, 15 May 2018 15:05:00 GMT'
|
||||
assert records[2]['value'] == 8.0
|
||||
assert records[3]['sport_id'] == 2
|
||||
assert records[3]['activity_id'] == 1
|
||||
assert records[3]['record_type'] == 'AS'
|
||||
assert records[3]['activity_date'] == 'Tue, 15 May 2018 15:05:00 GMT'
|
||||
assert records[3]['value'] == 8.0
|
||||
|
||||
def test_it_adds_notes_to_an_activity_wo_gpx(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(notes='test notes')),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 1
|
||||
assert 'creation_date' in data['data']['activities'][0]
|
||||
assert (
|
||||
data['data']['activities'][0]['activity_date']
|
||||
== 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
)
|
||||
assert data['data']['activities'][0]['user'] == 'test'
|
||||
assert data['data']['activities'][0]['sport_id'] == 1
|
||||
assert data['data']['activities'][0]['duration'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['title'] is None
|
||||
assert data['data']['activities'][0]['ascent'] is None
|
||||
assert data['data']['activities'][0]['ave_speed'] == 10.0
|
||||
assert data['data']['activities'][0]['descent'] is None
|
||||
assert data['data']['activities'][0]['distance'] == 10.0
|
||||
assert data['data']['activities'][0]['max_alt'] is None
|
||||
assert data['data']['activities'][0]['max_speed'] == 10.0
|
||||
assert data['data']['activities'][0]['min_alt'] is None
|
||||
assert data['data']['activities'][0]['moving'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['pauses'] is None
|
||||
assert data['data']['activities'][0]['with_gpx'] is False
|
||||
assert data['data']['activities'][0]['map'] is None
|
||||
assert data['data']['activities'][0]['weather_start'] is None
|
||||
assert data['data']['activities'][0]['weather_end'] is None
|
||||
assert data['data']['activities'][0]['notes'] == 'test notes'
|
||||
|
||||
records = data['data']['activities'][0]['records']
|
||||
assert len(records) == 4
|
||||
assert records[0]['sport_id'] == 1
|
||||
assert records[0]['activity_id'] == 1
|
||||
assert records[0]['record_type'] == 'MS'
|
||||
assert records[0]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[0]['value'] == 10.0
|
||||
assert records[1]['sport_id'] == 1
|
||||
assert records[1]['activity_id'] == 1
|
||||
assert records[1]['record_type'] == 'LD'
|
||||
assert records[1]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[1]['value'] == '1:00:00'
|
||||
assert records[2]['sport_id'] == 1
|
||||
assert records[2]['activity_id'] == 1
|
||||
assert records[2]['record_type'] == 'FD'
|
||||
assert records[2]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[2]['value'] == 10.0
|
||||
assert records[3]['sport_id'] == 1
|
||||
assert records[3]['activity_id'] == 1
|
||||
assert records[3]['record_type'] == 'AS'
|
||||
assert records[3]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[3]['value'] == 10.0
|
||||
|
||||
def test_returns_403_when_editing_an_activity_wo_gpx_from_different_user(
|
||||
self, app, user_1, user_2, sport_1_cycling, activity_cycling_user_2
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=2,
|
||||
duration=3600,
|
||||
activity_date='2018-05-15 15:05',
|
||||
distance=8,
|
||||
title='Activity test',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 403
|
||||
assert 'error' in data['status']
|
||||
assert 'You do not have permissions.' in data['message']
|
||||
|
||||
def test_it_updates_an_activity_wo_gpx_with_timezone(
|
||||
self,
|
||||
app,
|
||||
user_1_paris,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
activity_cycling_user_1,
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=2,
|
||||
duration=3600,
|
||||
activity_date='2018-05-15 15:05',
|
||||
distance=8,
|
||||
title='Activity test',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 1
|
||||
assert 'creation_date' in data['data']['activities'][0]
|
||||
assert (
|
||||
data['data']['activities'][0]['activity_date']
|
||||
== 'Tue, 15 May 2018 13:05:00 GMT'
|
||||
)
|
||||
assert data['data']['activities'][0]['user'] == 'test'
|
||||
assert data['data']['activities'][0]['sport_id'] == 2
|
||||
assert data['data']['activities'][0]['duration'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['title'] == 'Activity test'
|
||||
assert data['data']['activities'][0]['ascent'] is None
|
||||
assert data['data']['activities'][0]['ave_speed'] == 8.0
|
||||
assert data['data']['activities'][0]['descent'] is None
|
||||
assert data['data']['activities'][0]['distance'] == 8.0
|
||||
assert data['data']['activities'][0]['max_alt'] is None
|
||||
assert data['data']['activities'][0]['max_speed'] == 8.0
|
||||
assert data['data']['activities'][0]['min_alt'] is None
|
||||
assert data['data']['activities'][0]['moving'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['pauses'] is None
|
||||
assert data['data']['activities'][0]['with_gpx'] is False
|
||||
|
||||
records = data['data']['activities'][0]['records']
|
||||
assert len(records) == 4
|
||||
assert records[0]['sport_id'] == 2
|
||||
assert records[0]['activity_id'] == 1
|
||||
assert records[0]['record_type'] == 'MS'
|
||||
assert records[0]['activity_date'] == 'Tue, 15 May 2018 13:05:00 GMT'
|
||||
assert records[0]['value'] == 8.0
|
||||
assert records[1]['sport_id'] == 2
|
||||
assert records[1]['activity_id'] == 1
|
||||
assert records[1]['record_type'] == 'LD'
|
||||
assert records[1]['activity_date'] == 'Tue, 15 May 2018 13:05:00 GMT'
|
||||
assert records[1]['value'] == '1:00:00'
|
||||
assert records[2]['sport_id'] == 2
|
||||
assert records[2]['activity_id'] == 1
|
||||
assert records[2]['record_type'] == 'FD'
|
||||
assert records[2]['activity_date'] == 'Tue, 15 May 2018 13:05:00 GMT'
|
||||
assert records[2]['value'] == 8.0
|
||||
assert records[3]['sport_id'] == 2
|
||||
assert records[3]['activity_id'] == 1
|
||||
assert records[3]['record_type'] == 'AS'
|
||||
assert records[3]['activity_date'] == 'Tue, 15 May 2018 13:05:00 GMT'
|
||||
assert records[3]['value'] == 8.0
|
||||
|
||||
def test_it_updates_only_sport_and_distance_an_activity_wo_gpx(
|
||||
self,
|
||||
app,
|
||||
user_1,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
activity_cycling_user_1,
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(sport_id=2, distance=20)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 1
|
||||
assert 'creation_date' in data['data']['activities'][0]
|
||||
assert (
|
||||
data['data']['activities'][0]['activity_date']
|
||||
== 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
)
|
||||
assert data['data']['activities'][0]['user'] == 'test'
|
||||
assert data['data']['activities'][0]['sport_id'] == 2
|
||||
assert data['data']['activities'][0]['duration'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['title'] is None
|
||||
assert data['data']['activities'][0]['ascent'] is None
|
||||
assert data['data']['activities'][0]['ave_speed'] == 20.0
|
||||
assert data['data']['activities'][0]['descent'] is None
|
||||
assert data['data']['activities'][0]['distance'] == 20.0
|
||||
assert data['data']['activities'][0]['max_alt'] is None
|
||||
assert data['data']['activities'][0]['max_speed'] == 20.0
|
||||
assert data['data']['activities'][0]['min_alt'] is None
|
||||
assert data['data']['activities'][0]['moving'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['pauses'] is None
|
||||
assert data['data']['activities'][0]['with_gpx'] is False
|
||||
|
||||
records = data['data']['activities'][0]['records']
|
||||
assert len(records) == 4
|
||||
assert records[0]['sport_id'] == 2
|
||||
assert records[0]['activity_id'] == 1
|
||||
assert records[0]['record_type'] == 'MS'
|
||||
assert records[0]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[0]['value'] == 20.0
|
||||
assert records[1]['sport_id'] == 2
|
||||
assert records[1]['activity_id'] == 1
|
||||
assert records[1]['record_type'] == 'LD'
|
||||
assert records[1]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[1]['value'] == '1:00:00'
|
||||
assert records[2]['sport_id'] == 2
|
||||
assert records[2]['activity_id'] == 1
|
||||
assert records[2]['record_type'] == 'FD'
|
||||
assert records[2]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[2]['value'] == 20.0
|
||||
assert records[3]['sport_id'] == 2
|
||||
assert records[3]['activity_id'] == 1
|
||||
assert records[3]['record_type'] == 'AS'
|
||||
assert records[3]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[3]['value'] == 20.0
|
||||
|
||||
def test_it_returns_400_if_payload_is_empty(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict()),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 400
|
||||
assert 'error' in data['status']
|
||||
assert 'Invalid payload.' in data['message']
|
||||
|
||||
def test_it_returns_500_if_date_format_is_invalid(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3600,
|
||||
activity_date='15/2018',
|
||||
distance=10,
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 500
|
||||
assert 'error' in data['status']
|
||||
assert (
|
||||
'Error. Please try again or contact the administrator.'
|
||||
in data['message']
|
||||
)
|
||||
|
||||
def test_it_returns_404_if_edited_activity_doens_not_exists(
|
||||
self, app, user_1, sport_1_cycling
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3600,
|
||||
activity_date='2018-05-15 14:05',
|
||||
distance=10,
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 404
|
||||
assert 'not found' in data['status']
|
||||
assert len(data['data']['activities']) == 0
|
||||
|
||||
|
||||
class TestRefreshActivityWithGpx:
|
||||
def test_refresh_an_activity_with_gpx(
|
||||
self, app, user_1, sport_1_cycling, sport_2_running, gpx_file
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
|
||||
# Edit some activity data
|
||||
activity = Activity.query.filter_by(id=1).first()
|
||||
activity.ascent = 1000
|
||||
activity.min_alt = -100
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(refresh=True)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['activities']) == 1
|
||||
assert 1 == data['data']['activities'][0]['sport_id']
|
||||
assert 0.4 == data['data']['activities'][0]['ascent']
|
||||
assert 975.0 == data['data']['activities'][0]['min_alt']
|
188
fittrackee/tests/test_activities_api_3_delete.py
Normal file
188
fittrackee/tests/test_activities_api_3_delete.py
Normal file
@ -0,0 +1,188 @@
|
||||
import json
|
||||
import os
|
||||
from io import BytesIO
|
||||
|
||||
from fittrackee.activities.models import Activity
|
||||
from fittrackee.activities.utils import get_absolute_file_path
|
||||
|
||||
|
||||
def get_gpx_filepath(activity_id):
|
||||
activity = Activity.query.filter_by(id=activity_id).first()
|
||||
return activity.gpx
|
||||
|
||||
|
||||
class TestDeleteActivityWithGpx:
|
||||
def test_it_deletes_an_activity_with_gpx(
|
||||
self, app, user_1, sport_1_cycling, gpx_file
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
response = client.delete(
|
||||
'/api/activities/1',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
assert response.status_code == 204
|
||||
|
||||
def test_it_returns_403_when_deleting_an_activity_from_different_user(
|
||||
self, app, user_1, user_2, sport_1_cycling, gpx_file
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='toto@toto.com', password='87654321')),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.delete(
|
||||
'/api/activities/1',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 403
|
||||
assert 'error' in data['status']
|
||||
assert 'You do not have permissions.' in data['message']
|
||||
|
||||
def test_it_returns_404_if_activity_does_not_exist(self, app, user_1):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.delete(
|
||||
'/api/activities/9999',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 404
|
||||
assert 'not found' in data['status']
|
||||
|
||||
def test_it_returns_500_when_deleting_an_activity_with_gpx_invalid_file(
|
||||
self, app, user_1, sport_1_cycling, gpx_file
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
|
||||
gpx_filepath = get_gpx_filepath(1)
|
||||
gpx_filepath = get_absolute_file_path(gpx_filepath)
|
||||
os.remove(gpx_filepath)
|
||||
|
||||
response = client.delete(
|
||||
'/api/activities/1',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 500
|
||||
assert 'error' in data['status']
|
||||
assert (
|
||||
'Error. Please try again or contact the administrator.'
|
||||
in data['message']
|
||||
)
|
||||
|
||||
|
||||
class TestDeleteActivityWithoutGpx:
|
||||
def test_it_deletes_an_activity_wo_gpx(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.delete(
|
||||
'/api/activities/1',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
assert response.status_code == 204
|
||||
|
||||
def test_it_returns_403_when_deleting_an_activity_from_different_user(
|
||||
self, app, user_1, user_2, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='toto@toto.com', password='87654321')),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.delete(
|
||||
'/api/activities/1',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 403
|
||||
assert 'error' in data['status']
|
||||
assert 'You do not have permissions.' in data['message']
|
60
fittrackee/tests/test_activities_model.py
Normal file
60
fittrackee/tests/test_activities_model.py
Normal file
@ -0,0 +1,60 @@
|
||||
class TestActivityModel:
|
||||
def test_activity_model(
|
||||
self, app, sport_1_cycling, user_1, activity_cycling_user_1
|
||||
):
|
||||
activity_cycling_user_1.title = 'Test'
|
||||
|
||||
assert 1 == activity_cycling_user_1.id
|
||||
assert 1 == activity_cycling_user_1.user_id
|
||||
assert 1 == activity_cycling_user_1.sport_id
|
||||
assert '2018-01-01 00:00:00' == str(
|
||||
activity_cycling_user_1.activity_date
|
||||
)
|
||||
assert 10.0 == float(activity_cycling_user_1.distance)
|
||||
assert '1:00:00' == str(activity_cycling_user_1.duration)
|
||||
assert 'Test' == activity_cycling_user_1.title
|
||||
assert '<Activity \'Cycling\' - 2018-01-01 00:00:00>' == str(
|
||||
activity_cycling_user_1
|
||||
)
|
||||
|
||||
serialized_activity = activity_cycling_user_1.serialize()
|
||||
assert 1 == serialized_activity['id']
|
||||
assert 'test' == serialized_activity['user']
|
||||
assert 1 == serialized_activity['sport_id']
|
||||
assert serialized_activity['title'] == 'Test'
|
||||
assert 'creation_date' in serialized_activity
|
||||
assert serialized_activity['modification_date'] is not None
|
||||
assert (
|
||||
str(serialized_activity['activity_date']) == '2018-01-01 00:00:00'
|
||||
)
|
||||
assert serialized_activity['duration'] == '1:00:00'
|
||||
assert serialized_activity['pauses'] is None
|
||||
assert serialized_activity['moving'] == '1:00:00'
|
||||
assert serialized_activity['distance'] == 10.0
|
||||
assert serialized_activity['max_alt'] is None
|
||||
assert serialized_activity['descent'] is None
|
||||
assert serialized_activity['ascent'] is None
|
||||
assert serialized_activity['max_speed'] == 10.0
|
||||
assert serialized_activity['ave_speed'] == 10.0
|
||||
assert serialized_activity['with_gpx'] is False
|
||||
assert serialized_activity['bounds'] == []
|
||||
assert serialized_activity['previous_activity'] is None
|
||||
assert serialized_activity['next_activity'] is None
|
||||
assert serialized_activity['segments'] == []
|
||||
assert serialized_activity['records'] != []
|
||||
assert serialized_activity['map'] is None
|
||||
assert serialized_activity['weather_start'] is None
|
||||
assert serialized_activity['weather_end'] is None
|
||||
assert serialized_activity['notes'] is None
|
||||
|
||||
def test_activity_segment_model(
|
||||
self,
|
||||
app,
|
||||
sport_1_cycling,
|
||||
user_1,
|
||||
activity_cycling_user_1,
|
||||
activity_cycling_user_1_segment,
|
||||
):
|
||||
assert '<Segment \'0\' for activity \'1\'>' == str(
|
||||
activity_cycling_user_1_segment
|
||||
)
|
227
fittrackee/tests/test_app_config_api.py
Normal file
227
fittrackee/tests/test_app_config_api.py
Normal file
@ -0,0 +1,227 @@
|
||||
import json
|
||||
|
||||
|
||||
class TestGetConfig:
|
||||
def test_it_gets_application_config(self, app, user_1):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/config',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert data['data']['gpx_limit_import'] == 10
|
||||
assert data['data']['is_registration_enabled'] is True
|
||||
assert data['data']['max_single_file_size'] == 1048576
|
||||
assert data['data']['max_zip_file_size'] == 10485760
|
||||
assert data['data']['max_users'] == 100
|
||||
assert data['data']['map_attribution'] == (
|
||||
'© <a href="http://www.openstreetmap.org/copyright" '
|
||||
'target="_blank" rel="noopener noreferrer">OpenStreetMap</a> '
|
||||
'contributors'
|
||||
)
|
||||
|
||||
def test_it_returns_error_if_application_has_no_config(
|
||||
self, app_no_config, user_1_admin
|
||||
):
|
||||
client = app_no_config.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/config',
|
||||
content_type='application/json',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 500
|
||||
assert 'error' in data['status']
|
||||
assert 'Error on getting configuration.' in data['message']
|
||||
|
||||
def test_it_returns_error_if_application_has_several_config(
|
||||
self, app, app_config, user_1_admin
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/config',
|
||||
content_type='application/json',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 500
|
||||
assert 'error' in data['status']
|
||||
assert 'Error on getting configuration.' in data['message']
|
||||
|
||||
|
||||
class TestUpdateConfig:
|
||||
def test_it_updates_config_when_user_is_admin(self, app, user_1_admin):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.patch(
|
||||
'/api/config',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert data['data']['gpx_limit_import'] == 100
|
||||
assert data['data']['is_registration_enabled'] is True
|
||||
assert data['data']['max_single_file_size'] == 1048576
|
||||
assert data['data']['max_zip_file_size'] == 10485760
|
||||
assert data['data']['max_users'] == 10
|
||||
|
||||
def test_it_updates_all_config(self, app, user_1_admin):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/config',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
gpx_limit_import=20,
|
||||
max_single_file_size=10000,
|
||||
max_zip_file_size=25000,
|
||||
max_users=50,
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert data['data']['gpx_limit_import'] == 20
|
||||
assert data['data']['is_registration_enabled'] is True
|
||||
assert data['data']['max_single_file_size'] == 10000
|
||||
assert data['data']['max_zip_file_size'] == 25000
|
||||
assert data['data']['max_users'] == 50
|
||||
|
||||
def test_it_returns_403_when_user_is_not_an_admin(self, app, user_1):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/config',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 403
|
||||
assert 'success' not in data['status']
|
||||
assert 'error' in data['status']
|
||||
assert 'You do not have permissions.' in data['message']
|
||||
|
||||
def test_it_returns_400_if_invalid_is_payload(self, app, user_1_admin):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/config',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict()),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 400
|
||||
assert 'error' in data['status']
|
||||
assert 'Invalid payload.' in data['message']
|
||||
|
||||
def test_it_returns_error_on_update_if_application_has_no_config(
|
||||
self, app_no_config, user_1_admin
|
||||
):
|
||||
client = app_no_config.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/config',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 500
|
||||
assert 'error' in data['status']
|
||||
assert 'Error on updating configuration.' in data['message']
|
19
fittrackee/tests/test_app_config_model.py
Normal file
19
fittrackee/tests/test_app_config_model.py
Normal file
@ -0,0 +1,19 @@
|
||||
from fittrackee.application.models import AppConfig
|
||||
|
||||
|
||||
class TestConfigModel:
|
||||
def test_application_config(self, app):
|
||||
app_config = AppConfig.query.first()
|
||||
assert 1 == app_config.id
|
||||
|
||||
serialized_app_config = app_config.serialize()
|
||||
assert serialized_app_config['gpx_limit_import'] == 10
|
||||
assert serialized_app_config['is_registration_enabled'] is True
|
||||
assert serialized_app_config['max_single_file_size'] == 1048576
|
||||
assert serialized_app_config['max_zip_file_size'] == 10485760
|
||||
assert serialized_app_config['max_users'] == 100
|
||||
assert serialized_app_config['map_attribution'] == (
|
||||
'© <a href="http://www.openstreetmap.org/copyright" '
|
||||
'target="_blank" rel="noopener noreferrer">OpenStreetMap</a> '
|
||||
'contributors'
|
||||
)
|
1149
fittrackee/tests/test_auth_api.py
Normal file
1149
fittrackee/tests/test_auth_api.py
Normal file
File diff suppressed because it is too large
Load Diff
20
fittrackee/tests/test_config.py
Normal file
20
fittrackee/tests/test_config.py
Normal file
@ -0,0 +1,20 @@
|
||||
import os
|
||||
|
||||
|
||||
class TestConfig:
|
||||
def test_development_config(self, app):
|
||||
app.config.from_object('fittrackee.config.DevelopmentConfig')
|
||||
assert app.config['DEBUG']
|
||||
assert not app.config['TESTING']
|
||||
assert app.config['SQLALCHEMY_DATABASE_URI'] == os.environ.get(
|
||||
'DATABASE_URL'
|
||||
)
|
||||
|
||||
def test_testing_config(self, app):
|
||||
app.config.from_object('fittrackee.config.TestingConfig')
|
||||
assert app.config['DEBUG']
|
||||
assert app.config['TESTING']
|
||||
assert not app.config['PRESERVE_CONTEXT_ON_EXCEPTION']
|
||||
assert app.config['SQLALCHEMY_DATABASE_URI'] == os.environ.get(
|
||||
'DATABASE_TEST_URL'
|
||||
)
|
102
fittrackee/tests/test_email.py
Normal file
102
fittrackee/tests/test_email.py
Normal file
@ -0,0 +1,102 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from fittrackee import email_service
|
||||
from fittrackee.email.email import EmailMessage
|
||||
|
||||
from .template_results.password_reset_request import expected_en_text_body
|
||||
|
||||
|
||||
class TestEmailMessage:
|
||||
def test_it_generate_email_data(self):
|
||||
message = EmailMessage(
|
||||
sender='fittrackee@example.com',
|
||||
recipient='test@test.com',
|
||||
subject='Fittrackee - test email',
|
||||
html="""\
|
||||
<html>
|
||||
<body>
|
||||
<p>Hello !</p>
|
||||
</body>
|
||||
</html>
|
||||
""",
|
||||
text='Hello !',
|
||||
)
|
||||
message_data = message.generate_message()
|
||||
assert message_data.get('From') == 'fittrackee@example.com'
|
||||
assert message_data.get('To') == 'test@test.com'
|
||||
assert message_data.get('Subject') == 'Fittrackee - test email'
|
||||
message_string = message_data.as_string()
|
||||
assert 'Hello !' in message_string
|
||||
|
||||
|
||||
class TestEmailSending:
|
||||
|
||||
email_data = {
|
||||
'expiration_delay': '3 seconds',
|
||||
'username': 'test',
|
||||
'password_reset_url': 'http://localhost/password-reset?token=xxx',
|
||||
'operating_system': 'Linux',
|
||||
'browser_name': 'Firefox',
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def get_args(call_args):
|
||||
if len(call_args) == 2:
|
||||
args, _ = call_args
|
||||
else:
|
||||
_, args, _ = call_args
|
||||
return args
|
||||
|
||||
def assert_smtp(self, smtp):
|
||||
assert smtp.sendmail.call_count == 1
|
||||
call_args = self.get_args(smtp.sendmail.call_args)
|
||||
assert call_args[0] == 'fittrackee@example.com'
|
||||
assert call_args[1] == 'test@test.com'
|
||||
assert expected_en_text_body in call_args[2]
|
||||
|
||||
@patch('smtplib.SMTP_SSL')
|
||||
@patch('smtplib.SMTP')
|
||||
def test_it_sends_message(self, mock_smtp, mock_smtp_ssl, app):
|
||||
|
||||
email_service.send(
|
||||
template='password_reset_request',
|
||||
lang='en',
|
||||
recipient='test@test.com',
|
||||
data=self.email_data,
|
||||
)
|
||||
|
||||
smtp = mock_smtp.return_value.__enter__.return_value
|
||||
assert smtp.starttls.not_called
|
||||
self.assert_smtp(smtp)
|
||||
|
||||
@patch('smtplib.SMTP_SSL')
|
||||
@patch('smtplib.SMTP')
|
||||
def test_it_sends_message_with_ssl(
|
||||
self, mock_smtp, mock_smtp_ssl, app_ssl
|
||||
):
|
||||
email_service.send(
|
||||
template='password_reset_request',
|
||||
lang='en',
|
||||
recipient='test@test.com',
|
||||
data=self.email_data,
|
||||
)
|
||||
|
||||
smtp = mock_smtp_ssl.return_value.__enter__.return_value
|
||||
assert smtp.starttls.not_called
|
||||
self.assert_smtp(smtp)
|
||||
|
||||
@patch('smtplib.SMTP_SSL')
|
||||
@patch('smtplib.SMTP')
|
||||
def test_it_sends_message_with_tls(
|
||||
self, mock_smtp, mock_smtp_ssl, app_tls
|
||||
):
|
||||
email_service.send(
|
||||
template='password_reset_request',
|
||||
lang='en',
|
||||
recipient='test@test.com',
|
||||
data=self.email_data,
|
||||
)
|
||||
|
||||
smtp = mock_smtp.return_value.__enter__.return_value
|
||||
assert smtp.starttls.call_count == 1
|
||||
self.assert_smtp(smtp)
|
79
fittrackee/tests/test_email_template_password_request.py
Normal file
79
fittrackee/tests/test_email_template_password_request.py
Normal file
@ -0,0 +1,79 @@
|
||||
import pytest
|
||||
from fittrackee.email.email import EmailTemplate
|
||||
|
||||
from .template_results.password_reset_request import (
|
||||
expected_en_html_body,
|
||||
expected_en_text_body,
|
||||
expected_fr_html_body,
|
||||
expected_fr_text_body,
|
||||
)
|
||||
|
||||
|
||||
class TestEmailTemplateForPasswordRequest:
|
||||
@pytest.mark.parametrize(
|
||||
'lang, expected_subject',
|
||||
[
|
||||
('en', 'FitTrackee - Password reset request'),
|
||||
('fr', 'FitTrackee - Réinitialiser votre mot de passe'),
|
||||
],
|
||||
)
|
||||
def test_it_gets_subject(self, app, lang, expected_subject):
|
||||
email_template = EmailTemplate(app.config.get('TEMPLATES_FOLDER'))
|
||||
|
||||
subject = email_template.get_content(
|
||||
'password_reset_request', lang, 'subject.txt', {}
|
||||
)
|
||||
|
||||
assert subject == expected_subject
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'lang, expected_text_body',
|
||||
[('en', expected_en_text_body), ('fr', expected_fr_text_body)],
|
||||
)
|
||||
def test_it_gets_text_body(self, app, lang, expected_text_body):
|
||||
email_template = EmailTemplate(app.config.get('TEMPLATES_FOLDER'))
|
||||
email_data = {
|
||||
'expiration_delay': '3 seconds' if lang == 'en' else '3 secondes',
|
||||
'username': 'test',
|
||||
'password_reset_url': 'http://localhost/password-reset?token=xxx',
|
||||
'operating_system': 'Linux',
|
||||
'browser_name': 'Firefox',
|
||||
}
|
||||
|
||||
text_body = email_template.get_content(
|
||||
'password_reset_request', lang, 'body.txt', email_data
|
||||
)
|
||||
|
||||
assert text_body == expected_text_body
|
||||
|
||||
def test_it_gets_en_html_body(self, app):
|
||||
email_template = EmailTemplate(app.config.get('TEMPLATES_FOLDER'))
|
||||
email_data = {
|
||||
'expiration_delay': '3 seconds',
|
||||
'username': 'test',
|
||||
'password_reset_url': 'http://localhost/password-reset?token=xxx',
|
||||
'operating_system': 'Linux',
|
||||
'browser_name': 'Firefox',
|
||||
}
|
||||
|
||||
text_body = email_template.get_content(
|
||||
'password_reset_request', 'en', 'body.html', email_data
|
||||
)
|
||||
|
||||
assert expected_en_html_body in text_body
|
||||
|
||||
def test_it_gets_fr_html_body(self, app):
|
||||
email_template = EmailTemplate(app.config.get('TEMPLATES_FOLDER'))
|
||||
email_data = {
|
||||
'expiration_delay': '3 secondes',
|
||||
'username': 'test',
|
||||
'password_reset_url': 'http://localhost/password-reset?token=xxx',
|
||||
'operating_system': 'Linux',
|
||||
'browser_name': 'Firefox',
|
||||
}
|
||||
|
||||
text_body = email_template.get_content(
|
||||
'password_reset_request', 'fr', 'body.html', email_data
|
||||
)
|
||||
|
||||
assert expected_fr_html_body in text_body
|
39
fittrackee/tests/test_email_utils.py
Normal file
39
fittrackee/tests/test_email_utils.py
Normal file
@ -0,0 +1,39 @@
|
||||
import pytest
|
||||
from fittrackee.email.utils_email import InvalidEmailUrlScheme, parse_email_url
|
||||
|
||||
|
||||
class TestEmailUrlParser:
|
||||
def test_it_raises_error_if_url_scheme_is_invalid(self):
|
||||
url = 'stmp://username:password@localhost:587'
|
||||
with pytest.raises(InvalidEmailUrlScheme):
|
||||
parse_email_url(url)
|
||||
|
||||
def test_it_parses_email_url(self):
|
||||
url = 'smtp://test@example.com:12345678@localhost:25'
|
||||
parsed_email = parse_email_url(url)
|
||||
assert parsed_email['username'] == 'test@example.com'
|
||||
assert parsed_email['password'] == '12345678'
|
||||
assert parsed_email['host'] == 'localhost'
|
||||
assert parsed_email['port'] == 25
|
||||
assert parsed_email['use_tls'] is False
|
||||
assert parsed_email['use_ssl'] is False
|
||||
|
||||
def test_it_parses_email_url_with_tls(self):
|
||||
url = 'smtp://test@example.com:12345678@localhost:587?tls=True'
|
||||
parsed_email = parse_email_url(url)
|
||||
assert parsed_email['username'] == 'test@example.com'
|
||||
assert parsed_email['password'] == '12345678'
|
||||
assert parsed_email['host'] == 'localhost'
|
||||
assert parsed_email['port'] == 587
|
||||
assert parsed_email['use_tls'] is True
|
||||
assert parsed_email['use_ssl'] is False
|
||||
|
||||
def test_it_parses_email_url_with_ssl(self):
|
||||
url = 'smtp://test@example.com:12345678@localhost:465?ssl=True'
|
||||
parsed_email = parse_email_url(url)
|
||||
assert parsed_email['username'] == 'test@example.com'
|
||||
assert parsed_email['password'] == '12345678'
|
||||
assert parsed_email['host'] == 'localhost'
|
||||
assert parsed_email['port'] == 465
|
||||
assert parsed_email['use_tls'] is False
|
||||
assert parsed_email['use_ssl'] is True
|
12
fittrackee/tests/test_health_check_api.py
Normal file
12
fittrackee/tests/test_health_check_api.py
Normal file
@ -0,0 +1,12 @@
|
||||
import json
|
||||
|
||||
|
||||
class TestHealthCheck:
|
||||
def test_it_returns_pong_on_health_check(self, app):
|
||||
""" => Ensure the /health_check route behaves correctly."""
|
||||
client = app.test_client()
|
||||
response = client.get('/api/ping')
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'pong' in data['message']
|
||||
assert 'success' in data['status']
|
946
fittrackee/tests/test_records_api.py
Normal file
946
fittrackee/tests/test_records_api.py
Normal file
@ -0,0 +1,946 @@
|
||||
import json
|
||||
|
||||
|
||||
class TestGetRecords:
|
||||
def test_it_gets_records_for_authenticated_user(
|
||||
self,
|
||||
app,
|
||||
user_1,
|
||||
user_2,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
activity_cycling_user_1,
|
||||
activity_cycling_user_2,
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
)
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 1 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 'value' in data['data']['records'][0]
|
||||
|
||||
assert (
|
||||
'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
)
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 'value' in data['data']['records'][1]
|
||||
|
||||
assert (
|
||||
'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
)
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 1 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert 'value' in data['data']['records'][2]
|
||||
|
||||
assert (
|
||||
'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
== data['data']['records'][3]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][3]['user']
|
||||
assert 1 == data['data']['records'][3]['sport_id']
|
||||
assert 1 == data['data']['records'][3]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 'value' in data['data']['records'][3]
|
||||
|
||||
def test_it_gets_no_records_if_user_has_no_activity(
|
||||
self,
|
||||
app,
|
||||
user_1,
|
||||
user_2,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
activity_cycling_user_2,
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['records']) == 0
|
||||
|
||||
def test_it_gets_no_records_if_activity_has_zero_value(
|
||||
self, app, user_1, sport_1_cycling, sport_2_running
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=0,
|
||||
activity_date='2018-05-14 14:05',
|
||||
distance=0,
|
||||
title='Activity test',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['records']) == 0
|
||||
|
||||
def test_it_gets_updated_records_after_activities_post_and_patch(
|
||||
self, app, user_1, sport_1_cycling
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3600,
|
||||
activity_date='2018-05-14 14:05',
|
||||
distance=7,
|
||||
title='Activity test 1',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 1 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 7.0 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 1 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:00:00' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][3]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][3]['user']
|
||||
assert 1 == data['data']['records'][3]['sport_id']
|
||||
assert 1 == data['data']['records'][3]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 7.0 == data['data']['records'][3]['value']
|
||||
|
||||
# Post activity with lower duration (same sport)
|
||||
# => 2 new records: Average speed and Max speed
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3000,
|
||||
activity_date='2018-05-15 14:05',
|
||||
distance=7,
|
||||
title='Activity test 2',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Tue, 15 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 2 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 8.4 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 1 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:00:00' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Tue, 15 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 2 == data['data']['records'][0]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 8.4 == data['data']['records'][3]['value']
|
||||
|
||||
# Post activity with no new records
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3500,
|
||||
activity_date='2018-05-16 14:05',
|
||||
distance=6.5,
|
||||
title='Activity test 3',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Tue, 15 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 2 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 8.4 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 1 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:00:00' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Tue, 15 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 2 == data['data']['records'][0]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 8.4 == data['data']['records'][3]['value']
|
||||
|
||||
# Edit last activity
|
||||
# 1 new record: Longest duration
|
||||
client.patch(
|
||||
'/api/activities/3',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(duration=4000)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Tue, 15 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 2 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 8.4 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 3 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:06:40' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Tue, 15 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 2 == data['data']['records'][0]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 8.4 == data['data']['records'][3]['value']
|
||||
|
||||
# delete activity 2 => AS and MS record update
|
||||
client.delete(
|
||||
'/api/activities/2',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 1 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 7.0 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 3 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:06:40' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][3]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][3]['user']
|
||||
assert 1 == data['data']['records'][3]['sport_id']
|
||||
assert 1 == data['data']['records'][3]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 7.0 == data['data']['records'][3]['value']
|
||||
|
||||
# add an activity with the same data as activity 1 except with a
|
||||
# later date => no change in record
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3600,
|
||||
activity_date='2018-05-20 14:05',
|
||||
distance=7,
|
||||
title='Activity test 4',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 1 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 7.0 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 3 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:06:40' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][3]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][3]['user']
|
||||
assert 1 == data['data']['records'][3]['sport_id']
|
||||
assert 1 == data['data']['records'][3]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 7.0 == data['data']['records'][3]['value']
|
||||
|
||||
# add an activity with the same data as activity 1 except with
|
||||
# an earlier date
|
||||
# => record update (activity 5 replace activity 1)
|
||||
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3600,
|
||||
activity_date='2018-05-14 08:05',
|
||||
distance=7,
|
||||
title='Activity test 5',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 08:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 5 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 7.0 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 08:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 5 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 3 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:06:40' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 08:05:00 GMT'
|
||||
== data['data']['records'][3]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][3]['user']
|
||||
assert 1 == data['data']['records'][3]['sport_id']
|
||||
assert 5 == data['data']['records'][3]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 7.0 == data['data']['records'][3]['value']
|
||||
|
||||
# delete all activities - no more records
|
||||
client.delete(
|
||||
'/api/activities/1',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
client.delete(
|
||||
'/api/activities/3',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
client.delete(
|
||||
'/api/activities/4',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
client.delete(
|
||||
'/api/activities/5',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['records']) == 0
|
||||
|
||||
def test_it_gets_updated_records_after_sport_change(
|
||||
self, app, user_1, sport_1_cycling, sport_2_running
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3600,
|
||||
activity_date='2018-05-14 14:05',
|
||||
distance=7,
|
||||
title='Activity test 1',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=2,
|
||||
duration=3600,
|
||||
activity_date='2018-05-16 16:05',
|
||||
distance=20,
|
||||
title='Activity test 2',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3000,
|
||||
activity_date='2018-05-17 17:05',
|
||||
distance=3,
|
||||
title='Activity test 3',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=2,
|
||||
duration=3000,
|
||||
activity_date='2018-05-18 18:05',
|
||||
distance=10,
|
||||
title='Activity test 4',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['records']) == 8
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 1 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 7.0 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 1 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:00:00' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][3]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][3]['user']
|
||||
assert 1 == data['data']['records'][3]['sport_id']
|
||||
assert 1 == data['data']['records'][3]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 7.0 == data['data']['records'][3]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 16:05:00 GMT'
|
||||
== data['data']['records'][4]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][4]['user']
|
||||
assert 2 == data['data']['records'][4]['sport_id']
|
||||
assert 2 == data['data']['records'][4]['activity_id']
|
||||
assert 'AS' == data['data']['records'][4]['record_type']
|
||||
assert 20.0 == data['data']['records'][4]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 16:05:00 GMT'
|
||||
== data['data']['records'][5]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][5]['user']
|
||||
assert 2 == data['data']['records'][5]['sport_id']
|
||||
assert 2 == data['data']['records'][5]['activity_id']
|
||||
assert 'FD' == data['data']['records'][5]['record_type']
|
||||
assert 20.0 == data['data']['records'][5]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 16:05:00 GMT'
|
||||
== data['data']['records'][6]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][6]['user']
|
||||
assert 2 == data['data']['records'][6]['sport_id']
|
||||
assert 2 == data['data']['records'][6]['activity_id']
|
||||
assert 'LD' == data['data']['records'][6]['record_type']
|
||||
assert '1:00:00' == data['data']['records'][6]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 16:05:00 GMT'
|
||||
== data['data']['records'][7]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][7]['user']
|
||||
assert 2 == data['data']['records'][7]['sport_id']
|
||||
assert 2 == data['data']['records'][7]['activity_id']
|
||||
assert 'MS' == data['data']['records'][7]['record_type']
|
||||
assert 20.0 == data['data']['records'][7]['value']
|
||||
|
||||
client.patch(
|
||||
'/api/activities/2',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(sport_id=1)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['records']) == 8
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 16:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 2 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 20.0 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 16:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 2 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 20.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 1 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:00:00' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 16:05:00 GMT'
|
||||
== data['data']['records'][3]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][3]['user']
|
||||
assert 1 == data['data']['records'][3]['sport_id']
|
||||
assert 2 == data['data']['records'][3]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 20.0 == data['data']['records'][3]['value']
|
||||
|
||||
assert (
|
||||
'Fri, 18 May 2018 18:05:00 GMT'
|
||||
== data['data']['records'][4]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][4]['user']
|
||||
assert 2 == data['data']['records'][4]['sport_id']
|
||||
assert 4 == data['data']['records'][4]['activity_id']
|
||||
assert 'AS' == data['data']['records'][4]['record_type']
|
||||
assert 12.0 == data['data']['records'][4]['value']
|
||||
|
||||
assert (
|
||||
'Fri, 18 May 2018 18:05:00 GMT'
|
||||
== data['data']['records'][5]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][5]['user']
|
||||
assert 2 == data['data']['records'][5]['sport_id']
|
||||
assert 4 == data['data']['records'][5]['activity_id']
|
||||
assert 'FD' == data['data']['records'][5]['record_type']
|
||||
assert 10.0 == data['data']['records'][5]['value']
|
||||
|
||||
assert (
|
||||
'Fri, 18 May 2018 18:05:00 GMT'
|
||||
== data['data']['records'][6]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][6]['user']
|
||||
assert 2 == data['data']['records'][6]['sport_id']
|
||||
assert 4 == data['data']['records'][6]['activity_id']
|
||||
assert 'LD' == data['data']['records'][6]['record_type']
|
||||
assert '0:50:00' == data['data']['records'][6]['value']
|
||||
|
||||
assert (
|
||||
'Fri, 18 May 2018 18:05:00 GMT'
|
||||
== data['data']['records'][7]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][7]['user']
|
||||
assert 2 == data['data']['records'][7]['sport_id']
|
||||
assert 4 == data['data']['records'][7]['activity_id']
|
||||
assert 'MS' == data['data']['records'][7]['record_type']
|
||||
assert 12.0 == data['data']['records'][7]['value']
|
135
fittrackee/tests/test_records_model.py
Normal file
135
fittrackee/tests/test_records_model.py
Normal file
@ -0,0 +1,135 @@
|
||||
import datetime
|
||||
|
||||
from fittrackee.activities.models import Record
|
||||
|
||||
|
||||
class TestRecordModel:
|
||||
def test_record_model(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
record_ld = Record.query.filter_by(
|
||||
user_id=activity_cycling_user_1.user_id,
|
||||
sport_id=activity_cycling_user_1.sport_id,
|
||||
record_type='LD',
|
||||
).first()
|
||||
assert 'test' == record_ld.user.username
|
||||
assert 1 == record_ld.sport_id
|
||||
assert 1 == record_ld.activity_id
|
||||
assert 'LD' == record_ld.record_type
|
||||
assert '2018-01-01 00:00:00' == str(record_ld.activity_date)
|
||||
assert '<Record Cycling - LD - 2018-01-01>' == str(record_ld)
|
||||
|
||||
record_serialize = record_ld.serialize()
|
||||
assert 'id' in record_serialize
|
||||
assert 'user' in record_serialize
|
||||
assert 'sport_id' in record_serialize
|
||||
assert 'activity_id' in record_serialize
|
||||
assert 'record_type' in record_serialize
|
||||
assert 'activity_date' in record_serialize
|
||||
assert 'value' in record_serialize
|
||||
|
||||
def test_record_model_with_none_value(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
record_ld = Record.query.filter_by(
|
||||
user_id=activity_cycling_user_1.user_id,
|
||||
sport_id=activity_cycling_user_1.sport_id,
|
||||
record_type='LD',
|
||||
).first()
|
||||
record_ld.value = None
|
||||
assert 'test' == record_ld.user.username
|
||||
assert 1 == record_ld.sport_id
|
||||
assert 1 == record_ld.activity_id
|
||||
assert 'LD' == record_ld.record_type
|
||||
assert '2018-01-01 00:00:00' == str(record_ld.activity_date)
|
||||
assert '<Record Cycling - LD - 2018-01-01>' == str(record_ld)
|
||||
assert record_ld.value is None
|
||||
|
||||
record_serialize = record_ld.serialize()
|
||||
assert record_serialize['value'] is None
|
||||
|
||||
def test_average_speed_records(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
record_as = Record.query.filter_by(
|
||||
user_id=activity_cycling_user_1.user_id,
|
||||
sport_id=activity_cycling_user_1.sport_id,
|
||||
record_type='AS',
|
||||
).first()
|
||||
|
||||
assert isinstance(record_as.value, float)
|
||||
assert record_as.value == 10.0
|
||||
assert record_as._value == 1000
|
||||
|
||||
record_serialize = record_as.serialize()
|
||||
assert record_serialize.get('value') == 10.0
|
||||
assert isinstance(record_serialize.get('value'), float)
|
||||
|
||||
def test_add_farest_distance_records(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
record_fd = Record.query.filter_by(
|
||||
user_id=activity_cycling_user_1.user_id,
|
||||
sport_id=activity_cycling_user_1.sport_id,
|
||||
record_type='FD',
|
||||
).first()
|
||||
|
||||
assert isinstance(record_fd.value, float)
|
||||
assert record_fd.value == 10.0
|
||||
assert record_fd._value == 10000
|
||||
|
||||
record_serialize = record_fd.serialize()
|
||||
assert record_serialize.get('value') == 10.0
|
||||
assert isinstance(record_serialize.get('value'), float)
|
||||
|
||||
def test_add_longest_duration_records(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
record_ld = Record.query.filter_by(
|
||||
user_id=activity_cycling_user_1.user_id,
|
||||
sport_id=activity_cycling_user_1.sport_id,
|
||||
record_type='LD',
|
||||
).first()
|
||||
|
||||
assert isinstance(record_ld.value, datetime.timedelta)
|
||||
assert str(record_ld.value) == '1:00:00'
|
||||
assert record_ld._value == 3600
|
||||
|
||||
record_serialize = record_ld.serialize()
|
||||
assert record_serialize.get('value') == '1:00:00'
|
||||
assert isinstance(record_serialize.get('value'), str)
|
||||
|
||||
def test_add_longest_duration_records_with_zero(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
record_ld = Record.query.filter_by(
|
||||
user_id=activity_cycling_user_1.user_id,
|
||||
sport_id=activity_cycling_user_1.sport_id,
|
||||
record_type='LD',
|
||||
).first()
|
||||
record_ld.value = datetime.timedelta(seconds=0)
|
||||
|
||||
assert isinstance(record_ld.value, datetime.timedelta)
|
||||
assert str(record_ld.value) == '0:00:00'
|
||||
assert record_ld._value == 0
|
||||
|
||||
record_serialize = record_ld.serialize()
|
||||
assert record_serialize.get('value') == '0:00:00'
|
||||
assert isinstance(record_serialize.get('value'), str)
|
||||
|
||||
def test_max_speed_records_no_value(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
record_ms = Record.query.filter_by(
|
||||
user_id=activity_cycling_user_1.user_id,
|
||||
sport_id=activity_cycling_user_1.sport_id,
|
||||
record_type='MS',
|
||||
).first()
|
||||
|
||||
assert isinstance(record_ms.value, float)
|
||||
assert record_ms.value == 10.0
|
||||
assert record_ms._value == 1000
|
||||
|
||||
record_serialize = record_ms.serialize()
|
||||
assert record_serialize.get('value') == 10.0
|
||||
assert isinstance(record_serialize.get('value'), float)
|
408
fittrackee/tests/test_sports_api.py
Normal file
408
fittrackee/tests/test_sports_api.py
Normal file
@ -0,0 +1,408 @@
|
||||
import json
|
||||
|
||||
expected_sport_1_cycling_result = {
|
||||
'id': 1,
|
||||
'label': 'Cycling',
|
||||
'img': None,
|
||||
'is_active': True,
|
||||
}
|
||||
expected_sport_1_cycling_admin_result = expected_sport_1_cycling_result.copy()
|
||||
expected_sport_1_cycling_admin_result['has_activities'] = False
|
||||
|
||||
expected_sport_2_running_result = {
|
||||
'id': 2,
|
||||
'label': 'Running',
|
||||
'img': None,
|
||||
'is_active': True,
|
||||
}
|
||||
expected_sport_2_running_admin_result = expected_sport_2_running_result.copy()
|
||||
expected_sport_2_running_admin_result['has_activities'] = False
|
||||
|
||||
expected_sport_1_cycling_inactive_result = {
|
||||
'id': 1,
|
||||
'label': 'Cycling',
|
||||
'img': None,
|
||||
'is_active': False,
|
||||
}
|
||||
expected_sport_1_cycling_inactive_admin_result = (
|
||||
expected_sport_1_cycling_inactive_result.copy()
|
||||
)
|
||||
expected_sport_1_cycling_inactive_admin_result['has_activities'] = False
|
||||
|
||||
|
||||
class TestGetSports:
|
||||
def test_it_gets_all_sports(
|
||||
self, app, user_1, sport_1_cycling, sport_2_running
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/sports',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['sports']) == 2
|
||||
assert data['data']['sports'][0] == expected_sport_1_cycling_result
|
||||
assert data['data']['sports'][1] == expected_sport_2_running_result
|
||||
|
||||
def test_it_gets_all_sports_with_inactive_one(
|
||||
self, app, user_1, sport_1_cycling_inactive, sport_2_running
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/sports',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['sports']) == 2
|
||||
assert (
|
||||
data['data']['sports'][0]
|
||||
== expected_sport_1_cycling_inactive_result
|
||||
)
|
||||
assert data['data']['sports'][1] == expected_sport_2_running_result
|
||||
|
||||
def test_it_gets_all_sports_with_admin_rights(
|
||||
self, app, user_1_admin, sport_1_cycling_inactive, sport_2_running
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/sports',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['sports']) == 2
|
||||
assert (
|
||||
data['data']['sports'][0]
|
||||
== expected_sport_1_cycling_inactive_admin_result
|
||||
)
|
||||
assert (
|
||||
data['data']['sports'][1] == expected_sport_2_running_admin_result
|
||||
)
|
||||
|
||||
|
||||
class TestGetSport:
|
||||
def test_it_gets_a_sport(self, app, user_1, sport_1_cycling):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/sports/1',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['sports']) == 1
|
||||
assert data['data']['sports'][0] == expected_sport_1_cycling_result
|
||||
|
||||
def test_it_returns_404_if_sport_does_not_exist(self, app, user_1):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/sports/1',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 404
|
||||
assert 'not found' in data['status']
|
||||
assert len(data['data']['sports']) == 0
|
||||
|
||||
def test_it_gets_a_inactive_sport(
|
||||
self, app, user_1, sport_1_cycling_inactive
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.get(
|
||||
'/api/sports/1',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
|
||||
assert len(data['data']['sports']) == 1
|
||||
assert (
|
||||
data['data']['sports'][0]
|
||||
== expected_sport_1_cycling_inactive_result
|
||||
)
|
||||
|
||||
def test_it_get_an_inactive_sport_with_admin_rights(
|
||||
self, app, user_1_admin, sport_1_cycling_inactive
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.get(
|
||||
'/api/sports/1',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
|
||||
assert len(data['data']['sports']) == 1
|
||||
assert (
|
||||
data['data']['sports'][0]
|
||||
== expected_sport_1_cycling_inactive_admin_result
|
||||
)
|
||||
|
||||
|
||||
class TestUpdateSport:
|
||||
def test_it_disables_a_sport(self, app, user_1_admin, sport_1_cycling):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(is_active=False)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['sports']) == 1
|
||||
assert data['data']['sports'][0]['is_active'] is False
|
||||
assert data['data']['sports'][0]['has_activities'] is False
|
||||
|
||||
def test_it_enables_a_sport(self, app, user_1_admin, sport_1_cycling):
|
||||
sport_1_cycling.is_active = False
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(is_active=True)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['sports']) == 1
|
||||
assert data['data']['sports'][0]['is_active'] is True
|
||||
assert data['data']['sports'][0]['has_activities'] is False
|
||||
|
||||
def test_it_disables_a_sport_with_activities(
|
||||
self, app, user_1_admin, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(is_active=False)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['sports']) == 1
|
||||
assert data['data']['sports'][0]['is_active'] is False
|
||||
assert data['data']['sports'][0]['has_activities'] is True
|
||||
|
||||
def test_it_enables_a_sport_with_activities(
|
||||
self, app, user_1_admin, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
sport_1_cycling.is_active = False
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(is_active=True)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
assert len(data['data']['sports']) == 1
|
||||
assert data['data']['sports'][0]['is_active'] is True
|
||||
assert data['data']['sports'][0]['has_activities'] is True
|
||||
|
||||
def test_returns_error_if_user_has_no_admin_rights(
|
||||
self, app, user_1, sport_1_cycling
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='test@test.com', password='12345678')),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(is_active=False)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 403
|
||||
assert 'success' not in data['status']
|
||||
assert 'error' in data['status']
|
||||
assert 'You do not have permissions.' in data['message']
|
||||
|
||||
def test_returns_error_if_payload_is_invalid(self, app, user_1_admin):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict()),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 400
|
||||
assert 'error' in data['status']
|
||||
assert 'Invalid payload.' in data['message']
|
||||
|
||||
def test_it_returns_error_if_sport_does_not_exist(self, app, user_1_admin):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(is_active=False)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 404
|
||||
assert 'not found' in data['status']
|
||||
assert len(data['data']['sports']) == 0
|
28
fittrackee/tests/test_sports_model.py
Normal file
28
fittrackee/tests/test_sports_model.py
Normal file
@ -0,0 +1,28 @@
|
||||
class TestSportModel:
|
||||
@staticmethod
|
||||
def assert_sport_model(sport, is_admin=False):
|
||||
assert 1 == sport.id
|
||||
assert 'Cycling' == sport.label
|
||||
assert '<Sport \'Cycling\'>' == str(sport)
|
||||
|
||||
serialized_sport = sport.serialize(is_admin)
|
||||
assert 1 == serialized_sport['id']
|
||||
assert 'Cycling' == serialized_sport['label']
|
||||
assert serialized_sport['is_active'] is True
|
||||
return serialized_sport
|
||||
|
||||
def test_sport_model(self, app, sport_1_cycling):
|
||||
serialized_sport = self.assert_sport_model(sport_1_cycling)
|
||||
assert 'has_activities' not in serialized_sport
|
||||
|
||||
def test_sport_model_with_activity(
|
||||
self, app, sport_1_cycling, user_1, activity_cycling_user_1
|
||||
):
|
||||
serialized_sport = self.assert_sport_model(sport_1_cycling)
|
||||
assert 'has_activities' not in serialized_sport
|
||||
|
||||
def test_sport_model_with_activity_as_admin(
|
||||
self, app, sport_1_cycling, user_1, activity_cycling_user_1
|
||||
):
|
||||
serialized_sport = self.assert_sport_model(sport_1_cycling, True)
|
||||
assert serialized_sport['has_activities'] is True
|
1074
fittrackee/tests/test_stats_api.py
Normal file
1074
fittrackee/tests/test_stats_api.py
Normal file
File diff suppressed because it is too large
Load Diff
1389
fittrackee/tests/test_users_api.py
Normal file
1389
fittrackee/tests/test_users_api.py
Normal file
File diff suppressed because it is too large
Load Diff
37
fittrackee/tests/test_users_model.py
Normal file
37
fittrackee/tests/test_users_model.py
Normal file
@ -0,0 +1,37 @@
|
||||
from fittrackee.users.models import User
|
||||
|
||||
|
||||
class TestUserModel:
|
||||
def test_user_model(self, app, user_1):
|
||||
assert '<User \'test\'>' == str(user_1)
|
||||
|
||||
serialized_user = user_1.serialize()
|
||||
assert 'test' == serialized_user['username']
|
||||
assert 'created_at' in serialized_user
|
||||
assert serialized_user['admin'] is False
|
||||
assert serialized_user['first_name'] is None
|
||||
assert serialized_user['last_name'] is None
|
||||
assert serialized_user['bio'] is None
|
||||
assert serialized_user['location'] is None
|
||||
assert serialized_user['birth_date'] is None
|
||||
assert serialized_user['picture'] is False
|
||||
assert serialized_user['timezone'] is None
|
||||
assert serialized_user['weekm'] is False
|
||||
assert serialized_user['language'] is None
|
||||
assert serialized_user['nb_activities'] == 0
|
||||
assert serialized_user['nb_sports'] == 0
|
||||
assert serialized_user['total_distance'] == 0
|
||||
assert serialized_user['total_duration'] == '0:00:00'
|
||||
|
||||
def test_encode_auth_token(self, app, user_1):
|
||||
auth_token = user_1.encode_auth_token(user_1.id)
|
||||
assert isinstance(auth_token, bytes)
|
||||
|
||||
def test_encode_password_token(self, app, user_1):
|
||||
password_token = user_1.encode_password_reset_token(user_1.id)
|
||||
assert isinstance(password_token, bytes)
|
||||
|
||||
def test_decode_auth_token(self, app, user_1):
|
||||
auth_token = user_1.encode_auth_token(user_1.id)
|
||||
assert isinstance(auth_token, bytes)
|
||||
assert User.decode_auth_token(auth_token) == user_1.id
|
Reference in New Issue
Block a user