Пример #1
0
def create_item(
        password: str,
        item: str,
        template: str = None,
        locker: str = None,
        **kwargs
):
    """Create an Item in a Locker"""
    # if any env_var options are in there, calling the config
    # constructor should set the relevant environment variable
    CliConfig(**kwargs)
    store_info = set_store_config(**kwargs)
    if template == 'Empty':
        template = None
    try:
        item_inst = views.get_item(
            password=password,
            locker_name=locker,
            item_name=item,
            **kwargs
        )
        if item_inst:
            raise PhibesCliExistsError(
                f"{item} already exists in locker {store_info}\n"
                f"please use the `edit` command to modify\n"
            )
    except PhibesNotFoundError:
        pass
    template_is_file = False
    if template:
        try:
            found = views.get_item(
                password=password,
                locker_name=locker,
                item_name=template,
                **kwargs
            )
            content = found['body']
        except PhibesNotFoundError:
            try:
                # try to find a local file by that name
                content = Path(template).read_text()
                template_is_file = True
            except PhibesNotFoundError:
                raise PhibesCliNotFoundError(f"{template} not found")
            except FileNotFoundError:
                raise PhibesCliNotFoundError(f"{template} not found")
    else:
        content = ''
    if not template_is_file:
        content = user_edit_local_item(item_name=item, initial_content=content)
    return views.create_item(
        password=password,
        locker_name=locker,
        item_name=item,
        content=content,
        **kwargs
    )
Пример #2
0
 def test_get_wrong_name(self, setup_and_teardown):
     with pytest.raises(PhibesNotFoundError):
         get_item(
             password=self.password,
             locker_name=self.locker_name,
             item_name=self.missing_item_name
         )
     for locker_name in self.lockers.keys():
         with pytest.raises(PhibesNotFoundError):
             get_item(
                 password=self.password,
                 locker_name=locker_name,
                 item_name=self.missing_item_name
             )
Пример #3
0
def edit_item(password: str, item: str, locker: str = None, **kwargs):
    """Edit the contents of an Item in a Locker"""
    # if any env_var options are in there, calling the config
    # constructor should set the relevant environment variable
    CliConfig(**kwargs)
    set_store_config(**kwargs)
    try:
        item_inst = views.get_item(
            password=password, locker_name=locker, item_name=item, **kwargs
        )
        if not item_inst:
            raise PhibesNotFoundError
    except PhibesNotFoundError:
        raise PhibesCliNotFoundError(
            f"{item} does not exist in locker\n"
        )
    content = user_edit_local_item(
        item_name=item, initial_content=item_inst['body']
    )
    return views.update_item(
        password=password,
        locker_name=locker,
        item_name=item,
        content=content,
        **kwargs
    )
Пример #4
0
 def test_get_item(self, tmp_path, datadir, setup_and_teardown):
     inst = get_item(
         password=self.password,
         locker_name=self.locker_name,
         item_name=self.common_item_name
     )
     assert inst
     assert type(inst) is dict
     for fv in inst.values():
         assert self.my_locker.crypt_impl.key not in fv
     assert inst['body'] == self.content
     for locker_name, lck in self.lockers.items():
         inst = get_item(
             password=self.password,
             locker_name=self.locker_name,
             item_name=self.common_item_name
         )
         assert inst
         assert type(inst) is dict
         for fv in inst.values():
             assert lck.crypt_impl.key not in fv
         assert inst['body'] == self.content
Пример #5
0
def get_item(password: str, item: str, locker: str = None, **kwargs):
    """Get and display an Item from a Locker"""
    store_info = set_store_config(**kwargs)
    try:
        item_inst = views.get_item(
            password=password, locker_name=locker, item_name=item, **kwargs
        )
    except KeyError as err:
        raise PhibesCliError(err)
    except PhibesNotFoundError as err:
        raise PhibesCliNotFoundError(err)
    ret_val = present_item(item_inst)
    click.echo(f"{store_info}")
    click.echo(f"{ret_val}")
    return item_inst
Пример #6
0
    def test_repr(self, tmp_path, datadir, setup_and_teardown):
        # print('create a locker for each test user:'******'pw'],
                                         locker_name=user,
                                         crypt_id=crypto.default_id)
            assert type(locker) is dict, (f'{type(locker)=}')

        # print('get each locker:')
        for user, rec in self.mock_users.items():
            locker = views.get_locker(password=rec['pw'], locker_name=user)
            assert type(locker) is dict

        # print('add an item to the locker for each user')
        for user, rec in self.mock_users.items():
            item_name = 'greeting'
            item_content = f'Hello! My name is {user}'
            item = views.create_item(password=self.mock_users[user]['pw'],
                                     locker_name=user,
                                     item_name=item_name,
                                     content=item_content)
            assert type(item) is dict, (f'{type(item)=}')

        # print('retrieve the lockers and items:')
        for user, rec in self.mock_users.items():
            item_name = 'greeting'
            locker = views.get_locker(password=rec['pw'], locker_name=user)
            assert type(locker) is dict, (f'{type(item)=}')
            item = views.get_item(password=self.mock_users[user]['pw'],
                                  locker_name=user,
                                  item_name=item_name)
            assert type(item) is dict, (f'{type(item)=}')

        # print('update the items:')
        for user, rec in self.mock_users.items():
            item_name = 'greeting'
            locker = views.get_locker(password=rec['pw'], locker_name=user)
            assert type(locker) is dict
            item = views.update_item(password=self.mock_users[user]['pw'],
                                     locker_name=user,
                                     item_name=item_name,
                                     content=f'Goodbye! My name is {user}')
            assert type(item) is dict

        # print('retrieve the lockers and items:')
        for user, rec in self.mock_users.items():
            item_name = 'greeting'
            locker = views.get_locker(password=rec['pw'], locker_name=user)
            assert type(locker) is dict
            item = views.get_item(password=self.mock_users[user]['pw'],
                                  locker_name=user,
                                  item_name=item_name)
            assert type(item) is dict

        # print('retrieve the lockers and item lists:')
        for user, rec in self.mock_users.items():
            locker = views.get_locker(password=rec['pw'], locker_name=user)
            assert type(locker) is dict
            items = views.get_items(password=self.mock_users[user]['pw'],
                                    locker_name=user)
            assert type(items) is list
            for item in items:
                assert type(item) is dict

        # print('delete items:')
        for user, rec in self.mock_users.items():
            item_name = 'greeting'
            locker = views.get_locker(password=rec['pw'], locker_name=user)
            assert type(locker) is dict
            # print(f'{locker.__dict__=}')
            views.delete_item(password=self.mock_users[user]['pw'],
                              locker_name=user,
                              item_name=item_name)

        # print('delete lockers:')
        for user, rec in self.mock_users.items():
            views.delete_locker(password=rec['pw'], locker_name=user)