Exemple #1
0
    def test_mime(self):
        """
        Tests mime types on different types of content
        """
        ac = NNTPAsciiContent()
        bc = NNTPBinaryContent()

        # Mime Types aren't detectable with new files
        assert (ac.mime().type() == 'application/x-empty')
        assert (bc.mime().type() == 'application/x-empty')

        # Open up a jpeg
        bc = NNTPBinaryContent(join(self.var_dir, 'joystick.jpg'))
        assert (bc.mime().type() == 'image/jpeg')

        # Make a copy of our image as a different name
        assert (bc.save(join(self.tmp_dir, 'weird.name'), copy=True) is True)
        # We still know it's an image
        assert (bc.mime().type() == 'image/jpeg')

        # Create ourselves a new file
        tmp_file = join(self.tmp_dir, 'test.rar')
        assert (self.touch(tmp_file, size='2KB', random=True) is True)
        bc = NNTPBinaryContent(tmp_file)

        # Now we can guess the name from it's file type
        assert (bc.mime().type() == 'application/x-rar-compressed')
Exemple #2
0
    def test_mime(self):
        """
        Tests mime types on different types of content
        """
        ac = NNTPAsciiContent()
        bc = NNTPBinaryContent()

        # Mime Types aren't detectable with new files
        assert(ac.mime().type() == 'application/x-empty')
        assert(bc.mime().type() == 'application/x-empty')

        # Open up a jpeg
        bc = NNTPBinaryContent(join(self.var_dir, 'joystick.jpg'))
        assert(bc.mime().type() == 'image/jpeg')

        # Make a copy of our image as a different name
        assert(bc.save(join(self.tmp_dir, 'weird.name'), copy=True) is True)
        # We still know it's an image
        assert(bc.mime().type() == 'image/jpeg')

        # Create ourselves a new file
        tmp_file = join(self.tmp_dir, 'test.rar')
        assert(self.touch(tmp_file, size='2KB', random=True) is True)
        bc = NNTPBinaryContent(tmp_file)

        # Now we can guess the name from it's file type
        assert(bc.mime().type() == 'application/x-rar-compressed')
Exemple #3
0
    def test_general_features(self):
        """
        Detaching makes managing a file no longer managed by this
        NNTPContent. Test that this works

        """
        # No parameters should create a file
        aa = NNTPAsciiContent()
        ba = NNTPBinaryContent()

        # open a temporary file
        aa.open()
        ba.open()

        # Test Files
        aa_filepath = aa.filepath
        ba_filepath = ba.filepath
        assert (isfile(aa_filepath) is True)
        assert (isfile(ba_filepath) is True)

        # Test Length
        assert (len(aa) == 0)
        assert (len(ba) == 0)

        # Test that files are destroyed if the object is
        del aa
        del ba

        # Files are destroyed
        assert (isfile(aa_filepath) is False)
        assert (isfile(ba_filepath) is False)

        # Test some parameters out during initialization
        aa = NNTPAsciiContent(
            filepath="ascii.file",
            part=2,
            work_dir=self.tmp_dir,
        )

        ba = NNTPBinaryContent(
            filepath="binary.file",
            part="10",
            work_dir=self.tmp_dir,
        )

        # Check our parts
        assert (aa.part == 2)

        # Strings are converted okay
        assert (ba.part == 10)

        # open a temporary file
        aa.open()
        ba.open()

        # files don't exist yet
        assert (isfile(join(self.tmp_dir, "binary.file")) is False)
        assert (isfile(join(self.tmp_dir, "ascii.file")) is False)

        # Grab a copy of these file paths so we can check them later
        aa_filepath = aa.filepath
        ba_filepath = ba.filepath

        # Save our content
        aa.save()
        ba.save()

        # check that it was created okay
        assert (isfile(join(self.tmp_dir, "binary.file")) is True)
        assert (isfile(join(self.tmp_dir, "ascii.file")) is True)

        # Temporary files are gone (moved from the save() command above)
        assert (isfile(aa_filepath) is False)
        assert (isfile(ba_filepath) is False)

        # They were never the same after the save()
        assert (aa_filepath != aa.filepath)
        assert (ba_filepath != ba.filepath)

        # However after save is called; the filepath is updated to reflect
        # the proper path; so this is still true
        assert (isfile(aa.filepath) is True)
        assert (isfile(ba.filepath) is True)

        # Even after the objects are gone
        del aa
        del ba

        # Files still exist even after the objects displayed
        assert (isfile(join(self.tmp_dir, "binary.file")) is True)
        assert (isfile(join(self.tmp_dir, "ascii.file")) is True)

        # Cleanup
        unlink(join(self.tmp_dir, "ascii.file"))
        unlink(join(self.tmp_dir, "binary.file"))
