def setUp(self):
     """override one in unittest.TestCase"""
     # bruce
     doc = FileDocument(PROFILE_BRUCE, PROFILE_DIRECTORY)
     self.assert_(doc.load())
     self.bruce_doc = CacheDocument(PROFILE_BRUCE, PROFILE_DIRECTORY)
     self.bruce_doc.import_document(doc)
     # demi
     self.demi_doc = FileDocument(PROFILE_DEMI, PROFILE_DIRECTORY)
     self.assert_(self.demi_doc.load())
class ProfileTest(unittest.TestCase):
    """test interactactions between profiles"""
    
    def setUp(self):
        """override one in unittest.TestCase"""
        # bruce
        doc = FileDocument(PROFILE_BRUCE, PROFILE_DIRECTORY)
        self.assert_(doc.load())
        self.bruce_doc = CacheDocument(PROFILE_BRUCE, PROFILE_DIRECTORY)
        self.bruce_doc.import_document(doc)
        # demi
        self.demi_doc = FileDocument(PROFILE_DEMI, PROFILE_DIRECTORY)
        self.assert_(self.demi_doc.load())

    def test_bruce(self):
        """import bruce data"""
        self.assertEquals("Bruce", self.bruce_doc.get_firstname())
        self.assertEquals("Willis", self.bruce_doc.get_lastname())
        self.assertEquals("*****@*****.**", self.bruce_doc.get_email())
        self.assertEquals({'City': u'', 'Country': u'',
                           'Favourite Book': u'', 'music': u'jazz',
                           'Favourite Movie': u'', 'Sport': u'', 'Studies': u'',
                           'film': u'Die Hard'},
                          self.bruce_doc.get_custom_attributes())

    def test_peer_status(self):
        self.assertEquals(self.demi_doc.has_peer(self.bruce_doc.pseudo), False)
        self.demi_doc.fill_data((self.bruce_doc.pseudo, self.bruce_doc))
        self.assertEquals(self.demi_doc.has_peer(self.bruce_doc.pseudo), True)
        self.demi_doc.make_friend(self.bruce_doc.pseudo)
        self.assertEquals(self.demi_doc.has_peer(self.bruce_doc.pseudo), True)
        self.demi_doc.unmark_peer(self.bruce_doc.pseudo)
        self.assertEquals(self.demi_doc.has_peer(self.bruce_doc.pseudo), False)
 def test_import(self):
     # file -> cache
     new_doc = CacheDocument()
     new_doc.import_document(self.document)
     self._assertContent(new_doc)
     # cache -> cache
     cache_doc = CacheDocument()
     cache_doc.import_document(new_doc)
     self._assertContent(cache_doc)
     # cache -> file
     file_doc = FileDocument()
     file_doc.import_document(cache_doc)
     self._assertContent(file_doc)
     # file -> file
     other_doc = FileDocument()
     other_doc.import_document(file_doc)
     self._assertContent(other_doc)
 def test_load_and_save(self):
     self.document.load()
     new_doc = CacheDocument(PROFILE_TATA, PROFILE_DIRECTORY)
     new_doc.import_document(self.document)
     new_doc.del_file(REPO)
     self.assertEquals(new_doc.get_files(), {})
     new_doc.add_file(REPO+"/data/profiles")
     new_doc.add_file(REPO+"/data/subdir1")
     self.assertEquals(new_doc.get_files()[REPO+"/data/profiles"]._shared, True)
     self.assert_(new_doc.get_files()[REPO+"/data/subdir1"] != None)
     new_doc.save()
     check_doc = FileDocument(PROFILE_TATA, PROFILE_DIRECTORY)
     check_doc.load()
     self.assertEquals(check_doc.get_files()[REPO+"/data/profiles"]._shared, True)
     self.assert_(check_doc.get_files()[REPO+"/data/subdir1"] != None)
 def test_import(self):
     self.document.load()
     new_doc = CacheDocument(PROFILE_TEST, PROFILE_DIRECTORY)
     new_doc.import_document(self.document)
     self._assertContent(new_doc)
 def setUp(self):
     """override one in unittest.TestCase"""
     self.document = CacheDocument(PROFILE_TEST, PROFILE_DIRECTORY)
     self.desc = PeerDescriptor(PROFILE_TEST, document=self.document)
     self.view = HtmlView(self.desc)
