def test_change_book(): """Test that book can change.""" mock_view = MagicMock() mock_ctrl = MagicMock() # Have to return view so that we can exit mock_ctrl.return_value.basic_output.side_effect = [ { 'action': 'list', 'item': 'note', 'book': 'mock_book', }, { 'action': 'view', 'item': 'note', } ] mock_functions = { 'view': mock_view, 'list': None, } with patch.dict('foolscap.actor.FUNCTION_MAP', mock_functions),\ patch('foolscap.actor.Controller', mock_ctrl): actor.action('list', None) mock_ctrl.has_calls(calls=[call('general'), call('mock_book')]) mock_ctrl.return_value.basic_output.assert_called()
def test_list_remove(self): key = "my_awesome_key" mock_file = MagicMock() mock_file.__str__ = lambda x: key mock_file.exists.return_value = True mock_path = MagicMock(return_value=mock_file) mock_hashlib = MagicMock() mock_hashlib.md5.hexdigest.return_value = "HASH" mock_user = MagicMock() mock_user.path = "path" mock_user.name = "test" with patch.multiple('models.lists.keys', Path=mock_path, hashlib=mock_hashlib): keys = ListKeys(mock_user) keys.remove(key) mock_path.has_calls([ call("path", 'keydir', 'HASH'), call(mock_file, "test.pub"), ]) commit_message = "Removed key for user test" mock_user.git.commit.has_calls( [call(["my_awesome_key"], commit_message, action='remove')])
def test_list_remove(self): key = "my_awesome_key" mock_file = MagicMock() mock_file.__str__ = lambda x: key mock_file.exists.return_value = True mock_path = MagicMock(return_value=mock_file) mock_hashlib = MagicMock() mock_hashlib.md5.hexdigest.return_value = "HASH" mock_user = MagicMock() mock_user.path = "path" mock_user.name = "test" with patch.multiple('models.lists.keys', Path=mock_path, hashlib=mock_hashlib): keys = ListKeys(mock_user) keys.remove(key) mock_path.has_calls([ call("path", 'keydir', 'HASH'), call(mock_file, "test.pub"), ]) commit_message = "Removed key for user test" mock_user.git.commit.has_calls([call(["my_awesome_key"], commit_message, action='remove')])
def test_if_we_commit_after_a_key_append(self): key_path = "tests/fixtures/simple_key.pub" mock_file = MagicMock() mock_file.__str__ = lambda x: key_path mock_path = MagicMock(return_value=mock_file) mock_hashlib = MagicMock() mock_hashlib.md5.hexdigest.return_value = "HASH" mock_user = MagicMock() mock_user.path = "path" mock_user.name = "test" with patch.multiple('models.lists.keys', Path=mock_path, hashlib=mock_hashlib): keys = ListKeys(mock_user) keys.append(key_path) mock_path.has_calls([ call("path", key_path), call("path", "keydir", "HASH"), call(mock_file, "test"), ]) eq_(mock_file.isfile.call_count, 1) eq_(mock_file.mkdir.call_count, 1) mock_file.write_file.assert_called_once_with('nothing to see here\n')
def test_stage(self): mocked_repo = MagicMock() mocked_sanitize = MagicMock() mocked_queue = MagicMock() mocked_files = MagicMock(return_value=None) mocked_sanitize.return_value = ["to-stage"] current = CurrentView( repo=mocked_repo, repo_path="repo_path", queue=mocked_queue, ignore=CachedIgnore(), ) current._sanitize = mocked_sanitize current._get_files_from_path = mocked_files current._stage("message", ["add"], ["remove"]) mocked_queue.commit.assert_called_once_with(add=["to-stage"], remove=["to-stage"], message="message") mocked_repo.index.add.assert_called_once_with(["to-stage"]) mocked_repo.index.remove.assert_called_once_with(["to-stage"]) mocked_files.has_calls([call(["add"])]) mocked_sanitize.has_calls([call(["add"]), call(["remove"])])
def test_get_transaction_from_token(self): transaction = TransactionFactory() mocked_view = MagicMock() token = _get_jwt_token(transaction) self.assertEquals(get_transaction_from_token(mocked_view)(None, token), mocked_view()) mocked_view.has_calls([call(None, transaction, False), call()])
def test_get_transaction_from_token(self): transaction = TransactionFactory() mocked_view = MagicMock() token = _get_jwt_token(transaction) self.assertEqual(get_transaction_from_token(mocked_view)(None, token), mocked_view()) mocked_view.has_calls([call(None, transaction, False), call()])
def test_get_blob_size(self): mocked_repo = MagicMock() mocked_git_object = MagicMock() mocked_git_object().size = 42 repo = Repository(mocked_repo) repo.get_git_object = mocked_git_object assert repo.get_blob_size("tree", "path") == 42 mocked_git_object.has_calls([call("tree", "path")])
def test_get_blob_data(self): mocked_repo = MagicMock() mocked_git_object = MagicMock() mocked_git_object().data = "some data" repo = Repository(mocked_repo) repo.get_git_object = mocked_git_object assert repo.get_blob_data("tree", "path") == "some data" mocked_git_object.has_calls([call("tree", "path")])
def test_list_addition(self): mock_user = MagicMock() mock_append = MagicMock() keys = ListKeys(mock_user) keys.append = mock_append keys = keys + ["first_key", "second_key"] mock_append.has_calls([call("first_key"), call("second_key")])
def test_truncate(self): mocked_open = MagicMock() mocked_file = MagicMock(spec=file) with patch('gitfs.views.passthrough.open', create=True) as mocked_open: mocked_open().__enter__.return_value = mocked_file view = PassthroughView(repo=self.repo, repo_path=self.repo_path) view.truncate("/magic/path", 0, 0) mocked_open.has_calls([call("/the/root/path/magic/path", "r+")]) mocked_file.truncate.assert_called_once_with(0)
def test_truncate(self): mocked_open = MagicMock() mocked_file = MagicMock(spec=TextIOWrapper) with patch('gitfs.views.passthrough.open', create=True) as mocked_open: mocked_open().__enter__.return_value = mocked_file view = PassthroughView(repo=self.repo, repo_path=self.repo_path) view.truncate("/magic/path", 0, 0) mocked_open.has_calls([call("/the/root/path/magic/path", "r+")]) mocked_file.truncate.assert_called_once_with(0)
def test_get_transaction_from_expired_token(self): transaction = TransactionFactory() mocked_view = MagicMock() with patch('silver.utils.payments.datetime') as mocked_datetime: mocked_datetime.utcnow.return_value = datetime.utcnow() - timedelta(days=2 * 365) token = _get_jwt_token(transaction) self.assertEquals(get_transaction_from_token(mocked_view)(None, token), mocked_view()) mocked_view.has_calls([call(None, transaction, True), call()])
def test_get_transaction_from_expired_token(self): transaction = TransactionFactory() mocked_view = MagicMock() with patch('silver.utils.payments.datetime') as mocked_datetime: mocked_datetime.utcnow.return_value = datetime.utcnow() - timedelta(days=2 * 365) token = _get_jwt_token(transaction) self.assertEqual(get_transaction_from_token(mocked_view)(None, token), mocked_view()) mocked_view.has_calls([call(None, transaction, True), call()])
def test_rename(self): mocked_rename = MagicMock() mocked_rename.return_value = "magic" with patch('gitfs.views.passthrough.os.rename', mocked_rename): view = PassthroughView(repo=self.repo, repo_path=self.repo_path) result = view.rename("/magic/path", "/magic/new") assert result == "magic" old = '/the/root/path/magic/path' new = '/the/root/path/magic/new' mocked_rename.has_calls([call(old), call(new)])
def test_retry(self): mocked_time = MagicMock() mocked_method = MagicMock(side_effect=ValueError) with patch.multiple('gitfs.utils.decorators.retry', wraps=MockedWraps, time=mocked_time): again = retry(times=3) with pytest.raises(ValueError): again(mocked_method)("arg", kwarg="kwarg") mocked_time.sleep.has_calls([call(3), call(3), call(1)]) mocked_method.has_calls([call("arg", kwarg="kwarg")])
def test_list_addition(self): mock_user = MagicMock() mock_append = MagicMock() keys = ListKeys(mock_user) keys.append = mock_append keys = keys + ['first_key', 'second_key'] mock_append.has_calls([ call('first_key'), call('second_key'), ])
def test_stage(self): mocked_repo = MagicMock() mocked_sanitize = MagicMock() mocked_queue = MagicMock() mocked_sanitize.return_value = ["to-stage"] current = CurrentView(repo=mocked_repo, repo_path="repo_path", queue=mocked_queue, ignore=CachedIgnore("f")) current._sanitize = mocked_sanitize current._stage("message", ["add"], ["remove"]) mocked_queue.commit.assert_called_once_with(add=['to-stage'], remove=['to-stage'], message="message") mocked_repo.index.add.assert_called_once_with(["to-stage"]) mocked_repo.index.remove.assert_called_once_with(["to-stage"]) mocked_sanitize.has_calls([call(['add']), call(['remove'])])