Merges unit tests into the controllers directory

This commit is contained in:
Carlos Nihelton 2023-02-24 08:32:59 -03:00
parent 3a9fb2b132
commit a5478feb8c
No known key found for this signature in database
GPG Key ID: 6FE346D245197E9A
2 changed files with 60 additions and 70 deletions

View File

@ -13,13 +13,23 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from unittest import TestCase from unittest import (
TestCase,
IsolatedAsyncioTestCase,
)
from subiquity.common.types import ( from subiquity.common.types import (
AdAdminNameValidation, AdAdminNameValidation,
ADConnectionInfo,
AdDomainNameValidation, AdDomainNameValidation,
AdJoinResult,
AdPasswordValidation, AdPasswordValidation,
) )
from subiquity.server.controllers.ad import AdValidators from subiquity.server.controllers.ad import (
ADController,
AdValidators,
)
from subiquity.models.ad import ADModel
from subiquitycore.tests.mocks import make_app
class TestADValidation(TestCase): class TestADValidation(TestCase):
@ -119,3 +129,51 @@ class TestADValidation(TestCase):
admin = r'$Ubuntu{}' admin = r'$Ubuntu{}'
result = AdValidators.admin_user_name(admin) result = AdValidators.admin_user_name(admin)
self.assertEqual(AdAdminNameValidation.OK, result) self.assertEqual(AdAdminNameValidation.OK, result)
class TestAdJoin(IsolatedAsyncioTestCase):
def setUp(self):
self.app = make_app()
self.controller = ADController(self.app)
self.controller.model = ADModel()
async def test_never_join(self):
# Calling join_result_GET has no effect if the model is not set.
result = await self.controller.join_result_GET(wait=True)
self.assertEqual(result, AdJoinResult.UNKNOWN)
async def test_join_Unknown(self):
# Result remains UNKNOWN while ADController.join_domain is not called.
self.controller.model.set(ADConnectionInfo(domain_name='ubuntu.com',
admin_name='Helper',
password='1234'))
result = await self.controller.join_result_GET(wait=False)
self.assertEqual(result, AdJoinResult.UNKNOWN)
async def test_join_OK(self):
# The equivalent of a successful POST
self.controller.model.set(ADConnectionInfo(domain_name='ubuntu.com',
admin_name='Helper',
password='1234'))
# Mimics a client requesting the join result. Blocking by default.
result = self.controller.join_result_GET()
# Mimics a calling from the install controller.
await self.controller.join_domain('this', 'AD Join')
self.assertEqual(await result, AdJoinResult.OK)
async def test_join_Join_Error(self):
self.controller.model.set(ADConnectionInfo(domain_name='jubuntu.com',
admin_name='Helper',
password='1234'))
await self.controller.join_domain('this', 'AD Join')
result = await self.controller.join_result_GET(wait=True)
self.assertEqual(result, AdJoinResult.JOIN_ERROR)
async def test_join_Pam_Error(self):
self.controller.model.set(ADConnectionInfo(domain_name='pubuntu.com',
admin_name='Helper',
password='1234'))
await self.controller.join_domain('this', 'AD Join')
result = await self.controller.join_result_GET(wait=True)
self.assertEqual(result, AdJoinResult.PAM_ERROR)

View File

@ -1,68 +0,0 @@
# Copyright 2023 Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import unittest
from subiquity.common.types import ADConnectionInfo, AdJoinResult
from subiquity.server.controllers.ad import ADController
from subiquity.models.ad import ADModel
from subiquitycore.tests.mocks import make_app
class TestAdJoin(unittest.IsolatedAsyncioTestCase):
def setUp(self):
self.app = make_app()
self.controller = ADController(self.app)
self.controller.model = ADModel()
async def test_never_join(self):
# Calling join_result_GET has no effect if the model is not set.
result = await self.controller.join_result_GET(wait=True)
self.assertEqual(result, AdJoinResult.UNKNOWN)
async def test_join_Unknown(self):
# Result remains UNKNOWN while ADController.join_domain is not called.
self.controller.model.set(ADConnectionInfo(domain_name='ubuntu.com',
admin_name='Helper',
password='1234'))
result = await self.controller.join_result_GET(wait=False)
self.assertEqual(result, AdJoinResult.UNKNOWN)
async def test_join_OK(self):
# The equivalent of a successful POST
self.controller.model.set(ADConnectionInfo(domain_name='ubuntu.com',
admin_name='Helper',
password='1234'))
# Mimics a client requesting the join result. Blocking by default.
result = self.controller.join_result_GET()
# Mimics a calling from the install controller.
await self.controller.join_domain('this', 'AD Join')
self.assertEqual(await result, AdJoinResult.OK)
async def test_join_Join_Error(self):
self.controller.model.set(ADConnectionInfo(domain_name='jubuntu.com',
admin_name='Helper',
password='1234'))
await self.controller.join_domain('this', 'AD Join')
result = await self.controller.join_result_GET(wait=True)
self.assertEqual(result, AdJoinResult.JOIN_ERROR)
async def test_join_Pam_Error(self):
self.controller.model.set(ADConnectionInfo(domain_name='pubuntu.com',
admin_name='Helper',
password='1234'))
await self.controller.join_domain('this', 'AD Join')
result = await self.controller.join_result_GET(wait=True)
self.assertEqual(result, AdJoinResult.PAM_ERROR)