API - update user model to store first day of week - #23

This commit is contained in:
Sam 2019-08-31 16:33:46 +02:00
parent 8716bb05a1
commit b00b48e138
9 changed files with 115 additions and 10 deletions

View File

@ -398,6 +398,7 @@
<li><p><strong>password</strong> (<em>string</em>) user password</p></li>
<li><p><strong>password_conf</strong> (<em>string</em>) user password confirmation</p></li>
<li><p><strong>timezone</strong> (<em>string</em>) user time zone</p></li>
<li><p><strong>weekm</strong> (<em>string</em>) does week start on Monday?</p></li>
</ul>
</dd>
<dt class="field-even">Request Headers</dt>

File diff suppressed because one or more lines are too long

View File

@ -89,6 +89,15 @@ def user_2():
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')

View File

@ -431,6 +431,7 @@ def test_user_profile_minimal(app, user_1):
assert data['data']['created_at']
assert not data['data']['admin']
assert data['data']['timezone'] is None
assert data['data']['weekm'] is False
assert data['data']['nb_activities'] == 0
assert data['data']['nb_sports'] == 0
assert data['data']['total_distance'] == 0
@ -465,6 +466,7 @@ def test_user_profile_full(app, user_1_full):
assert data['data']['bio'] == 'just a random guy'
assert data['data']['location'] == 'somewhere'
assert data['data']['timezone'] == 'America/New_York'
assert data['data']['weekm'] is False
assert data['data']['nb_activities'] == 0
assert data['data']['nb_sports'] == 0
assert data['data']['total_distance'] == 0
@ -534,11 +536,12 @@ def test_user_profile_valid_update(app, user_1):
first_name='John',
last_name='Doe',
location='Somewhere',
bio='just a random guy',
bio='Nothing to tell',
birth_date='1980-01-01',
password='87654321',
password_conf='87654321',
timezone='America/New_York',
weekm=True,
)
),
headers=dict(
@ -550,6 +553,21 @@ def test_user_profile_valid_update(app, user_1):
assert data['status'] == 'success'
assert data['message'] == 'User profile updated.'
assert response.status_code == 200
assert data['data']['username'] == 'test'
assert data['data']['email'] == 'test@test.com'
assert not data['data']['admin']
assert data['data']['created_at']
assert data['data']['first_name'] == 'John'
assert data['data']['last_name'] == 'Doe'
assert data['data']['birth_date']
assert data['data']['bio'] == 'Nothing to tell'
assert data['data']['location'] == 'Somewhere'
assert data['data']['timezone'] == 'America/New_York'
assert data['data']['weekm'] is True
assert data['data']['nb_activities'] == 0
assert data['data']['nb_sports'] == 0
assert data['data']['total_distance'] == 0
assert data['data']['total_duration'] == '0:00:00'
def test_user_profile_valid_update_without_password(app, user_1):
@ -567,8 +585,10 @@ def test_user_profile_valid_update_without_password(app, user_1):
first_name='John',
last_name='Doe',
location='Somewhere',
bio='just a random guy',
bio='Nothing to tell',
birth_date='1980-01-01',
timezone='America/New_York',
weekm=True,
)
),
headers=dict(
@ -580,9 +600,24 @@ def test_user_profile_valid_update_without_password(app, user_1):
assert data['status'] == 'success'
assert data['message'] == 'User profile updated.'
assert response.status_code == 200
assert data['data']['username'] == 'test'
assert data['data']['email'] == 'test@test.com'
assert not data['data']['admin']
assert data['data']['created_at']
assert data['data']['first_name'] == 'John'
assert data['data']['last_name'] == 'Doe'
assert data['data']['birth_date']
assert data['data']['bio'] == 'Nothing to tell'
assert data['data']['location'] == 'Somewhere'
assert data['data']['timezone'] == 'America/New_York'
assert data['data']['weekm'] is True
assert data['data']['nb_activities'] == 0
assert data['data']['nb_sports'] == 0
assert data['data']['total_distance'] == 0
assert data['data']['total_duration'] == '0:00:00'
def test_user_profile_valid_update_with_one_field(app, user_1):
def test_user_profile_invalid_update_with_missing_fields(app, user_1):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
@ -599,9 +634,9 @@ def test_user_profile_valid_update_with_one_field(app, user_1):
),
)
data = json.loads(response.data.decode())
assert data['status'] == 'success'
assert data['message'] == 'User profile updated.'
assert response.status_code == 200
assert data['status'] == 'error'
assert data['message'] == 'Invalid payload.'
assert response.status_code == 400
def test_user_profile_update_invalid_json(app, user_1):
@ -646,6 +681,7 @@ def test_user_profile_invalid_password(app, user_1):
password='87654321',
password_conf='876543210',
timezone='America/New_York',
weekm=True,
)
),
headers=dict(
@ -680,6 +716,7 @@ def test_user_profile_missing_password_conf(app, user_1):
birth_date='1980-01-01',
password='87654321',
timezone='America/New_York',
weekm=True,
)
),
headers=dict(

View File

@ -44,6 +44,7 @@ def test_single_user(app, user_1):
assert data['data']['bio'] is None
assert data['data']['location'] is None
assert data['data']['timezone'] is None
assert data['data']['weekm'] is False
assert data['data']['nb_activities'] == 0
assert data['data']['nb_sports'] == 0
assert data['data']['total_distance'] == 0
@ -88,6 +89,7 @@ def test_single_user_with_activities(
assert data['data']['bio'] is None
assert data['data']['location'] is None
assert data['data']['timezone'] is None
assert data['data']['weekm'] is False
assert data['data']['nb_activities'] == 2
assert data['data']['nb_sports'] == 2
assert data['data']['total_distance'] == 22
@ -140,7 +142,7 @@ def test_single_user_wrong_id(app, user_1):
assert 'User does not exist.' in data['message']
def test_users_list(app, user_1, user_2):
def test_users_list(app, user_1, user_2, user_3):
"""=> Ensure get single user behaves correctly."""
client = app.test_client()
@ -161,23 +163,34 @@ def test_users_list(app, user_1, user_2):
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['users']) == 2
assert len(data['data']['users']) == 3
assert 'created_at' in data['data']['users'][0]
assert 'created_at' in data['data']['users'][1]
assert 'created_at' in data['data']['users'][2]
assert 'test' in data['data']['users'][0]['username']
assert 'toto' in data['data']['users'][1]['username']
assert 'sam' in data['data']['users'][2]['username']
assert 'test@test.com' in data['data']['users'][0]['email']
assert 'toto@toto.com' in data['data']['users'][1]['email']
assert 'sam@test.com' in data['data']['users'][2]['email']
assert data['data']['users'][0]['timezone'] is None
assert data['data']['users'][0]['weekm'] is False
assert data['data']['users'][0]['nb_activities'] == 0
assert data['data']['users'][0]['nb_sports'] == 0
assert data['data']['users'][0]['total_distance'] == 0
assert data['data']['users'][0]['total_duration'] == '0:00:00'
assert data['data']['users'][1]['timezone'] is None
assert data['data']['users'][1]['weekm'] is False
assert data['data']['users'][1]['nb_activities'] == 0
assert data['data']['users'][1]['nb_sports'] == 0
assert data['data']['users'][1]['total_distance'] == 0
assert data['data']['users'][1]['total_duration'] == '0:00:00'
assert data['data']['users'][2]['timezone'] is None
assert data['data']['users'][2]['weekm'] is True
assert data['data']['users'][2]['nb_activities'] == 0
assert data['data']['users'][2]['nb_sports'] == 0
assert data['data']['users'][2]['total_distance'] == 0
assert data['data']['users'][2]['total_duration'] == '0:00:00'
def test_encode_auth_token(app, user_1):

View File

@ -13,6 +13,7 @@ def test_user_model(app, user_1):
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['nb_activities'] == 0
assert serialized_user['nb_sports'] == 0
assert serialized_user['total_distance'] == 0

View File

@ -408,6 +408,7 @@ def edit_user(user_id):
:<json string password: user password
:<json string password_conf: user password confirmation
:<json string timezone: user time zone
:<json string weekm: does week start on Monday?
:reqheader Authorization: OAuth 2.0 Bearer Token
@ -424,7 +425,16 @@ def edit_user(user_id):
"""
# get post data
post_data = request.get_json()
if not post_data:
user_mandatory_data = {
'first_name',
'last_name',
'bio',
'birth_date',
'location',
'timezone',
'weekm',
}
if not post_data or not post_data.keys() >= user_mandatory_data:
response_object = {'status': 'error', 'message': 'Invalid payload.'}
return jsonify(response_object), 400
first_name = post_data.get('first_name')
@ -435,6 +445,7 @@ def edit_user(user_id):
password = post_data.get('password')
password_conf = post_data.get('password_conf')
timezone = post_data.get('timezone')
weekm = post_data.get('weekm')
if password is not None and password != '':
if password_conf != password:
@ -460,11 +471,13 @@ def edit_user(user_id):
if password is not None and password != '':
user.password = password
user.timezone = timezone
user.weekm = weekm
db.session.commit()
response_object = {
'status': 'success',
'message': 'User profile updated.',
'data': user.serialize(),
}
return jsonify(response_object), 200

View File

@ -23,6 +23,8 @@ class User(db.Model):
bio = db.Column(db.String(200), nullable=True)
picture = db.Column(db.String(255), nullable=True)
timezone = db.Column(db.String(50), nullable=True)
# does the week start Monday?
weekm = db.Column(db.Boolean(50), default=False, nullable=False)
activities = db.relationship(
'Activity', lazy=True, backref=db.backref('users', lazy='joined')
)
@ -113,6 +115,7 @@ class User(db.Model):
'birth_date': self.birth_date,
'picture': self.picture is not None,
'timezone': self.timezone,
'weekm': self.weekm,
'nb_activities': nb_activity,
'nb_sports': len(sports),
'total_distance': float(total[0]) if total[0] else 0,

View File

@ -0,0 +1,28 @@
"""add weekm in 'User' table
Revision ID: 27425324c9e3
Revises: 096dd0b43beb
Create Date: 2019-08-31 15:25:09.412426
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '27425324c9e3'
down_revision = '096dd0b43beb'
branch_labels = None
depends_on = None
def upgrade():
op.add_column('users', sa.Column('weekm', sa.Boolean(create_constraint=50), nullable=True))
op.execute("UPDATE users SET weekm = false")
op.alter_column('users', 'weekm', nullable=False)
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('users', 'weekm')
# ### end Alembic commands ###