# Path: skipper/creds.py
# class Creds(NestedDict):
#     """
#     Dictonary to store various credential details (e.g. secret keys,
#     private keys, etc)
#     """
#     def __init__(self, storage, *args, **kwargs):
#         super(Creds, self).__init__(*args, **kwargs)
#         self.storage = storage
#         self.update(self.storage.retrieve())
# 
#     def __setitem__(self, *args, **kwargs):
#         super(Creds, self).__setitem__(*args, **kwargs)
#         self.storage.save(self)
# 
#     def __delitem__(self, *args, **kwargs):
#         super(Creds, self).__delitem__(*args, **kwargs)
#         self.storage.save(self)
# 
#     def save(self):
#         self.storage.save(self)
# 
# class JSONStorage(object):
#     """
#     Stores a creds in a json format in a .skippercfg file.
#     """
#     def __init__(self, path=None):
#         if path:
#             self.path = path
#         else:
#             self.path = os.getcwd()
# 
#     def retrieve(self):
#         try:
#             with open('%s/.skippercfg' % self.path, 'r') as f:
#                 try:
#                     return json.load(f)
#                 except ValueError:
#                     return {}
#         except IOError:
#             return {}
# 
#     def save(self, creds):
#         with open('%s/.skippercfg' % self.path, 'w+') as f:
#             f.write(json.dumps(creds, indent=2))


import pytest
import mock
from skipper.creds import Creds, JSONStorage
    del creds['foo']
    assert creds == {}
    creds.storage.save.assert_called_with({})


def test_nested_creds(creds):
    assert creds['foo']['bar'] == {}
    creds['foo']['bar'] = 'buzz'
    assert creds['foo']['bar'] == 'buzz'
    creds.storage.save.assert_called_once_with({
        'foo': {
            'bar': 'buzz'
        }})


def test_config_retrieve():
    storage = mock.MagicMock()
    existing = {
        'foo': 'bar',
        'foo2': {
            'bar2': 'buzz'
        }
    }
    storage.retrieve.return_value = existing
    creds = Creds(storage=storage)
    assert creds == existing
    assert storage.retrieve.called


def test_storage_retrieve(tmpdir):

# Path: skipper/creds.py
# class Creds(NestedDict):
#     """
#     Dictonary to store various credential details (e.g. secret keys,
#     private keys, etc)
#     """
#     def __init__(self, storage, *args, **kwargs):
#         super(Creds, self).__init__(*args, **kwargs)
#         self.storage = storage
#         self.update(self.storage.retrieve())
# 
#     def __setitem__(self, *args, **kwargs):
#         super(Creds, self).__setitem__(*args, **kwargs)
#         self.storage.save(self)
# 
#     def __delitem__(self, *args, **kwargs):
#         super(Creds, self).__delitem__(*args, **kwargs)
#         self.storage.save(self)
# 
#     def save(self):
#         self.storage.save(self)
# 
# class JSONStorage(object):
#     """
#     Stores a creds in a json format in a .skippercfg file.
#     """
#     def __init__(self, path=None):
#         if path:
#             self.path = path
#         else:
#             self.path = os.getcwd()
# 
#     def retrieve(self):
#         try:
#             with open('%s/.skippercfg' % self.path, 'r') as f:
#                 try:
#                     return json.load(f)
#                 except ValueError:
#                     return {}
#         except IOError:
#             return {}
# 
#     def save(self, creds):
#         with open('%s/.skippercfg' % self.path, 'w+') as f:
#             f.write(json.dumps(creds, indent=2))


# Path: tests/test_creds.py
import pytest
import mock
from skipper.creds import Creds, JSONStorage

    del creds['foo']
    assert creds == {}
    creds.storage.save.assert_called_with({})


def test_nested_creds(creds):
    assert creds['foo']['bar'] == {}
    creds['foo']['bar'] = 'buzz'
    assert creds['foo']['bar'] == 'buzz'
    creds.storage.save.assert_called_once_with({
        'foo': {
            'bar': 'buzz'
        }})


def test_config_retrieve():
    storage = mock.MagicMock()
    existing = {
        'foo': 'bar',
        'foo2': {
            'bar2': 'buzz'
        }
    }
    storage.retrieve.return_value = existing
    creds = Creds(storage=storage)
    assert creds == existing
    assert storage.retrieve.called


def test_storage_retrieve(tmpdir):

    storage = JSONStorage(path=tmpdir)