2022-08-17 21:15:32 +03:00
# -*- 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 logging
from django . test import TestCase , TransactionTestCase
from django . test . client import Client
2022-08-25 18:55:51 +03:00
from django . http . response import HttpResponse
2022-08-17 21:15:32 +03:00
from django . conf import settings
from uds import models
from uds . REST . handlers import AUTH_TOKEN_HEADER
from uds . core . managers . crypto import CryptoManager
logger = logging . getLogger ( __name__ )
2022-08-25 23:28:57 +03:00
2022-08-25 18:55:51 +03:00
class UDSHttpResponse ( HttpResponse ) :
"""
Custom response class to be able to access the response content
"""
2022-08-25 23:28:57 +03:00
2022-08-25 18:55:51 +03:00
def __init__ ( self , content , * args , * * kwargs ) :
super ( ) . __init__ ( content , * args , * * kwargs )
self . content = content
def json ( self ) - > typing . Any :
return super ( ) . json ( ) # type: ignore
2022-08-25 23:28:57 +03:00
2022-08-17 21:15:32 +03:00
class UDSClient ( Client ) :
2022-08-19 01:46:38 +03:00
headers : typing . Dict [ str , str ] = {
' HTTP_USER_AGENT ' : ' Testing user agent ' ,
}
2022-08-17 21:15:32 +03:00
def __init__ (
2022-08-25 23:28:57 +03:00
self ,
enforce_csrf_checks : bool = False ,
raise_request_exception : bool = True ,
* * defaults : typing . Any
2022-08-17 21:15:32 +03:00
) :
# Ensure only basic middleware are enabled.
settings . MIDDLEWARE = [
' django.contrib.sessions.middleware.SessionMiddleware ' ,
' django.middleware.locale.LocaleMiddleware ' ,
' django.middleware.common.CommonMiddleware ' ,
' django.middleware.csrf.CsrfViewMiddleware ' ,
' django.contrib.messages.middleware.MessageMiddleware ' ,
' uds.core.util.middleware.request.GlobalRequestMiddleware ' ,
]
2022-08-25 23:28:57 +03:00
# Update settings security options
settings . RSA_KEY = ' -----BEGIN PRIVATE KEY----- \n MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDcANi/08cnpn04 \n jKW/o2G1k4SIa6dJks8DmT4MQHOWqYC46YSIIPzqGoBPcvkbDSPSFBnByo3HhMY+ \n k4JHc9SUwEmHSJWDCHjt7XSXX/ryqH0QQIJtSjk9Bc+GkOU24mnbITiw7ORjp7VN \n vgFdFhjVsZM/NjX/6Y9DoCPC1mGj0O9Dd4MfCsNwUxRhhR6LdrEnpRUSVW0Ksxbz \n cTfpQjdFr86+1BeUbzqN2HDcEGhvioj+0lGPXcOZoRNYU16H7kjLNP+o+rC7f/q/ \n foOYLzDSkmzePbcG+g0H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oAkG8ygDoK3uOR2kJXkTJ2T4QQPCo \n kIp/GaGcGxdviyo+IJyjGijmR1FJTrvotwG22iZKTQKBgQCIh50Dz0/rqZB4Om5g \n LLdZn1C8/lOR8hdK9WUyPHZfJKpQaDOlNdiy9x6xD6+uIQlbNsJhlDbOudHDurfI \n ghGbJ1sy1FUloP+V3JAFS88zIwrddcGEso8YMFMCE1fH2/q35XGwZEnUq7ttDaxx \n HmTQ2w37WASIUgCl2GhM25np0Q== \n -----END PRIVATE KEY----- \n '
settings . CERTIFICATE = ' -----BEGIN CERTIFICATE----- \n MIICzTCCAjYCCQCOUQEWpuEa3jANBgkqhkiG9w0BAQUFADCBqjELMAkGA1UEBhMC \n RVMxDzANBgNVBAgMBk1hZHJpZDEUMBIGA1UEBwwLQWxjb3Jjw4PCs24xHTAbBgNV \n BAoMFFZpcnR1YWwgQ2FibGUgUy5MLlUuMRQwEgYDVQQLDAtEZXZlbG9wbWVudDEY \n MBYGA1UEAwwPQWRvbGZvIEfDg8KzbWV6MSUwIwYJKoZIhvcNAQkBFhZhZ29tZXpA \n dmlydHVhbGNhYmxlLmVzMB4XDTEyMDYyNTA0MjM0MloXDTEzMDYyNTA0MjM0Mlow \n gaoxCzAJBgNVBAYTAkVTMQ8wDQYDVQQIDAZNYWRyaWQxFDASBgNVBAcMC0FsY29y \n Y8ODwrNuMR0wGwYDVQQKDBRWaXJ0dWFsIENhYmxlIFMuTC5VLjEUMBIGA1UECwwL \n RGV2ZWxvcG1lbnQxGDAWBgNVBAMMD0Fkb2xmbyBHw4PCs21lejElMCMGCSqGSIb3 \n DQEJARYWYWdvbWV6QHZpcnR1YWxjYWJsZS5lczCBnzANBgkqhkiG9w0BAQEFAAOB \n jQAwgYkCgYEA35iGyHS/GVdWk3n9kQ+wsCLR++jd9Vez/s407/natm8YDteKksA0 \n MwIvDAX722blm8PUya2NOlnum8KdyUPDOq825XERDlsIA+sTd6lb1c7w44qZ/pb+ \n 68mhXoRx2VJsu//+zhBkaQ1/KcugeHa4WLRIH35YLxdQDxrXS1eQWccCAwEAATAN \n BgkqhkiG9w0BAQUFAAOBgQAk+fJPpY+XvUsxR2A4SaQ8TGnE2x4PtpwCrCVzKEU9 \n W2ugdXvysxkHbib3+JdA6s+lJjHs5HiMZPo/ak8adEKke+d10EU5YcUaJRRUpStY \n qQHziaqOl5Hgi75Kjskq6+tCU0Iui+s9pBg0V6y1AQsCmH2xFs7t1oEOGRFVarfF \n 4Q== \n -----END CERTIFICATE----- '
2022-08-17 21:15:32 +03:00
# Instantiate the client and add basic user agent
2022-08-19 01:46:38 +03:00
super ( ) . __init__ ( enforce_csrf_checks , raise_request_exception )
2022-08-25 23:28:57 +03:00
2022-08-17 21:15:32 +03:00
# and required UDS cookie
self . cookies [ ' uds ' ] = CryptoManager ( ) . randomString ( 48 )
2022-08-19 01:46:38 +03:00
def add_header ( self , name : str , value : str ) :
self . headers [ name ] = value
def request ( self , * * request : typing . Any ) :
# Copy request dict
request = request . copy ( )
# Add headers
request . update ( self . headers )
return super ( ) . request ( * * request )
2022-08-25 18:55:51 +03:00
def get ( self , * args , * * kwargs ) - > ' UDSHttpResponse ' :
return typing . cast ( ' UDSHttpResponse ' , super ( ) . get ( * args , * * kwargs ) )
2022-08-24 23:16:47 +03:00
2022-08-25 18:55:51 +03:00
def post ( self , * args , * * kwargs ) - > ' UDSHttpResponse ' :
return typing . cast ( ' UDSHttpResponse ' , super ( ) . post ( * args , * * kwargs ) )
2022-08-24 23:16:47 +03:00
2022-08-17 21:15:32 +03:00
class UDSTestCase ( TestCase ) :
client_class : typing . Type = UDSClient
2022-08-19 01:46:38 +03:00
client : UDSClient
2022-08-25 23:28:57 +03:00
2022-08-24 23:16:47 +03:00
class UDSTransactionTestCase ( TransactionTestCase ) :
2022-08-17 21:15:32 +03:00
client_class : typing . Type = UDSClient
2022-08-19 01:46:38 +03:00
client : UDSClient