def delete(self, request_id): try: self._validate_request_id(request_id) friend_request = FriendRequestModel.get_friend_request(request_id) self._validate_request_participant(friend_request) friend_request = FriendRequestModel.remove_friend_request( request_id) return self._get_friends_requests_response(friend_request) except NoFriendRequestFoundException as e: return ErrorHandler.create_error_response(str(e), 404) except UserMismatchException as e: return ErrorHandler.create_error_response(str(e), 409) except DBConnectionError as e: return ErrorHandler.create_error_response(str(e), 500)
def test_delete_request_success(self, auth_mock, auth_user_mock, model_mock): auth_mock.return_value = True auth_user_mock.return_value = True request_id = '6ae66a31d4ef925dac59a95b' model_mock.get_friend_request.return_value = FriendRequestModel.get_friend_request(request_id) model_mock.remove_friend_request.return_value = "deleted" response = self.__make_get_request(request_id) self.assertEqual(200, response.status_code) model_mock.get_friend_request.assert_called_with(request_id)
def test_create_friend_request_successful(self, db_controller_name_mock): db_controller_mock = db_controller_name_mock.return_value side_effects = [mock.Mock(), MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD), MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD), MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD), MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD), MongoController.get_mongodb_instance(MONGODB_USER, MONGODB_PASSWD)] db_controller_mock.get_mongodb_instance = mock.Mock(side_effect=side_effects) FriendRequestModel.friend_request_exists = mock.MagicMock(return_value= False) request = FriendRequestModel.create_friend_request(1, 5, 'hello', 'picId') self.assertEqual(request["user_id_sender"], friends_requests_creation_successful_mock["user_id_sender"]) self.assertEqual(request["user_id_rcv"], friends_requests_creation_successful_mock["user_id_rcv"]) self.assertEqual(request["message"], friends_requests_creation_successful_mock["message"]) self.assertEqual(request["picture"], friends_requests_creation_successful_mock["picture"])
def _validate_receiver(self, request_id): friend_request = FriendRequestModel.get_friend_request(request_id) receiver_id = friend_request.get('user_id_rcv') validate_sender(receiver_id)
def _validate_request_id(self, requestId): return FriendRequestModel.exists_request(requestId)
def _accept_friend_request(self, request_id): friend_request = FriendRequestModel.remove_friend_request(request_id) friend_request.pop('_id') friend_res = FriendController() friend = friend_res.create_friendship(friend_request) return friend
def _create_be_friend_request(self, user_sender_id, user_rcv_id, msg, picture): friend_request = FriendRequestModel.create_friend_request(user_sender_id, user_rcv_id, msg, picture) return friend_request