Skip to content

Commit c8908e0

Browse files
author
Matthew Fortunka
committed
more linting issues addressed
1 parent 79248b6 commit c8908e0

File tree

11 files changed

+43
-47
lines changed

11 files changed

+43
-47
lines changed

api_app/api/dependencies/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async def _get_store_key(cls, credential) -> str:
6969
) as cosmosdb_mng_client:
7070
database_keys = await cosmosdb_mng_client.database_accounts.list_keys(
7171
resource_group_name=RESOURCE_GROUP_NAME,
72-
account_name=COSMOSDB_ACCOUNT_NAME,
72+
account_name=COSMOSDB_ACCOUNT_NAME
7373
)
7474
primary_master_key = database_keys.primary_master_key
7575

api_app/api/routes/workspace_users.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from fastapi import APIRouter, Depends,Response,status
1+
from fastapi import APIRouter, Depends, Response, status
22
from api.dependencies.workspaces import get_workspace_by_id_from_path
33
from resources import strings
44
from services.authentication import get_access_service
55
from models.schemas.users import UsersInResponse, AssignableUsersInResponse
66
from models.schemas.roles import RolesInResponse
7-
from services.authentication import get_current_admin_user,get_current_workspace_owner_or_researcher_user_or_airlock_manager_or_tre_admin
7+
from services.authentication import get_current_admin_user, get_current_workspace_owner_or_researcher_user_or_airlock_manager_or_tre_admin
88

99
workspaces_users_admin_router = APIRouter(dependencies=[Depends(get_current_admin_user)])
1010
workspaces_users_shared_router = APIRouter(dependencies=[Depends(get_current_workspace_owner_or_researcher_user_or_airlock_manager_or_tre_admin)])
@@ -48,9 +48,7 @@ async def assign_workspace_user(response: Response, user_email: str, role_name:
4848
return UsersInResponse(users=users)
4949

5050

51-
@workspaces_users_admin_router.delete("/workspaces/{workspace_id}/users/assign",
52-
status_code=status.HTTP_202_ACCEPTED,
53-
name=strings.API_REMOVE_WORKSPACE_USER_ASSIGNMENT)
51+
@workspaces_users_admin_router.delete("/workspaces/{workspace_id}/users/assign", status_code=status.HTTP_202_ACCEPTED, name=strings.API_REMOVE_WORKSPACE_USER_ASSIGNMENT)
5452
async def remove_workspace_user_assignment(user_email: str,
5553
role_name: str,
5654
workspace=Depends(get_workspace_by_id_from_path)) -> UsersInResponse:

api_app/models/domain/authentication.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ class User(BaseModel):
1414
roles: List[str] = Field([])
1515
roleAssignments: List[RoleAssignment] = Field([])
1616

17+
1718
class AssignableUser(BaseModel):
1819
name: str
1920
email: str
2021

22+
2123
class Role(BaseModel):
2224
id: str
2325
value: str

api_app/models/schemas/roles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pydantic import BaseModel, Field
22
from typing import List
3-
43
from models.domain.authentication import Role
54

5+
66
class RolesInResponse(BaseModel):
77
roles: List[Role] = Field(..., title="Roles", description="List of roles in a workspace")

api_app/models/schemas/users.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ class Config:
2727
}
2828
}
2929

30+
3031
class AssignableUsersInResponse(BaseModel):
3132
assignable_users: List[AssignableUser] = Field(..., title="Assignable Users", description="List of users assignable to a workspace")

api_app/services/aad_authentication.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import base64
2-
import json
32
from collections import defaultdict
43
from enum import Enum
54
from typing import List, Optional
@@ -12,7 +11,7 @@
1211