class HtmlTest(unittest.TestCase):
    """test that all fields are correctly validated"""

    def setUp(self):
        """override one in unittest.TestCase"""
        self.document = CacheDocument(PROFILE_TEST, PROFILE_DIRECTORY)
        self.desc = PeerDescriptor(PROFILE_TEST, document=self.document)
        self.view = HtmlView(self.desc)

    def assert_template(self):
        """diff result from view with expeceted template"""
        self.view.import_desc(self.desc)
        result = difflib.ndiff([line.strip() for line in self.print_template().splitlines()],
                               [line.strip() for line in self.view.get_view(True).splitlines()],
                               charjunk=lambda x: x in [' ', '\t'])
        result = list(result)
        for index, line in enumerate(result):
            if not line.startswith("  "):
                print self.view.get_view()
                print "*************"
                print '\n'.join(result[index-2:index+10])
            self.assert_(line.startswith("  "))
 
    def print_template(self):
        """fill template with values"""
        return TEMPLATE % (self.document.get_photo(),
                           self.document.pseudo,
                           self.document.get_title(),
                           self.document.get_firstname(),
                           self.document.get_lastname(),
                           self.document.get_email(),
                           self.print_custom(),
                           self.print_files(),
                           self.print_peers())

    def print_custom(self):
        html = ["<div><b>%s</b>: <span>%s</span></div>"% (key, value)
                for key, value in self.document.get_custom_attributes().iteritems()]
        return ''.join(html)

    def print_files(self):
        html = []
        repos = self.document.get_repositories()
        repos.sort()
        for repo in repos:
            html.append("""
    <dt>%s</dt>
    <dd>%d shared files</dd>
"""% (os.path.basename(repo), len(self.document.get_shared(repo))))
        # concatenate all description of directories & return result
        return ''.join(html)

    def print_peers(self):
        html = []
        peers = self.document.get_peers()
        keys = peers.keys()
        keys.sort()
        for key in keys:
            peers_desc = peers[key]
            doc = peers_desc.document
            html.append("""<tr>
      <td>%s</td>
      <td>%s</td>
      <td>%s</td>
    </tr>"""% (peers_desc.pseudo,
               peers_desc.state,
               doc and doc.get_email() or "--"))
        return ''.join(html)
        
    
    # PERSONAL TAB
    def test_personal(self):
        """personal info"""
        self.document.set_title(u"Mr")
        self.document.set_firstname(u"Bruce")
        self.document.set_lastname(u"Willis")
        self.document.set_photo(os.sep.join([images_dir(), u"profile_male.gif"]))
        self.document.set_email(u"*****@*****.**")
        self.assert_template()
       
    # CUSTOM TAB
    def test_custom(self):
        self.document.add_custom_attributes([u"zic", u"jazz"])
        self.document.add_custom_attributes([u"cinema", u"Die Hard"])
        self.assert_template()
        
    # FILE TAB       
    def test_files(self):
        self.document.add_file(abspath("."))
        self.document.expand_dir(abspath("data"))
        self.document.share_files((abspath("data"), ["routage"], True))
        self.document.share_file((abspath("data/emptydir"), True))
        self.assert_template()
            
    # OTHERS TAB
    def test_ordered_peer(self):
        self.document.set_peer((u"emb", PeerDescriptor(PSEUDO)))
        self.document.set_peer((u"albert", PeerDescriptor(PSEUDO)))
        self.document.set_peer((u"star", PeerDescriptor(PSEUDO)))
        self.document.make_friend(u"emb")
        self.document.make_friend(u"star")
        self.document.make_friend(u"albert")
        self.assert_template()
        
    def test_adding_peer(self):
        self.document.set_peer((u"nico", PeerDescriptor(PSEUDO)))
        self.assert_template()
        
    def test_filling_data(self):
        self.document.fill_data((u"emb", FileDocument(PROFILE_TEST, PROFILE_DIRECTORY)))
        self.assert_template()
    
    def test_peers_status(self):
        self.document.set_peer((u"nico", PeerDescriptor(PSEUDO)))
        self.document.blacklist_peer(u"nico")
        self.assert_template()
 def test_multiple_repos(self):
     """coherency when several repos in use"""
     document = CacheDocument(PROFILE_TEST, PROFILE_DIRECTORY)
     # create 2 repos
     document.add_file(REPO + "/data/profiles")
     document.tag_files((REPO + "/data/profiles", ["bruce.prf", ".svn"], u"first"))
     document.share_files((REPO + "/data/profiles", ["bruce.prf", "demi.prf"], False))
     document.add_file(REPO + "/data/subdir1")
     document.tag_files((REPO + "/data/subdir1", ["date.doc", ".svn"], u"second"))
     document.share_files((REPO + "/data/subdir1", ["date.doc", "subsubdir"], False))
     # check sharing state
     self.assertEquals(document.get_container(
         abspath("data/profiles/bruce.prf"))._shared, False)
     self.assertEquals(document.get_container(
         abspath("data/profiles/demi.prf"))._shared, False)
     self.assertEquals(document.get_container(
         abspath("data/profiles/.svn"))._shared, True)
     self.assertEquals(document.get_container(
         abspath("data/subdir1/date.doc"))._shared, False)
     self.assertEquals(document.get_container(
         abspath("data/subdir1/subsubdir"))._shared, False)
     self.assertEquals(document.get_container(
         abspath("data/subdir1/.svn"))._shared, True)
     # check tag
     self.assertRaises(ValueError, document.add_file, REPO + "/data/subdir1/subsubdir")
     self.assertRaises(ValueError, document.add_file, REPO + "/data")
 def test_get_shared_files(self):
     document = CacheDocument(PROFILE_TEST, PROFILE_DIRECTORY)
     document.add_file(REPO)
     document.expand_dir(abspath("data"))
     document.expand_dir(abspath("data/subdir1"))
     document.share_files((abspath("data"),
                           [REPO + '/data/.path',
                            REPO + '/data/.svn',
                            REPO + '/data/date.txt',
                            REPO + '/data/emptydir',
                            REPO + '/data/profiles',
                            REPO + '/data/subdir1/.svn',
                            REPO + '/data/subdir1/subsubdir'],
                           False))
     document.share_files((abspath("data"),
                           [REPO + '/data',
                            REPO + "/data/.path",
                            REPO + "/data/date.txt",
                            REPO + "/data/routage",
                            REPO + '/data/subdir1',
                            REPO + "/data/subdir1/TOtO.txt",
                            REPO + "/data/subdir1/date.doc"],
                           True))
     shared_files = [file_container.get_path() for file_container
                     in document.get_shared_files()[REPO]]
     shared_files.sort()
     self.assertEquals(shared_files, [REPO + "/data/.path",
                                      REPO + "/data/date.txt",
                                      REPO + "/data/routage",
                                      REPO + "/data/subdir1/TOtO.txt",
                                      REPO + "/data/subdir1/date.doc"])
 def test_multiple_repos(self):
     """coherency when several repos in use"""
     document = CacheDocument()
     # create 2 repos
     document.add_repository(os.sep.join([TEST_DIR, "data", "profiles"]))
     tag_files(document, os.sep.join([TEST_DIR, "data", "profiles"]), ["bruce.prf", ".svn"], u"first")
     document.share_files(os.sep.join([TEST_DIR, "data", "profiles"]), ["bruce.prf", "demi.prf"], True)
     document.add_repository(os.sep.join([TEST_DIR, "data", "subdir1"]))
     tag_files(document, os.sep.join([TEST_DIR, "data", "subdir1"]), ["date.doc", ".svn"], u"second")
     document.share_files(os.sep.join([TEST_DIR, "data", "subdir1"]), ["date.doc", "subsubdir"], True)
     # check sharing state
     self.assertEquals(document.get_container(
         abspath(os.sep.join(["data", "profiles", "bruce.prf"])))._shared, True)
     self.assertEquals(document.get_container(
         abspath(os.sep.join(["data", "profiles", "demi.prf"])))._shared, True)
     self.assertEquals(document.get_container(
         abspath(os.sep.join(["data", "profiles", ".svn"])))._shared, False)
     self.assertEquals(document.get_container(
         abspath(os.sep.join(["data", "subdir1", "date.doc"])))._shared, True)
     self.assertEquals(document.get_container(
         abspath(os.sep.join(["data", "subdir1", "subsubdir"])))._shared, True)
     self.assertEquals(document.get_container(
         abspath(os.sep.join(["data", "subdir1", ".svn"])))._shared, False)
     # check tag
     self.assertRaises(ContainerException, document.add_repository, os.sep.join([TEST_DIR, "data", "subdir1", "subsubdir"]))
     self.assertRaises(ContainerException, document.add_repository, os.sep.join([TEST_DIR, "data"]))
 def test_get_shared_files(self):
     document = CacheDocument()
     document.add_repository(TEST_DIR)
     document.expand_dir(abspath("data"))
     document.expand_dir(abspath(os.path.join("data", "subdir1")))
     document.share_files(abspath("data"),
                           [os.sep.join([TEST_DIR, "data", ".path"]),
                            os.sep.join([TEST_DIR, "data", ".svn"]),
                            os.sep.join([TEST_DIR, "data", "date.txt"]),
                            os.sep.join([TEST_DIR, "data", "emptydir"]),
                            os.sep.join([TEST_DIR, "data", "profiles"]),
                            os.sep.join([TEST_DIR, "data", "subdir1", ".svn"]),
                            os.sep.join([TEST_DIR, "data", "subdir1", "subsubdir"])],
                           False)
     document.share_files(abspath("data"),
                           [os.sep.join([TEST_DIR, "data"]),
                            os.sep.join([TEST_DIR, "data", ".path"]),
                            os.sep.join([TEST_DIR, "data", "date.txt"]),
                            os.sep.join([TEST_DIR, "data", "routage"]),
                            os.sep.join([TEST_DIR, "data", "subdir1"]),
                            os.sep.join([TEST_DIR, "data", "subdir1", "TOtO.txt"]),
                            os.sep.join([TEST_DIR, "data", "subdir1", "date.doc"])],
                           True)
     shared_files = [file_container.get_path() for file_container
                     in document.get_shared_files()[TEST_DIR]]
     shared_files.sort()
     self.assertEquals(shared_files, [os.sep.join([TEST_DIR, "data", ".path"]),
                                      os.sep.join([TEST_DIR, "data", "02_b_1280x1024.jpg"]),
                                      os.sep.join([TEST_DIR, "data", "Python-2.3.5.zip"]),
                                      os.sep.join([TEST_DIR, "data", "arc en ciel 6.gif"]),
                                      os.sep.join([TEST_DIR, "data", "date.txt"]),
                                      os.sep.join([TEST_DIR, "data", "pywin32-203.win32-py2.3.exe"]),
                                      os.sep.join([TEST_DIR, "data", "routage"]),
                                      os.sep.join([TEST_DIR, "data", "subdir1", "TOtO.txt"]),
                                      os.sep.join([TEST_DIR, "data", "subdir1", "date.doc"])])
 def test_get_shared_files(self):
     document = CacheDocument()
     document.add_repository(TEST_DIR)
     document.expand_dir(abspath("data"))
     document.expand_dir(abspath(os.path.join("data", "subdir1")))
     document.share_files(
         abspath("data"),
         [
             os.sep.join([TEST_DIR, "data", ".path"]),
             os.sep.join([TEST_DIR, "data", ".svn"]),
             os.sep.join([TEST_DIR, "data", "date.txt"]),
             os.sep.join([TEST_DIR, "data", "emptydir"]),
             os.sep.join([TEST_DIR, "data", "profiles"]),
             os.sep.join([TEST_DIR, "data", "subdir1", ".svn"]),
             os.sep.join([TEST_DIR, "data", "subdir1", "subsubdir"]),
         ],
         False,
     )
     document.share_files(
         abspath("data"),
         [
             os.sep.join([TEST_DIR, "data"]),
             os.sep.join([TEST_DIR, "data", ".path"]),
             os.sep.join([TEST_DIR, "data", "date.txt"]),
             os.sep.join([TEST_DIR, "data", "routage"]),
             os.sep.join([TEST_DIR, "data", "subdir1"]),
             os.sep.join([TEST_DIR, "data", "subdir1", "TOtO.txt"]),
             os.sep.join([TEST_DIR, "data", "subdir1", "date.doc"]),
         ],
         True,
     )
     shared_files = [file_container.get_path() for file_container in document.get_shared_files()[TEST_DIR]]
     shared_files.sort()
     self.assertEquals(
         shared_files,
         [
             os.sep.join([TEST_DIR, "data", ".path"]),
             os.sep.join([TEST_DIR, "data", "date.txt"]),
             os.sep.join([TEST_DIR, "data", "routage"]),
             os.sep.join([TEST_DIR, "data", "subdir1", "TOtO.txt"]),
             os.sep.join([TEST_DIR, "data", "subdir1", "date.doc"]),
         ],
     )