def get_request_data(cls, request):
        """Extract common data from the current request

        request: A `pylons.request` object
        """
        data = {}
        cookies = request.cookies

        try:
            session_str = unquote(cookies.get("reddit_session", ""))
            user_id = int(session_str.split(",")[0])
        except ValueError:
            user_id = None

        if user_id:
            data["user_id"] = user_id
        else:
            data["loid"] = cookies.get("loid", None)
            data["loid_created"] = cookies.get("loidcreated", None)

        data["domain"] = request.host
        data["user_agent"] = request.user_agent
        data["user_agent_parsed"] = useragent.parse(request.user_agent)

        http_referrer = request.headers.get("Referer", None)
        if http_referrer:
            data["referrer_url"] = http_referrer
            data["referrer_domain"] = _get_domain(http_referrer)

        return data
Пример #2
0
    def get_request_data(cls, request):
        """Extract common data from the current request

        request: A `pylons.request` object
        """
        data = {}
        cookies = request.cookies

        try:
            session_str = unquote(cookies.get("reddit_session", ""))
            user_id = int(session_str.split(",")[0])
        except ValueError:
            user_id = None

        if user_id:
            data["user_id"] = user_id
        else:
            data["loid"] = cookies.get("loid", None)
            data["loid_created"] = cookies.get("loidcreated", None)

        data["domain"] = request.host
        data["user_agent"] = request.user_agent
        data["user_agent_parsed"] = useragent.parse(request.user_agent)

        http_referrer = request.headers.get("Referer", None)
        if http_referrer:
            data["referrer_url"] = http_referrer
            data["referrer_domain"] = _get_domain(http_referrer)

        return data
    def test_google_crawler(self):
        google_bot = (
            "Mozilla/5.0 ("
            "compatible; Googlebot/2.1;"
            " +http://www.google.com/bot.html)"
        )

        parsed = useragent.parse(google_bot)

        self.assertTrue(parsed["bot"])

        self.assertEqual(parsed["browser_name"], "GoogleBot")
        self.assertEqual(parsed["browser_version"], "2.1")

        self.assertEqual(parsed["platform_name"], None)
        self.assertEqual(parsed["platform_version"], None)
    def test_user_agent_firefox_windows(self):
        firefox_osx = (
            "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0)"
            " Gecko/20100101 Firefox/40.1"
        )

        parsed = useragent.parse(firefox_osx)

        self.assertFalse(parsed["bot"])

        self.assertEqual(parsed["browser_name"], "Firefox")
        self.assertEqual(parsed["browser_version"], "40.1")

        self.assertEqual(parsed["os_name"], "Windows")

        self.assertEqual(parsed["platform_name"], "Windows")
        self.assertEqual(parsed["platform_version"], "7")
    def test_user_agent_firefox_osx(self):
        firefox_osx = (
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10; rv:33.0)"
            " Gecko/20100101 Firefox/33.0"
        )

        parsed = useragent.parse(firefox_osx)

        self.assertFalse(parsed["bot"])

        self.assertEqual(parsed["browser_name"], "Firefox")
        self.assertEqual(parsed["browser_version"], "33.0")

        self.assertEqual(parsed["os_name"], "Macintosh")

        self.assertEqual(parsed["platform_name"], "Mac OS")
        self.assertEqual(parsed["platform_version"], "X 10.10")
    def test_user_agent_chrome_osx(self):
        chrome_osx = (
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5)"
            " AppleWebKit/537.36 (KHTML, like Gecko)"
            " Chrome/50.0.2661.102 Safari/537.36"
        )

        parsed = useragent.parse(chrome_osx)

        self.assertFalse(parsed["bot"])

        self.assertEqual(parsed["browser_name"], "Chrome")
        self.assertEqual(parsed["browser_version"], "50.0.2661.102")

        self.assertEqual(parsed["os_name"], "Macintosh")

        self.assertEqual(parsed["platform_name"], "Mac OS")
        self.assertEqual(parsed["platform_version"], "X 10.9.5")
    def test_user_agent_ios_safari(self):
        safari_ios = (
            "Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X)"
            " AppleWebKit/537.51.1 (KHTML, like Gecko)"
            " Version/7.0 Mobile/11A465 Safari/9537.53"
        )

        parsed = useragent.parse(safari_ios)

        self.assertFalse(parsed["bot"])

        self.assertEqual(parsed["browser_name"], "Safari")
        self.assertEqual(parsed["browser_version"], "7.0")

        self.assertEqual(parsed["os_name"], "iOS")

        self.assertEqual(parsed["platform_name"], "iOS")
        self.assertEqual(parsed["platform_version"], "7.0")
        self.assertEqual(parsed["sub-platform"], "IPad")
    useragent,
)

IP_V4 = "192.168.1.1"
IP_V6 = "2001:0db8:0a0b:12f0:0000:0000:0000:0001"
USER_ID = 99999
REDDIT_SESSION = "%d,2016-06-15T11:17:12,abcd12345" % USER_ID
LOID = "FlFS2kMsdssc1Cyen"
LOID_CREATED = "2016-06-14T19:20:59.072Z"
USER_AGENT = (
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5)"
    " AppleWebKit/537.36 (KHTML, like Gecko)"
    " Chrome/50.0.2661.102 Safari/537.36"
)

USER_AGENT_PARSED = useragent.parse(USER_AGENT)


class EventQueueTests(unittest.TestCase):
    @patch_service("events.EventQueue.__init__")
    def setUp(self, __init__):
        # ensure `MessageQueues` aren't created since POSIX queues
        # don't play nice with the testing environment.
        __init__.return_value = None

        self.queue = events.EventQueue("test")

    def test_put(self):
        mock_event = mock.MagicMock(autospec=events.Event)

        with patch_service("events.baseplate.events.EventQueue.put") as put: