1010from django .test import Client
1111from django .core .urlresolvers import reverse
1212from django .core .files .uploadedfile import InMemoryUploadedFile
13- from django .contrib .auth .models import User
13+ from django .contrib .auth .models import User , Group
1414
1515from api .models .events import Event
1616from api .models import UserProfile
1717
18+ from avatar .models import Avatar
19+ from avatar .util import get_primary_avatar
20+
21+ from web .processors .event import create_or_update_event
22+ from web .processors .event import count_approved_events_for_country
23+
1824from web .tests import EventFactory , ApprovedEventFactory
1925
2026class EventViewsTestCase (TestCase ):
@@ -49,13 +55,40 @@ def test_index_view_without_approved_events(self):
4955 def test_index_view_changing_remote_in_request (self ):
5056 #setup
5157 response = self .client .get (reverse ('web.index' ), {},
52- HTTP_X_FORWARDED_FOR = '93.103.53.11, 93.103.53.11' )
58+ HTTP_X_FORWARDED_FOR = '93.103.53.11, 93.103.53.11' )
5359
5460 #assert
5561 self .assertEquals (200 , response .status_code )
5662 self .assertEquals ((46.0 , 15.0 ), response .context ['lan_lon' ])
5763
5864
65+ def test_search_events_with_search_query (self ):
66+ ApprovedEventFactory .create (title = 'Event Arglebargle - Approved' )
67+ response = self .client .get (reverse ('web.search_events' ), {'q' :'arglebargle' }, REMOTE_ADDR = '93.103.53.11' )
68+
69+ self .assertEquals (1 ,response .context ['events' ].count ())
70+ self .assertEquals ('SI' , response .context ['country' ])
71+
72+ def test_search_events_with_unicode_tag_in_search_query (self ):
73+ ApprovedEventFactory .create (tags = ["jabolčna čežana" ,"José" , "Django" ])
74+ response = self .client .get (reverse ('web.search_events' ), {'q' :'čežana' }, REMOTE_ADDR = '93.103.53.11' )
75+
76+ self .assertEquals (1 ,response .context ['events' ].count ())
77+ self .assertEquals ('SI' , response .context ['country' ])
78+
79+ def test_search_events_with_search_query_multiple_events (self ):
80+ approved1 = ApprovedEventFactory .create (title = "Event Arglebargle - Approved" , country = "SI" )
81+ approved2 = ApprovedEventFactory .create (title = "Event Arglebargle - Approved" , country = "AT" )
82+
83+ response = self .client .get (reverse ('web.search_events' ), {'q' :'arglebargle' }, REMOTE_ADDR = '93.103.53.11' )
84+
85+ self .assertEquals (1 ,response .context ['events' ].count ())
86+ self .assertEquals ('SI' , response .context ['country' ])
87+
88+ approved1 .delete ()
89+ approved2 .delete ()
90+
91+
5992 def test_view_event_without_picture (self ):
6093 test_event = EventFactory .create ()
6194 response = self .client .get (reverse ('web.view_event' , args = [test_event .pk , test_event .slug ]))
@@ -66,6 +99,7 @@ def test_view_event_without_picture(self):
6699 test_event .delete ()
67100
68101
102+
69103@pytest .mark .django_db
70104def test_create_event_with_image (admin_user , admin_client , db ):
71105 with open (local (__file__ ).dirname + '/../../static/img/team/alja.jpg' ) as fp :
@@ -98,6 +132,7 @@ def test_create_event_with_image(admin_user, admin_client, db):
98132 response = admin_client .get (response .url )
99133 assert 'event_picture/alja' in response .content
100134
135+
101136@pytest .mark .django_db
102137def test_edit_event_with_image (admin_user , admin_client , db ):
103138 # First create event
@@ -192,27 +227,138 @@ def test_edit_event_with_image(admin_user, admin_client, db):
192227 response = admin_client .get (event .get_absolute_url ())
193228 assert 'event_picture/ercchy' not in response .content
194229
230+
231+ @pytest .mark .django_db
232+ def test_scoreboard_links_and_results (admin_user , db , client ):
233+
234+ test_country_name = "Slovenia"
235+ test_country_code = "SI"
236+
237+ search_url = (reverse ('web.search_events' ) +
238+ "?country_code=%s&past=yes" % test_country_code )
239+
240+ event_data = {
241+ 'audience' : [3 ],
242+ 'theme' : [1 ,2 ],
243+ 'country' : test_country_code ,
244+ 'description' : u'Lorem ipsum dolor sit amet.' ,
245+ 'location' : test_country_name ,
246+ 'organizer' : u'testko' ,
247+ "creator" : admin_user ,
248+ 'start_date' : datetime .datetime .now (),
249+ 'end_date' : datetime .datetime .now () + datetime .timedelta (days = 3 , hours = 3 ),
250+ 'title' : u'Test Approved Event' ,
251+ 'status' :"APPROVED" ,
252+ }
253+
254+ test_approved_event = create_or_update_event (event_id = None , ** event_data )
255+
256+ for country in count_approved_events_for_country ():
257+ if country ['country_code' ] == test_country_code :
258+ event_count = country ['events' ]
259+
260+ response = client .get (reverse ('web.scoreboard' ))
261+
262+ # We're expecting to see this bit of HTML code with the right
263+ # search URL and the right count for events
264+ expected_result = '''
265+ <span class="country-name">%s</span><p> is participating with </p>
266+ <a href="%s">
267+ <span class="event-number">%s event
268+ ''' % (test_country_name , search_url , event_count )
269+
270+ expected_result = expected_result .replace ('\t ' , '' ).replace ('\n ' , '' )
271+ scoreboard_content = response .content .replace ('\t ' , '' ).replace ('\n ' , '' )
272+
273+ # The search URL shown on scoreboard also has to match search results
274+ search_response = client .get (search_url )
275+ expected_search_result = '<div class="search-counter">%s event' % event_count
276+
277+ assert expected_result in scoreboard_content
278+ assert expected_search_result in search_response .content
279+
280+ test_approved_event .delete ()
281+
282+
283+ @pytest .mark .django_db
284+ def test_ambassadors_list (db , client ):
285+ test_country_name = "Austria"
286+ test_country_code = "AT"
287+
288+ test_username = 'test-amb'
289+ 290+ test_first_name = 'Testko'
291+ test_last_name = 'Test'
292+ test_full_name = test_first_name + " " + test_last_name
293+
294+ test_ambassador = User .objects .create (username = test_username ,
295+ email = test_email ,
296+ first_name = test_first_name ,
297+ last_name = test_last_name )
298+ test_ambassador_profile = UserProfile .objects .create (user = test_ambassador ,
299+ country = test_country_code )
300+
301+ group = Group .objects .get (name = "ambassadors" )
302+ group .user_set .add (test_ambassador )
303+
304+ with open (local (__file__ ).dirname + '/../../static/img/team/alja.jpg' ) as fp :
305+ io = StringIO .StringIO ()
306+ io .write (fp .read ())
307+ uploaded_picture = InMemoryUploadedFile (io , None , "alja17.jpg" , "jpeg" , io .len , None )
308+ uploaded_picture .seek (0 )
309+
310+ avatar = Avatar (user = test_ambassador , primary = True )
311+ avatar .avatar .save (uploaded_picture .name , uploaded_picture )
312+ avatar .save ()
313+
314+ new_avatar = get_primary_avatar (test_ambassador , size = 80 )
315+ test_amb_avatar = new_avatar .avatar_url (80 )
316+
317+ response = client .get (reverse ('web.ambassadors' ))
318+
319+ # We're expecting to the Ambassador under the right country,
320+ # with the right avatar and the right email contact
321+ expected_result = '''
322+ <h2 class="clearfix" id="%s">%s</h2>
323+ <div class="ambassador clearfix">
324+ <img src="%s" alt="%s" width="80" height="80" class="img-circle" />
325+ <h4>%s <span> <a href="mailto:%s" alt="Send me an email"><i class="fa fa-envelope"></i></a>
326+ ''' % (test_country_name , test_country_name , test_amb_avatar , test_username , test_full_name , test_email )
327+
328+ expected_result = expected_result .replace ('\t ' , '' ).replace ('\n ' , '' )
329+ ambassadors_content = response .content .replace ('\t ' , '' ).replace ('\n ' , '' )
330+
331+ assert expected_result in ambassadors_content
332+
333+ test_ambassador .delete ()
334+ avatar .delete ()
335+
336+
195337@pytest .mark .django_db
196338def test_nonexistent_event (db , client ):
197339 response = client .get (reverse ('web.view_event' , args = [1234 , 'shouldnt-exist' ]))
198340
199341 assert response .status_code == 404
200342
343+
201344@pytest .mark .django_db
202345def test_geoip_slovenian_ip (db , client ):
203346 response = client .get ('/' , REMOTE_ADDR = '93.103.53.1' )
204347
205348 assert 'List all events in <span id="country"> Slovenia' in response .content
206349
350+
207351@pytest .mark .django_db
208352def test_geoip_invalid_ip (db , client ):
209353 response = client .get ('/' , REMOTE_ADDR = '127.0.0.1' )
210354
211355 assert 'List all events' in response .content
212356 assert 'List all events <span' not in response .content
213357
358+
214359@pytest .mark .django_db
215360def test_list_events_for_country_code (db , client ):
216361 response = client .get (reverse ('web.view_event_by_country' , args = ['SI' ]))
217362
218363 assert response .status_code == 200
364+
0 commit comments