Best Python code snippet using localstack_python
test_django.py
Source:test_django.py
...20except ImportError:21 django = None22import pyamf23from pyamf import remoting, util24def make_http_request(method, body=''):25 http_request = http.HttpRequest()26 http_request.method = method27 version = _django.VERSION[:2]28 if version <= (1, 2):29 http_request.raw_post_data = body30 else:31 http_request._stream = StringIO(body)32 # fix a django 1.3 bug where this would not be set33 http_request._read_started = False34 return http_request35class BaseTestCase(unittest.TestCase):36 """37 """38 def setUp(self):39 if not django:40 self.skipTest("'django' not available")41class DjangoGatewayTestCase(BaseTestCase):42 def setUp(self):43 BaseTestCase.setUp(self)44 import new45 self.mod_name = '%s.%s' % (__name__, 'settings')46 self.settings = sys.modules[self.mod_name] = new.module(self.mod_name)47 self.old_env = os.environ.get('DJANGO_SETTINGS_MODULE', None)48 os.environ['DJANGO_SETTINGS_MODULE'] = self.mod_name49 self.settings.SECRET_KEY = 'unittest'50 def tearDown(self):51 if self.old_env is not None:52 os.environ['DJANGO_SETTINGS_MODULE'] = self.old_env53 del sys.modules[self.mod_name]54 def test_csrf(self):55 gw = django.DjangoGateway()56 self.assertTrue(gw.csrf_exempt)57 def test_settings(self):58 from django import conf59 settings_mod = sys.modules[self.mod_name]60 settings_mod.DEBUG = True61 settings_mod.AMF_TIME_OFFSET = 100062 old_settings = conf.settings63 conf.settings = conf.Settings(self.mod_name)64 gw = django.DjangoGateway()65 try:66 self.assertTrue(gw.debug)67 self.assertEqual(gw.timezone_offset, 1000)68 finally:69 conf.settings = old_settings70 def test_request_method(self):71 gw = django.DjangoGateway()72 http_request = make_http_request('GET')73 http_response = gw(http_request)74 self.assertEqual(http_response.status_code, 405)75 def test_bad_request(self):76 gw = django.DjangoGateway()77 request = util.BufferedByteStream()78 request.write('Bad request')79 request.seek(0, 0)80 http_request = make_http_request('POST', request.getvalue())81 http_response = gw(http_request)82 self.assertEqual(http_response.status_code, 400)83 def test_unknown_request(self):84 gw = django.DjangoGateway()85 request = util.BufferedByteStream()86 request.write(87 '\x00\x00\x00\x00\x00\x01\x00\x09test.test\x00'88 '\x02/1\x00\x00\x00\x14\x0a\x00\x00\x00\x01\x08\x00\x00\x00\x00'89 '\x00\x01\x61\x02\x00\x01\x61\x00\x00\x09'90 )91 request.seek(0, 0)92 http_request = make_http_request('POST', request.getvalue())93 http_response = gw(http_request)94 envelope = remoting.decode(http_response.content)95 message = envelope['/1']96 self.assertEqual(message.status, remoting.STATUS_ERROR)97 body = message.body98 self.assertTrue(isinstance(body, remoting.ErrorFault))99 self.assertEqual(body.code, 'Service.ResourceNotFound')100 def test_expose_request(self):101 self.executed = False102 def test(request):103 self.assertEqual(http_request, request)104 self.assertTrue(hasattr(request, 'amf_request'))105 self.executed = True106 gw = django.DjangoGateway({'test.test': test}, expose_request=True)107 request = util.BufferedByteStream()108 request.write(109 '\x00\x00\x00\x00\x00\x01\x00\x09test.test\x00'110 '\x02/1\x00\x00\x00\x05\x0a\x00\x00\x00\x00'111 )112 request.seek(0, 0)113 http_request = make_http_request('POST', request.getvalue())114 gw(http_request)115 self.assertTrue(self.executed)116 def _raiseException(self, e, *args, **kwargs):117 raise e()118 def test_really_bad_decode(self):119 self.old_method = remoting.decode120 remoting.decode = lambda *args, **kwargs: self._raiseException(121 Exception, *args, **kwargs122 )123 http_request = make_http_request('POST', '')124 gw = django.DjangoGateway()125 try:126 http_response = gw(http_request)127 except:128 remoting.decode = self.old_method129 raise130 remoting.decode = self.old_method131 self.assertTrue(132 isinstance(http_response, http.HttpResponseServerError)133 )134 self.assertEqual(http_response.status_code, 500)135 self.assertEqual(136 http_response.content,137 '500 Internal Server Error\n\nAn unexpected error occurred.'138 )139 def test_expected_exceptions_decode(self):140 self.old_method = remoting.decode141 gw = django.DjangoGateway()142 http_request = make_http_request('POST', '')143 try:144 for x in (KeyboardInterrupt, SystemExit):145 remoting.decode = lambda *args, **kwargs: self._raiseException(146 x, *args, **kwargs147 )148 self.assertRaises(x, gw, http_request)149 except:150 remoting.decode = self.old_method151 raise152 remoting.decode = self.old_method153 def test_timezone(self):154 import datetime155 self.executed = False156 td = datetime.timedelta(hours=-5)157 now = datetime.datetime.utcnow()158 def echo(d):159 self.assertEqual(d, now + td)160 self.executed = True161 return d162 gw = django.DjangoGateway(163 {'test.test': echo},164 timezone_offset=-18000,165 expose_request=False166 )167 msg = remoting.Envelope(amfVersion=pyamf.AMF0)168 msg['/1'] = remoting.Request(target='test.test', body=[now])169 http_request = make_http_request(170 'POST',171 remoting.encode(msg).getvalue()172 )173 res = remoting.decode(gw(http_request).content)174 self.assertTrue(self.executed)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!