def test_dcos_add_user(dcos_api_session):
    """
    dcos_add_user.py script adds a user to IAM using the
    script dcos_add_user.py.
    """

    email_address = uuid.uuid4().hex + '@example.com'
    cli = dcos_cli.DcosCli('', '', '')
    command = ['python', '/opt/mesosphere/bin/dcos_add_user.py', email_address]
    cli.exec_command(command)

    try:
        r = dcos_api_session.get('/acs/api/v1/users')
        r.raise_for_status()
        expected_user_data = {
            "uid": email_address,
            "description": "",
            "url": "/acs/api/v1/users/" + email_address,
            "is_remote": True,
            "is_service": False,
            "provider_type": "oidc",
            "provider_id": "https://dcos.auth0.com/"
        }
        assert expected_user_data in r.json()['array']
    finally:
        delete_user(dcos_api_session, email_address)
def test_exec_command(caplog):
    cli = dcos_cli.DcosCli('')
    stdout, stderr = cli.exec_command(
        ['/bin/sh', '-c', 'echo "hello, world!"'])
    assert stdout == 'hello, world!\n'
    assert stderr == ''
    assert any(rec.message.startswith('CMD:') for rec in caplog.records)
    assert any(rec.message.startswith('STDOUT:') for rec in caplog.records)
    assert any(rec.message.startswith('STDERR:') for rec in caplog.records)
def test_check_message_on_adding_user_twice(dcos_api_session):
    """
    Check that the correct message is emitted on adding the
    same user for the second time.
    """

    email_address = uuid.uuid4().hex + '@example.com'
    cli = dcos_cli.DcosCli('', '', '')
    command = ['python', '/opt/mesosphere/bin/dcos_add_user.py', email_address]
    stdout, stderr = cli.exec_command(command)

    try:
        expected_output = '[INFO] Created IAM user `' + email_address + '`\n'
        assert '' == stdout
        assert expected_output == stderr

        stdout, stderr = cli.exec_command(command)
        expected_error = '[INFO] User `' + email_address + '` already exists\n'
        assert expected_error == stderr
        assert '' == stdout
    finally:
        delete_user(dcos_api_session, email_address)
def test_exec_command_fail(caplog):
    cli = dcos_cli.DcosCli('')
    with pytest.raises(subprocess.CalledProcessError):
        cli.exec_command(['/bin/sh', '-c', 'does-not-exist'])
    assert any(rec.message.startswith('CMD:') for rec in caplog.records)
    assert any(rec.message.startswith('STDERR:') for rec in caplog.records)