Skip to content

Commit

Permalink
Lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jarydo committed Oct 12, 2023
1 parent 69512e2 commit c4214fa
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 50 deletions.
23 changes: 10 additions & 13 deletions backend/app/graphql/user_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,50 +40,47 @@ def mutate(self, info, requestor_id, id, userInfo):
)
)


class ActivateUserByID(Mutation):
class Arguments:
requestor_id = graphene.String(required=True)
id = graphene.String(required=True)

user = graphene.Field(User)

def mutate(self, info, requestor_id, id):
def mutate(self, info, requestor_id, id):
user_service = services["user_service"]
requestor_auth_id = user_service.get_auth_id_by_user_id(requestor_id)
requestor_role = user_service.get_user_role_by_auth_id(requestor_auth_id)

if requestor_role == "Admin":
if requestor_role == "Admin":
activate_user_dto = user_service.activate_user_by_id(id)

return ActivateUserByID(
user=User(
id=activate_user_dto.id,
info=activate_user_dto.info
)
user=User(id=activate_user_dto.id, info=activate_user_dto.info)
)


class DeactivateUserByID(Mutation):
class Arguments:
requestor_id = graphene.String(required=True)
id = graphene.String(required=True)

user = graphene.Field(User)

def mutate(self, info, requestor_id, id):
user_service = services["user_service"]
requestor_auth_id = user_service.get_auth_id_by_user_id(requestor_id)
requestor_role = user_service.get_user_role_by_auth_id(requestor_auth_id)

if requestor_role == "Admin" or requestor_id == id:
if requestor_role == "Admin" or requestor_id == id:
deactivate_user_dto = user_service.deactivate_user_by_id(id)

return DeactivateUserByID(
user=User(
id=deactivate_user_dto.id,
info=deactivate_user_dto.info
)
user=User(id=deactivate_user_dto.id, info=deactivate_user_dto.info)
)


class UserMutations(MutationList):
updateUserByID = UpdateUserByID.Field()
activateUserByID = ActivateUserByID.Field()
Expand Down
4 changes: 2 additions & 2 deletions backend/app/resources/validate_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def validate_userinfo(userinfo, error_list):
"role_info",
"primary_contact",
"onsite_contacts",
"active"
"active",
]
if not isinstance(userinfo, dict):
error_list.append("The info supplied is not a dict.")
Expand All @@ -85,7 +85,7 @@ def validate_userinfo(userinfo, error_list):
userinfo["role"], val, "info.role_info", error_list
)
elif key == "active" and type(val) is not bool:
error_list.append(f"The field info.active supplied is not a boolean.")
error_list.append("The field info.active supplied is not a boolean.")
elif type(val) is not str and key != "active":
error_list.append(f"The field info.{key} supplied is not a string.")
elif val == "":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def create_onboarding_request(self, userInfo):
role_info=userInfo.role_info,
primary_contact=userInfo.primary_contact,
onsite_contacts=userInfo.onsite_contacts,
active=userInfo.active
active=userInfo.active,
)
# Create OnboardingRequest object
new_onboarding_request = OnboardingRequest(
Expand Down
34 changes: 16 additions & 18 deletions backend/app/services/implementations/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,44 +263,42 @@ def update_user_by_id(self, user_id, update_user_dto):
"info": updated_user_dict["info"],
}
return UserDTO(**kwargs)

def activate_user_by_id(self, user_id):
try:
user = User.objects(id=user_id).first()
if not user:
raise Exception(f"user_id {user_id} not found")

user.info.active = True # activate user

user.save() # save changes
user.info.active = True # activate user

updated_user_dict = UserService.__user_to_serializable_dict_and_remove_auth_id(user)
kwargs = {
"id": updated_user_dict["id"],
"info": updated_user_dict["info"]
}
user.save() # save changes

updated_user_dict = (
UserService.__user_to_serializable_dict_and_remove_auth_id(user)
)
kwargs = {"id": updated_user_dict["id"], "info": updated_user_dict["info"]}

return UserDTO(**kwargs)

except Exception as e:
self.logger.error(f"Failed to activate user. Reason = {e}")
raise e

def deactivate_user_by_id(self, user_id):
try:
user = User.objects(id=user_id).first()
if not user:
raise Exception(f"user_id {user_id} not found")

user.info.active = False # deactivate user

user.save() # save changes
user.info.active = False # deactivate user

updated_user_dict = UserService.__user_to_serializable_dict_and_remove_auth_id(user)
kwargs = {
"id": updated_user_dict["id"],
"info": updated_user_dict["info"]
}
user.save() # save changes

