def test_priv_grant_read(self): """Test ability to grant read but not write privs""" self.create_all_tables() try: self.engine.execute("CREATE USER pytest WITH PASSWORD 'pyt3st'") g = PsqlGraphDriver(self.host, 'pytest', 'pyt3st', self.database) #: If this failes, this test (not the code) is wrong! with self.assertRaises(ProgrammingError): with g.session_scope(): g.nodes().count() pgadmin.main(pgadmin.get_parser().parse_args([ 'graph-grant', '--read=pytest', ] + self.base_args)) with g.session_scope(): g.nodes().count() with self.assertRaises(ProgrammingError): with g.session_scope() as s: s.merge(models.Case('1')) finally: self.engine.execute("DROP OWNED BY pytest; DROP USER pytest")
def test_priv_revoke_write(self): """Test ability to revoke read/write privs""" self.create_all_tables() try: self.engine.execute("CREATE USER pytest WITH PASSWORD 'pyt3st'") g = PsqlGraphDriver(self.host, 'pytest', 'pyt3st', self.database) pgadmin.main(pgadmin.get_parser().parse_args([ 'graph-grant', '--write=pytest', ] + self.base_args)) pgadmin.main(pgadmin.get_parser().parse_args([ 'graph-revoke', '--write=pytest', ] + self.base_args)) with g.session_scope() as s: g.nodes().count() with self.assertRaises(ProgrammingError): with g.session_scope() as s: s.merge(models.Case('1')) finally: self.engine.execute("DROP OWNED BY pytest; DROP USER pytest")
def test_create_fails_blocked_without_force(self): """Test table creation fails when blocked w/o force""" q = Queue() # to communicate with blocking process args = pgadmin.get_parser().parse_args( ['graph-create', '--delay', '1', '--retries', '1'] + self.base_args) pgadmin.main(args) self.drop_a_table() def blocker(): with self.g.session_scope() as s: s.merge(models.Case('1')) q.put(0) # Tell main thread we're ready q.get() # Wait for main thread to tell us to exit p = Process(target=blocker) p.daemon = True p.start() q.get() with self.assertRaises(RuntimeError): pgadmin.main(args) q.put(0) p.terminate()
def test_create_force(self): """Test ability to force table creation""" q = Queue() # to communicate with blocking process args = pgadmin.get_parser().parse_args( ['graph-create', '--delay', '1', '--retries', '1', '--force'] + self.base_args) pgadmin.main(args) self.drop_a_table() def blocker(): with self.g.session_scope() as s: s.merge(models.Case('1')) q.put(0) # Tell main thread we're ready q.get() # This get should block until this prcoess is killed assert False, 'Should not be reachable!' p = Process(target=blocker) p.daemon = True p.start() q.get() try: pgadmin.main(args) except: p.terminate() raise q.put(0) p.terminate()
def test_priv_revoke_write(self): """Test ability to revoke read/write privs""" self.create_all_tables() self.engine.execute("CREATE USER pytest WITH PASSWORD 'pyt3st'") try: g = PsqlGraphDriver(self.host, 'pytest', 'pyt3st', self.database) pgadmin.main(pgadmin.get_parser().parse_args([ 'graph-grant', '--write=pytest', ] + self.base_args)) pgadmin.main(pgadmin.get_parser().parse_args([ 'graph-revoke', '--write=pytest', ] + self.base_args)) with g.session_scope() as s: g.nodes().count() with self.assertRaises(ProgrammingError): with g.session_scope() as s: s.merge(models.Case('1')) finally: self.engine.execute("DROP OWNED BY pytest; DROP USER pytest")
def test_priv_grant_read(self): """Test ability to grant read but not write privs""" self.create_all_tables() self.engine.execute("CREATE USER pytest WITH PASSWORD 'pyt3st'") try: g = PsqlGraphDriver(self.host, 'pytest', 'pyt3st', self.database) #: If this failes, this test (not the code) is wrong! with self.assertRaises(ProgrammingError): with g.session_scope(): g.nodes().count() pgadmin.main(pgadmin.get_parser().parse_args([ 'graph-grant', '--read=pytest', ] + self.base_args)) with g.session_scope(): g.nodes().count() with self.assertRaises(ProgrammingError): with g.session_scope() as s: s.merge(models.Case('1')) finally: self.engine.execute("DROP OWNED BY pytest; DROP USER pytest")
def test_create_force(self): """Test ability to force table creation""" q = Queue() # to communicate with blocking process args = pgadmin.get_parser().parse_args([ 'graph-create', '--delay', '1', '--retries', '1', '--force' ] + self.base_args) pgadmin.main(args) self.drop_a_table() def blocker(): with self.g.session_scope() as s: s.merge(models.Case('1')) q.put(0) # Tell main thread we're ready q.get() # This get should block until this prcoess is killed assert False, 'Should not be reachable!' p = Process(target=blocker) p.daemon = True p.start() q.get() try: pgadmin.main(args) except: p.terminate() raise q.put(0) p.terminate()
def test_create_fails_blocked_without_force(self): """Test table creation fails when blocked w/o force""" q = Queue() # to communicate with blocking process args = pgadmin.get_parser().parse_args([ 'graph-create', '--delay', '1', '--retries', '1' ] + self.base_args) pgadmin.main(args) self.drop_a_table() def blocker(): with self.g.session_scope() as s: s.merge(models.Case('1')) q.put(0) # Tell main thread we're ready q.get() # Wait for main thread to tell us to exit p = Process(target=blocker) p.daemon = True p.start() q.get() with self.assertRaises(RuntimeError): pgadmin.main(args) q.put(0) p.terminate()
def test_create_single(self): """Test simple table creation""" pgadmin.main(pgadmin.get_parser().parse_args( ['graph-create', '--delay', '1', '--retries', '0'] + self.base_args)) self.engine.execute('SELECT * from node_case')
def test_create_single(self): """Test simple table creation""" pgadmin.main(pgadmin.get_parser().parse_args([ 'graph-create', '--delay', '1', '--retries', '0' ] + self.base_args)) self.engine.execute('SELECT * from node_case')
def test_priv_grant_write(self): """Test ability to grant read/write privs""" self.create_all_tables() try: self.engine.execute("CREATE USER pytest WITH PASSWORD 'pyt3st'") self.engine.execute("GRANT USAGE ON SCHEMA public TO pytest") g = PsqlGraphDriver(self.host, 'pytest', 'pyt3st', self.database) pgadmin.main(pgadmin.get_parser().parse_args([ 'graph-grant', '--write=pytest', ] + self.base_args)) with g.session_scope() as s: g.nodes().count() s.merge(models.Case('1')) finally: self.engine.execute("DROP OWNED BY pytest; DROP USER pytest")
def test_create_tables(db_config, namespace): """ Tests tables can be created with the admin script using either a custom dictionary or the default Args: db_config (dict[str,str]): db connection config namespace (str): module namespace, None for default """ # simulate loading a different dictionary if namespace: models.load_dictionary(dictionary=None, package_namespace=namespace) # drop existing tables admin = get_admin_driver(db_config, namespace) drop_all_tables(admin) # get the proper module for loading classes m = models if namespace: m = getattr(models, namespace) # test tables currently do not exist. Testing one table should be ok with pytest.raises(ProgrammingError) as e: with admin.session_scope(): assert admin.nodes(m.Case).count() == 0 # Hard coded message expected to be in the exception assert 'relation "node_case" does not exist' in str(e.value) # create tables using admin script args = ['graph-create', '--delay', '1', '--retries', '0' ] + get_base_args(namespace=namespace) parsed_args = pgadmin.get_parser().parse_args(args) pgadmin.main(parsed_args) # validate case and program tables was created with admin.session_scope(): assert admin.nodes(m.Case).count() == 0 assert admin.nodes(m.Program).count() == 0
def create_all_tables(cls): parser = pgadmin.get_parser() args = parser.parse_args( ['graph-create', '--delay', '1', '--retries', '0'] + cls.base_args) pgadmin.main(args)
def run_admin_command(args, namespace=None): args += get_base_args(namespace=namespace) parsed_args = pgadmin.get_parser().parse_args(args) pgadmin.main(parsed_args)
def create_all_tables(cls): parser = pgadmin.get_parser() args = parser.parse_args([ 'graph-create', '--delay', '1', '--retries', '0', '--force' ] + cls.base_args) pgadmin.main(args)