comparison test/test_liveserver.py @ 8414:cc3edb260c1b reauth-confirm_id

feat: fix tests under postgresql. Wierd issue. Calling self.db.user.lookup('reauth') fails under posttgresql with a cursor closed error. This same call works with anydbm. I tried adding a setup/teardown to open the tracker (self.instance.open('admin') ...etc. But that made the wasgi server fail to shut down for some reason. So I hard coded the id for the reauth user. Also for the postgresql test case, the reauth triggering auditor and user setup weren't done. I tried to reuse the WsgiSetup.setup_class and then add in the few extra things I needed, but it failed. So I copypastaed the code and modified it. Also corrected docstring for one of the test classes.
author John Rouillard <rouilj@ieee.org>
date Thu, 14 Aug 2025 10:34:40 -0400
parents 0663a7bcef6c
children 98e17dd0197f
comparison
equal deleted inserted replaced
8413:8a90350cc78b 8414:cc3edb260c1b
3 import shutil, errno, pytest, json, gzip, mimetypes, os, re 3 import shutil, errno, pytest, json, gzip, mimetypes, os, re
4 4
5 from roundup import date as rdate 5 from roundup import date as rdate
6 from roundup import i18n 6 from roundup import i18n
7 from roundup import password 7 from roundup import password
8 from roundup.anypy.strings import b2s 8 from roundup.anypy.strings import b2s, s2b
9 from roundup.cgi.wsgi_handler import RequestDispatcher 9 from roundup.cgi.wsgi_handler import RequestDispatcher
10 from .wsgi_liveserver import LiveServerTestCase 10 from .wsgi_liveserver import LiveServerTestCase
11 from . import db_test_base 11 from . import db_test_base
12 from textwrap import dedent 12 from textwrap import dedent
13 from time import sleep 13 from time import sleep
375 (note the file contents will be gone because 375 (note the file contents will be gone because
376 preserving that requires javascript) 376 preserving that requires javascript)
377 377
378 enter good password 378 enter good password
379 verify on user page (look for 379 verify on user page (look for
380 "(the default is 0)" hint for timezone) 380 "(the default is" hint for timezone)
381 verify new name present 381 verify new name present
382 verify success banner 382 verify success banner
383 """ 383 """
384 from html.parser import HTMLParser 384 from html.parser import HTMLParser
385 class HTMLExtractForm(HTMLParser): 385 class HTMLExtractForm(HTMLParser):
432 432
433 def get_fields(self): 433 def get_fields(self):
434 return self.fields 434 return self.fields
435 435
436 436
437 # for some reason the lookup works with anydbm but
438 # returns a cursor closed error under postgresql.
439 # adding setup/teardown to TestPostgresWsgiServer
440 # with self.db = self.instance.open('admin') looks like
441 # it caused the wsgi server to hang. So hardcode the id.
442 # self.db.user.lookup('reauth')
443 reauth_id = '4'
444
437 user_url = "%s/user%s" % (self.url_base(), 445 user_url = "%s/user%s" % (self.url_base(),
438 self.db.user.lookup('reauth')) 446 reauth_id)
439 447
440 session, _response = self.create_login_session() 448 session, _response = self.create_login_session()
441 449
442 user_page = session.get(user_url) 450 user_page = session.get(user_url)
443 451
520 pass_reauth = session.post(user_url, 528 pass_reauth = session.post(user_url,
521 files=reauth_submit) 529 files=reauth_submit)
522 self.assertNotIn(b'id="reauth_form"', pass_reauth.content) 530 self.assertNotIn(b'id="reauth_form"', pass_reauth.content)
523 self.assertNotIn(b'Please enter your password to continue with', 531 self.assertNotIn(b'Please enter your password to continue with',
524 pass_reauth.content) 532 pass_reauth.content)
525 self.assertIn(b'user 4 realname edited ok', pass_reauth.content) 533 self.assertIn(b'user %s realname edited ok' % s2b(reauth_id),
526 self.assertIn(b'(the default is 0)', pass_reauth.content) 534 pass_reauth.content)
535 self.assertIn(b'(the default is', pass_reauth.content)
527 536
528 def test_cookie_attributes(self): 537 def test_cookie_attributes(self):
529 session, _response = self.create_login_session() 538 session, _response = self.create_login_session()
530 539
531 cookie_box = session.cookies._cookies['localhost.local']['/'] 540 cookie_box = session.cookies._cookies['localhost.local']['/']
1829 1838
1830 # tests in this class. 1839 # tests in this class.
1831 # set up and open a tracker 1840 # set up and open a tracker
1832 cls.instance = db_test_base.setupTracker(cls.dirname, cls.backend) 1841 cls.instance = db_test_base.setupTracker(cls.dirname, cls.backend)
1833 1842
1843 # add an auditor that triggers a Reauth
1844 with open("%s/detectors/reauth.py" % cls.dirname, "w") as f:
1845 auditor = dedent("""
1846 from roundup.cgi.exceptions import Reauth
1847
1848 def trigger_reauth(db, cl, nodeid, newvalues):
1849 if 'realname' in newvalues and not hasattr(db, 'reauth_done'):
1850 raise Reauth('Add an optional message to the user')
1851
1852 def init(db):
1853 db.user.audit('set', trigger_reauth, priority=110)
1854 """)
1855 f.write(auditor)
1856
1834 # open the database 1857 # open the database
1835 cls.db = cls.instance.open('admin') 1858 cls.db = cls.instance.open('admin')
1836 1859
1837 # add a user without edit access for status. 1860 # add a user without edit access for status.
1838 cls.db.user.create(username="fred", roles='User', 1861 cls.db.user.create(username="fred", roles='User',
1839 password=password.Password('sekrit'), address='fred@example.com') 1862 password=password.Password('sekrit'), address='fred@example.com')
1863
1864 # add a user for reauth tests
1865 cls.db.user.create(username="reauth",
1866 realname="reauth test user",
1867 password=password.Password("reauth"),
1868 address="reauth@example.com", roles="User")
1840 1869
1841 # set the url the test instance will run at. 1870 # set the url the test instance will run at.
1842 cls.db.config['TRACKER_WEB'] = cls.tracker_web 1871 cls.db.config['TRACKER_WEB'] = cls.tracker_web
1843 # set up mailhost so errors get reported to debuging capture file 1872 # set up mailhost so errors get reported to debuging capture file
1844 cls.db.config.MAILHOST = "localhost" 1873 cls.db.config.MAILHOST = "localhost"
1845 cls.db.config.MAIL_HOST = "localhost" 1874 cls.db.config.MAIL_HOST = "localhost"
1846 cls.db.config.MAIL_DEBUG = "../_test_tracker_mail.log" 1875 cls.db.config.MAIL_DEBUG = "../_test_tracker_mail.log"
1847 1876
1877 # also report it in the web.
1878 cls.db.config.WEB_DEBUG = "yes"
1879
1848 # added to enable csrf forgeries/CORS to be tested 1880 # added to enable csrf forgeries/CORS to be tested
1849 cls.db.config.WEB_CSRF_ENFORCE_HEADER_ORIGIN = "required" 1881 cls.db.config.WEB_CSRF_ENFORCE_HEADER_ORIGIN = "required"
1850 cls.db.config.WEB_ALLOWED_API_ORIGINS = "https://client.com" 1882 cls.db.config.WEB_ALLOWED_API_ORIGINS = "https://client.com"
1851 cls.db.config['WEB_CSRF_ENFORCE_HEADER_X-REQUESTED-WITH'] = "required" 1883 cls.db.config['WEB_CSRF_ENFORCE_HEADER_X-REQUESTED-WITH'] = "required"
1852 1884
1885 # use native indexer
1853 cls.db.config.INDEXER = "native-fts" 1886 cls.db.config.INDEXER = "native-fts"
1854 1887
1855 # disable web login rate limiting. The fast rate of tests 1888 # disable web login rate limiting. The fast rate of tests
1856 # causes them to trip the rate limit and fail. 1889 # causes them to trip the rate limit and fail.
1857 cls.db.config.WEB_LOGIN_ATTEMPTS_MIN = 0 1890 cls.db.config.WEB_LOGIN_ATTEMPTS_MIN = 0
1865 cls.db.close() 1898 cls.db.close()
1866 1899
1867 # re-open the database to get the updated INDEXER 1900 # re-open the database to get the updated INDEXER
1868 cls.db = cls.instance.open('admin') 1901 cls.db = cls.instance.open('admin')
1869 1902
1903 # add an issue to allow testing retrieval.
1904 # also used for text searching.
1870 result = cls.db.issue.create(title="foo bar RESULT") 1905 result = cls.db.issue.create(title="foo bar RESULT")
1871 1906
1872 # add a message to allow retrieval 1907 # add a message to allow retrieval
1873 result = cls.db.msg.create(author = "1", 1908 result = cls.db.msg.create(author = "1",
1874 content = "a message foo bar RESULT", 1909 content = "a message foo bar RESULT",
1875 date=rdate.Date(), 1910 date=rdate.Date(),
1876 messageid="test-msg-id") 1911 messageid="test-msg-id")
1912
1913 # add a query using @current_user
1914 result = cls.db.query.create(
1915 klass="issue",
1916 name="I created",
1917 private_for=None,
1918 url=("@columns=title,id,activity,status,assignedto&"
1919 "@sort=activity&@group=priority&@filter=creator&"
1920 "@pagesize=50&@startwith=0&creator=%40current_user")
1921 )
1877 1922
1878 cls.db.commit() 1923 cls.db.commit()
1879 cls.db.close() 1924 cls.db.close()
1880 1925
1881 # Force locale config to find locales in checkout not in 1926 # Force locale config to find locales in checkout not in
1897 f = requests.get(self.url_base() + "?@search_text=ts:RESULT") 1942 f = requests.get(self.url_base() + "?@search_text=ts:RESULT")
1898 self.assertIn("foo bar RESULT", f.text) 1943 self.assertIn("foo bar RESULT", f.text)
1899 1944
1900 @skip_requests 1945 @skip_requests
1901 class TestApiRateLogin(WsgiSetup): 1946 class TestApiRateLogin(WsgiSetup):
1902 """Class to run test in BaseTestCases with the cache_tracker 1947 """Test api rate limiting on login use sqlite db.
1903 feature flag enabled when starting the wsgi server
1904 """ 1948 """
1905 1949
1906 backend = 'sqlite' 1950 backend = 'sqlite'
1907 1951
1908 @classmethod 1952 @classmethod

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