1312
from services.access_service import AccessService, AuthConfigValidationError, UserRoleAssignmentError
1413
from core import config
15-
from db.errors import DuplicateEntity, EntityDoesNotExist
14+
from db.errors import EntityDoesNotExist
1615
from models.domain.authentication import User, AssignableUser, Role, RoleAssignment
1716
from models.domain.workspace import Workspace, WorkspaceRole
1817
from resources import strings
@@ -283,13 +282,13 @@ def _get_users_inc_groups_from_response(self, users_graph_data, roles_graph_data
283282
user_email = user_data["body"]["userPrincipalName"]
284283
# if user with id does not already exist in users
285284
user_roles=self._get_roles_for_principal(user_id, roles_graph_data, app_id_to_role_name)
285+
286286
if not any(user.id == user_id for user in users):
287287
users.append(User(id=user_id, name=user_name, email=user_email, roles=user_roles))
288288
else:
289289
user = next((user for user in users if user.id == user_id), None)
290290
user.roles = list(set(user.roles + user_roles))
291291

292-
293292
# Handle group endpoint response
294293
elif "directoryObjects" in user_data["body"]["@odata.context"]:
295294
group_id = user_data["id"]
@@ -299,6 +298,7 @@ def _get_users_inc_groups_from_response(self, users_graph_data, roles_graph_data
299298
user_email = group_member["userPrincipalName"]
300299

301300
group_roles=self._get_roles_for_principal(group_id, roles_graph_data, app_id_to_role_name)
301+
302302
if not any(user.id == user_id for user in users):
303303
users.append(User(id=user_id, name=user_name, email=user_email, roles=group_roles))
304304
else:
@@ -339,11 +339,11 @@ def get_workspace_roles(self, workspace: Workspace) -> List[Role]:
339339

340340
for role in graph_data["value"]:
341341
roles.append(Role(id=role["id"], value=role["value"],
342-
isEnabled=role["isEnabled"],
343-
description=role["description"],
344-
displayName=role["displayName"],
345-
origin=role["origin"],
346-
allowedMemberTypes=role["allowedMemberTypes"]))
342+
isEnabled=role["isEnabled"],
343+
description=role["description"],
344+
displayName=role["displayName"],
345+
origin=role["origin"],
346+
allowedMemberTypes=role["allowedMemberTypes"]))
347347

348348
return roles
349349

@@ -359,7 +359,7 @@ def get_workspace_user_emails_by_role_assignment(self, workspace: Workspace):
359359
return workspace_role_assignments_details
360360

361361
def get_workspace_role_by_name(self, name: str, workspace: Workspace) -> Role:
362-
app_roles_endpoint = f"{MICROSOFT_GRAPH_URL}/v1.0/servicePrincipals/{workspace.properties['sp_id']}/appRoles"
362+
app_roles_endpoint = f"{MICROSOFT_GRAPH_URL}/v1.0/servicePrincipals/{workspace.properties['sp_id']}/appRoles"
363363
graph_data = self._ms_graph_query(app_roles_endpoint, "GET")
364364

