Source

trustlink / test.py

Full commit
#!/usr/bin/env python
from unittest import TestCase
import unittest
import os.path
import logging

from trustlink import phpserialize
from trustlink.client import TrustlinkClient, TrustlinkDownloader

ROOT = os.path.dirname(os.path.realpath(__file__))
DATA_DIR = os.path.join(ROOT, 'test_data')
VAR_DIR = os.path.join(ROOT, 'test_var')

class TrustlinkClientTest(TestCase):
    def test_init(self):
        user_id = '23423423'
        url = 'http://domain.com/foo?bar=baz'
        tr = TrustlinkClient(user_id, url, VAR_DIR)
        self.assertEqual('domain.com', tr.host)
        self.assertEqual(url, tr.url)

        tr = TrustlinkClient(user_id, url, VAR_DIR, static=True)
        self.assertEqual('domain.com', tr.host)
        self.assertEqual('/foo', tr.url)
        
        tr = TrustlinkClient(user_id, url, VAR_DIR)
        self.assertEqual(tr.isrobot, False)
        self.assertEqual(tr.test, False)
        self.assertEqual(tr.verbose, False)
        self.assertEqual(tr.force_show_code, False)

        tr = TrustlinkClient(user_id, url, VAR_DIR, request_headers={'Trustlink': user_id})
        self.assertEqual(tr.isrobot, True)
        self.assertEqual(tr.test, True)
        self.assertEqual(tr.verbose, True)
        self.assertEqual(tr.force_show_code, False)

        tr = TrustlinkClient(user_id, url, VAR_DIR, request_params={'trustlink_test': user_id})
        self.assertEqual(tr.isrobot, False)
        self.assertEqual(tr.test, False)
        self.assertEqual(tr.verbose, True)
        self.assertEqual(tr.force_show_code, True)


class TrustlinkDownloaderTest(TestCase):
    def test_download(self):
        user_id = '23423423'
        host = 'example.com'
        example_data = open(os.path.join(DATA_DIR, 'example_response.txt')).read()
        tr = TrustlinkDownloader(user_id, host, data_dir=VAR_DIR)
        tr.network_request = lambda x: example_data
        tr.download()


class TrustlinkCompleteTest(TestCase):
    def test_download(self):
        user_id = '23423423'
        host = 'test.com'
        example_data = open(os.path.join(DATA_DIR, 'example_response.txt')).read()
        tr = TrustlinkDownloader(user_id, host, data_dir=VAR_DIR)
        tr.network_request = lambda x: example_data
        tr.download()

        mapping = phpserialize.loads(example_data)
        tl_ip = mapping['__trustlink_robots__'][0]

        tr = TrustlinkClient(user_id, 'http://test.com', VAR_DIR,
                             remote_ip=tl_ip)
        resp = tr.build_links()
        print resp 
        self.assertTrue(len(resp['prepend']) > 0)
        self.assertTrue('<!--' in resp['prepend'])
        self.assertFalse('<noindex>' in resp['prepend'])

        tr = TrustlinkClient(user_id, 'http://test.com', VAR_DIR,
                             remote_ip='9.8.7.6')
        resp = tr.build_links()
        print resp 
        self.assertTrue(len(resp['prepend']) == 0)
        self.assertFalse('<!--' in resp['prepend'])

        tr = TrustlinkClient(user_id, 'http://test.com', VAR_DIR,
                             remote_ip='9.8.7.6',
                             request_headers={'Trustlink': user_id})
        resp = tr.build_links()
        print resp 
        self.assertTrue(len(resp['prepend']) > 0)
        self.assertTrue('<noindex>' in resp['prepend'])


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    unittest.main()