Exemple #4
0
    def test_general_features(self):
        """
        Detaching makes managing a file no longer managed by this
        NNTPContent. Test that this works

        """
        # No parameters should create a file
        aa = NNTPAsciiContent()
        ba = NNTPBinaryContent()

        # open a temporary file
        aa.open()
        ba.open()

        # Test Files
        aa_filepath = aa.filepath
        ba_filepath = ba.filepath
        assert(isfile(aa_filepath) is True)
        assert(isfile(ba_filepath) is True)

        # Test Length
        assert(len(aa) == 0)
        assert(len(ba) == 0)

        # Test that files are destroyed if the object is
        del aa
        del ba

        # Files are destroyed
        assert(isfile(aa_filepath) is False)
        assert(isfile(ba_filepath) is False)

        # Test some parameters out during initialization
        aa = NNTPAsciiContent(
            filepath="ascii.file",
            part=2,
            work_dir=self.tmp_dir,
        )

        ba = NNTPBinaryContent(
            filepath="binary.file",
            part="10",
            work_dir=self.tmp_dir,
        )

        # Check our parts
        assert(aa.part == 2)

        # Strings are converted okay
        assert(ba.part == 10)

        # open a temporary file
        aa.open()
        ba.open()

        # files don't exist yet
        assert(isfile(join(self.tmp_dir, "binary.file")) is False)
        assert(isfile(join(self.tmp_dir, "ascii.file")) is False)

        # Grab a copy of these file paths so we can check them later
        aa_filepath = aa.filepath
        ba_filepath = ba.filepath

        # Save our content
        aa.save()
        ba.save()

        # check that it was created okay
        assert(isfile(join(self.tmp_dir, "binary.file")) is True)
        assert(isfile(join(self.tmp_dir, "ascii.file")) is True)

        # Temporary files are gone (moved from the save() command above)
        assert(isfile(aa_filepath) is False)
        assert(isfile(ba_filepath) is False)

        # They were never the same after the save()
        assert(aa_filepath != aa.filepath)
        assert(ba_filepath != ba.filepath)

        # However after save is called; the filepath is updated to reflect
        # the proper path; so this is still true
        assert(isfile(aa.filepath) is True)
        assert(isfile(ba.filepath) is True)

        # Even after the objects are gone
        del aa
        del ba

        # Files still exist even after the objects displayed
        assert(isfile(join(self.tmp_dir, "binary.file")) is True)
        assert(isfile(join(self.tmp_dir, "ascii.file")) is True)

        # Cleanup
        unlink(join(self.tmp_dir, "ascii.file"))
        unlink(join(self.tmp_dir, "binary.file"))
