|
| 1 | +# Tests for the state the knowl and user backends derive from the database |
| 2 | +# session, which psycodict asks them to refresh when it replaces a connection. |
| 3 | +# |
| 4 | +# A real failover is not something a test suite can arrange, so these tests |
| 5 | +# stand in for one: they change what the database reports about the session and |
| 6 | +# call the hook psycodict would call, which is the part of the mechanism that |
| 7 | +# lives in the LMFDB. |
| 8 | + |
| 9 | +import unittest |
| 10 | + |
| 11 | +from lmfdb import db |
| 12 | +from lmfdb.knowledge.knowl import knowldb |
| 13 | +from lmfdb.users.pwdmanager import userdb |
| 14 | + |
| 15 | + |
| 16 | +class ConnectionResetTest(unittest.TestCase): |
| 17 | + def _set_capability(self, attr, value): |
| 18 | + """ |
| 19 | + Make the database report ``value`` for one of its capability flags, |
| 20 | + returning what it reported before. |
| 21 | + """ |
| 22 | + old = getattr(db, attr) |
| 23 | + setattr(db, attr, value) |
| 24 | + return old |
| 25 | + |
| 26 | + def test_knowl_backend_follows_the_session(self): |
| 27 | + old = self._set_capability("_read_and_write_knowls", False) |
| 28 | + try: |
| 29 | + knowldb._connection_reset() |
| 30 | + assert not knowldb.can_read_write_knowls(), \ |
| 31 | + "knowl editing stayed enabled after the session lost the privilege" |
| 32 | + |
| 33 | + self._set_capability("_read_and_write_knowls", True) |
| 34 | + knowldb._connection_reset() |
| 35 | + assert knowldb.can_read_write_knowls(), \ |
| 36 | + "knowl editing stayed disabled after the session regained the privilege" |
| 37 | + finally: |
| 38 | + self._set_capability("_read_and_write_knowls", old) |
| 39 | + knowldb._connection_reset() |
| 40 | + |
| 41 | + def test_user_backend_follows_the_session(self): |
| 42 | + old = self._set_capability("_read_and_write_userdb", False) |
| 43 | + try: |
| 44 | + userdb._connection_reset() |
| 45 | + assert not userdb.can_read_write_userdb(), \ |
| 46 | + "userdb stayed writable after the session lost the privilege" |
| 47 | + # Failing closed means the columns the session could not confirm |
| 48 | + # are not left behind either. |
| 49 | + assert userdb._cols == userdb._username_full_name |
| 50 | + |
| 51 | + self._set_capability("_read_and_write_userdb", True) |
| 52 | + userdb._connection_reset() |
| 53 | + assert userdb.can_read_write_userdb(), \ |
| 54 | + "userdb stayed read-only after the session regained the privilege" |
| 55 | + finally: |
| 56 | + self._set_capability("_read_and_write_userdb", old) |
| 57 | + userdb._connection_reset() |
| 58 | + |
| 59 | + def test_grant_policy_is_the_lmfdb_one(self): |
| 60 | + # Only meaningful once psycodict has policies at all; before that it |
| 61 | + # grants these same privileges on its own. |
| 62 | + try: |
| 63 | + from psycodict.grants import LMFDBGrantPolicy |
| 64 | + except ImportError: |
| 65 | + self.skipTest("this psycodict has no grant policies") |
| 66 | + assert db.grant_policy == LMFDBGrantPolicy(), \ |
| 67 | + "the database is not granting what the website needs on the tables it creates" |
0 commit comments