def test_fetch(self): """ Test that the security document is retrieved as expected """ sdoc = SecurityDocument(self.db) sdoc.fetch() self.assertDictEqual(sdoc, self.sdoc)
def test_json(self): """ Test the security document dictionary renders as a JSON string """ sdoc = SecurityDocument(self.db) sdoc.fetch() sdoc_as_json_string = sdoc.json() self.assertIsInstance(sdoc_as_json_string, str) sdoc_as_a_dict = json.loads(sdoc_as_json_string) self.assertDictEqual(sdoc_as_a_dict, sdoc)
def test_context_manager(self): """ Test that the context SecurityDocument context manager enter and exit routines work as expected. """ with SecurityDocument(self.db) as sdoc: self.assertDictEqual(sdoc, self.sdoc) sdoc.update(self.mod_sdoc) mod_sdoc = SecurityDocument(self.db) mod_sdoc.fetch() self.assertDictEqual(mod_sdoc, self.mod_sdoc)
def test_save(self): """ Test that the security document is updated correctly """ sdoc = SecurityDocument(self.db) sdoc.fetch() sdoc.update(self.mod_sdoc) sdoc.save() mod_sdoc = SecurityDocument(self.db) mod_sdoc.fetch() self.assertDictEqual(mod_sdoc, self.mod_sdoc)
def test_document_url(self): """ Test that the document url is populated correctly """ sdoc = SecurityDocument(self.db) self.assertEqual(sdoc.document_url, '/'.join([self.db.database_url, '_security']))
def test_constructor(self): """ Test constructing a SecurityDocument """ sdoc = SecurityDocument(self.db) self.assertIsInstance(sdoc, SecurityDocument) self.assertEqual(sdoc.r_session, self.db.r_session)
def set_security(self, secobj): """ set database securrity object """ with SecurityDocument(self.cloudant_database) as sec_doc: # context manager saves for key in sec_doc: del sec_doc[key] for k, v in secobj.items(): sec_doc[k] = v return self.get_security()
def add_user_or_role_to_db( name: str, database: CloudantDatabase, user_class="members", in_roles=False ): section = "roles" if in_roles else "names" with SecurityDocument(database) as security_document: class_dict = security_document.get(user_class, {}) names = class_dict.get(section, []) if name not in names: names.append(name) class_dict[section] = names security_document[user_class] = class_dict
def create_dbs(): dbs = cloudant_client.all_dbs() for db in CL_DBS: if db in dbs: print('Found database', db) else: db_handle = cloudant_client.create_database(db) if db_handle.exists(): print('Created database', db) # Make all dbs except for the Authentication DB readable by everyone if not db == CL_AUTHDB: print('Making {0} database readable by everyone'.format(db)) with SecurityDocument(db_handle) as security_document: security_document.update({'cloudant': {'nobody': ['_reader']}}) else: print('Problem creating database', db)