def test_update( input_data, expected_command, expected_message, non_expected_message, client ): Command.create(**default_expected_command) response = update_command_processor( user_id=user_id, team_id=team_id, channel_id=channel_id, command_to_update=command_name, **input_data, ) message = response.get("message") if expected_message: assert expected_message in message.content if non_expected_message: assert non_expected_message not in message.content assert message.status == MessageStatus.INFO assert message.visibility == MessageVisibility.NORMAL updated_command = ( Command.find_one_by_name_and_chanel(name=command_name, channel_id=channel_id) .to_son() .to_dict() ) for key in expected_command: assert updated_command[key] == expected_command[key]
def test_slash_command_update(text, expected, client): Command.create( name="test_update", channel_id="1234", label="label", description="description", pick_list=["1", "2"], weight_list=[1 / 2, 1 / 2], strategy=Strategy.uniform.name, self_exclude=True, only_active_users=False, created_by_user_id="4321", ) response, slack_message = call_webhook(client, text) assert response.status_code == 200 assert "4321 updated *test_update*." in slack_message updated_command = (Command.find_one_by_name_and_chanel( "test_update", "1234").to_son().to_dict()) for key in expected: func_to_apply = lambda x: x # noqa: E731 if type(expected[key]) == list: func_to_apply = sorted assert func_to_apply(updated_command[key]) == func_to_apply( expected[key])
def create_command_processor( *, user_id: str, team_id: str, channel_id: str, new_command_name: str, label: str = "", description: str = "", pick_list: list[str], strategy: str = Strategy.uniform.name, self_exclude: bool = False, only_active_users: bool = False, ) -> dict[str, any]: pick_list = format_pick_list(pick_list, team_id, channel_id) strategy_enum = Strategy[strategy] weight_list = strategy_enum.value.create_weight_list(len(pick_list)) Command.create( name=new_command_name, channel_id=channel_id, label=label, description=description, pick_list=pick_list, self_exclude=self_exclude, only_active_users=only_active_users, weight_list=weight_list, strategy=strategy_enum.name, created_by_user_id=user_id, ) created_command = Command.find_one_by_name_and_chanel( new_command_name, channel_id) message_content = format_new_command_message( command_name=created_command.name, team_id=team_id, pick_list=created_command.pick_list, command_description=created_command.description, current_user_id=user_id, ) return { "message": Message( content=message_content, status=MessageStatus.SUCCESS, visibility=MessageVisibility.NORMAL, ) }
def test_slash_command_custom(client): Command.create( name="test_custom", channel_id="1234", label="label", description="description", pick_list=["pick_1", "pick_2"], weight_list=[1 / 2, 1 / 2], strategy=Strategy.uniform.name, self_exclude=False, only_active_users=False, created_by_user_id="4321", ) text = "test_custom" response, slack_message = call_webhook(client, text) assert response.status_code == 200 assert "Hey !" in slack_message
def test_delete(client): Command.create( name=command_name, channel_id=channel_id, label="label", description="description", pick_list=["1", "2"], weight_list=[1 / 2, 1 / 2], strategy=Strategy.uniform.name, self_exclude=True, only_active_users=False, created_by_user_id="4321", ) response = delete_command_processor(channel_id=channel_id, command_to_delete=command_name) message = response.get("message") assert "Command test_delete successfully deleted." == message.content assert message.status == MessageStatus.INFO assert message.visibility == MessageVisibility.NORMAL
def test_slash_command_delete_fail(client): Command.create( name="test_delete", channel_id="1234", label="label", description="description", pick_list=["1", "2"], weight_list=[1 / 2, 1 / 2], strategy=Strategy.uniform.name, self_exclude=True, only_active_users=False, created_by_user_id="4321", ) text = "delete test_delete_unknown_command" response, slack_message = call_webhook(client, text) assert response.status_code == 200 assert "Command test_delete_unknown_command does not exist" in slack_message command = Command.find_one_by_name_and_chanel("test_delete", "1234") assert command is not None
def test_slash_command_delete(client): Command.create( name="test_delete", channel_id="1234", label="label", description="description", pick_list=["1", "2"], weight_list=[1 / 2, 1 / 2], strategy=Strategy.uniform.name, self_exclude=True, only_active_users=False, created_by_user_id="4321", ) text = "delete test_delete" response, slack_message = call_webhook(client, text) assert response.status_code == 200 assert "Command test_delete successfully deleted." in slack_message with pytest.raises(Command.DoesNotExist): Command.find_one_by_name_and_chanel("test_delete", "1234", catch=False)
def test_command() -> Command: return Command.create( name=TEST_COMMAND_NAME, channel_id=TEST_COMMAND_CHANNEL_ID, label=TEST_COMMAND_LABEL, description=TEST_COMMAND_DESCRIPTION, pick_list=TEST_COMMAND_PICK_LIST, weight_list=TEST_WEIGHT_LIST, strategy=TEST_STRATEGY, self_exclude=False, only_active_users=False, created_by_user_id=TEST_COMMAND_CREATED_BY, )
def test_slash_command_custom_update_weight_list(client): name = "test_custom" channel_id = "1234" Command.create( name="test_custom", channel_id="1234", label="label", description="description", pick_list=["pick_1", "pick_2"], weight_list=[1, 0], strategy=Strategy.round_robin.name, self_exclude=False, only_active_users=False, created_by_user_id="4321", ) response, slack_message = call_webhook(client, name) assert response.status_code == 200 assert "Hey !" in slack_message command = Command.find_one_by_name_and_chanel(name=name, channel_id=channel_id) assert command.weight_list == [0, 1]
def create_and_return_command(**kwargs): Command.create(**kwargs) return Command.find_one_by_name_and_chanel(kwargs["name"], kwargs["channel_id"])