diff --git a/backend/app/graphql/user_mutations.py b/backend/app/graphql/user_mutations.py index d623fc69..d683c3db 100644 --- a/backend/app/graphql/user_mutations.py +++ b/backend/app/graphql/user_mutations.py @@ -40,33 +40,32 @@ 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): @@ -74,16 +73,14 @@ def mutate(self, info, requestor_id, id): 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() diff --git a/backend/app/resources/validate_utils.py b/backend/app/resources/validate_utils.py index e3e71702..199795cd 100644 --- a/backend/app/resources/validate_utils.py +++ b/backend/app/resources/validate_utils.py @@ -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.") @@ -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 == "": diff --git a/backend/app/services/implementations/onboarding_request_service.py b/backend/app/services/implementations/onboarding_request_service.py index 88e99eda..26698d14 100644 --- a/backend/app/services/implementations/onboarding_request_service.py +++ b/backend/app/services/implementations/onboarding_request_service.py @@ -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( diff --git a/backend/app/services/implementations/user_service.py b/backend/app/services/implementations/user_service.py index 8bc556c3..fcf6c3de 100644 --- a/backend/app/services/implementations/user_service.py +++ b/backend/app/services/implementations/user_service.py @@ -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) diff --git a/backend/tests/graphql/mock_test_data.py b/backend/tests/graphql/mock_test_data.py index 3fb4f942..ad173813 100644 --- a/backend/tests/graphql/mock_test_data.py +++ b/backend/tests/graphql/mock_test_data.py @@ -19,7 +19,7 @@ {"name": "abc", "phone": "123-456-7890", "email": "abc@uwblueprint.org"}, {"name": "Jane Doe", "phone": "111-222-3333", "email": "example@domain.com"}, ], - "active": True + "active": True, } MOCK_INFO1_CAMEL = { @@ -43,7 +43,7 @@ {"name": "abc", "phone": "123-456-7890", "email": "abc@uwblueprint.org"}, {"name": "Jane Doe", "phone": "111-222-3333", "email": "example@domain.com"}, ], - "active": True + "active": True, } MOCK_INFO2_SNAKE = { @@ -68,7 +68,7 @@ {"name": "def", "phone": "098-765-4321", "email": "def@uwblueprint.org"}, {"name": "John Doe", "phone": "444-555-6666", "email": "elpmaxe@niamod.moc"}, ], - "active": True + "active": True, } MOCK_INFO2_CAMEL = { @@ -93,7 +93,7 @@ {"name": "def", "phone": "098-765-4321", "email": "def@uwblueprint.org"}, {"name": "John Doe", "phone": "444-555-6666", "email": "elpmaxe@niamod.moc"}, ], - "active": True + "active": True, } MOCK_INFO3_SNAKE = { @@ -112,7 +112,7 @@ {"name": "ghi", "phone": "135-792-4680", "email": "ghi@uwblueprint.org"}, {"name": "Jack Doe", "phone": "777-888-999", "email": "com@domain.email"}, ], - "active": False + "active": False, } MOCK_INFO3_CAMEL = { @@ -131,7 +131,7 @@ {"name": "ghi", "phone": "135-792-4680", "email": "ghi@uwblueprint.org"}, {"name": "Jack Doe", "phone": "777-888-999", "email": "com@domain.email"}, ], - "active": False + "active": False, } MOCK_USER1_SNAKE = { diff --git a/backend/tests/graphql/test_all_user_mutations.py b/backend/tests/graphql/test_all_user_mutations.py index 647efef4..3c5c8f14 100644 --- a/backend/tests/graphql/test_all_user_mutations.py +++ b/backend/tests/graphql/test_all_user_mutations.py @@ -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 @@ -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 @@ -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 {{ @@ -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)}", @@ -418,4 +420,4 @@ def test_deactivate_user_by_id(user_setup, mocker): }} }} }}""" - ) \ No newline at end of file + ) diff --git a/backend/tests/unit/test_models.py b/backend/tests/unit/test_models.py index 6ffe11ca..51fd9315 100644 --- a/backend/tests/unit/test_models.py +++ b/backend/tests/unit/test_models.py @@ -31,7 +31,7 @@ {"name": "Abu", "phone": "123-456-7890", "email": "abu@uwblueprint.org"}, {"name": "Jane Doe", "phone": "111-222-3333", "email": "example@domain.com"}, ], - "active": True + "active": True, } @@ -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"]