365365
for role in graph_data["value"]:
@@ -399,7 +399,7 @@ def _is_workspace_role_group_in_use(self, workspace: Workspace) -> bool:
399399
def _get_workspace_group_name(self, workspace: Workspace, role: Role) -> tuple:
400400
tre_id = workspace.properties["tre_id"]
401401
workspace_id = workspace.properties["workspace_id"]
402-
group_suffix = ""
402+
group_name = ""
403403
app_role_id_suffix = ""
404404
if role.value == "WorkspaceResearcher":
405405
group_name = "Workspace Researchers"
@@ -487,7 +487,7 @@ def remove_workspace_role_user_assignment(self, user: User,
487487
if self._is_workspace_role_group_in_use(workspace):
488488
self._remove_workspace_user_from_application_group(user, workspace, role)
489489
else:
490-
self._remove_workspace_user_from_application(user,role_assignment)
490+
self._remove_workspace_user_from_application(user, role_assignment)
491491

492492
def _remove_workspace_user_from_application(self, user: User, role_assignment: dict) -> requests.Response:
493493
msgraph_token = self._get_msgraph_token()

api_app/services/access_service.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
class AuthConfigValidationError(Exception):
1010
"""Raised when the input auth information is invalid"""
1111

12+
1213
class UserRoleAssignmentError(Exception):
1314
"""Raised when a user role assignment fails"""
1415

16+
1517
class AccessService(OAuth2AuthorizationCodeBearer):
1618
@abstractmethod
1719
def extract_workspace_auth_information(self, data: dict) -> dict:

api_app/tests_ma/test_api/conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
22
import pytest_asyncio
33
from mock import patch
4-
from unittest.mock import patch
54

65
from fastapi import FastAPI
76
from httpx import AsyncClient

api_app/tests_ma/test_api/test_routes/test_workspace_users.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from unittest.mock import AsyncMock
21
import pytest
32
from mock import patch
43

@@ -24,6 +23,7 @@
2423
CLIENT_ID = 'f0acf127-a672-a672-a672-a15e5bf9f127'
2524
OPERATION_ID = '11111111-7265-4b5f-9eae-a1a62928772f'
2625

26+
2727
def sample_workspace(workspace_id=WORKSPACE_ID, auth_info: dict = {}) -> Workspace:
2828
workspace = Workspace(
2929
id=workspace_id,
@@ -83,14 +83,13 @@ async def test_get_workspace_users_returns_users(self, _, auth_class, app, clien
8383
assert response.status_code == status.HTTP_200_OK
8484
assert response.json()["users"] == users
8585

86-
8786
@pytest.mark.parametrize("auth_class", ["aad_authentication.AzureADAuthorization"])
8887
@patch("api.dependencies.workspaces.WorkspaceRepository.get_workspace_by_id", return_value=sample_workspace())
8988
async def test_assign_workspace_user_assigns_workspace_user(self, get_workspace_by_id_mock, auth_class, app, client):
9089
with patch(f"services.{auth_class}.get_user_by_email") as get_user_by_email_mock, \
91-
patch(f"services.{auth_class}.get_workspace_role_by_name") as get_workspace_role_by_name_mock, \
92-
patch(f"services.{auth_class}.assign_workspace_user") as assign_workspace_user_mock, \
93-
patch(f"services.{auth_class}.get_workspace_users") as get_workspace_users_mock:
90+
patch(f"services.{auth_class}.get_workspace_role_by_name") as get_workspace_role_by_name_mock, \
91+
patch(f"services.{auth_class}.assign_workspace_user") as assign_workspace_user_mock, \
92+
patch(f"services.{auth_class}.get_workspace_users") as get_workspace_users_mock:
9493

9594
workspace = get_workspace_by_id_mock.return_value
9695

@@ -182,7 +181,6 @@ async def test_get_assignable_users_returns_assignable_users(self, get_workspace
182181
assert response.status_code == status.HTTP_200_OK
183182
assert response.json()["assignable_users"] == assignable_users
184183

185-
186184
@pytest.mark.parametrize("auth_class", ["aad_authentication.AzureADAuthorization"])
187185
@patch("api.dependencies.workspaces.WorkspaceRepository.get_workspace_by_id", return_value=sample_workspace())
188186
async def test_get_workspace_roles_returns_workspace_roles(self, get_workspace_by_id_mock, auth_class, app, client):

api_app/tests_ma/test_api/test_routes/test_workspaces.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from db.errors import EntityDoesNotExist
1616
from db.repositories.workspaces import WorkspaceRepository
1717
from db.repositories.workspace_services import WorkspaceServiceRepository
18-
from models.domain.authentication import RoleAssignment, User, Role
18+
from models.domain.authentication import RoleAssignment
1919
from models.domain.operation import Operation, OperationStep, Status
2020
from models.domain.resource import ResourceHistoryItem, ResourceType
2121
from models.domain.user_resource import UserResource

api_app/tests_ma/test_services/test_aad_access_service.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def get_app_sp_graph_data_mock():
8585
]
8686
}
8787

88+
8889
@pytest.fixture
8990
def workspace_with_groups():
9091
return Workspace(
@@ -105,6 +106,7 @@ def workspace_with_groups():
105106
}
106107
)
107108

109+
108110
@pytest.fixture
109111
def workspace_without_groups():
110112
return Workspace(
@@ -125,14 +127,17 @@ def workspace_without_groups():
125127
}
126128
)
127129

