Exemple #1
0
    def get_quota_used(self):
        if self.url is None:
            return [None, None]

        url = "%s/%s/%s/info/quota" % (self.url, self.version, self.username)
        status, headers, body = get_url(url, user=self.username,
                                        password=self.password,
                                        timeout=self.timeout)
        if status is not 200:
            return [None, None]

        result = json.loads(body)
        if result[1] is 'null':
            result[1] = None
        return result
    def _proxy(self, method, url, data=None, headers=None):
        """Proxies and return the result from the other server.

        - scheme: http or https
        - netloc: proxy location
        """
        if data is not None:
            data = json.dumps(data)

        status, headers, body = get_url(url, method, data, headers)
        if body:
            try:
                body = json.loads(body)
            except Exception:
                self.logger.error("bad json body from sreg (%s): %s" %
                                  (url, body))
        return status, body
Exemple #3
0
    def _proxy(self, method, url, data=None, headers=None):
        """Proxies and return the result from the other server.

        - scheme: http or https
        - netloc: proxy location
        """
        if data is not None:
            data = json.dumps(data)

        status, headers, body = get_url(url, method, data, headers)
        if body:
            try:
                body = json.loads(body)
            except Exception:
                logger.error("bad json body from sreg (%s): %s" %
                                                        (url, body))
        return status, body
Exemple #4
0
    def authenticate_user(self, user, credentials, attrs=None):
        password = credentials.get("password")
        if not password:
            return None

        username = user.get("username")
        if username is None:
            return None

        code, headers, body = get_url(self.whoami_uri,
                                      "GET",
                                      user=username,
                                      password=password)
        if code == 401:
            return None
        if code != 200:
            logger = CLIENT_HOLDER.default_client
            logger.error("whoami API unexpected behaviour")
            logger.error("  code: %r", code)
            logger.error("  headers: %r", headers)
            logger.error("  body: %r", body)
            raise BackendError("whoami API unexpected behaviour")

        try:
            user_data = json.loads(body)
        except ValueError:
            logger = CLIENT_HOLDER.default_client
            logger.error("whoami API produced invalid JSON")
            logger.error("  code: %r", code)
            logger.error("  headers: %r", headers)
            logger.error("  body: %r", body)
            raise BackendError("whoami API produced invalid JSON")

        user.update({
            "userid": user_data["userid"],
            "username": username,
            "syncNode": user_data.get("syncNode", ""),
        })
        return user["userid"]
Exemple #5
0
    def delete_data(self, collection=None, id=None):
        if self.url is None:
            return True

        url = "%s/%s/%s/storage" % (self.url, self.version, self.username)
        if collection:
            url += "/%s" % collection
            if id:
                url += "/%s" % id

        extra = {"X-Confirm-Delete": "true"}

        status, headers, body = get_url(url, method='DELETE',
                                        user=self.username,
                                        password=self.password,
                                        timeout=self.timeout,
                                        extra_headers=extra)

        if status not in (200, 404):
            logging.error("delete_data failure: %s (url: %s) %s"
                           % (status, url, body))
            return False

        return True
    def test_get_url(self):

        # malformed url
        self.assertRaises(ValueError, get_url, 'impossible url')

        # unknown location
        code, headers, body = get_url('http://dwqkndwqpihqdw.com',
                                      get_body=False)
        self.assertEquals(code, 502)
        self.assertTrue('Name or service not known' in body)

        # any page
        code, headers, body = get_url('http://google.com', get_body=False)
        self.assertEquals(code, 200)
        self.assertEquals(body, '')

        # page with auth failure
        code, headers, body = get_url('http://badauth',
                                      user='******',
                                      password='******')
        self.assertEquals(code, 401)

        # page with right auth
        code, headers, body = get_url('http://goodauth',
                                      user='******',
                                      password='******')
        self.assertEquals(code, 200)
        self.assertEquals(body, '{}')

        # page that times out
        code, headers, body = get_url('http://timeout', timeout=0.1)
        self.assertEquals(code, 504)

        # page that fails
        code, headers, body = get_url('http://error', get_body=False)
        self.assertEquals(code, 500)
    def test_get_url(self):

        # malformed url
        self.assertRaises(ValueError, get_url, 'impossible url')

        # unknown location
        code, headers, body = get_url('http://dwqkndwqpihqdw.com',
                                      get_body=False)
        self.assertEquals(code, 502)
        self.assertTrue('Name or service not known' in body)

        # any page
        code, headers, body = get_url('http://google.com', get_body=False)
        self.assertEquals(code, 200)
        self.assertEquals(body, '')

        # page with auth failure
        code, headers, body = get_url('http://badauth',
                                      user='******',
                                      password='******')
        self.assertEquals(code, 401)

        # page with right auth
        code, headers, body = get_url('http://goodauth',
                                      user='******',
                                      password='******')
        self.assertEquals(code, 200)
        self.assertEquals(body, '{}')

        # page that times out
        code, headers, body = get_url('http://timeout', timeout=0.1)
        self.assertEquals(code, 504)

        # page that fails
        code, headers, body = get_url('http://error', get_body=False)
        self.assertEquals(code, 500)
Exemple #8
0
    reg_url = args[1].rstrip("/") + "/user/1.0"
    print "TESTING USER API AT", reg_url
    if len(args) == 2:
        node_name = "weave"
        node_url = reg_url
    else:
        node_name = "sync"
        node_url = args[2].rstrip("/") + "/1.0"
        print "TESTING NODE API AT", node_url

    reg_url = reg_url + "/" + username
    node_url = node_url + "/" + username + "/node/" + node_name

    # Delete the account if it already exists.
    print "CHECKING PRE-EXISTING ACCOUNT"
    status, headers, body = get_url(reg_url)
    assertEquals(status, 200)
    assertOneOf(body, ("1", "0"))
    if body == "1":
        print "DELETING PRE-EXISTING ACCOUNT"
        status, headers, body = get_url(reg_url, "DELETE", **creds)
        assertEquals(status, 200)

    # Create the account.
    print "CREATING THE ACCOUNT"
    user_data = {
        "email": opts.username,
        "password": opts.password,
    }
    status, headers, body = get_url(reg_url, "PUT", json.dumps(user_data))
    if status == 400: