def validate_locker(name, pw): lock = Locker.get(locker_name=name, password=pw) assert len(lock.list_items()) == len(plain_texts) for name, pt in plain_texts.items(): it = lock.get_item(name) content = (f"{pt}\nsite:www.zombo.com\npassword: YouCanD0NEthing!") assert it.content == content return
def encrypt(name, password, filename): my_locker = Locker.get(name=name, password=password) text_file = Path(my_locker.path).joinpath(filename) with text_file.open("r") as tf: content = tf.read() print(f"{content}") my_locker.encrypt(content) return
def decrypt(name, password, filename): my_locker = Locker.get(name=name, password=password) crypt_file = Path(my_locker.path).joinpath(filename) with crypt_file.open("r") as cf: cipher_text = cf.read() print(f"{cipher_text}") my_locker.decrypt(cipher_text) return
def prep_and_run(self, arg_dict): self.my_locker = Locker.create(password=self.password, crypt_id=arg_dict['crypt_id']) new_item = self.my_locker.create_item(item_name=self.test_item_name) new_item.content = self.test_content self.my_locker.add_item(item=new_item) # change the configured working path to the test directory update_config_option_default(self.target_cmd, self.test_path) return self.invoke(arg_dict=arg_dict)
def create_locker( password: str, crypt_id: str, locker_name: str = None, **kwargs ): return Locker.create( password=password, crypt_id=crypt_id, locker_name=locker_name ).to_dict()
def test_delete_locker_main(self, setup_and_teardown, crypt_id): inst = Locker.get(password=self.pw, locker_name=self.locker_name) assert inst result = CliRunner().invoke( cli=self.target_cmd, args=[ "--config", self.test_path, "--locker", self.locker_name, "--password", self.pw ], input="y\n" ) assert result.exit_code == 0, ( f'{result.exception=}\n' f'{result.output=}\n' ) with pytest.raises(PhibesNotFoundError): Locker.get(password=self.pw, locker_name=self.locker_name) return
def update_item( password: str, locker_name: str, item_name: str, content: str, **kwargs ): locker = Locker.get(password=password, locker_name=locker_name) item = locker.get_item(item_name) item.content = content locker.update_item(item) return get_item( password=password, locker_name=locker_name, item_name=item_name )
def test_success(self, crypt_id, setup_and_teardown): result = self.prep_and_run({'crypt_id': str(crypt_id)}) assert result assert result.exit_code == 0, (f"{crypt_id=}\n" f"{result.exception=}\n" f"{result.output=}\n") crypt = crypt_choices.choice_dict[crypt_id] assert f'Crypt ID {crypt}' in result.output inst = Locker.get(password=self.password, locker_name=None) assert (inst.data_model.storage.locker_file == self.test_path / LOCKER_FILE)
def custom_setup(self, tmp_path): super(EmptyLocker, self).custom_setup(tmp_path) for name in self.lockers: try: Locker.delete(name, self.password) except PhibesNotFoundError: pass self.lockers = {} try: Locker.delete(self.locker_name, self.password) except PhibesNotFoundError: pass finally: self.my_locker = Locker.create( password=self.password, crypt_id=crypto.default_id, locker_name=self.locker_name ) # create a locker for each registered crypt instance for crypt_id in crypto.list_crypts(): # dedupe the names with random numbers wart = str(random.randint(1000, 9999)) # but make sure there isn't a freak collision while self.locker_name + str(wart) in self.lockers: wart = str(random.randint(1000, 9999)) locker_name = self.locker_name + wart self.lockers[locker_name] = Locker.create( password=self.password, crypt_id=crypt_id, locker_name=locker_name ) return
def prep_and_run(self, arg_dict): self.my_locker = Locker.create( password=self.password, crypt_id=arg_dict['crypt_id'], locker_name=None ) # change the configured working path to the test directory update_config_option_default(self.target_cmd, self.test_path) arg_list = [ "--path", arg_dict.get('path', self.test_path), "--password", arg_dict.get('password', self.password) ] return CliRunner().invoke(cli=self.target_cmd, args=arg_list)
def test_found(self, crypt_id, setup_and_teardown): result = self.prep_and_run({'crypt_id': crypt_id}) assert result assert result.exit_code == 0, ( f"{crypt_id=}\n" f"{result.exception=}\n" f"{result.output=}\n" ) assert f'Crypt ID {crypt_id}' in result.output inst = Locker.get(password=self.password, locker_name=None) assert ( inst.data_model.storage.locker_file == self.test_path / LOCKER_FILE )
def test_found(self, crypt_id, config_arg, setup_and_teardown): all_lockers = list(self.lockers.keys()) + [self.locker_name] for lck in all_lockers: result = self.prep_and_run(config_arg, {'name': lck}) assert result assert result.exit_code == 0, ( f"{config_arg=}\n" f"{result.exception=}\n" f"{result.output=}\n" ) inst = Locker.get(password=self.password, locker_name=lck) assert ( inst.data_model.storage.locker_file == self.test_path / inst.locker_id / LOCKER_FILE ) # alert if tests are messing up actual user home dir assert not Path.home().joinpath(self.locker_name).exists() assert not Path.home().joinpath(inst.locker_id).exists()
def test_success(self, crypt_id, tmp_path, setup_and_teardown): self.my_locker = Locker.create(password=self.password, crypt_id=crypt_id) new_item = self.my_locker.create_item(item_name=self.test_item_name) new_item.content = self.start_content self.my_locker.add_item(item=new_item) conf = CliConfig() conf.store = { 'store_type': conf.store['store_type'], 'store_path': tmp_path } conf.editor = f'echo {self.edit_content}> ' write_config_file(tmp_path, update=True) load_config_file(tmp_path) # change the configured working path to the test directory update_config_option_default(self.target_cmd, self.test_path) result = self.prep_and_run({'crypt_id': crypt_id}) assert result assert result.exit_code == 0, (f"{crypt_id=}\n" f"{result.exception=}\n" f"{result.output=}\n") assert self.start_content not in result.output inst = self.my_locker.get_item(self.test_item_name) assert self.edit_content == inst.content.strip()
def custom_teardown(self, tmp_path): super(TestCreateLocker, self).custom_teardown(tmp_path) try: Locker.delete(self.locker_name, self.pw) except PhibesNotFoundError: pass
def delete_locker(password: str, locker_name: str, **kwargs): return Locker.delete(password=password, locker_name=locker_name)
def get_item(password: str, locker_name: str, item_name: str, **kwargs): locker = Locker.get(password=password, locker_name=locker_name) return locker.get_item(item_name=item_name).as_dict()
def get_items(password: str, locker_name: str, **kwargs): locker = Locker.get(password=password, locker_name=locker_name) return [item.as_dict() for item in locker.list_items()]
def delete_item( password: str, locker_name: str, item_name: str, **kwargs ): locker = Locker.get(password=password, locker_name=locker_name) return locker.delete_item(item_name=item_name)
def test_good_auth(self, setup_and_teardown): # auth is 'built in' to getting a crypt for existing locker assert Locker.get(locker_name=self.locker_name, password=self.password)
def test_fail_auth(self, tmp_path, setup_and_teardown): wrong_pw = "ThisWillNotBeIt" with pytest.raises(PhibesAuthError): Locker.get(password=wrong_pw, locker_name=self.locker_name)
def test_create_without_arg(self, crypt_id, setup_and_teardown): Locker.create(password=self.password, crypt_id=crypt_id) found = Locker.get(password=self.password, locker_name=None) assert found
def custom_teardown(self, tmp_path): super(TestDeleteLocker, self).custom_teardown(tmp_path) try: Locker.delete(password=self.pw, locker_name=self.locker_name) except PhibesNotFoundError: pass
def test_good(self, tmp_path): for name in self.lockers.keys(): inst = Locker.get(password=self.password, locker_name=name) assert inst
def get_locker(password: str, locker_name: str, **kwargs): return Locker.get(password=password, locker_name=locker_name).to_dict()