1#!/usr/bin/env python
2# Copyright (c) 2011 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import os
7import tempfile
8import unittest
9
10import webforms_aggregator
11
12
13class WebformsAggregatorTest(unittest.TestCase):
14  """Unit tests for the webforms_aggregator module."""
15
16  def setUp(self):
17    self.cookie_file = 'test.cookie'
18    self.url1 = 'http://www.google.com'
19    self.url2 = 'http://www.macys.com'
20    self.domain = 'google.com'
21    self.url_file = tempfile.NamedTemporaryFile(suffix='.txt', delete=False)
22    self.url_file.file.write(
23        'URLs to crawl:\n%s\n%s\n' % (self.url1, self.url2))
24    self.url_file.close()
25
26  def tearDown(self):
27    if os.path.isfile(self.cookie_file):
28      os.unlink(self.cookie_file)
29    if os.path.isfile(self.url_file.name):
30      self.url_file.close()
31      os.unlink(self.url_file.name)
32
33  def testRetrieverDownloadsPage(self):
34    """Verify the retriever can download a page."""
35    r = webforms_aggregator.Retriever(self.url1, self.domain, self.cookie_file)
36    self.assertTrue(r.Download(),
37                    msg='Retriever could not download "%s"' % self.url1)
38
39  def testCrawlerFindsRegPageFromUrl(self):
40    """Verify that the crawler is able to find a reg page from the given URL."""
41    c = webforms_aggregator.Crawler(self.url2)
42    self.assertTrue(
43        c.Run(), msg='Crawler could not find the reg page of "%s"' % self.url2)
44
45  def testThreadedCrawlerFindsRegPageFromUrlsFile(self):
46    """Verify the threaded crawler finds reg page from a file of URLs."""
47    c = webforms_aggregator.ThreadedCrawler(self.url_file.name)
48    self.assertNotEqual(
49        c.Run(), -1,
50        msg='Threaded crawler could not find the reg page from the URLs file')
51
52
53if __name__ == '__main__':
54  suite = unittest.TestLoader().loadTestsFromTestCase(
55      WebformsAggregatorTest)
56  unittest.TextTestRunner(verbosity=2).run(suite)
57