Skip to content

Commit 67ce20b

Browse files
committed
feat: change PUT for POST and DELETE #229
1 parent da5024f commit 67ce20b

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

tests/time_tracker_api/users/users_namespace_test.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_update_user_role_response_contains_expected_props(
2929
return_value={'name': 'dummy', 'email': 'dummy', 'role': 'dummy'}
3030
)
3131

32-
response = client.put(
32+
response = client.post(
3333
f'/users/{user_id}/roles',
3434
headers=valid_header,
3535
json=valid_user_role_data,
@@ -42,15 +42,15 @@ def test_update_user_role_response_contains_expected_props(
4242

4343

4444
@patch('utils.azure_users.AzureConnection.update_user_role', new_callable=Mock)
45-
def test_update_user_role_is_being_called_with_valid_arguments(
45+
def test_on_post_update_user_role_is_being_called_with_valid_arguments(
4646
update_user_role_mock,
4747
client: FlaskClient,
4848
valid_header: dict,
4949
user_id: str,
5050
):
5151

5252
valid_user_role_data = {'role': 'admin'}
53-
response = client.put(
53+
response = client.post(
5454
f'/users/{user_id}/roles',
5555
headers=valid_header,
5656
json=valid_user_role_data,
@@ -60,3 +60,19 @@ def test_update_user_role_is_being_called_with_valid_arguments(
6060
update_user_role_mock.assert_called_once_with(
6161
user_id, valid_user_role_data['role']
6262
)
63+
64+
65+
@patch('utils.azure_users.AzureConnection.update_user_role', new_callable=Mock)
66+
def test_on_delete_update_user_role_is_being_called_with_valid_arguments(
67+
update_user_role_mock,
68+
client: FlaskClient,
69+
valid_header: dict,
70+
user_id: str,
71+
):
72+
73+
response = client.delete(
74+
f'/users/{user_id}/roles/time-tracker-admin', headers=valid_header,
75+
)
76+
77+
assert HTTPStatus.OK == response.status_code
78+
update_user_role_mock.assert_called_once_with(user_id, role=None)

time_tracker_api/users/users_namespace.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
from time_tracker_api.api import common_fields, api, NullableString
66

7-
faker = Faker()
7+
from utils.azure_users import AzureConnection
8+
9+
10+
azure_connection = AzureConnection()
811

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

@@ -17,34 +20,34 @@
1720
title='Name',
1821
max_length=50,
1922
description='Name of the user',
20-
example=faker.word(['Marcelo', 'Sandro']),
23+
example=Faker().word(['Marcelo', 'Sandro']),
2124
),
2225
'email': fields.String(
2326
title="User's Email",
2427
max_length=50,
2528
description='Email of the user that belongs to the tenant',
26-
example=faker.email(),
29+
example=Faker().email(),
2730
),
2831
'role': NullableString(
2932
title="User's Role",
3033
max_length=50,
3134
description='Role assigned to the user by the tenant',
32-
example=faker.word(['time-tracker-admin']),
35+
example=Faker().word(['time-tracker-admin']),
3336
),
3437
},
3538
)
3639

3740
user_response_fields.update(common_fields)
3841

39-
user_input_fields = ns.model(
40-
'UserInput',
42+
user_role_input_fields = ns.model(
43+
'UserRoleInput',
4144
{
4245
'role': NullableString(
4346
title="User's Role",
4447
required=True,
4548
max_length=50,
4649
description='Role assigned to the user by the tenant',
47-
example=faker.word(['time-tracker-admin']),
50+
example=Faker().word(['time-tracker-admin']),
4851
),
4952
},
5053
)
@@ -56,26 +59,33 @@ class Users(Resource):
5659
@ns.marshal_list_with(user_response_fields)
5760
def get(self):
5861
"""List all users"""
59-
from utils.azure_users import AzureConnection
60-
61-
azure_connection = AzureConnection()
6262
return azure_connection.users()
6363

6464

6565
@ns.route('/<string:id>/roles')
6666
@ns.response(HTTPStatus.NOT_FOUND, 'User not found')
6767
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
6868
@ns.param('id', 'The user identifier')
69-
class UserRole(Resource):
70-
@ns.doc('update_user_role')
71-
@ns.expect(user_input_fields)
69+
class UserRoles(Resource):
70+
@ns.doc('create_user_role')
71+
@ns.expect(user_role_input_fields)
7272
@ns.response(
7373
HTTPStatus.BAD_REQUEST, 'Invalid format or structure of the user'
7474
)
7575
@ns.marshal_with(user_response_fields)
76-
def put(self, id):
77-
"""Update user's role"""
78-
from utils.azure_users import AzureConnection
79-
80-
azure_connection = AzureConnection()
76+
def post(self, id):
77+
"""Create user's role"""
8178
return azure_connection.update_user_role(id, ns.payload['role'])
79+
80+
81+
@ns.route('/<string:user_id>/roles/<string:role_id>')
82+
@ns.response(HTTPStatus.NOT_FOUND, 'User not found')
83+
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
84+
@ns.param('user_id', 'The user identifier')
85+
@ns.param('role_id', 'The role name identifier')
86+
class UserRole(Resource):
87+
@ns.doc('delete_user_role')
88+
@ns.marshal_with(user_response_fields)
89+
def delete(self, user_id, role_id):
90+
"""Delete user's role"""
91+
return azure_connection.update_user_role(user_id, role=None)

0 commit comments

Comments
 (0)