def run(dry_run):
    slack = init_slack_workspace(QONTRACT_INTEGRATION)
    desired_state = get_desired_state(slack)
    usergroups = [d['usergroup'] for d in desired_state]
    current_state = get_current_state(slack, usergroups)
    slack_usergroups.print_diff(current_state, desired_state)

    if not dry_run:
        # just so we can re-use the logic from slack_usergroups
        slack_map = {slack.workspace_name: {'slack': slack}}
        slack_usergroups.act(desired_state, slack_map)
def run(dry_run):
    slack = slackapi_from_queries(QONTRACT_INTEGRATION)
    desired_state = get_desired_state(slack)
    usergroups = []
    for _, workspace_state in desired_state.items():
        for usergroup, _ in workspace_state.items():
            usergroups.append(usergroup)
    current_state = get_current_state(slack, usergroups)

    # just so we can re-use the logic from slack_usergroups
    slack_map = {slack.workspace_name: {"slack": slack}}
    slack_usergroups.act(current_state, desired_state, slack_map, dry_run)
Esempio n. 3
0
def test_act_add_new_usergroups(base_state):

    current_state = base_state
    desired_state = copy.deepcopy(base_state)

    desired_state["slack-workspace"].update({
        "usergroup-2": {
            "workspace": "slack-workspace",
            "usergroup": "usergroup-2",
            "usergroup_id": "USERGB",
            "users": {
                "USERB": "userb",
                "USERC": "userc"
            },
            "channels": {
                "CHANB": "channelb",
                "CHANC": "channelc"
            },
            "description": "A new usergroup",
        }
    })

    desired_state["slack-workspace"].update({
        "usergroup-3": {
            "workspace": "slack-workspace",
            "usergroup": "usergroup-3",
            "usergroup_id": "USERGC",
            "users": {
                "USERF": "userf",
                "USERG": "userg"
            },
            "channels": {
                "CHANF": "channelf",
                "CHANG": "channelg"
            },
            "description": "Another new usergroup",
        }
    })

    slack_client_mock = create_autospec(SlackApi)
    slack_map = {"slack-workspace": {"slack": slack_client_mock}}

    act(current_state, desired_state, slack_map, dry_run=False)

    assert slack_client_mock.update_usergroup.call_args_list == [
        call("USERGB", ["CHANB", "CHANC"], "A new usergroup"),
        call("USERGC", ["CHANF", "CHANG"], "Another new usergroup"),
    ]
    assert slack_client_mock.update_usergroup_users.call_args_list == [
        call("USERGB", ["USERB", "USERC"]),
        call("USERGC", ["USERF", "USERG"]),
    ]
def test_act_add_new_usergroups(base_state):

    current_state = base_state
    desired_state = copy.deepcopy(base_state)

    desired_state['slack-workspace'].update({
        'usergroup-2': {
            'workspace': 'slack-workspace',
            'usergroup': 'usergroup-2',
            'usergroup_id': 'USERGB',
            'users': {
                'USERB': 'userb',
                'USERC': 'userc'
            },
            'channels': {
                'CHANB': 'channelb',
                'CHANC': 'channelc'
            },
            'description': 'A new usergroup'
        }
    })

    desired_state['slack-workspace'].update({
        'usergroup-3': {
            'workspace': 'slack-workspace',
            'usergroup': 'usergroup-3',
            'usergroup_id': 'USERGC',
            'users': {
                'USERF': 'userf',
                'USERG': 'userg'
            },
            'channels': {
                'CHANF': 'channelf',
                'CHANG': 'channelg'
            },
            'description': 'Another new usergroup'
        }
    })

    slack_client_mock = create_autospec(SlackApi)
    slack_map = {'slack-workspace': {'slack': slack_client_mock}}

    act(current_state, desired_state, slack_map, dry_run=False)

    assert slack_client_mock.update_usergroup.call_args_list == [
        call('USERGB', ['CHANB', 'CHANC'], 'A new usergroup'),
        call('USERGC', ['CHANF', 'CHANG'], 'Another new usergroup')
    ]
    assert slack_client_mock.update_usergroup_users.call_args_list == [
        call('USERGB', ['USERB', 'USERC']),
        call('USERGC', ['USERF', 'USERG'])
    ]
Esempio n. 5
0
def test_act_no_changes_detected(base_state):
    """No changes should be made when the states are identical."""

    current_state = base_state
    desired_state = base_state

    slack_client_mock = create_autospec(SlackApi)
    slack_map = {"slack-workspace": {"slack": slack_client_mock}}

    act(current_state, desired_state, slack_map, dry_run=False)

    slack_client_mock.update_usergroup.assert_not_called()
    slack_client_mock.update_usergroup_users.assert_not_called()
