API & Client : replace id with username to fetch a user

This commit is contained in:
Sam 2020-02-08 13:09:01 +01:00
parent 33ed19a7e7
commit 2c3bc0f9bc
10 changed files with 28 additions and 49 deletions

View File

@ -217,8 +217,8 @@
</dd></dl>
<dl class="get">
<dt id="get--api-users-(user_id)">
<code class="sig-name descname">GET </code><code class="sig-name descname">/api/users/</code><span class="sig-paren">(</span><em class="sig-param">user_id</em><span class="sig-paren">)</span><a class="headerlink" href="#get--api-users-(user_id)" title="Permalink to this definition"></a></dt>
<dt id="get--api-users-(user_name)">
<code class="sig-name descname">GET </code><code class="sig-name descname">/api/users/</code><span class="sig-paren">(</span><em class="sig-param">user_name</em><span class="sig-paren">)</span><a class="headerlink" href="#get--api-users-(user_name)" title="Permalink to this definition"></a></dt>
<dd><p>Get single user details</p>
<p><strong>Example request</strong>:</p>
<div class="highlight-http notranslate"><div class="highlight"><pre><span></span><span class="nf">GET</span> <span class="nn">/api/users/1</span> <span class="kr">HTTP</span><span class="o">/</span><span class="m">1.1</span>
@ -264,7 +264,7 @@
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>auth_user_id</strong> (<em>integer</em>) authenticate user id (from JSON Web Token)</p></li>
<li><p><strong>user_id</strong> (<em>integer</em>) user id</p></li>
<li><p><strong>user_name</strong> (<em>integer</em>) user name</p></li>
</ul>
</dd>
<dt class="field-even">Request Headers</dt>

View File

@ -217,12 +217,12 @@
<tr>
<td></td>
<td>
<a href="api/users.html#get--api-users-(user_id)"><code class="xref">GET /api/users/(user_id)</code></a></td><td>
<a href="api/users.html#get--api-users-(user_id)-picture"><code class="xref">GET /api/users/(user_id)/picture</code></a></td><td>
<em></em></td></tr>
<tr>
<td></td>
<td>
<a href="api/users.html#get--api-users-(user_id)-picture"><code class="xref">GET /api/users/(user_id)/picture</code></a></td><td>
<a href="api/users.html#get--api-users-(user_name)"><code class="xref">GET /api/users/(user_name)</code></a></td><td>
<em></em></td></tr>
<tr>
<td></td>

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@ -13,7 +13,7 @@ def test_ping(app):
assert 'success' in data['status']
def test_single_user(app, user_1):
def test_single_user(app, user_1, user_2):
"""=> Get single user details"""
client = app.test_client()
resp_login = client.post(
@ -22,7 +22,7 @@ def test_single_user(app, user_1):
content_type='application/json',
)
response = client.get(
f'/api/users/{user_1.id}',
f'/api/users/{user_2.username}',
content_type='application/json',
headers=dict(
Authorization='Bearer '
@ -36,8 +36,8 @@ def test_single_user(app, user_1):
assert len(data['data']['users']) == 1
user = data['data']['users'][0]
assert user['username'] == 'test'
assert user['email'] == 'test@test.com'
assert user['username'] == 'toto'
assert user['email'] == 'toto@toto.com'
assert user['created_at']
assert not user['admin']
assert user['first_name'] is None
@ -71,7 +71,7 @@ def test_single_user_with_activities(
content_type='application/json',
)
response = client.get(
f'/api/users/{user_1.id}',
f'/api/users/{user_1.username}',
content_type='application/json',
headers=dict(
Authorization='Bearer '
@ -104,30 +104,7 @@ def test_single_user_with_activities(
assert user['total_duration'] == '1:57:04'
def test_single_user_no_id(app, user_1):
"""=> Ensure error is thrown if an id is not provided."""
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/users/blah',
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 == 404
assert 'fail' in data['status']
assert 'User does not exist.' in data['message']
def test_single_user_wrong_id(app, user_1):
def test_single_user_no_existing(app, user_1):
"""=> Ensure error is thrown if the id does not exist."""
client = app.test_client()
resp_login = client.post(
@ -136,7 +113,7 @@ def test_single_user_wrong_id(app, user_1):
content_type='application/json',
)
response = client.get(
'/api/users/99999999999',
'/api/users/not_existing',
content_type='application/json',
headers=dict(
Authorization='Bearer '

View File

@ -98,9 +98,9 @@ def get_users(auth_user_id):
return jsonify(response_object), 200
@users_blueprint.route('/users/<user_id>', methods=['GET'])
@users_blueprint.route('/users/<user_name>', methods=['GET'])
@authenticate
def get_single_user(auth_user_id, user_id):
def get_single_user(auth_user_id, user_name):
"""
Get single user details
@ -149,7 +149,7 @@ def get_single_user(auth_user_id, user_id):
}
:param integer auth_user_id: authenticate user id (from JSON Web Token)
:param integer user_id: user id
:param integer user_name: user name
:reqheader Authorization: OAuth 2.0 Bearer Token
@ -164,7 +164,7 @@ def get_single_user(auth_user_id, user_id):
response_object = {'status': 'fail', 'message': 'User does not exist.'}
try:
user = User.query.filter_by(id=int(user_id)).first()
user = User.query.filter_by(username=user_name).first()
if not user:
return jsonify(response_object), 404
else:

View File

@ -41,7 +41,9 @@ class AdminUsers extends React.Component {
<tr key={user.id}>
<th scope="row">{user.id}</th>
<td>
<Link to={`/users/${user.id}`}>{user.username}</Link>
<Link to={`/users/${user.username}`}>
{user.username}
</Link>
</td>
<td>{user.email}</td>
<td>

View File

@ -48,7 +48,7 @@ class App extends React.Component {
<Route exact path="/profile" component={CurrentUserProfile} />
<Route exact path="/activities/history" component={Activities} />
<Route exact path="/activities/statistics" component={Statistics} />
<Route exact path="/users/:userId" component={UserProfile} />
<Route exact path="/users/:userName" component={UserProfile} />
<Route path="/activities" component={Activity} />
<Route path="/admin" component={Admin} />
<Route component={NotFound} />

View File

@ -7,12 +7,12 @@ import { getOrUpdateData } from '../../actions'
class UserProfile extends React.Component {
componentDidMount() {
this.props.loadUser(this.props.match.params.userId)
this.props.loadUser(this.props.match.params.userName)
}
componentDidUpdate(prevProps) {
if (prevProps.match.params.userId !== this.props.match.params.userId) {
this.props.loadUser(this.props.match.params.userId)
if (prevProps.match.params.userName !== this.props.match.params.userName) {
this.props.loadUser(this.props.match.params.userName)
}
}
@ -40,8 +40,8 @@ export default withTranslation()(
users: state.users.data,
}),
dispatch => ({
loadUser: userId => {
dispatch(getOrUpdateData('getData', 'users', { id: userId }))
loadUser: userName => {
dispatch(getOrUpdateData('getData', 'users', { username: userName }))
},
})
)(UserProfile)

View File

@ -3,8 +3,8 @@ import { createApiRequest } from '../utils'
export default class FitTrackeeApi {
static getData(target, data = {}) {
let url = target
if (data.id) {
url = `${url}/${data.id}`
if (data.id || (target === 'users' && data.username)) {
url = `${url}/${data.username ? data.username : data.id}`
} else if (Object.keys(data).length > 0) {
url += '?'
Object.keys(data).map(key => (url += `&${key}=${data[key]}`))