Browse Source

HUE-8737 [core] Fix desktop tests for py3

Ying Chen 6 years ago
parent
commit
7ef66615d0

+ 2 - 2
desktop/core/src/desktop/api2.py

@@ -522,7 +522,7 @@ def delete_document(request):
 @api_error_handler
 @api_error_handler
 @require_POST
 @require_POST
 def copy_document(request):
 def copy_document(request):
-  uuid = json.loads(request.POST.get('uuid'), '""')
+  uuid = json.loads(request.POST.get('uuid', '""'))
 
 
   if not uuid:
   if not uuid:
     raise PopupException(_('copy_document requires uuid'))
     raise PopupException(_('copy_document requires uuid'))
@@ -724,7 +724,7 @@ def export_documents(request):
     zfile.close()
     zfile.close()
     response = HttpResponse(content_type="application/zip")
     response = HttpResponse(content_type="application/zip")
     response["Content-Length"] = len(f.getvalue())
     response["Content-Length"] = len(f.getvalue())
-    response['Content-Disposition'] = 'attachment; filename="%s".zip' % filename
+    response['Content-Disposition'] = b'attachment; filename="%s".zip' % filename
     response.write(f.getvalue())
     response.write(f.getvalue())
     return response
     return response
   else:
   else:

+ 1 - 1
desktop/core/src/desktop/api_tests.py

@@ -59,7 +59,7 @@ class TestDocModelTags(object):
   def share_doc(self, doc, permissions):
   def share_doc(self, doc, permissions):
     response = self.client.post("/desktop/api/doc/update_permissions", {
     response = self.client.post("/desktop/api/doc/update_permissions", {
         'doc_id': doc.id,
         'doc_id': doc.id,
-        'data': json.dumps(*permissions)
+        'data': json.dumps(permissions)
     })
     })
 
 
   def share_doc_read_only(self, doc):
   def share_doc_read_only(self, doc):

+ 9 - 0
desktop/core/src/desktop/document2_tests.py

@@ -1332,6 +1332,9 @@ class TestDocument2ImportExport(object):
     response = self.client.get('/desktop/api2/doc/export/', {'documents': json.dumps([owned_query.id]), 'format': 'json'})
     response = self.client.get('/desktop/api2/doc/export/', {'documents': json.dumps([owned_query.id]), 'format': 'json'})
     documents = response.content
     documents = response.content
 
 
+    if isinstance(documents, bytes):
+      documents = documents.decode('utf-8')
+
     response = self.client.post('/desktop/api2/doc/import/', {'documents': documents})
     response = self.client.post('/desktop/api2/doc/import/', {'documents': documents})
     data = json.loads(response.content)
     data = json.loads(response.content)
 
 
@@ -1380,6 +1383,9 @@ class TestDocument2ImportExport(object):
     response = self.client.get('/desktop/api2/doc/export/', {'documents': json.dumps([owned_query.id]), 'format': 'json'})
     response = self.client.get('/desktop/api2/doc/export/', {'documents': json.dumps([owned_query.id]), 'format': 'json'})
     documents = response.content
     documents = response.content
 
 
+    if isinstance(documents, bytes):
+      documents = documents.decode('utf-8')
+
     # Test that importing non-owned doc copies it, sets parent to home
     # Test that importing non-owned doc copies it, sets parent to home
     response = self.client_not_me.post('/desktop/api2/doc/import/', {'documents': documents})
     response = self.client_not_me.post('/desktop/api2/doc/import/', {'documents': documents})
 
 
@@ -1415,6 +1421,9 @@ class TestDocument2ImportExport(object):
     query2.delete()
     query2.delete()
     workflow.delete()
     workflow.delete()
 
 
+    if not isinstance(documents, str):
+      documents = documents.decode('utf-8')
+
     response = self.client_not_me.post('/desktop/api2/doc/import/', {'documents': documents})
     response = self.client_not_me.post('/desktop/api2/doc/import/', {'documents': documents})
     assert_true(Document2.objects.filter(name='query1.sql').exists())
     assert_true(Document2.objects.filter(name='query1.sql').exists())
     assert_false(Document2.objects.filter(name='query2.sql').exists())
     assert_false(Document2.objects.filter(name='query2.sql').exists())

+ 1 - 1
desktop/core/src/desktop/redaction/tests.py

@@ -83,7 +83,7 @@ class TestRedactionRule(object):
 
 
 
 
   def test_parse_redaction_policy_from_file(self):
   def test_parse_redaction_policy_from_file(self):
