1
1
import json
2
- import shutil
3
- import tempfile
4
2
from datetime import date , datetime
5
- from io import BytesIO
6
3
from operator import attrgetter
7
4
from unittest .mock import patch
8
5
9
6
import stripe
10
7
from django .conf import settings
11
8
from django .core import mail
12
- from django .core .files .images import ImageFile
13
9
from django .template .defaultfilters import date as date_filter
14
10
from django .test import TestCase
15
11
from django .urls import reverse
16
12
from django_hosts .resolvers import reverse as django_hosts_reverse
17
13
from django_recaptcha .client import RecaptchaResponse
18
- from PIL import Image
19
14
20
15
from members .models import CorporateMember , Invoice
21
16
22
17
from ..models import DjangoHero , Donation
23
-
24
-
25
- def _make_image (name , width = 1 , height = 1 , color = (12 , 75 , 51 )):
26
- img = Image .new ("RGB" , (width , height ), color = color )
27
- out = BytesIO ()
28
- img .save (out , format = name .split ("." )[- 1 ])
29
- return ImageFile (out , name = name )
30
-
31
-
32
- class TemporaryMediaRootMixin :
33
- """
34
- A TestCase mixin that overrides settings.MEDIA_ROOT for every test on the
35
- class to point to a temporary directory that is destroyed when the tests
36
- finished.
37
- The content of the directory persists between different tests on the class.
38
- """
39
-
40
- @classmethod
41
- def setUpClass (cls ):
42
- super ().setUpClass ()
43
- cls .tmpdir = tempfile .mkdtemp (prefix = "djangoprojectcom_" )
44
-
45
- @classmethod
46
- def tearDownClass (cls ):
47
- shutil .rmtree (cls .tmpdir , ignore_errors = True )
48
- super ().tearDownClass ()
49
-
50
- def run (self , result = None ):
51
- with self .settings (MEDIA_ROOT = self .tmpdir ):
52
- return super ().run (result )
18
+ from .utils import ImageFileFactory , TemporaryMediaRootMixin
53
19
54
20
55
21
class TestIndex (TestCase ):
@@ -77,7 +43,9 @@ def test_corporate_member_without_logo(self):
77
43
78
44
def test_corporate_member_with_logo (self ):
79
45
member = CorporateMember .objects .create (
80
- display_name = "Test Member" , membership_level = 1 , logo = _make_image ("logo.png" )
46
+ display_name = "Test Member" ,
47
+ membership_level = 1 ,
48
+ logo = ImageFileFactory ("logo.png" ),
81
49
)
82
50
Invoice .objects .create (amount = 100 , expiration_date = date .today (), member = member )
83
51
response = self .client .get (self .index_url )
@@ -96,7 +64,7 @@ def test_corporate_member_with_logo(self):
96
64
)
97
65
98
66
def test_corporate_member_with_non_square_logo (self ):
99
- logo = _make_image ("wide.png" , width = 10 )
67
+ logo = ImageFileFactory ("wide.png" , width = 10 )
100
68
member = CorporateMember .objects .create (
101
69
display_name = "Test Member" , membership_level = 1 , logo = logo
102
70
)
@@ -130,7 +98,7 @@ def test_anonymous_donor_with_logo(self):
130
98
is_visible = True ,
131
99
approved = True ,
132
100
hero_type = "individual" ,
133
- logo = _make_image ("anonymous.png" ),
101
+ logo = ImageFileFactory ("anonymous.png" ),
134
102
)
135
103
donation = hero .donation_set .create (subscription_amount = "5" )
136
104
donation .payment_set .create (amount = "5" )
0 commit comments