130+
128131
@pytest.fixture
129132
def role_owner():
130133
return Role(id="owner-role-id", value="WorkspaceOwner", isEnabled=True, description="Owner", displayName="Owner", origin="", allowedMemberTypes=[])
131134

135+
132136
@pytest.fixture
133137
def user_without_role():
134138
return User(id="user1", name="Test User", email="[email protected]", roles=[])
135139

140+
136141
@pytest.fixture
137142
def user_with_role():
138143
return User(id="user2", name="Test User 2", email="[email protected]", roles=["WorkspaceOwner"])
@@ -654,21 +659,6 @@ def test_get_workspace_role_by_name(mock_ms_graph_query):
654659

655660
@patch("services.aad_authentication.AzureADAuthorization.get_user_by_email")
656661
def test_get_user_by_email(mock_get_user_by_email):
657-
workspace = Workspace(
658-
id="abc",
659-
etag="",
660-
templateName="template-name",
661-
templateVersion="0.1.0",
662-
resourcePath="test",
663-
properties={
664-
"client_id": "1234",
665-
"sp_id": "abc127",
666-
"app_role_id_workspace_owner": "abc128",
667-
"app_role_id_workspace_researcher": "abc129",
668-
"app_role_id_workspace_airlock_manager": "abc130",
669-
},
670-
)
671-
672662
mock_get_user_by_email.return_value = User(id="1", name="John Doe", email="[email protected]", roles=["WorkspaceOwner"])
673663

674664
access_service = AzureADAuthorization()
@@ -754,6 +744,7 @@ def get_mock_role_response(principal_roles):
754744
)
755745
return response
756746

