def test_get_team():
    az = AzDevOps(org='foo', token='bar')
    project = SimpleNamespace(id=1)

    result = az._get_team(project, 'foo')

    assert result.id == 1
    assert az._get_team(project, 'something') is None
def test_get_project(patched_sleep):
    az = AzDevOps(org='foo', token='bar')

    result = az._get_project('foo')
    assert result.id == 1

    az.clients.reset_mock()
    assert az._get_project('something') is None
    assert az.clients.get_core_client.return_value.get_projects.call_count == 20
def test_auth(monkeypatch):
    monkeypatch.undo()
    with pytest.raises(ValueError) as exc:
        az = AzDevOps(org='foo')  # NOQA
    assert "incorrect parameters were passed" in str(exc.value)

    with pytest.raises(ValueError) as exc:
        az = AzDevOps(token='bar')  # NOQA
    assert "incorrect parameters were passed" in str(exc.value)
def test_initialize_repo(fs):
    az = AzDevOps(org='foo', token='bar')
    project = SimpleNamespace(id=1)

    path = './workitems/correct'

    MockedFiles._mock_correct_file_system(fs)
    attachments = MockedFiles._mock_file_list()

    az._initialize_repo('foo', project, path, attachments)
    assert az.clients.get_git_client.return_value.create_push.called
    assert az.connection._client.send.called
def test_deploy(fs):
    MockedFiles._mockCorrectFileSystem(fs)

    def mock_return_None(*args, **kwargs):
        return None

    az = AzDevOps(org='foo', token='bar')
    az._create_project = MagicMock(return_value=mock_return_None)
    az._get_project = MagicMock(return_value=SimpleNamespace(id=1))
    az._enable_epics = MagicMock(return_value=mock_return_None)
    az._create_work_item = MagicMock(return_value=mock_return_None)

    backlog = Backlog()
    config = backlog._getConfig('workitems/correct')
    work_items = backlog._buildWorkItems(MockedFiles._mockParsedFileList(),
                                         config)

    args = argparse.Namespace()
    args.org = 'foo'
    args.repo = 'testUser'
    args.project = 'testProject'
    args.backlog = 'correct'

    az.deploy(args, work_items)

    az._create_project.assert_called_with('testProject')
    az._enable_epics.assert_called()

    assert az._create_work_item.call_count == 20
def test_deploy(fs):
    MockedFiles._mock_correct_file_system(fs)

    def mock_return_None(*args, **kwargs):
        return None

    az = AzDevOps(org='foo', token='bar')
    az._create_project = MagicMock(return_value=mock_return_None)
    az._get_project = MagicMock(return_value=SimpleNamespace(id=1))
    az._enable_epics = MagicMock(return_value=mock_return_None)
    az._create_work_item = MagicMock(return_value=mock_return_None)

    backlog = Backlog()
    config = backlog._get_config('workitems/correct', 'azure')
    config["_repository_path"] = './workitems/correct'
    work_items = backlog._build_work_items(
        MockedFiles._mock_parsed_file_list(), config)

    args = argparse.Namespace()
    args.org = 'foo'
    args.repo = 'testUser'
    args.project = 'testProject'
    args.backlog = 'correct'

    az.deploy(args, work_items, config, [])

    az._create_project.assert_called_with('testProject', 'Sample description')
    az._enable_epics.assert_called()

    assert az._create_work_item.call_count == 20
def test_enable_epics():
    az = AzDevOps(org='foo', token='bar')
    az._get_team = MagicMock(
        return_value=SimpleNamespace(id=1, project_id=1, name='foo'))
    project = SimpleNamespace(id=1)

    az._enable_epics(project, 'foo')

    assert az.clients.get_work_client.return_value.update_team_settings.called_with(
        {
            "backlogVisibilities": {
                "Microsoft.EpicCategory": 'true',
                "Microsoft.FeatureCategory": 'true',
                "Microsoft.RequirementCategory": 'true'
            }
        }, SimpleNamespace(team_id=1, project_id=1))
def test_create_work_item():
    az = AzDevOps(org='foo', token='bar')
    project = SimpleNamespace(id=1)

    patch = [{
        "op": "add",
        "path": "/fields/System.Title",
        "value": 'foo'
    }, {
        "op": "add",
        "path": "/fields/System.Description",
        "value": 'bar'
    }]
    az._create_work_item(project, 'test', 'foo', 'bar')
    assert az.clients.get_work_item_tracking_client.return_value.create_work_item.called_with(
        patch, project, 'test')

    tags_patch = [{
        "op": "add",
        "path": "/fields/System.Title",
        "value": 'foo'
    }, {
        "op": "add",
        "path": "/fields/System.Description",
        "value": 'bar'
    }, {
        "op": "add",
        "path": "/fields/System.Tags",
        "value": 'foo; bar; baz'
    }]
    az._create_work_item(project, 'test', 'foo', 'bar', tags='foo; bar; baz')
    assert az.clients.get_work_item_tracking_client.return_value.create_work_item.called_with(
        tags_patch, project, 'test')

    parent_patch = [{
        "op": "add",
        "path": "/fields/System.Title",
        "value": 'foo'
    }, {
        "op": "add",
        "path": "/fields/System.Description",
        "value": 'bar'
    }, {
        "op": "add",
        "path": "/relations/-",
        "value": {
            "rel": "System.LinkTypes.Hierarchy-Reverse",
            "url": 'http://foobar.com'
        }
    }]
    az._create_work_item(project,
                         'test',
                         'foo',
                         'bar',
                         parent=SimpleNamespace(url="http://foobar.com"))
    assert az.clients.get_work_item_tracking_client.return_value.create_work_item.called_with(
        parent_patch, project, 'test')
def test_check_status():
    az = AzDevOps(org='foo', token='bar')

    az.clients.get_operations_client.return_value.get_operation.return_value = SimpleNamespace(
        status='succeeded')
    result = az._check_status(0)
    assert result is True

    az.clients.get_operations_client.return_value.get_operation.return_value = SimpleNamespace(
        status='cancelled')
    result = az._check_status(0)
    assert result is False

    az.clients.get_operations_client.return_value.get_operation.return_value = SimpleNamespace(
        status='failed')
    result = az._check_status(0)
    assert result is False
def test_create_tags():
    az = AzDevOps(org='foo', token='bar')

    tags = []
    assert az._create_tags(tags) is None

    tag = Tag()
    tag.title = 'tag1'
    tags.append(tag)
    tag = Tag()
    tag.title = 'tag2'
    tags.append(tag)
    tag = Tag()
    tag.title = 'tag3'
    tags.append(tag)

    result = az._create_tags(tags)

    assert result == 'tag1; tag2; tag3'
def test_create_project(monkeypatch):
    az = AzDevOps(org='foo', token='bar')

    az._check_status = MagicMock(return_value=True)
    assert az._create_project('foo') is True

    az._check_status = MagicMock(return_value=False)
    with pytest.raises(RuntimeError) as exc:
        az._create_project('bar')
    assert "failed creating project" in str(exc.value)