Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: change PUT for POST and DELETE #229
  • Loading branch information
Angeluz-07 committed Nov 19, 2020
commit 67ce20bff82e235fc3f9e18278d99231267ab5b8
22 changes: 19 additions & 3 deletions tests/time_tracker_api/users/users_namespace_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_update_user_role_response_contains_expected_props(
return_value={'name': 'dummy', 'email': 'dummy', 'role': 'dummy'}
)

response = client.put(
response = client.post(
f'/users/{user_id}/roles',
headers=valid_header,
json=valid_user_role_data,
Expand All @@ -42,15 +42,15 @@ def test_update_user_role_response_contains_expected_props(


@patch('utils.azure_users.AzureConnection.update_user_role', new_callable=Mock)
def test_update_user_role_is_being_called_with_valid_arguments(
def test_on_post_update_user_role_is_being_called_with_valid_arguments(
update_user_role_mock,
client: FlaskClient,
valid_header: dict,
user_id: str,
):

valid_user_role_data = {'role': 'admin'}
response = client.put(
response = client.post(
f'/users/{user_id}/roles',
headers=valid_header,
json=valid_user_role_data,
Expand All @@ -60,3 +60,19 @@ def test_update_user_role_is_being_called_with_valid_arguments(
update_user_role_mock.assert_called_once_with(
user_id, valid_user_role_data['role']
)


@patch('utils.azure_users.AzureConnection.update_user_role', new_callable=Mock)
def test_on_delete_update_user_role_is_being_called_with_valid_arguments(
update_user_role_mock,
client: FlaskClient,
valid_header: dict,
user_id: str,
):

response = client.delete(
f'/users/{user_id}/roles/time-tracker-admin', headers=valid_header,
)

assert HTTPStatus.OK == response.status_code
update_user_role_mock.assert_called_once_with(user_id, role=None)
46 changes: 28 additions & 18 deletions time_tracker_api/users/users_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

from time_tracker_api.api import common_fields, api, NullableString

faker = Faker()
from utils.azure_users import AzureConnection


azure_connection = AzureConnection()

ns = api.namespace('users', description='Namespace of the API for users')

Expand All @@ -17,34 +20,34 @@
title='Name',
max_length=50,
description='Name of the user',
example=faker.word(['Marcelo', 'Sandro']),
example=Faker().word(['Marcelo', 'Sandro']),
),
'email': fields.String(
title="User's Email",
max_length=50,
description='Email of the user that belongs to the tenant',
example=faker.email(),
example=Faker().email(),
),
'role': NullableString(
title="User's Role",
max_length=50,
description='Role assigned to the user by the tenant',
example=faker.word(['time-tracker-admin']),
example=Faker().word(['time-tracker-admin']),
),
},
)

user_response_fields.update(common_fields)

user_input_fields = ns.model(
'UserInput',
user_role_input_fields = ns.model(
'UserRoleInput',
{
'role': NullableString(
title="User's Role",
required=True,
max_length=50,
description='Role assigned to the user by the tenant',
example=faker.word(['time-tracker-admin']),
example=Faker().word(['time-tracker-admin']),
),
},
)
Expand All @@ -56,26 +59,33 @@ class Users(Resource):
@ns.marshal_list_with(user_response_fields)
def get(self):
"""List all users"""
from utils.azure_users import AzureConnection

azure_connection = AzureConnection()
return azure_connection.users()


@ns.route('/<string:id>/roles')
@ns.response(HTTPStatus.NOT_FOUND, 'User not found')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User not found? I would say it is better to say Role not found

@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
@ns.param('id', 'The user identifier')
class UserRole(Resource):
@ns.doc('update_user_role')
@ns.expect(user_input_fields)
class UserRoles(Resource):
@ns.doc('create_user_role')
@ns.expect(user_role_input_fields)
@ns.response(
HTTPStatus.BAD_REQUEST, 'Invalid format or structure of the user'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invalid format or structure of the user? Is not this for roles?

)
@ns.marshal_with(user_response_fields)
def put(self, id):
"""Update user's role"""
from utils.azure_users import AzureConnection

azure_connection = AzureConnection()
def post(self, id):
"""Create user's role"""
return azure_connection.update_user_role(id, ns.payload['role'])


@ns.route('/<string:user_id>/roles/<string:role_id>')
@ns.response(HTTPStatus.NOT_FOUND, 'User not found')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User not found? IMO, it is better to say Role not found

@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
@ns.param('user_id', 'The user identifier')
@ns.param('role_id', 'The role name identifier')
class UserRole(Resource):
@ns.doc('delete_user_role')
@ns.marshal_with(user_response_fields)
def delete(self, user_id, role_id):
"""Delete user's role"""
return azure_connection.update_user_role(user_id, role=None)