Exemple #5
0
    def test_yenc_multi_message(self):
        """
        Tests the handling of a yenc multi-message
        """

        # Create a non-secure connection
        sock = NNTPConnection(
            host=self.nttp_ipaddr,
            port=self.nntp_portno,
            username='******',
            password='******',
            secure=False,
            join_group=True,
        )

        assert sock.connect() is True
        assert sock._iostream == NNTPIOStream.RFC3977_GZIP

        articles = sortedset(key=lambda x: x.key())

        # We intententionally fetch the content out of order
        # ideally we'd want 20 followed by 21
        articles.add(sock.get(id='21', work_dir=self.tmp_dir, group=self.common_group))
        assert sock.group_name == self.common_group
        articles.add(sock.get(id='20', work_dir=self.tmp_dir))
        assert sock.group_name == self.common_group

        newfile = NNTPBinaryContent(
            # This looks rough;
            # we're basically looking at the first article stored (since our
            # set is sorted, and then we're looking at the first content entry

            # TODO: update the article function so it's much easier to get
            # an iterator to decoded list
            filepath=iter(iter(articles).next().decoded).next().filename,
            work_dir=self.tmp_dir,
        )

        for article in articles:
            assert isinstance(article, NNTPArticle) is True
            assert len(article.decoded) == 1
            assert isinstance(iter(article.decoded).next(), NNTPBinaryContent)
            assert iter(article.decoded).next().is_valid() is True

            # Build on new file
            newfile.append(iter(article.decoded).next())
            # keep open file count low
            iter(article.decoded).next().close()

        # Compare File
        decoded_filepath = join(self.var_dir, 'joystick.jpg')
        assert isfile(decoded_filepath)
        with open(decoded_filepath, 'r') as fd_in:
            decoded = fd_in.read()

        assert isfile(newfile.filepath) is True
        old_filepath = newfile.filepath
        newfile.save()
        new_filepath = newfile.filepath
        assert old_filepath != new_filepath
        assert isfile(old_filepath) is False
        assert isfile(new_filepath) is True

        assert decoded == newfile.getvalue()

        # Close up our socket
        sock.close()

        while len(articles):
            article = articles.pop()
            # length hasn't changed
            assert len(article.decoded) == 1
            old_filepath = iter(article.decoded).next().filepath
            assert isfile(old_filepath) is True

            # If we remove the article, we automatically destroy
            # all associated decoded with it (that aren't detached)
            del article

            # Since there is only 1 attachment per article in this test
            # we can see that the file is now gone
            assert isfile(old_filepath) is False

        # Remove the file
        del newfile

        # We called save() so the file has been detached and will still exist!
        assert isfile(new_filepath) is True

        # cleanup our file
        unlink(new_filepath)
Exemple #6
0
    def test_yenc_multi_message(self):
        """
        Tests the handling of a yenc multi-message
        """

        # Create a non-secure connection
        sock = NNTPConnection(
            host=self.nttp_ipaddr,
            port=self.nntp_portno,
            username='******',
            password='******',
            secure=False,
            join_group=True,
        )

        assert sock.connect() is True
        assert sock._iostream == NNTPIOStream.RFC3977_GZIP

        articles = sortedset(key=lambda x: x.key())

        # We intententionally fetch the content out of order
        # ideally we'd want 20 followed by 21
        articles.add(
            sock.get(id='21', work_dir=self.tmp_dir, group=self.common_group))
        assert sock.group_name == self.common_group
        articles.add(sock.get(id='20', work_dir=self.tmp_dir))
        assert sock.group_name == self.common_group

        newfile = NNTPBinaryContent(
            # This looks rough;
            # we're basically looking at the first article stored (since our
            # set is sorted, and then we're looking at the first content entry

            # TODO: update the article function so it's much easier to get
            # an iterator to decoded list
            filepath=iter(iter(articles).next().decoded).next().filename,
            work_dir=self.tmp_dir,
        )

        for article in articles:
            assert isinstance(article, NNTPArticle) is True
            assert len(article.decoded) == 1
            assert isinstance(iter(article.decoded).next(), NNTPBinaryContent)
            assert iter(article.decoded).next().is_valid() is True

            # Build on new file
            newfile.append(iter(article.decoded).next())
            # keep open file count low
            iter(article.decoded).next().close()

        # Compare File
        decoded_filepath = join(self.var_dir, 'joystick.jpg')
        assert isfile(decoded_filepath)
        with open(decoded_filepath, 'r') as fd_in:
            decoded = fd_in.read()

        assert isfile(newfile.filepath) is True
        old_filepath = newfile.filepath
        newfile.save()
        new_filepath = newfile.filepath
        assert old_filepath != new_filepath
        assert isfile(old_filepath) is False
        assert isfile(new_filepath) is True

        assert decoded == newfile.getvalue()

        # Close up our socket
        sock.close()

        while len(articles):
            article = articles.pop()
            # length hasn't changed
            assert len(article.decoded) == 1
            old_filepath = iter(article.decoded).next().filepath
            assert isfile(old_filepath) is True

            # If we remove the article, we automatically destroy
            # all associated decoded with it (that aren't detached)
            del article

            # Since there is only 1 attachment per article in this test
            # we can see that the file is now gone
            assert isfile(old_filepath) is False

        # Remove the file
        del newfile

        # We called save() so the file has been detached and will still exist!
        assert isfile(new_filepath) is True

        # cleanup our file
        unlink(new_filepath)