| 1 |
# -------------------------------------------------------------------------------------------- |
| 2 |
# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 |
# -------------------------------------------------------------------------------------------- |
| 5 |
|
| 6 |
import logging |
| 7 |
import unittest |
| 8 |
from azure_functions_devops_build.organization.organization_manager import OrganizationManager |
| 9 |
from azure_functions_devops_build.project.project_manager import ProjectManager |
| 10 |
from msrest.exceptions import HttpOperationError |
| 11 |
from ._config import CREATE_DEVOPS_OBJECTS, ORGANIZATION_NAME, PROJECT_NAME |
| 12 |
from ._helpers import get_credentials |
| 13 |
|
| 14 |
class TestProjectManager(unittest.TestCase): |
| 15 |
@classmethod |
| 16 |
def setUpClass(cls): |
| 17 |
logging.disable(logging.CRITICAL) |
| 18 |
|
| 19 |
@classmethod |
| 20 |
def tearDownClass(cls): |
| 21 |
logging.disable(logging.NOTSET) |
| 22 |
|
| 23 |
def setUp(self): |
| 24 |
self.project_manager = ProjectManager(creds=get_credentials(), organization_name=ORGANIZATION_NAME) |
| 25 |
|
| 26 |
@unittest.skipIf( |
| 27 |
not CREATE_DEVOPS_OBJECTS, |
| 28 |
"Set CREATE_DEVOPS_OBJECTS to True if you want to create resources for unit testing" |
| 29 |
) |
| 30 |
def test_create_project(self): |
| 31 |
existing_project_names = [ |
| 32 |
proj.name for proj in self.project_manager.list_projects().value |
| 33 |
] |
| 34 |
|
| 35 |
# If the project exists, we will skip this test |
| 36 |
if PROJECT_NAME in existing_project_names: |
| 37 |
raise unittest.SkipTest("Project already exists. No need to create a new project.") |
| 38 |
|
| 39 |
result = self.project_manager.create_project(PROJECT_NAME) |
| 40 |
self.assertIsNotNone(result.id) |
| 41 |
self.assertEqual(result.name, PROJECT_NAME) |
| 42 |
|
| 43 |
def test_invalid_create_duplicated_project(self): |
| 44 |
existing_project_names = [ |
| 45 |
proj.name for proj in self.project_manager.list_projects().value |
| 46 |
] |
| 47 |
|
| 48 |
# If no project exists, we will skip this test |
| 49 |
if len(existing_project_names) == 0: |
| 50 |
raise unittest.SkipTest("There is no existing project. Cannot create a duplicate.") |
| 51 |
|
| 52 |
result = self.project_manager.create_project(existing_project_names[0]) |
| 53 |
self.assertFalse(result.valid) |
| 54 |
|
| 55 |
def test_invalid_create_project_with_bad_name(self): |
| 56 |
bad_project_name = "invalid-project-name#" |
| 57 |
result = self.project_manager.create_project(bad_project_name) |
| 58 |
self.assertFalse(result.valid) |
| 59 |
|
| 60 |
def test_list_projects(self): |
| 61 |
projects = self.project_manager.list_projects() |
| 62 |
self.assertIsNotNone(projects) |
| 63 |
self.assertIsNotNone(projects.value) |
| 64 |
self.assertGreaterEqual(projects.count, 0) |
| 65 |
|
| 66 |
if __name__ == '__main__': |
| 67 |
unittest.main() |