Skip to content

Commit

Permalink
Merge pull request #39 from konishi-project/next
Browse files Browse the repository at this point in the history
Add: Getting User Info
  • Loading branch information
Zeth Leonardo authored Sep 29, 2019
2 parents 9530628 + aa1938d commit c23fa46
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
10 changes: 5 additions & 5 deletions zimmerman/main/controller/user_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def post(self):
data = request.get_json()
return UserService.register(data)

@api.route('/get')
@api.route('/get/<string:username>')
class UserGet(Resource):

@api.doc('Get a specific user',
Expand All @@ -33,13 +33,13 @@ class UserGet(Resource):
)
@jwt_required
def get(self):
""" Get a specific user using its public id """
""" Get a specific user by their username """
# Check for arguments
current_user = load_user(get_jwt_identity())
current_public_id = current_user.public_id
current_username = current_user.username

user_public_id = request.args.get("user_public_id", default=current_public_id)
return UserService.get_user_info(user_public_id)
username = request.args.get("username", default=current_username)
return UserService.get_user_info(username)

@api.route('/update')
class UserUpdate(Resource):
Expand Down
14 changes: 8 additions & 6 deletions zimmerman/main/service/auth_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ def login_user(data):

access_token = create_access_token(identity=user.id)
if access_token:
return {
'message': 'Successfully logged in',
response_object = {
'success': True,
'message': 'Successfully logged in.',
'user': user_info,
'Authorization': access_token
}, 200
}
return response_object, 200
# Return Incorrect pass if the others fail
else:
return {
'message': 'Failed to log in, password may be incorrect.',
response_object = {
'success': False,
}, 403
'message': 'Failed to log in, password may be incorrect.',
}
return response_object, 403

except Exception as error:
response_object = {
Expand Down
6 changes: 3 additions & 3 deletions zimmerman/main/service/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ def register(data):
}
return response_object, 500

# Query user INFO by its public id
def get_user_info(user_public_id):
user = User.query.filter_by(public_id=user_public_id).first()
# Get user INFO by its username
def get_user_info(username):
user = User.query.filter_by(username=username).first()
if not user:
response_object = {
'success': False,
Expand Down
7 changes: 4 additions & 3 deletions zimmerman/test/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def register_user(self):
content_type = 'application/json'
)

def get_user(self, access_token):
def get_user(self, access_token, username):
return self.client.get(
'/user/get',
'/user/get/%s' % username,
headers = {
'Authorization': 'Bearer %s' % access_token
},
Expand Down Expand Up @@ -80,7 +80,8 @@ def test_get_user(self):
access_token = login_response_data['Authorization']

# Get the user data
get_response = get_user(self, access_token)
username = login_response_data['user']['username']
get_response = get_user(self, access_token, username)
get_response_data = json.loads(get_response.data.decode())

self.assertEqual(get_response.status_code, 200)
Expand Down

0 comments on commit c23fa46

Please sign in to comment.