Пример #1
0
    def test_404(self):

        httpretty.register_uri(
            'GET',
            EXAMPLE_WEBFINGER_URL,
            status=404,
            headers={
                'Content-Type': 'text/plain',
            },
            body="never heard of them",
        )

        webfinger = get_webfinger(
            EXAMPLE_USERNAME,
            EXAMPLE_HOSTNAME,
        )

        self.assertEqual(
            webfinger.url,
            None,
        )
Пример #2
0
    def test_simple(self):

        httpretty.register_uri(
            'GET',
            EXAMPLE_WEBFINGER_URL,
            status=200,
            headers={
                'Content-Type': 'application/jrd+json',
            },
            body=EXAMPLE_WEBFINGER_RESULT,
        )

        webfinger = get_webfinger(
            EXAMPLE_USERNAME,
            EXAMPLE_HOSTNAME,
        )

        self.assertEqual(
            webfinger.url,
            EXAMPLE_USER_URL,
        )
Пример #3
0
    def test_410(self):

        httpretty.register_uri(
            'GET',
            EXAMPLE_WEBFINGER_URL,
            status=410,
            headers={
                'Content-Type': 'text/plain',
            },
            body="this bird has flown",
        )

        webfinger = get_webfinger(
            EXAMPLE_USERNAME,
            EXAMPLE_HOSTNAME,
        )

        self.assertEqual(
            webfinger.url,
            None,
        )
Пример #4
0
    def test_no_such_host(self):
        def no_such_host(request, uri, headers):
            raise requests.ConnectionError()

        httpretty.register_uri(
            'GET',
            EXAMPLE_WEBFINGER_URL,
            status=200,
            headers={
                'Content-Type': 'application/jrd+json',
            },
            body=no_such_host,
        )

        with suppress_thread_exceptions():
            webfinger = get_webfinger(
                EXAMPLE_USERNAME,
                EXAMPLE_HOSTNAME,
            )

        self.assertEqual(
            webfinger.url,
            None,
        )
Пример #5
0
def _fetch_remote(address, wanted):

    # Do we already know about them?

    if wanted['is_atstyle']:
        # XXX Not certain about this (or indeed the benefit
        # of storing "acct" in the Person object). Shouldn't we ask
        # the webfinger module whether it knows them?
        kwargs = {"acct": address}
    else:
        kwargs = {"remote_url": address}

        try:
            failure = sombrero_models.Failure.objects.get(
                    url = address,
                    )
            logger.debug("%s: %s",
                    address, failure)

            return None
        except sombrero_models.Failure.DoesNotExist:
            # all good then
            pass

    try:
        result = wanted['type'].remote_form().objects.get(
                **kwargs,
                )

        logger.debug("%s: already known: %s",
                address, result)

        return result

    except AttributeError:
        # Types don't have to support object lookup
        pass

    except wanted['type'].DoesNotExist:
        pass

    # No, so create them (but don't save yet).

    logger.debug("%s: wanted %s; kwargs=%s",
            address, wanted, kwargs)

    if wanted['is_atstyle']:

        webfinger = get_webfinger(
                username = wanted['username'],
                hostname = wanted['hostname'],
                )

        if webfinger.url is None:
            logger.info("%s: webfinger lookup failed; bailing",
                    address)
            return None

        logger.info("%s: webfinger gave us %s",
                address, webfinger.url)
        address = webfinger.url

    # okay, time to go looking online

    try:
        response = requests.get(
                address,
                headers = {
                    'Accept': 'application/activity+json',
                    },
                )
    except requests.ConnectionError:

        logger.info("%s: can't reach host",
            address)

        sombrero_models.Failure(
                url = address,
                status = 0,
                ).save()

        return None

    except requests.exceptions.Timeout:

        logger.info("%s: timeout reaching host",
            address)

        sombrero_models.Failure(
                url = address,
                status = 0,
                ).save()

        return None

    # so, we have *something*...

    if response.status_code!=200:
        # HTTP error; bail immediately

        logger.info("%s: unexpected status code from status lookup: %d",
                address, response.status_code,
                )

        sombrero_models.Failure(
                url = address,
                status = response.status_code,
                ).save()

        return None

    try:
        found = response.json()
    except ValueError as ve:
        logger.info("%s: response was not JSON (%s); dropping",
                address, ve)

        # Not actually an HTTP failure, so don't create a Failure here

        return None

    log_one_message(
            direction = "retrieved",
            body = found,
            )

    if 'type' not in found:
        logger.info("%s: retrieved JSON did not include a type; dropping",
                address)

        return None

    if 'id' in found:
        if found['id'] != address:
            logger.info(
                    "%s: user's id was not the source url: got %s; dropping",
                    address, found['id'],
                    )
            return None

    result = bowler_create.deserialise(
            found,
            address,
            )

    if result is None:
        logger.info("%s:    -- can't deserialise; returning None",
                address)
        return None

    logger.info("%s:    -- deserialised as %s",
            address, result)

    if not isinstance(result, wanted['type']):
        logger.info("%s:      -- which wasn't %s; returning None",
                address, wanted['type'])

        return None

    return result