API & Client: Profile update
This commit is contained in:
@ -2,7 +2,7 @@ import json
|
||||
import time
|
||||
|
||||
from mpwo_api.tests.base import BaseTestCase
|
||||
from mpwo_api.tests.utils import add_user
|
||||
from mpwo_api.tests.utils import add_user, add_user_full
|
||||
|
||||
|
||||
class TestAuthBlueprint(BaseTestCase):
|
||||
@ -328,14 +328,14 @@ class TestAuthBlueprint(BaseTestCase):
|
||||
data['message'] == 'Invalid token. Please log in again.')
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_user_profile(self):
|
||||
add_user('test', 'test@test.com', 'test')
|
||||
def test_user_profile_minimal(self):
|
||||
add_user('test', 'test@test.com', '12345678')
|
||||
with self.client:
|
||||
resp_login = self.client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(
|
||||
email='test@test.com',
|
||||
password='test'
|
||||
password='12345678'
|
||||
)),
|
||||
content_type='application/json'
|
||||
)
|
||||
@ -356,6 +356,39 @@ class TestAuthBlueprint(BaseTestCase):
|
||||
self.assertFalse(data['data']['admin'])
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_user_profile_full(self):
|
||||
add_user_full('test', 'test@test.com', '12345678')
|
||||
with self.client:
|
||||
resp_login = self.client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(
|
||||
email='test@test.com',
|
||||
password='12345678'
|
||||
)),
|
||||
content_type='application/json'
|
||||
)
|
||||
response = self.client.get(
|
||||
'/api/auth/profile',
|
||||
headers=dict(
|
||||
Authorization='Bearer ' + json.loads(
|
||||
resp_login.data.decode()
|
||||
)['auth_token']
|
||||
)
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
self.assertTrue(data['status'] == 'success')
|
||||
self.assertTrue(data['data'] is not None)
|
||||
self.assertTrue(data['data']['username'] == 'test')
|
||||
self.assertTrue(data['data']['email'] == 'test@test.com')
|
||||
self.assertTrue(data['data']['created_at'])
|
||||
self.assertFalse(data['data']['admin'])
|
||||
self.assertTrue(data['data']['first_name'] == 'John')
|
||||
self.assertTrue(data['data']['last_name'] == 'Doe')
|
||||
self.assertTrue(data['data']['birth_date'])
|
||||
self.assertTrue(data['data']['bio'] == 'just a random guy')
|
||||
self.assertTrue(data['data']['location'] == 'somewhere')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_invalid_profile(self):
|
||||
with self.client:
|
||||
response = self.client.get(
|
||||
@ -366,3 +399,89 @@ class TestAuthBlueprint(BaseTestCase):
|
||||
self.assertTrue(
|
||||
data['message'] == 'Invalid token. Please log in again.')
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_user_profile_valid_update(self):
|
||||
add_user('test', 'test@test.com', '12345678')
|
||||
with self.client:
|
||||
resp_login = self.client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(
|
||||
email='test@test.com',
|
||||
password='12345678'
|
||||
)),
|
||||
content_type='application/json'
|
||||
)
|
||||
response = self.client.post(
|
||||
'/api/auth/profile/edit',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(
|
||||
first_name='John',
|
||||
last_name='Doe',
|
||||
location='Somewhere',
|
||||
bio='just a random guy',
|
||||
birth_date='01/01/1980'
|
||||
)),
|
||||
headers=dict(
|
||||
Authorization='Bearer ' + json.loads(
|
||||
resp_login.data.decode()
|
||||
)['auth_token']
|
||||
)
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
self.assertTrue(data['status'] == 'success')
|
||||
self.assertTrue(data['message'] == 'User profile updated.')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_user_profile_valid_update_with_one_field(self):
|
||||
add_user('test', 'test@test.com', '12345678')
|
||||
with self.client:
|
||||
resp_login = self.client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(
|
||||
email='test@test.com',
|
||||
password='12345678'
|
||||
)),
|
||||
content_type='application/json'
|
||||
)
|
||||
response = self.client.post(
|
||||
'/api/auth/profile/edit',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(
|
||||
first_name='John'
|
||||
)),
|
||||
headers=dict(
|
||||
Authorization='Bearer ' + json.loads(
|
||||
resp_login.data.decode()
|
||||
)['auth_token']
|
||||
)
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
self.assertTrue(data['status'] == 'success')
|
||||
self.assertTrue(data['message'] == 'User profile updated.')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_user_profile_update_invalid_json(self):
|
||||
add_user('test', 'test@test.com', '12345678')
|
||||
with self.client:
|
||||
resp_login = self.client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(
|
||||
email='test@test.com',
|
||||
password='12345678'
|
||||
)),
|
||||
content_type='application/json'
|
||||
)
|
||||
response = self.client.post(
|
||||
'/api/auth/profile/edit',
|
||||
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())
|
||||
self.assertEqual(response.status_code, 400)
|
||||
self.assertIn('Invalid payload.', data['message'])
|
||||
self.assertIn('error', data['status'])
|
||||
|
@ -1,3 +1,5 @@
|
||||
import datetime
|
||||
|
||||
from mpwo_api import db
|
||||
from mpwo_api.users.models import User
|
||||
|
||||
@ -7,3 +9,15 @@ def add_user(username, email, password):
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user
|
||||
|
||||
|
||||
def add_user_full(username, email, password):
|
||||
user = User(username=username, email=email, password=password)
|
||||
user.first_name = 'John'
|
||||
user.last_name = 'Doe'
|
||||
user.bio = 'just a random guy'
|
||||
user.location = 'somewhere'
|
||||
user.birth_date = datetime.datetime.strptime('01/01/1980', '%d/%m/%Y')
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user
|
||||
|
@ -1,3 +1,4 @@
|
||||
import datetime
|
||||
from flask import Blueprint, jsonify, request
|
||||
from sqlalchemy import exc, or_
|
||||
|
||||
@ -155,6 +156,57 @@ def get_user_status(user_id):
|
||||
'email': user.email,
|
||||
'created_at': user.created_at,
|
||||
'admin': user.admin,
|
||||
'first_name': user.first_name,
|
||||
'last_name': user.last_name,
|
||||
'bio': user.bio,
|
||||
'location': user.location,
|
||||
'birth_date': user.birth_date,
|
||||
}
|
||||
}
|
||||
return jsonify(response_object), 200
|
||||
|
||||
|
||||
@auth_blueprint.route('/auth/profile/edit', methods=['POST'])
|
||||
@authenticate
|
||||
def edit_user(user_id):
|
||||
# get post data
|
||||
post_data = request.get_json()
|
||||
if not post_data:
|
||||
response_object = {
|
||||
'status': 'error',
|
||||
'message': 'Invalid payload.'
|
||||
}
|
||||
return jsonify(response_object), 400
|
||||
first_name = post_data.get('first_name')
|
||||
last_name = post_data.get('last_name')
|
||||
bio = post_data.get('bio')
|
||||
birth_date = post_data.get('birth_date')
|
||||
location = post_data.get('location')
|
||||
try:
|
||||
user = User.query.filter_by(id=user_id).first()
|
||||
user.first_name = first_name
|
||||
user.last_name = last_name
|
||||
user.bio = bio
|
||||
user.location = location
|
||||
user.birth_date = (
|
||||
datetime.datetime.strptime(birth_date, '%d/%m/%Y')
|
||||
if birth_date
|
||||
else None
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
response_object = {
|
||||
'status': 'success',
|
||||
'message': 'User profile updated.'
|
||||
}
|
||||
return jsonify(response_object), 200
|
||||
|
||||
# handler errors
|
||||
except (exc.IntegrityError, exc.OperationalError, ValueError) as e:
|
||||
db.session.rollback()
|
||||
appLog.error(e)
|
||||
response_object = {
|
||||
'status': 'error',
|
||||
'message': 'Error. Please try again or contact the administrator.'
|
||||
}
|
||||
return jsonify(response_object), 500
|
||||
|
@ -9,18 +9,23 @@ from mpwo_api import bcrypt, db
|
||||
class User(db.Model):
|
||||
__tablename__ = "users"
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
username = db.Column(db.String(80), unique=True, nullable=False)
|
||||
username = db.Column(db.String(20), unique=True, nullable=False)
|
||||
email = db.Column(db.String(120), unique=True, nullable=False)
|
||||
password = db.Column(db.String(255), nullable=False)
|
||||
created_at = db.Column(db.DateTime, nullable=False)
|
||||
admin = db.Column(db.Boolean, default=False, nullable=False)
|
||||
first_name = db.Column(db.String(80), nullable=True)
|
||||
last_name = db.Column(db.String(80), nullable=True)
|
||||
birth_date = db.Column(db.DateTime, nullable=True)
|
||||
location = db.Column(db.String(80), nullable=True)
|
||||
bio = db.Column(db.String(200), nullable=True)
|
||||
|
||||
def __repr__(self):
|
||||
return '<User %r>' % self.username
|
||||
|
||||
def __init__(
|
||||
self, username, email, password,
|
||||
created_at=datetime.datetime.utcnow()):
|
||||
created_at=datetime.datetime.now()):
|
||||
self.username = username
|
||||
self.email = email
|
||||
self.password = bcrypt.generate_password_hash(
|
||||
@ -30,7 +35,11 @@ class User(db.Model):
|
||||
|
||||
@staticmethod
|
||||
def encode_auth_token(user_id):
|
||||
"""Generates the auth token"""
|
||||
"""
|
||||
Generates the auth token
|
||||
:param user_id: -
|
||||
:return: JWToken
|
||||
"""
|
||||
try:
|
||||
payload = {
|
||||
'exp': datetime.datetime.utcnow() + datetime.timedelta(
|
||||
|
@ -5,21 +5,18 @@ from mpwo_api.users.models import User
|
||||
|
||||
|
||||
@app.cli.command()
|
||||
def recreate_db():
|
||||
"""Recreates a database."""
|
||||
def init_db():
|
||||
"""Init the database."""
|
||||
db.drop_all()
|
||||
db.create_all()
|
||||
db.session.commit()
|
||||
print('Database (re)creation done.')
|
||||
|
||||
|
||||
@app.cli.command()
|
||||
def seed_db():
|
||||
"""Seeds the database."""
|
||||
admin = User(username='admin', email='admin@example.com', password='admin')
|
||||
admin = User(
|
||||
username='admin',
|
||||
email='admin@example.com',
|
||||
password='mpwoadmin')
|
||||
admin.admin = True
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
print('Database initialization done.')
|
||||
|
||||
|
||||
@app.cli.command()
|
||||
|
Reference in New Issue
Block a user