-
Notifications
You must be signed in to change notification settings - Fork 0
229 make admin #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
229 make admin #235
Changes from all commits
4d129a9
bcd2564
68f4961
47e21af
d6f5ad5
da5024f
67ce20b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,78 @@ | ||
| from unittest.mock import Mock | ||
| from unittest.mock import Mock, patch | ||
| from flask import json | ||
| from flask.testing import FlaskClient | ||
| from flask_restplus._http import HTTPStatus | ||
| from utils.azure_users import AzureConnection | ||
|
|
||
|
|
||
| def test_users_response_contains_expected_props( | ||
| client: FlaskClient, | ||
| valid_header: dict, | ||
| client: FlaskClient, valid_header: dict, | ||
| ): | ||
|
|
||
| AzureConnection.users = Mock( | ||
| return_value=[{'name': 'dummy', 'email': 'dummy', 'role': 'dummy'}] | ||
| ) | ||
|
|
||
| response = client.get( | ||
| '/users', | ||
| headers=valid_header, | ||
| ) | ||
| response = client.get('/users', headers=valid_header,) | ||
|
|
||
| assert HTTPStatus.OK == response.status_code | ||
| assert 'name' in json.loads(response.data)[0] | ||
| assert 'email' in json.loads(response.data)[0] | ||
| assert 'role' in json.loads(response.data)[0] | ||
|
|
||
|
|
||
| def test_update_user_role_response_contains_expected_props( | ||
| client: FlaskClient, valid_header: dict, user_id: str, | ||
| ): | ||
| valid_user_role_data = {'role': 'admin'} | ||
| AzureConnection.update_user_role = Mock( | ||
| return_value={'name': 'dummy', 'email': 'dummy', 'role': 'dummy'} | ||
| ) | ||
|
|
||
| response = client.post( | ||
| f'/users/{user_id}/roles', | ||
| headers=valid_header, | ||
| json=valid_user_role_data, | ||
| ) | ||
|
|
||
| assert HTTPStatus.OK == response.status_code | ||
| assert 'name' in json.loads(response.data) | ||
| assert 'email' in json.loads(response.data) | ||
| assert 'role' in json.loads(response.data) | ||
|
|
||
|
|
||
| @patch('utils.azure_users.AzureConnection.update_user_role', new_callable=Mock) | ||
| 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.post( | ||
| f'/users/{user_id}/roles', | ||
| headers=valid_header, | ||
| json=valid_user_role_data, | ||
| ) | ||
|
|
||
| assert HTTPStatus.OK == response.status_code | ||
| 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) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,12 @@ | |
| from flask_restplus import fields, Resource | ||
| from flask_restplus._http import HTTPStatus | ||
|
|
||
| from time_tracker_api.api import common_fields, api | ||
| 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') | ||
|
|
||
|
|
@@ -17,45 +20,72 @@ | |
| 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': fields.String( | ||
| 'role': NullableString( | ||
| title="User's Role", | ||
| max_length=50, | ||
| description='Role assigned to the user by the tenant', | ||
| example=faker.word(['admin']), | ||
| example=Faker().word(['time-tracker-admin']), | ||
| ), | ||
| }, | ||
| ) | ||
|
|
||
| user_response_fields.update(common_fields) | ||
|
|
||
| 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']), | ||
| ), | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| @ns.route('') | ||
| class Users(Resource): | ||
| @ns.doc('list_users') | ||
| @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>') | ||
| @ns.route('/<string:id>/roles') | ||
| @ns.response(HTTPStatus.NOT_FOUND, 'User not found') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. User not found? I would say it is better to say |
||
| @ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format') | ||
| @ns.param('id', 'The user identifier') | ||
| class User(Resource): | ||
| @ns.doc('get_user') | ||
| 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' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
Angeluz-07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. User not found? IMO, it is better to say |
||
| @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 get(self, id): | ||
| """Get an user""" | ||
| return {} | ||
| def delete(self, user_id, role_id): | ||
| """Delete user's role""" | ||
| return azure_connection.update_user_role(user_id, role=None) | ||
Uh oh!
There was an error while loading. Please reload this page.