Exemplo n.º 1
0
    def test_upload_file(self):
        small_file_path = self._get_path("test_pattern.png")
        messages = upload_file(small_file_path, "Author")
        self.assertIsNotNone(messages)
        if "NET_TEST" in os.environ:
            self.discord.send(messages=messages)
        self.assertEqual(2, len(messages))
        embed, snapshot = messages[0]
        self.assertBasicEmbed(embed,
                              author="Author",
                              color=COLOR_INFO,
                              title="Uploaded test_pattern.png",
                              description=None)

        embed, snapshot = messages[1]
        self.assertEqual(snapshot.filename, "test_pattern.png")
        with open(small_file_path, 'rb') as f:
            snapshot.fp.seek(0)
            self.assertEqual(f.read(), snapshot.fp.read())

        # create large file, that requires splitting
        large_file_path = self._get_path("large_file_temp")
        data = bytearray(1024)
        for i in range(1024):
            data[i] = i % 0xff
        data = bytes(data)
        with open(large_file_path, 'wb') as f:
            for i in range(0, int(round(DISCORD_MAX_FILE_SIZE / 1024 * 6))):
                f.write(data)

        messages = upload_file(large_file_path, author="Author")
        self.assertIsNotNone(messages)
        if "NET_TEST" in os.environ:
            self.discord.send(messages)
        self.assertEqual(8, len(messages))
        embed, snapshot = messages[0]
        self.assertBasicEmbed(embed,
                              author="Author",
                              color=COLOR_INFO,
                              title="Uploaded large_file_temp in 7 parts",
                              description=None)

        with open("rebuilt.zip", 'wb') as f:
            i = 1
            for embed, snapshot in messages[1:]:
                self.assertEquals("large_file_temp.zip.%.03i" % i, snapshot.filename)
                snapshot.fp.seek(0)
                data = snapshot.fp.read()
                self.assertGreater(len(data), 0)
                self.assertLessEqual(len(data), DISCORD_MAX_FILE_SIZE)
                f.write(data)
                i += 1

        with zipfile.ZipFile("rebuilt.zip", 'r') as zip_file:
            with open(large_file_path, 'rb') as f:
                self.assertEquals(f.read(), zip_file.read("large_file_temp"))

        os.remove("rebuilt.zip")
        os.remove(large_file_path)
    def getfile(self, params):
        filename = " ".join(params[1:])
        foundfile = self.find_file(filename)
        if foundfile is None:
            return None, error_embed(author=self.plugin.get_printer_name(),
                                     title="Failed to find file matching the name given")
        file_path = self.plugin.get_file_manager().path_on_disk(foundfile['location'], foundfile['path'])

        return upload_file(file_path)
    def gettimelapse(self, params):
        filename = " ".join(params[1:]).upper()
        path = os.path.join(os.getcwd(), self.plugin._data_folder, '..', '..', 'timelapse')
        path = os.path.abspath(path)

        for root, dirs, files in os.walk(path):
            for name in files:
                file_path = os.path.join(root, name)
                if filename in file_path.upper():
                    return upload_file(file_path)

        return None, error_embed(author=self.plugin.get_printer_name(),
                                 title="Failed to find file matching the name given")
    def test_send(self):
        self.discord._dispatch_message = mock.Mock()
        mock_snapshot = mock.Mock()
        mock_embed = mock.Mock()
        self.assertTrue(
            self.discord.send(snapshots=[mock_snapshot], embeds=[mock_embed]))

        self.assertEqual(2, self.discord._dispatch_message.call_count)
        calls = [
            mock.call(snapshot=mock_snapshot),
            mock.call(embed=mock_embed)
        ]
        self.discord._dispatch_message.assert_has_calls(calls=calls)

        large_file_path = self._get_path("large_file_temp")
        with open(large_file_path, 'w') as f:
            for i in range(0, DISCORD_MAX_FILE_SIZE):
                f.write(str(i))

        embeds = upload_file(large_file_path)
        self.discord.send(embeds=embeds)