747+
757748
@patch("services.aad_authentication.AzureADAuthorization._is_user_in_role", return_value=True)
758749
@patch("services.aad_authentication.AzureADAuthorization._is_workspace_role_group_in_use")
759750
@patch("services.aad_authentication.AzureADAuthorization._assign_workspace_user_to_application_group")
@@ -769,12 +760,13 @@ def test_assign_workspace_user_already_has_role(workspace_role_in_use_mock,
769760
assert assign_user_to_group_mock.call_count == 0
770761
assert assign_user_to_role_mock.call_count == 0
771762

763+
772764
@patch("services.aad_authentication.AzureADAuthorization._is_user_in_role", return_value=False)
773765
@patch("services.aad_authentication.AzureADAuthorization._is_workspace_role_group_in_use", return_value=False)
774766
@patch("services.aad_authentication.AzureADAuthorization._assign_workspace_user_to_application_group")
775767
@patch("services.aad_authentication.AzureADAuthorization._assign_workspace_user_to_application")
776768
def test_assign_workspace_user_if_no_groups(assign_user_to_role_mock,assign_user_to_group_mock,
777-
workspace_without_groups, role_owner,
769+
workspace_without_groups, role_owner,
778770
user_with_role):
779771

780772
access_service = AzureADAuthorization()
@@ -785,12 +777,13 @@ def test_assign_workspace_user_if_no_groups(assign_user_to_role_mock,assign_user
785777
assert assign_user_to_group_mock.call_count == 0
786778
assert assign_user_to_role_mock.call_count == 1
787779

780+
788781
@patch("services.aad_authentication.AzureADAuthorization._is_user_in_role", return_value=False)
789782
@patch("services.aad_authentication.AzureADAuthorization._is_workspace_role_group_in_use", return_value=True)
790783
@patch("services.aad_authentication.AzureADAuthorization._assign_workspace_user_to_application_group")
791784
@patch("services.aad_authentication.AzureADAuthorization._assign_workspace_user_to_application")
792785
def test_assign_workspace_user_if_groups(assign_user_to_role_mock,assign_user_to_group_mock,
793-
workspace_without_groups, role_owner,
786+
workspace_without_groups, role_owner,
794787
user_with_role):
795788

796789
access_service = AzureADAuthorization()
@@ -801,12 +794,13 @@ def test_assign_workspace_user_if_groups(assign_user_to_role_mock,assign_user_to
801794
assert assign_user_to_group_mock.call_count == 1
802795
assert assign_user_to_role_mock.call_count == 0
803796

797+
804798
@patch("services.aad_authentication.AzureADAuthorization._is_workspace_role_group_in_use", return_value=False)
805799
@patch("services.aad_authentication.AzureADAuthorization._remove_workspace_user_from_application_group")
806800
@patch("services.aad_authentication.AzureADAuthorization._remove_workspace_user_from_application")
807801
@patch("services.aad_authentication.AzureADAuthorization._get_role_assignment_for_user")
808802
def test_remove_workspace_user_if_no_groups(get_role_assignment_mock,
809-
remove_user_to_role_mock,remove_user_to_group_mock,
803+
remove_user_to_role_mock, remove_user_to_group_mock,
810804
workspace_without_groups, role_owner,
811805
user_with_role):
812806

@@ -819,12 +813,13 @@ def test_remove_workspace_user_if_no_groups(get_role_assignment_mock,
819813
assert remove_user_to_group_mock.call_count == 0
820814
assert remove_user_to_role_mock.call_count == 1
821815

816+
822817
@patch("services.aad_authentication.AzureADAuthorization._is_workspace_role_group_in_use", return_value=True)
823818
@patch("services.aad_authentication.AzureADAuthorization._remove_workspace_user_from_application_group")
824819
@patch("services.aad_authentication.AzureADAuthorization._remove_workspace_user_from_application")
825820
@patch("services.aad_authentication.AzureADAuthorization._get_role_assignment_for_user")
826821
def test_remove_workspace_user_if_groups(get_role_assignment_mock,
827-
remove_user_to_role_mock,remove_user_to_group_mock,
822+
remove_user_to_role_mock, remove_user_to_group_mock,
828823
workspace_without_groups, role_owner,
829824
user_with_role):
830825

@@ -865,6 +860,7 @@ def test_get_assignable_users_returns_users(_, request_get_mock, mock_headers):
865860
assert users[0].name == "User 1"
866861
assert users[0].email == "[email protected]"
867862

863+
868864
@patch("services.aad_authentication.AzureADAuthorization._get_msgraph_token", return_value="token")
869865
@patch("services.aad_authentication.AzureADAuthorization._ms_graph_query")
870866
@patch("services.aad_authentication.AzureADAuthorization._get_auth_header")
@@ -882,8 +878,8 @@ def test_get_workspace_roles_returns_roles(_, ms_graph_query_mock, mock_headers,
882878
Role(id=1, value="AirlockManager", isEnabled=True, description="", displayName="Airlock Manager", origin="", allowedMemberTypes=[]).dict(),
883879
Role(id=2, value="WorkspaceResearcher", isEnabled=True, description="", displayName="Workspace Researcher", origin="", allowedMemberTypes=[]).dict(),
884880
Role(id=3, value="WorkspaceOwner", isEnabled=True, description="", displayName="Workspace Owner", origin="", allowedMemberTypes=[]).dict(),
885-
]
886-
}
881+
]
882+
}
887883
ms_graph_query_mock.return_value = request_get_mock_response
888884
roles = access_service.get_workspace_roles(workspace_without_groups)
889885

0 commit comments

Comments
 (0)