adding tests

This commit is contained in:
Adolfo Gómez García 2022-08-15 00:29:39 +02:00
parent 07e1c6214e
commit 17b040d9b3
6 changed files with 125 additions and 10 deletions

View File

@ -35,11 +35,13 @@ Created on Jun 22, 2012
import logging
import random
import string
import dataclasses
import typing
from django.utils.translation import gettext_noop as _
from uds.core import services
from uds.core import module
from .service import ServiceTestNoCache, ServiceTestCache
# Not imported at runtime, just for type checking
@ -48,7 +50,6 @@ if typing.TYPE_CHECKING:
logger = logging.getLogger(__name__)
class Provider(services.ServiceProvider):
"""
This class represents the simple Test provider.
@ -82,7 +83,21 @@ class Provider(services.ServiceProvider):
# If we don't indicate an order, the output order of fields will be
# "random"
# No fields
# Simple data for testing pourposes
@dataclasses.dataclass
class Data:
"""
This is the data we will store in the storage
"""
name: str = ''
integer: int = 0
data: Data = dataclasses.field(default_factory=Data)
def initialize(self, values: 'module.Module.ValuesType') -> None:
if values:
name = random.SystemRandom().choices(string.ascii_letters, k=10)
return super().initialize(values)
@staticmethod
def test(
@ -94,5 +109,5 @@ class Provider(services.ServiceProvider):
"""
returns a random name for testing pourposes
"""
return '-'.join(random.choices(string.ascii_letters, k=10))
return self.data.name

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2022 Virtual Cable S.L.
# Copyright (c) 2022 Virtual Cable S.L.U.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -11,7 +11,7 @@
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
@ -44,7 +44,7 @@ def createAuthenticator(
authenticator: typing.Optional[models.Authenticator] = None,
) -> models.Authenticator:
"""
Creates a sample authenticator
Creates a testing authenticator
"""
if authenticator is None:
from uds.auths.InternalDB.authenticator import InternalDBAuth
@ -91,7 +91,7 @@ def createGroups(
authenticator: models.Authenticator, number_of_groups: int = 1
) -> typing.List[models.Group]:
"""
Creates a sample authenticator
Creates a testing authenticator
"""
groups = [
authenticator.groups.create(
@ -111,7 +111,7 @@ def createMetaGroups(
authenticator: models.Authenticator, number_of_meta: int = 1
) -> typing.List[models.Group]:
"""
Creates a sample authenticator
Creates a testing authenticator
"""
meta_groups = [
authenticator.groups.create(

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2022 Virtual Cable S.L.
# Copyright (c) 2022 Virtual Cable S.L.U.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -11,7 +11,7 @@
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#

View File

@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2022 Virtual Cable S.L.U.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import typing
from uds import models
from uds.core.util import states
from uds.core.managers.crypto import CryptoManager
# Counters so we can reinvoke the same method and generate new data
glob = {
'service_id': 0,
}
def createProvider(
provider: typing.Optional[models.Provider] = None,
) -> models.Provider:
"""
Creates a testing provider
"""
if provider is None:
from uds.services.Test.provider import Provider
provider = models.Provider()
provider.name = 'Testing provider'
provider.comments = 'Tesging provider'
provider.data_type = Provider.typeType
provider.data = provider.getInstance().serialize()
provider.save()
return provider
def createServices(
provider: models.Provider,
number_of_services: int = 1,
type_of_service: typing.Union[typing.Literal[1], typing.Literal[2]] = 1,
) -> typing.List[models.Service]:
"""
Creates some ramdon services
"""
return []

View File

@ -1,3 +1,33 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2022 Virtual Cable S.L.U.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import typing
import datetime
import random