[damned-lies] Mock network access in get_image_size test



commit 1f6d27bc9513ecaeecc42e46ebacdbb56f22df00
Author: Claude Paroz <claude 2xlibre net>
Date:   Thu Oct 8 22:07:08 2020 +0200

    Mock network access in get_image_size test

 people/tests.py | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
---
diff --git a/people/tests.py b/people/tests.py
index b65bf1ea..ba0219db 100644
--- a/people/tests.py
+++ b/people/tests.py
@@ -1,7 +1,8 @@
 import datetime
-from unittest import skipUnless
+from unittest import mock, skipUnless
 
 from django.contrib import auth
+from django.contrib.staticfiles.finders import find
 from django.core import mail
 from django.core.exceptions import ValidationError
 from django.test import TestCase
@@ -211,19 +212,23 @@ class PeopleTestCase(TestCase):
             
'src="https://seccdn.libravatar.org/avatar/618b8b6c1c973c780ec218242c49cbe7?s=80&d=identicon&r=g";>'
         )
 
-    def test_get_image_size(self):
-        from people.forms import get_image_size
-        self.assertEqual(
-            get_image_size("https://l10n.gnome.org/static/img/nobody.png";),
-            (48, 48)
-        )
+    @mock.patch('people.forms.urlopen')
+    def test_get_image_size(self, m_urlopen):
+        with open(find('img/nobody.png'), mode='rb') as fh:
+            m_urlopen.return_value = fh
+            self.assertEqual(
+                forms.get_image_size("https://l10n.gnome.org/static/img/nobody.png";),
+                (48, 48)
+            )
+
+    def test_invalid_images(self):
         invalid_urls = (
             "absolutely invalid",
             "http://hopefullythisurlshouldnotexist.com/grstzqwer.jpg";,
         )
         for url in invalid_urls:
             with self.assertRaises(ValidationError):
-                get_image_size(url)
+                forms.get_image_size(url)
 
     @skipUnless(pyicu_present, "PyICU package is required for this test")
     def test_all_languages_list_order(self):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]