Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 25 additions & 0 deletions tests/time_tracker_api/users/users_namespace_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from unittest.mock import Mock
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,
):

AzureConnection.users = Mock(
return_value=[{'name': 'dummy', 'email': 'dummy', 'role': 'dummy'}]
)

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]
4 changes: 4 additions & 0 deletions time_tracker_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ def init_app(app: Flask):

api.add_namespace(technologies_namespace.ns)

from time_tracker_api.users import users_namespace

api.add_namespace(users_namespace.ns)


"""
Error handlers
Expand Down
61 changes: 61 additions & 0 deletions time_tracker_api/users/users_namespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from faker import Faker
from flask_restplus import fields, Resource
from flask_restplus._http import HTTPStatus

from time_tracker_api.api import common_fields, api

faker = Faker()

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

# User Model

user_response_fields = ns.model(
'User',
{
'name': fields.String(
title='Name',
max_length=50,
description='Name of the user',
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(),
),
'role': fields.String(
title="User's Role",
max_length=50,
description='Role assigned to the user by the tenant',
example=faker.word(['admin']),
),
},
)

user_response_fields.update(common_fields)


@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.response(HTTPStatus.NOT_FOUND, 'User not found')
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
@ns.param('id', 'The user identifier')
class User(Resource):
@ns.doc('get_user')
@ns.marshal_with(user_response_fields)
def get(self, id):
"""Get an user"""
return {}
19 changes: 16 additions & 3 deletions utils/azure_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ def __call__(self, r):


class AzureUser:
def __init__(self, id, name, email):
def __init__(self, id, name, email, role):
self.id = id
self.name = name
self.email = email
self.role = role


class AzureConnection:
Expand All @@ -66,12 +67,24 @@ def get_token(self):
def users(self) -> List[AzureUser]:
def to_azure_user(item) -> AzureUser:
there_is_email = len(item['otherMails']) > 0
there_is_role = (
'extension_1d76efa96f604499acc0c0ee116a1453_role' in item
)

id = item['objectId']
name = item['displayName']
email = item['otherMails'][0] if there_is_email else ''
return AzureUser(id, name, email)
role = (
item['extension_1d76efa96f604499acc0c0ee116a1453_role']
if there_is_role
else None
)
return AzureUser(id, name, email, role)

endpoint = f"{self.config.ENDPOINT}/users?api-version=1.6&$select=displayName,otherMails,objectId"
endpoint = "{endpoint}/users?api-version=1.6&$select=displayName,otherMails,objectId,{role_field}".format(
endpoint=self.config.ENDPOINT,
role_field='extension_1d76efa96f604499acc0c0ee116a1453_role',
)
response = requests.get(endpoint, auth=BearerAuth(self.access_token))

assert 200 == response.status_code
Expand Down