Esempio n. 6
0
def test_act_update_usergroup_description(base_state):

    current_state = base_state
    desired_state = copy.deepcopy(base_state)

    desired_state["slack-workspace"]["usergroup-1"][
        "description"] = "A different description"

    slack_client_mock = create_autospec(SlackApi)
    slack_map = {"slack-workspace": {"slack": slack_client_mock}}

    act(current_state, desired_state, slack_map, dry_run=False)

    assert slack_client_mock.update_usergroup.call_args_list == [
        call("USERGA", ["CHANA"], "A different description")
    ]
    slack_client_mock.update_usergroup_users.assert_not_called()
Esempio n. 7
0
def test_act_dryrun_no_changes_made(base_state):
    """No changes should be made when dryrun mode is enabled."""

    current_state = base_state
    desired_state = copy.deepcopy(base_state)

    desired_state["slack-workspace"]["usergroup-1"]["users"] = {
        "USERB": "someotherusername"
    }

    slack_client_mock = create_autospec(SlackApi)
    slack_map = {"slack-workspace": {"slack": slack_client_mock}}

    act(current_state, desired_state, slack_map, dry_run=True)

    slack_client_mock.update_usergroup.assert_not_called()
    slack_client_mock.update_usergroup_users.assert_not_called()
def test_act_update_usergroup_description(base_state):

    current_state = base_state
    desired_state = copy.deepcopy(base_state)

    desired_state['slack-workspace']['usergroup-1']['description'] = \
        'A different description'

    slack_client_mock = create_autospec(SlackApi)
    slack_map = {'slack-workspace': {'slack': slack_client_mock}}

    act(current_state, desired_state, slack_map, dry_run=False)

    assert slack_client_mock.update_usergroup.call_args_list == [
        call('USERGA', ['CHANA'], 'A different description')
    ]
    slack_client_mock.update_usergroup_users.assert_not_called()
def test_act_update_usergroup_channels(base_state):

    current_state = base_state
    desired_state = copy.deepcopy(base_state)

    desired_state['slack-workspace']['usergroup-1']['channels'] = {
        'CHANB': 'someotherchannel'
    }

    slack_client_mock = create_autospec(SlackApi)
    slack_map = {'slack-workspace': {'slack': slack_client_mock}}

    act(current_state, desired_state, slack_map, dry_run=False)

    assert slack_client_mock.update_usergroup.call_args_list == [
        call('USERGA', ['CHANB'], 'Some description')
    ]
    slack_client_mock.update_usergroup_users.assert_not_called()
Esempio n. 10
0
def test_act_update_usergroup_users(base_state):

    current_state = base_state
    desired_state = copy.deepcopy(base_state)

    desired_state["slack-workspace"]["usergroup-1"]["users"] = {
        "USERB": "someotherusername",
        "USERC": "anotheruser",
    }

    slack_client_mock = create_autospec(SlackApi)
    slack_map = {"slack-workspace": {"slack": slack_client_mock}}

    act(current_state, desired_state, slack_map, dry_run=False)

    slack_client_mock.update_usergroup.assert_not_called()
    assert slack_client_mock.update_usergroup_users.call_args_list == [
        call("USERGA", ["USERB", "USERC"])
    ]
def test_act_update_usergroup_users(base_state):

    current_state = base_state
    desired_state = copy.deepcopy(base_state)

    desired_state['slack-workspace']['usergroup-1']['users'] = {
        'USERB': 'someotherusername',
        'USERC': 'anotheruser'
    }

    slack_client_mock = create_autospec(SlackApi)
    slack_map = {'slack-workspace': {'slack': slack_client_mock}}

    act(current_state, desired_state, slack_map, dry_run=False)

    slack_client_mock.update_usergroup.assert_not_called()
    assert slack_client_mock.update_usergroup_users.call_args_list == [
        call('USERGA', ['USERB', 'USERC'])
    ]
Esempio n. 12
0
def test_act_empty_current_state(base_state):
    """
    An empty current state should be able to be handled properly (watching for
    TypeErrors, etc).
    """

    current_state = {}
    desired_state = base_state

    slack_client_mock = create_autospec(SlackApi)
    slack_map = {"slack-workspace": {"slack": slack_client_mock}}

    act(current_state, desired_state, slack_map, dry_run=False)

    assert slack_client_mock.update_usergroup.call_args_list == [
        call("USERGA", ["CHANA"], "Some description")
    ]
    assert slack_client_mock.update_usergroup_users.call_args_list == [
        call("USERGA", ["USERA"])
    ]