|
| 1 | +import os |
| 2 | +import filecmp |
| 3 | +import shutil |
| 4 | + |
1 | 5 | from django.test import TestCase, override_settings |
2 | 6 | from django.core.exceptions import ImproperlyConfigured |
3 | 7 | from django.test.utils import captured_stdout |
| 8 | +from django.conf import settings |
4 | 9 |
|
5 | | -from peps.converters import get_pep0_page, get_pep_page, add_pep_image |
| 10 | +from peps.converters import get_pep0_page, get_pep_page, add_pep_image, pep_url |
6 | 11 |
|
7 | 12 | from . import FAKE_PEP_REPO |
8 | 13 |
|
@@ -43,6 +48,41 @@ def test_add_image_not_found(self): |
43 | 48 | r"Image Path '(.*)/path/that/does/not/exist(.*)' does not exist, skipping" |
44 | 49 | ) |
45 | 50 |
|
| 51 | + def test_add_image_update(self): |
| 52 | + """Test that image modifications are adopted. """ |
| 53 | + pep = '3001' |
| 54 | + pep_img_name = 'pep-3001-1.png' |
| 55 | + pep_img_path_src = os.path.join(FAKE_PEP_REPO, pep_img_name) |
| 56 | + pep_img_path_src_backup = pep_img_path_src + ".backup" |
| 57 | + pep_img_path_dst = os.path.join( |
| 58 | + settings.MEDIA_ROOT, pep_url(pep), pep_img_name) |
| 59 | + |
| 60 | + # We need to remove a prior uploaded file to fix an inconsistency |
| 61 | + # between db and filesystem. |
| 62 | + # TODO: This should be dealt with in page.model or converters module |
| 63 | + if os.path.exists(pep_img_path_dst): |
| 64 | + os.remove(pep_img_path_dst) |
| 65 | + |
| 66 | + # Create a pep page and "upload" a file |
| 67 | + get_pep_page(FAKE_PEP_REPO, pep) |
| 68 | + image = add_pep_image(FAKE_PEP_REPO, pep, pep_img_name) |
| 69 | + |
| 70 | + # The source and destination files should be the same |
| 71 | + self.assertTrue(filecmp.cmp(pep_img_path_src, pep_img_path_dst)) |
| 72 | + |
| 73 | + # Create a backup, modify the source file and "re-upload" |
| 74 | + shutil.copyfile(pep_img_path_src, pep_img_path_src_backup) |
| 75 | + with open(pep_img_path_src, "ab") as f: |
| 76 | + f.write(b"TEST") |
| 77 | + image = add_pep_image(FAKE_PEP_REPO, pep, pep_img_name) |
| 78 | + |
| 79 | + # Again, modified src and re-uploaded dst files should be the same |
| 80 | + self.assertTrue(filecmp.cmp(pep_img_path_src, pep_img_path_dst)) |
| 81 | + |
| 82 | + # Restore source file |
| 83 | + shutil.move(pep_img_path_src_backup, pep_img_path_src) |
| 84 | + |
| 85 | + |
46 | 86 | def test_html_do_not_prettify(self): |
47 | 87 | pep = get_pep_page(FAKE_PEP_REPO, '3001') |
48 | 88 | self.assertEqual( |
|
0 commit comments