comparison test/test_liveserver.py @ 6567:34199d2fef48

issue2551178 - Traceback in Apache WSGI (file upload) Added tests to test_liveserver.py for: issue creation with file upload using HTML interface file creation using REST interface The rest interface test causes a crash with a 500 status if I revert the fix for issue2551178. So this tests the orignal failing code path.
author John Rouillard <rouilj@ieee.org>
date Tue, 21 Dec 2021 01:27:17 -0500
parents de5f5f9c02f2
children 59eebe55ca1d
comparison
equal deleted inserted replaced
6566:8f1fddb71422 6567:34199d2fef48
60 # set the url the test instance will run at. 60 # set the url the test instance will run at.
61 cls.db.config['TRACKER_WEB'] = "http://localhost:9001/" 61 cls.db.config['TRACKER_WEB'] = "http://localhost:9001/"
62 # set up mailhost so errors get reported to debuging capture file 62 # set up mailhost so errors get reported to debuging capture file
63 cls.db.config.MAILHOST = "localhost" 63 cls.db.config.MAILHOST = "localhost"
64 cls.db.config.MAIL_HOST = "localhost" 64 cls.db.config.MAIL_HOST = "localhost"
65 cls.db.config.MAIL_DEBUG = "../mail.log.t" 65 cls.db.config.MAIL_DEBUG = "../_test_tracker_mail.log"
66 66
67 # enable static precompressed files 67 # enable static precompressed files
68 cls.db.config.WEB_USE_PRECOMPRESSED_FILES = 1 68 cls.db.config.WEB_USE_PRECOMPRESSED_FILES = 1
69 69
70 cls.db.config.save() 70 cls.db.config.save()
889 print(f.headers) 889 print(f.headers)
890 890
891 self.assertEqual(f.status_code, 200) 891 self.assertEqual(f.status_code, 200)
892 self.assertEqual(f.headers['Cache-Control'], 'public, max-age=1209600') 892 self.assertEqual(f.headers['Cache-Control'], 'public, max-age=1209600')
893 893
894 def test_new_issue_with_file_upload(self):
895 # Set up session to manage cookies <insert blue monster here>
896 session = requests.Session()
897
898 # login using form
899 login = {"__login_name": 'admin', '__login_password': 'sekrit',
900 "@action": "login"}
901 f = session.post(self.url_base()+'/', data=login)
902 # look for change in text in sidebar post login
903 self.assertIn('Hello, admin', f.text)
904
905 # create a new issue and upload a file
906 file_content = 'this is a test file\n'
907 file = {"@file": ('test1.txt', file_content, "text/plain") }
908 issue = {"title": "my title", "priority": "1", "@action": "new"}
909 f = session.post(self.url_base()+'/issue?@template=item', data=issue, files=file)
910 # we have an issue display, verify filename is listed there
911 self.assertIn("test1.txt", f.text)
912 # verify message in redirected url: file 1 created\nissue 1 created
913 # warning may fail if another test loads tracker with files.
914 self.assertEqual('http://localhost:9001/issue1?@ok_message=file%201%20created%0Aissue%201%20created&@template=item', f.url)
915
916 # download file and verify content
917 f = session.get(self.url_base()+'/file1/text1.txt')
918 self.assertEqual(f.text, file_content)
919 print(f.text)
920
921
922 def test_new_file_via_rest(self):
923
924 session = requests.Session()
925 session.auth = ('admin', 'sekrit')
926
927 url = self.url_base() + '/rest/data/'
928 fname = 'a-bigger-testfile'
929 d = dict(name = fname, type='application/octet-stream')
930 c = dict (content = r'xyzzy')
931 r = session.post(url + 'file', files = c, data = d,
932 headers = {'x-requested-with': "rest"}
933 )
934
935 # was a 500 before fix for issue2551178
936 self.assertEqual(r.status_code, 201)
937 # just compare the path leave off the number
938 self.assertIn('http://localhost:9001/rest/data/file/',
939 r.headers["location"])
940 json_dict = json.loads(r.text)
941 self.assertEqual(json_dict["data"]["link"], r.headers["location"])
942
943 # download file and verify content
944 r = session.get(r.headers["location"] +'/content')
945 json_dict = json.loads(r.text)
946 self.assertEqual(json_dict['data']['data'], c["content"])
947 print(r.text)
948
949 # Upload a file via rest interface - no auth
950 session.auth = None
951 r = session.post(url + 'file', files = c, data = d,
952 headers = {'x-requested-with': "rest"}
953 )
954 self.assertEqual(r.status_code, 403)
955
956 # get session variable from web form login
957 # and use it to upload file
958 # login using form
959 login = {"__login_name": 'admin', '__login_password': 'sekrit',
960 "@action": "login"}
961 f = session.post(self.url_base()+'/', data=login)
962 # look for change in text in sidebar post login
963 self.assertIn('Hello, admin', f.text)
964
965 r = session.post(url + 'file', files = c, data = d,
966 headers = {'x-requested-with': "rest"}
967 )
968 self.assertEqual(r.status_code, 201)
969 print(r.status_code)
970
971

Roundup Issue Tracker: http://roundup-tracker.org/