1
+ from urllib .parse import urlencode
2
+
1
3
from django .contrib .auth .models import Permission
2
4
from django .urls import reverse_lazy
3
5
from django .utils .text import slugify
@@ -20,6 +22,15 @@ def setUp(self):
20
22
self .authorization = f'Token { token .key } '
21
23
self .sponsors = baker .make (Sponsor , _create_files = True , _quantity = 3 )
22
24
25
+ sponsorships = baker .make_recipe ("sponsors.tests.finalized_sponsorship" , sponsor = iter (self .sponsors ),
26
+ _quantity = 3 )
27
+ self .sp1 , self .sp2 , self .sp3 = sponsorships
28
+ baker .make_recipe ("sponsors.tests.logo_at_download_feature" , sponsor_benefit__sponsorship = self .sp1 )
29
+ baker .make_recipe ("sponsors.tests.logo_at_sponsors_feature" , sponsor_benefit__sponsorship = self .sp1 )
30
+ baker .make_recipe ("sponsors.tests.logo_at_sponsors_feature" , sponsor_benefit__sponsorship = self .sp2 )
31
+ baker .make_recipe ("sponsors.tests.logo_at_pypi_feature" , sponsor_benefit__sponsorship = self .sp3 ,
32
+ link_to_sponsors_page = True , describe_as_sponsor = True )
33
+
23
34
def tearDown (self ):
24
35
for sponsor in Sponsor .objects .all ():
25
36
if sponsor .web_logo :
@@ -28,12 +39,6 @@ def tearDown(self):
28
39
sponsor .print_logo .delete ()
29
40
30
41
def test_list_logo_placement_as_expected (self ):
31
- sp1 , sp2 , sp3 = baker .make_recipe ("sponsors.tests.finalized_sponsorship" , sponsor = iter (self .sponsors ), _quantity = 3 )
32
- baker .make_recipe ("sponsors.tests.logo_at_download_feature" , sponsor_benefit__sponsorship = sp1 )
33
- baker .make_recipe ("sponsors.tests.logo_at_sponsors_feature" , sponsor_benefit__sponsorship = sp1 )
34
- baker .make_recipe ("sponsors.tests.logo_at_sponsors_feature" , sponsor_benefit__sponsorship = sp2 )
35
- baker .make_recipe ("sponsors.tests.logo_at_pypi_feature" , sponsor_benefit__sponsorship = sp3 , link_to_sponsors_page = True , describe_as_sponsor = True )
36
-
37
42
response = self .client .get (self .url , HTTP_AUTHORIZATION = self .authorization )
38
43
data = response .json ()
39
44
@@ -50,15 +55,15 @@ def test_list_logo_placement_as_expected(self):
50
55
[p for p in data if p ["publisher" ] == PublisherChoices .FOUNDATION .value ][0 ]['sponsor_url' ]
51
56
)
52
57
self .assertEqual (
53
- f"http://testserver/psf/sponsors/#{ slugify (sp3 .sponsor .name )} " ,
58
+ f"http://testserver/psf/sponsors/#{ slugify (self . sp3 .sponsor .name )} " ,
54
59
[p for p in data if p ["publisher" ] == PublisherChoices .PYPI .value ][0 ]['sponsor_url' ]
55
60
)
56
61
self .assertCountEqual (
57
- [sp1 .sponsor .description , sp1 .sponsor .description , sp2 .sponsor .description ],
62
+ [self . sp1 .sponsor .description , self . sp1 .sponsor .description , self . sp2 .sponsor .description ],
58
63
[p ['description' ] for p in data if p ["publisher" ] == PublisherChoices .FOUNDATION .value ]
59
64
)
60
65
self .assertEqual (
61
- [f"{ sp3 .sponsor .name } is a { sp3 .level_name } sponsor of the Python Software Foundation." ],
66
+ [f"{ self . sp3 .sponsor .name } is a { self . sp3 .level_name } sponsor of the Python Software Foundation." ],
62
67
[p ['description' ] for p in data if p ["publisher" ] == PublisherChoices .PYPI .value ]
63
68
)
64
69
@@ -86,3 +91,41 @@ def test_user_must_have_required_permission(self):
86
91
self .user .user_permissions .remove (self .permission )
87
92
response = self .client .get (self .url , HTTP_AUTHORIZATION = self .authorization )
88
93
self .assertEqual (403 , response .status_code )
94
+
95
+ def test_filter_sponsorship_by_publisher (self ):
96
+ querystring = urlencode ({
97
+ "publisher" : PublisherChoices .PYPI .value ,
98
+ })
99
+ url = f"{ self .url } ?{ querystring } "
100
+ response = self .client .get (url , HTTP_AUTHORIZATION = self .authorization )
101
+ data = response .json ()
102
+
103
+ self .assertEqual (200 , response .status_code )
104
+ self .assertEqual (1 , len (data ))
105
+ self .assertEqual (self .sp3 .sponsor .name , data [0 ]["sponsor" ])
106
+
107
+ def test_filter_sponsorship_by_flight (self ):
108
+ querystring = urlencode ({
109
+ "flight" : LogoPlacementChoices .SIDEBAR .value ,
110
+ })
111
+ url = f"{ self .url } ?{ querystring } "
112
+ response = self .client .get (url , HTTP_AUTHORIZATION = self .authorization )
113
+ data = response .json ()
114
+
115
+ self .assertEqual (200 , response .status_code )
116
+ self .assertEqual (1 , len (data ))
117
+ self .assertEqual (self .sp3 .sponsor .name , data [0 ]["sponsor" ])
118
+ self .assertEqual (self .sp3 .sponsor .slug , data [0 ]["sponsor_slug" ])
119
+
120
+ def test_bad_request_for_invalid_filters (self ):
121
+ querystring = urlencode ({
122
+ "flight" : "invalid-flight" ,
123
+ "publisher" : "invalid-publisher"
124
+ })
125
+ url = f"{ self .url } ?{ querystring } "
126
+ response = self .client .get (url , HTTP_AUTHORIZATION = self .authorization )
127
+ data = response .json ()
128
+
129
+ self .assertEqual (400 , response .status_code )
130
+ self .assertIn ("flight" , data )
131
+ self .assertIn ("publisher" , data )
0 commit comments