-    with tempfile.NamedTemporaryFile() as f:
+    with tempfile.NamedTemporaryFile(mode='w') as f:
       json.dump({
       json.dump({
           'version': 1,
           'version': 1,
           'rules': [
           'rules': [

+ 24 - 8
desktop/core/src/desktop/tests.py

@@ -207,7 +207,7 @@ def test_download_log_view():
 def test_dump_config():
 def test_dump_config():
   c = make_logged_in_client()
   c = make_logged_in_client()
 
 
-  CANARY = "abracadabra"
+  CANARY = b"abracadabra"
 
 
   # Depending on the order of the conf.initialize() in settings, the set_for_testing() are not seen in the global settings variable
   # Depending on the order of the conf.initialize() in settings, the set_for_testing() are not seen in the global settings variable
   clear = HIVE_SERVER_HOST.set_for_testing(CANARY)
   clear = HIVE_SERVER_HOST.set_for_testing(CANARY)
@@ -229,6 +229,8 @@ def test_dump_config():
 
 
   try:
   try:
     response1 = c.get(reverse('desktop.views.dump_config'))
     response1 = c.get(reverse('desktop.views.dump_config'))
+    if not isinstance(CANARY, bytes):
+      CANARY = CANARY.encode('utf-8')
     assert_true(CANARY in response1.content)
     assert_true(CANARY in response1.content)
   finally:
   finally:
     clear()
     clear()
@@ -239,6 +241,8 @@ def test_dump_config():
 
 
   try:
   try:
     response1 = c.get(reverse('desktop.views.dump_config'))
     response1 = c.get(reverse('desktop.views.dump_config'))
+    if not isinstance(CANARY, bytes):
+      CANARY = CANARY.encode('utf-8')
     assert_true(CANARY in response1.content, response1.content)
     assert_true(CANARY in response1.content, response1.content)
   finally:
   finally:
     clear()
     clear()
@@ -247,26 +251,29 @@ def test_dump_config():
   finish = proxy.conf.WHITELIST.set_for_testing(CANARY)
   finish = proxy.conf.WHITELIST.set_for_testing(CANARY)
   try:
   try:
     response = c.get(reverse('desktop.views.dump_config'))
     response = c.get(reverse('desktop.views.dump_config'))
-    assert_true(CANARY in response.content, response.content)
+    response_content = response.content
+    if not isinstance(response_content, str):
+      response_content = response_content.decode('utf-8')
+    assert_true(CANARY in response_content, response_content)
   finally:
   finally:
     finish()
     finish()
 
 
   # Not showing some passwords
   # Not showing some passwords
   response = c.get(reverse('desktop.views.dump_config'))
   response = c.get(reverse('desktop.views.dump_config'))
-  assert_false('bind_password' in response.content)
+  assert_false(b'bind_password' in response.content)
 
 
   # Login as someone else
   # Login as someone else
   client_not_me = make_logged_in_client(username='not_me', is_superuser=False, groupname='test')
   client_not_me = make_logged_in_client(username='not_me', is_superuser=False, groupname='test')
   grant_access("not_me", "test", "desktop")
   grant_access("not_me", "test", "desktop")
 
 
   response = client_not_me.get(reverse('desktop.views.dump_config'))
   response = client_not_me.get(reverse('desktop.views.dump_config'))
-  assert_true("You must be a superuser" in response.content, response.content)
+  assert_true(b"You must be a superuser" in response.content, response.content)
 
 
   prev_env_conf = os.environ.get("HUE_CONF_DIR")
   prev_env_conf = os.environ.get("HUE_CONF_DIR")
   try:
   try:
     os.environ["HUE_CONF_DIR"] = "/tmp/test_hue_conf_dir"
     os.environ["HUE_CONF_DIR"] = "/tmp/test_hue_conf_dir"
     resp = c.get(reverse('desktop.views.dump_config'))
     resp = c.get(reverse('desktop.views.dump_config'))
-    assert_true('/tmp/test_hue_conf_dir' in resp.content, resp)
+    assert_true(b'/tmp/test_hue_conf_dir' in resp.content, resp)
   finally:
   finally:
     if prev_env_conf is None:
     if prev_env_conf is None:
       os.environ.pop("HUE_CONF_DIR", None)
       os.environ.pop("HUE_CONF_DIR", None)
@@ -394,7 +401,7 @@ def test_paginator():
 def test_thread_dump():
 def test_thread_dump():
   c = make_logged_in_client()
   c = make_logged_in_client()
   response = c.get("/desktop/debug/threads", HTTP_X_REQUESTED_WITH='XMLHttpRequest')
   response = c.get("/desktop/debug/threads", HTTP_X_REQUESTED_WITH='XMLHttpRequest')
-  assert_true("test_thread_dump" in response.content)
+  assert_true(b"test_thread_dump" in response.content)
 
 
 def test_truncating_model():
 def test_truncating_model():
   class TinyModel(TruncatingModel):
   class TinyModel(TruncatingModel):
@@ -726,7 +733,9 @@ def test_404_handling():
   c = make_logged_in_client()
   c = make_logged_in_client()
   response = c.get(view_name)
   response = c.get(view_name)
   assert_true(any(['404.mako' in _template.filename for _template in response.templates]), response.templates)
   assert_true(any(['404.mako' in _template.filename for _template in response.templates]), response.templates)
-  assert_true('not found' in response.content)
+  assert_true(b'not found' in response.content)
+  if not isinstance(view_name, bytes):
+    view_name = view_name.encode('utf-8')
   assert_true(view_name in response.content)
   assert_true(view_name in response.content)
 
 
 class RecordingHandler(logging.Handler):
 class RecordingHandler(logging.Handler):
@@ -859,6 +868,8 @@ def test_ui_customizations():
   try:
   try:
     c = make_logged_in_client()
     c = make_logged_in_client()
     c.logout()
     c.logout()
+    if not isinstance(custom_message, bytes):
+      custom_message = custom_message.encode('utf-8')
     resp = c.get('/hue/accounts/login/', follow=False)
     resp = c.get('/hue/accounts/login/', follow=False)
     assert_true(custom_message in resp.content, resp)
     assert_true(custom_message in resp.content, resp)
     resp = c.get('/hue/about', follow=True)
     resp = c.get('/hue/about', follow=True)
@@ -896,10 +907,14 @@ def test_cx_Oracle():
 class TestStrictRedirection(object):
 class TestStrictRedirection(object):
 
 
   def setUp(self):
   def setUp(self):
+    self.finish = desktop.conf.AUTH.BACKEND.set_for_testing(['desktop.auth.backend.AllowFirstUserDjangoBackend'])
     self.client = make_logged_in_client()
     self.client = make_logged_in_client()
     self.user = dict(username="test", password="test")
     self.user = dict(username="test", password="test")
     desktop.conf.REDIRECT_WHITELIST.set_for_testing('^\/.*$,^http:\/\/example.com\/.*$')
     desktop.conf.REDIRECT_WHITELIST.set_for_testing('^\/.*$,^http:\/\/example.com\/.*$')
 
 
+  def tearDown(self):
+    self.finish()
+
   def test_redirection_blocked(self):
   def test_redirection_blocked(self):
     # Redirection with code 301 should be handled properly
     # Redirection with code 301 should be handled properly
     # Redirection with Status code 301 example reference: http://www.somacon.com/p145.php
     # Redirection with Status code 301 example reference: http://www.somacon.com/p145.php
@@ -921,13 +936,14 @@ class TestStrictRedirection(object):
     self._test_redirection(redirection_url='http://example.com/', expected_status_code=302)
     self._test_redirection(redirection_url='http://example.com/', expected_status_code=302)
 
 
   def _test_redirection(self, redirection_url, expected_status_code, **kwargs):
   def _test_redirection(self, redirection_url, expected_status_code, **kwargs):
-    self.client.get('/accounts/logout', **kwargs)
     data = self.user.copy()
     data = self.user.copy()
     data['next'] = redirection_url
     data['next'] = redirection_url
     response = self.client.post('/hue/accounts/login/', data, **kwargs )
     response = self.client.post('/hue/accounts/login/', data, **kwargs )
     assert_equal(expected_status_code, response.status_code)
     assert_equal(expected_status_code, response.status_code)
     if expected_status_code == 403:
     if expected_status_code == 403:
         error_msg = 'Redirect to ' + redirection_url + ' is not allowed.'
         error_msg = 'Redirect to ' + redirection_url + ' is not allowed.'
+        if not isinstance(error_msg, bytes):
+          error_msg = error_msg.encode('utf-8')
         assert_true(error_msg in response.content, response.content)
         assert_true(error_msg in response.content, response.content)