updated_user_dict = (
UserService.__user_to_serializable_dict_and_remove_auth_id(user)
)
kwargs = {"id": updated_user_dict["id"], "info": updated_user_dict["info"]}

return UserDTO(**kwargs)

Expand Down
12 changes: 6 additions & 6 deletions backend/tests/graphql/mock_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{"name": "abc", "phone": "123-456-7890", "email": "[email protected]"},
{"name": "Jane Doe", "phone": "111-222-3333", "email": "[email protected]"},
],
"active": True
"active": True,
}

MOCK_INFO1_CAMEL = {
Expand All @@ -43,7 +43,7 @@
{"name": "abc", "phone": "123-456-7890", "email": "[email protected]"},
{"name": "Jane Doe", "phone": "111-222-3333", "email": "[email protected]"},
],
"active": True
"active": True,
}

MOCK_INFO2_SNAKE = {
Expand All @@ -68,7 +68,7 @@
{"name": "def", "phone": "098-765-4321", "email": "[email protected]"},
{"name": "John Doe", "phone": "444-555-6666", "email": "[email protected]"},
],
"active": True
"active": True,
}

MOCK_INFO2_CAMEL = {
Expand All @@ -93,7 +93,7 @@
{"name": "def", "phone": "098-765-4321", "email": "[email protected]"},
{"name": "John Doe", "phone": "444-555-6666", "email": "[email protected]"},
],
"active": True
"active": True,
}

MOCK_INFO3_SNAKE = {
Expand All @@ -112,7 +112,7 @@
{"name": "ghi", "phone": "135-792-4680", "email": "[email protected]"},
{"name": "Jack Doe", "phone": "777-888-999", "email": "[email protected]"},
],
"active": False
"active": False,
}

MOCK_INFO3_CAMEL = {
Expand All @@ -131,7 +131,7 @@
{"name": "ghi", "phone": "135-792-4680", "email": "[email protected]"},
{"name": "Jack Doe", "phone": "777-888-999", "email": "[email protected]"},
],
"active": False
"active": False,
}

MOCK_USER1_SNAKE = {
Expand Down
12 changes: 7 additions & 5 deletions backend/tests/graphql/test_all_user_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def test_number_of_kids_cant_be_set_negative(user_setup, mocker):
# Check error message is appropriate
assert "num_kids must be greater than or equal to zero" in error


def test_activate_user_by_id(user_setup, mocker):
_, _, user_3 = user_setup

Expand Down Expand Up @@ -290,7 +291,8 @@ def test_activate_user_by_id(user_setup, mocker):
}}"""
)

assert activate_user.data["activateUserByID"]["user"]["info"]["active"] == True
assert activate_user.data["activateUserByID"]["user"]["info"]["active"] is True


def test_deactivate_user_by_id(user_setup, mocker):
user_1, user_2, user_3 = user_setup
Expand Down Expand Up @@ -335,7 +337,7 @@ def test_deactivate_user_by_id(user_setup, mocker):
}}"""
)

assert deactivate_user.data["deactivateUserByID"]["user"]["info"]["active"] == False
assert deactivate_user.data["deactivateUserByID"]["user"]["info"]["active"] is False

deactivate_user = graphql_schema.execute(
f"""mutation testDeactivateUserById {{
Expand Down Expand Up @@ -377,10 +379,10 @@ def test_deactivate_user_by_id(user_setup, mocker):
}}"""
)

assert deactivate_user.data["deactivateUserByID"]["user"]["info"]["active"] == False
assert deactivate_user.data["deactivateUserByID"]["user"]["info"]["active"] is False

# necessary to prevent further tests from failing
reactivate_user = graphql_schema.execute(
graphql_schema.execute(
f"""mutation testActivateUserById {{
activateUserByID (
requestorId: "{str(user_3.id)}",
Expand Down Expand Up @@ -418,4 +420,4 @@ def test_deactivate_user_by_id(user_setup, mocker):
}}
}}
}}"""
)
)
7 changes: 2 additions & 5 deletions backend/tests/unit/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
{"name": "Abu", "phone": "123-456-7890", "email": "[email protected]"},
{"name": "Jane Doe", "phone": "111-222-3333", "email": "[email protected]"},
],
"active": True
"active": True,
}


Expand Down Expand Up @@ -97,7 +97,4 @@ def test_create_onboarding_request():
onboarding_request.info.onsite_contacts[i].email
== test_user_info["onsite_contacts"][i]["email"]
)
assert (
onboarding_request.info.active
== test_user_info["active"]
)
assert onboarding_request.info.active == test_user_info["active"]

0 comments on commit c4214fa

Please sign in to comment.