예제 #1
0
파일: test_auth.py 프로젝트: coreos/kpm
def test_get_token_from_file(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    f = open(k.tokenfile, 'w')
    f.write("{'auths': {'a': 'titib'}}")
    f.close()
    assert k.token('a') == "titib"
예제 #2
0
def test_get_token_from_file(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    f = open(k.tokenfile, 'w')
    f.write("{'auths': {'a': 'titib'}}")
    f.close()
    assert k.token('a') == "titib"
예제 #3
0
파일: test_auth.py 프로젝트: ryanj/kpm
def test_create_delete_get_token(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    k.token = "titia"
    assert k.token == "titia"
    k.delete_token()
    assert k.token is None
예제 #4
0
파일: test_auth.py 프로젝트: coreos/kpm
def test_create_token_value(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    k.add_token('a', "titic")
    k.add_token('b', "titib")
    assert k.token('a') == "titic"
    assert k.token('a') == "titic"
    assert k.token('c') is None
예제 #5
0
파일: test_auth.py 프로젝트: coreos/kpm
def test_create_token_file(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    k.add_token('a', "titib")
    assert os.path.exists(k.tokenfile) is True
    f = open(k.tokenfile, 'r')
    r = f.read()
    assert  {'auths': {'a': 'titib'}} == yaml.load(r)
예제 #6
0
def test_create_token_file(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    k.add_token('a', "titib")
    assert os.path.exists(k.tokenfile) is True
    f = open(k.tokenfile, 'r')
    r = f.read()
    assert {'auths': {'a': 'titib'}} == yaml.load(r)
예제 #7
0
파일: test_auth.py 프로젝트: ryanj/kpm
def test_create_token_file(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    k.token = "titib"
    assert os.path.exists(k.tokenfile) is True
    f = open(k.tokenfile, 'r')
    r = f.read()
    f.close()
    assert r == "titib"
예제 #8
0
파일: registry.py 프로젝트: ryanj/kpm
 def __init__(self, endpoint=DEFAULT_REGISTRY):
     if endpoint is None:
         endpoint = DEFAULT_REGISTRY
     self.endpoint = urlparse(endpoint)
     self.auth = KpmAuth()
     self._headers = {
         'Content-Type': 'application/json',
         'User-Agent': "kpmpy-cli: %s" % kpm.__version__
     }
예제 #9
0
def test_create_delete_get_token(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    k.add_token('a', "titia")
    assert k.token('a') == "titia"
    k.delete_token('a')
    assert k.token('a') is None
예제 #10
0
파일: test_auth.py 프로젝트: ryanj/kpm
def test_get_token_from_file(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    f = open(k.tokenfile, 'w')
    f.write("mytoken")
    f.close()
    assert k.token == "mytoken"
예제 #11
0
파일: registry.py 프로젝트: jonboulle/kpm
 def __init__(self, endpoint=DEFAULT_REGISTRY):
     if endpoint is None:
         endpoint = DEFAULT_REGISTRY
     self.endpoint = urlparse(endpoint)
     self.auth = KpmAuth()
     self._headers = {'Content-Type': 'application/json',
                      'User-Agent': "kpmpy-cli: %s" % kpm.__version__}
예제 #12
0
파일: test_auth.py 프로젝트: coreos/kpm
def test_create_delete_get_token(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    k.add_token('a', "titia")
    assert k.token('a') == "titia"
    k.delete_token('a')
    assert k.token('a') is None
예제 #13
0
def test_delete_token(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    k.add_token('*', "titid")
    assert k.token('*') == "titid"
    assert k.delete_token('*') == "titid"
    assert k.token('*') is None
예제 #14
0
파일: test_auth.py 프로젝트: coreos/kpm
def test_delete_token(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    k.add_token('*', "titid")
    assert k.token('*') == "titid"
    assert k.delete_token('*') == "titid"
    assert k.token('*') is None
예제 #15
0
def test_create_token_value(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    k.add_token('a', "titic")
    k.add_token('b', "titib")
    assert k.token('a') == "titic"
    assert k.token('a') == "titic"
    assert k.token('c') is None
예제 #16
0
파일: registry.py 프로젝트: jonboulle/kpm
class Registry(object):
    def __init__(self, endpoint=DEFAULT_REGISTRY):
        if endpoint is None:
            endpoint = DEFAULT_REGISTRY
        self.endpoint = urlparse(endpoint)
        self.auth = KpmAuth()
        self._headers = {'Content-Type': 'application/json',
                         'User-Agent': "kpmpy-cli: %s" % kpm.__version__}

    def _url(self, path):
        return urljoin(self.endpoint.geturl(), API_PREFIX + path)

    @property
    def headers(self):
        token = self.auth.token
        headers = {}
        headers.update(self._headers)
        if token is not None:
            headers['Authorization'] = token
        return headers

    def pull(self, name, version=None):
        if ishosted(name):
            sources = discover_sources(name)
            path = sources[0]
        else:
            organization, name = name.split("/")
            path = self._url("/packages/%s/%s/pull" % (organization, name))
        params = {"version": version}
        r = requests.get(path, params=params, headers=self.headers)
        r.raise_for_status()
        return r.content

    def list_packages(self, user=None, organization=None):
        path = "/packages"
        params = {}
        if user:
            params['username'] = user
        if organization:
            params["organization"] = organization
        r = requests.get(self._url(path), params=params, headers=self.headers)
        r.raise_for_status()
        return r.json()

    def generate(self, name, namespace=None, variables=None, version=None, tarball=False):
        path = "/packages/%s/generate" % name
        params = {}
        body = {}
        if tarball:
            params['tarball'] = 'true'
        if version:
            params['version'] = version
        if namespace:
            params['namespace'] = namespace
        if variables:
            body = {'variables':  variables}
        r = requests.get(self._url(path), data=json.dumps(body), params=params, headers=self.headers)
        r.raise_for_status()
        return r.json()

    def push(self, name, body, force=False):
        organization, pname = name.split("/")
        body['name'] = pname
        body['organization'] = organization
        body['package'] = name
        path = "/packages/%s/%s" % (organization, pname)
        r = requests.post(self._url(path),
                          params={"force": str(force).lower()},
                          data=json.dumps(body), headers=self.headers)
        r.raise_for_status()
        return r.json()

    def login(self, username, password):
        path = "/users/login"
        self.auth.delete_token()
        r = requests.post(self._url(path),
                          params={"user[username]": username,
                                  "user[password]": password},
                          headers=self.headers)
        r.raise_for_status()
        result = r.json()
        self.auth.token = result['token']
        return result

    def signup(self, username, password, password_confirmation, email):
        path = "/users"
        self.auth.delete_token()
        r = requests.post(self._url(path),
                          params={"user[username]": username,
                                  "user[password]": password,
                                  "user[password_confirmation]": password_confirmation,
                                  "user[email]": email,
                                  },
                          headers=self.headers)
        r.raise_for_status()
        result = r.json()
        self.auth.token = result['token']
        return result

    def delete_package(self, name, version=None):
        organization, name = name.split("/")
        path = "/packages/%s/%s" % (organization, name)
        params = {}
        if version:
            params['version'] = version
        r = requests.delete(self._url(path), params=params, headers=self.headers)
        r.raise_for_status()
        return True
예제 #17
0
def test_delete_empty_token(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    assert k.delete_token('*') is None
예제 #18
0
def test_get_empty_token(fake_home):
    k = KpmAuth()
    assert k.token('*') is None
    assert k.tokens is None
예제 #19
0
def test_init_token_empty(fake_home):
    k = KpmAuth()
    assert os.path.exists(k.tokenfile) is False
예제 #20
0
def test_init_create_dir(fake_home):
    KpmAuth()
    assert os.path.exists(os.path.join(str(fake_home), ".kpm"))
예제 #21
0
파일: test_auth.py 프로젝트: coreos/kpm
def test_get_empty_token(fake_home):
    k = KpmAuth()
    assert k.token('*') is None
    assert k.tokens is None
예제 #22
0
파일: test_auth.py 프로젝트: coreos/kpm
def test_delete_empty_token(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    assert k.delete_token('*') is None
예제 #23
0
파일: test_auth.py 프로젝트: ryanj/kpm
def test_delete_token(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    k.token = "titid"
    assert k.delete_token() == "titid"
    assert os.path.exists(k.tokenfile) is False
예제 #24
0
파일: test_auth.py 프로젝트: ryanj/kpm
def test_create_token_value(fake_home):
    """ Should not fail if there is no token """
    k = KpmAuth()
    k.token = "titic"
    assert k.token == "titic"
예제 #25
0
파일: registry.py 프로젝트: ryanj/kpm
class Registry(object):
    def __init__(self, endpoint=DEFAULT_REGISTRY):
        if endpoint is None:
            endpoint = DEFAULT_REGISTRY
        self.endpoint = urlparse(endpoint)
        self.auth = KpmAuth()
        self._headers = {
            'Content-Type': 'application/json',
            'User-Agent': "kpmpy-cli: %s" % kpm.__version__
        }

    def _url(self, path, prefix=API_PREFIX):
        return urljoin(self.endpoint.geturl(),
                       self.endpoint.path + prefix + path)

    @property
    def headers(self):
        token = self.auth.token
        headers = {}
        headers.update(self._headers)
        if token is not None:
            headers['Authorization'] = token
        return headers

    def version(self):
        path = "/version"
        r = requests.get(self._url(path, prefix=""), headers=self.headers)
        r.raise_for_status()
        return r.json()

    def pull(self, name, version=None):
        if ishosted(name):
            sources = discover_sources(name)
            path = sources[0]
        else:
            organization, name = name.split("/")
            path = self._url("/packages/%s/%s/pull" % (organization, name))
        params = {"version": version}
        r = requests.get(path, params=params, headers=self.headers)
        r.raise_for_status()
        return r.content

    def list_packages(self, user=None, organization=None):
        path = "/packages"
        params = {}
        if user:
            params['username'] = user
        if organization:
            params["organization"] = organization
        r = requests.get(self._url(path), params=params, headers=self.headers)
        r.raise_for_status()
        return r.json()

    def generate(self,
                 name,
                 namespace=None,
                 variables=None,
                 version=None,
                 tarball=False,
                 shards=None):
        path = "/packages/%s/generate" % name
        params = {}
        body = {}
        if tarball:
            params['tarball'] = 'true'
        if version:
            params['version'] = version
        if namespace:
            params['namespace'] = namespace
        if variables:
            body['variables'] = variables
        if shards:
            body['shards'] = shards
        r = requests.get(self._url(path),
                         data=json.dumps(body),
                         params=params,
                         headers=self.headers)
        r.raise_for_status()
        return r.json()

    def push(self, name, body, force=False):
        organization, pname = name.split("/")
        body['name'] = pname
        body['organization'] = organization
        body['package'] = name
        path = "/packages/%s/%s" % (organization, pname)
        r = requests.post(self._url(path),
                          params={"force": str(force).lower()},
                          data=json.dumps(body),
                          headers=self.headers)
        r.raise_for_status()
        return r.json()

    def login(self, username, password):
        path = "/users/login"
        self.auth.delete_token()
        r = requests.post(self._url(path),
                          data=json.dumps({
                              "user": {
                                  "username": username,
                                  "password": password
                              }
                          }),
                          headers=self.headers)
        r.raise_for_status()
        result = r.json()
        self.auth.token = result['token']
        return result

    def signup(self, username, password, password_confirmation, email):
        path = "/users"
        self.auth.delete_token()
        r = requests.post(self._url(path),
                          data=json.dumps({
                              "user": {
                                  "username": username,
                                  "password": password,
                                  "password_confirmation":
                                  password_confirmation,
                                  "email": email
                              }
                          }),
                          headers=self.headers)
        r.raise_for_status()
        result = r.json()
        self.auth.token = result['token']
        return result

    def delete_package(self, name, version=None):
        organization, name = name.split("/")
        path = "/packages/%s/%s" % (organization, name)
        params = {}
        if version:
            params['version'] = version
        r = requests.delete(self._url(path),
                            params=params,
                            headers=self.headers)
        r.raise_for_status()
        return r.json()

    def _crud_channel(self, name, channel='', action='get'):
        if channel is None:
            channel = ''
        path = "/packages/%s/channels/%s" % (name, channel)
        r = getattr(requests, action)(self._url(path),
                                      params={},
                                      headers=self.headers)
        r.raise_for_status()
        return r.json()

    def show_channels(self, name, channel=None):
        return self._crud_channel(name, channel)

    def create_channel(self, name, channel):
        return self._crud_channel(name, channel, 'post')

    def delete_channel(self, name, channel):
        return self._crud_channel(name, channel, 'delete')

    def create_channel_release(self, name, channel, release):
        path = "%s/%s" % (channel, release)
        return self._crud_channel(name, path, 'post')

    def delete_channel_release(self, name, channel, release):
        path = "%s/%s" % (channel, release)
        return self._crud_channel(name, path, 'delete')
예제 #26
0
 def _call(self):
     KpmAuth().delete_token()
     self.status = "Logout complete"