Skip to content

Commit cc82d46

Browse files
committed
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.
1 parent cf71c10 commit cc82d46

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

test/test_liveserver.py

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from roundup import date as rdate
66
from roundup import i18n
77
from roundup import password
8-
from roundup.anypy.strings import b2s
8+
from roundup.anypy.strings import b2s, s2b
99
from roundup.cgi.wsgi_handler import RequestDispatcher
1010
from .wsgi_liveserver import LiveServerTestCase
1111
from . import db_test_base
@@ -377,7 +377,7 @@ def test_reauth_workflow(self):
377377
378378
enter good password
379379
verify on user page (look for
380-
"(the default is 0)" hint for timezone)
380+
"(the default is" hint for timezone)
381381
verify new name present
382382
verify success banner
383383
"""
@@ -434,8 +434,16 @@ def get_fields(self):
434434
return self.fields
435435

436436

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+
437445
user_url = "%s/user%s" % (self.url_base(),
438-
self.db.user.lookup('reauth'))
446+
reauth_id)
439447

440448
session, _response = self.create_login_session()
441449

@@ -522,8 +530,9 @@ def get_fields(self):
522530
self.assertNotIn(b'id="reauth_form"', pass_reauth.content)
523531
self.assertNotIn(b'Please enter your password to continue with',
524532
pass_reauth.content)
525-
self.assertIn(b'user 4 realname edited ok', pass_reauth.content)
526-
self.assertIn(b'(the default is 0)', pass_reauth.content)
533+
self.assertIn(b'user %s realname edited ok' % s2b(reauth_id),
534+
pass_reauth.content)
535+
self.assertIn(b'(the default is', pass_reauth.content)
527536

528537
def test_cookie_attributes(self):
529538
session, _response = self.create_login_session()
@@ -1831,25 +1840,49 @@ def setup_class(cls):
18311840
# set up and open a tracker
18321841
cls.instance = db_test_base.setupTracker(cls.dirname, cls.backend)
18331842

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+
18341857
# open the database
18351858
cls.db = cls.instance.open('admin')
18361859

18371860
# add a user without edit access for status.
18381861
cls.db.user.create(username="fred", roles='User',
18391862
password=password.Password('sekrit'), address='[email protected]')
18401863

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="[email protected]", roles="User")
1869+
18411870
# set the url the test instance will run at.
18421871
cls.db.config['TRACKER_WEB'] = cls.tracker_web
18431872
# set up mailhost so errors get reported to debuging capture file
18441873
cls.db.config.MAILHOST = "localhost"
18451874
cls.db.config.MAIL_HOST = "localhost"
18461875
cls.db.config.MAIL_DEBUG = "../_test_tracker_mail.log"
18471876

1877+
# also report it in the web.
1878+
cls.db.config.WEB_DEBUG = "yes"
1879+
18481880
# added to enable csrf forgeries/CORS to be tested
18491881
cls.db.config.WEB_CSRF_ENFORCE_HEADER_ORIGIN = "required"
18501882
cls.db.config.WEB_ALLOWED_API_ORIGINS = "https://client.com"
18511883
cls.db.config['WEB_CSRF_ENFORCE_HEADER_X-REQUESTED-WITH'] = "required"
18521884

1885+
# use native indexer
18531886
cls.db.config.INDEXER = "native-fts"
18541887

18551888
# disable web login rate limiting. The fast rate of tests
@@ -1867,6 +1900,8 @@ def setup_class(cls):
18671900
# re-open the database to get the updated INDEXER
18681901
cls.db = cls.instance.open('admin')
18691902

1903+
# add an issue to allow testing retrieval.
1904+
# also used for text searching.
18701905
result = cls.db.issue.create(title="foo bar RESULT")
18711906

18721907
# add a message to allow retrieval
@@ -1875,6 +1910,16 @@ def setup_class(cls):
18751910
date=rdate.Date(),
18761911
messageid="test-msg-id")
18771912

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+
)
1922+
18781923
cls.db.commit()
18791924
cls.db.close()
18801925

@@ -1899,8 +1944,7 @@ def test_native_fts(self):
18991944

19001945
@skip_requests
19011946
class TestApiRateLogin(WsgiSetup):
1902-
"""Class to run test in BaseTestCases with the cache_tracker
1903-
feature flag enabled when starting the wsgi server
1947+
"""Test api rate limiting on login use sqlite db.
19041948
"""
19051949

19061950
backend = 'sqlite'

0 commit comments

Comments
 (0)