| 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.user.user_manager import UserManager |
| 10 |
from msrest.exceptions import HttpOperationError |
| 11 |
from ._config import CREATE_DEVOPS_OBJECTS, ORGANIZATION_NAME |
| 12 |
from ._helpers import get_credentials, id_generator |
| 13 |
|
| 14 |
class TestOrganizationManager(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.organization_manager = OrganizationManager(creds=get_credentials()) |
| 25 |
|
| 26 |
def tearDown(self): |
| 27 |
self.organization_manager.close_connection() |
| 28 |
|
| 29 |
def test_valid_organization_name(self): |
| 30 |
valid_name = "organization-name-" + id_generator(size=3).lower() |
| 31 |
validation = self.organization_manager.validate_organization_name(valid_name) |
| 32 |
self.assertTrue(validation.valid) |
| 33 |
|
| 34 |
def test_invalid_no_organization_name(self): |
| 35 |
invalid_name = None |
| 36 |
validation = self.organization_manager.validate_organization_name(invalid_name) |
| 37 |
self.assertFalse(validation.valid) |
| 38 |
|
| 39 |
def test_invalid_empty_organization_name(self): |
| 40 |
invalid_name = '' |
| 41 |
validation = self.organization_manager.validate_organization_name(invalid_name) |
| 42 |
self.assertFalse(validation.valid) |
| 43 |
|
| 44 |
def test_invalid_organization_name_characters(self): |
| 45 |
invalid_name = 'invalid-organization-name#' |
| 46 |
validation = self.organization_manager.validate_organization_name(invalid_name) |
| 47 |
self.assertFalse(validation.valid) |
| 48 |
|
| 49 |
def test_invalid_collided_organization_name(self): |
| 50 |
organizations = self.organization_manager.list_organizations() |
| 51 |
if organizations.count > 0: |
| 52 |
existing_name = organizations.value[0].accountName |
| 53 |
validation = self.organization_manager.validate_organization_name(existing_name) |
| 54 |
self.assertFalse(validation.valid) |
| 55 |
|
| 56 |
def test_list_organizations(self): |
| 57 |
organizations = self.organization_manager.list_organizations() |
| 58 |
self.assertIsNotNone(organizations) |
| 59 |
self.assertIsNotNone(organizations.value) |
| 60 |
self.assertGreaterEqual(organizations.count, 0) |
| 61 |
|
| 62 |
def test_invalid_organization_without_credential(self): |
| 63 |
no_cred_organization_manager = OrganizationManager(creds=None) |
| 64 |
with self.assertRaises(HttpOperationError): |
| 65 |
no_cred_organization_manager.list_organizations() |
| 66 |
|
| 67 |
@unittest.skipIf( |
| 68 |
not CREATE_DEVOPS_OBJECTS, |
| 69 |
"Set CREATE_DEVOPS_OBJECTS to True if you want to create resources for unit testing" |
| 70 |
) |
| 71 |
def test_create_organization(self): |
| 72 |
existing_organization_names = [ |
| 73 |
org.accountName for org in self.organization_manager.list_organizations().value |
| 74 |
] |
| 75 |
|
| 76 |
# If the organization exists, we will skip this test |
| 77 |
if ORGANIZATION_NAME in existing_organization_names: |
| 78 |
raise unittest.SkipTest("Organization already exists. No need to create a new organization.") |
| 79 |
|
| 80 |
result = self.organization_manager.create_organization('CUS', ORGANIZATION_NAME) |
| 81 |
self.assertIsNotNone(result.id) |
| 82 |
self.assertEqual(result.name, ORGANIZATION_NAME) |
| 83 |
|
| 84 |
def test_invalid_create_duplicated_organization(self): |
| 85 |
existing_organization_names = [ |
| 86 |
org.accountName for org in self.organization_manager.list_organizations().value |
| 87 |
] |
| 88 |
|
| 89 |
# If there is no existing organization, we will skip this test |
| 90 |
if existing_organization_names.count == 0: |
| 91 |
raise unittest.SkipTest("There is no existing organization. Cannot create a duplicate.") |
| 92 |
|
| 93 |
organization_name = existing_organization_names[0] |
| 94 |
with self.assertRaises(HttpOperationError): |
| 95 |
self.organization_manager.create_organization('CUS', organization_name) |
| 96 |
|
| 97 |
def test_list_regions(self): |
| 98 |
regions = self.organization_manager.list_regions() |
| 99 |
self.assertIsNotNone(regions) |
| 100 |
self.assertIsNotNone(regions.value) |
| 101 |
self.assertGreaterEqual(regions.count, 0) |
| 102 |
|
| 103 |
if __name__ == '__main__': |
| 104 |
unittest.main() |