55import random
66import jwt
77from unittest .case import TestCase
8+ import json
89
910import os
11+ import sys
1012import datetime
1113import copy
1214import requests
1315from stream import serializer
14- from requests .exceptions import ConnectionError , MissingSchema
16+ from requests .exceptions import MissingSchema
1517
1618try :
1719 from urlparse import urlparse , parse_qs
1820except ImportError :
1921 from urllib .parse import urlparse , parse_qs
2022
23+
2124def connect_debug ():
25+ try :
26+ key = os .environ ['STREAM_KEY' ]
27+ secret = os .environ ['STREAM_SECRET' ]
28+ except KeyError :
29+ print ('To run the tests the STREAM_KEY and STREAM_SECRET variables '
30+ 'need to be available. \n '
31+ 'Please create a pull request if you are an external '
32+ 'contributor, because these variables are automatically added '
33+ 'by Travis.' )
34+ sys .exit (1 )
35+
2236 return stream .connect (
23- 'q56mdvdzreye' ,
24- 'spmf6x2b2v2tqg93sfp5t393wfcxru58zm7jr3ynf7dmmndw5y8chux25hs63znf' ,
37+ key ,
38+ secret ,
2539 location = 'us-east' ,
2640 timeout = 10
2741 )
@@ -385,15 +399,20 @@ def test_follow_and_delete(self):
385399 agg_feed .follow (user_feed .slug , user_feed .user_id )
386400 user_feed .remove_activity (activity_id )
387401
388- self ._test_sleep (10 , 0.25 )
389-
390- for x in range (5 ):
402+ for x in range (40 ):
391403 activities = agg_feed .get (limit = 3 )['results' ]
392404 activity = self ._get_first_aggregated_activity (activities )
393- activity_id_found = activity ['id' ] if activity is not None else None
394- self .assertNotEqual (activity_id_found , activity_id )
395- # self._test_sleep(1, 0.1)
405+ activity_id_found = (activity ['id' ] if activity is not None
406+ else None )
407+
408+ try :
409+ self .assertNotEqual (activity_id_found , activity_id )
410+ except AssertionError :
411+ self ._test_sleep (1 , 0.1 )
412+ else :
413+ break
396414
415+ self .assertNotEqual (activity_id_found , activity_id )
397416
398417 # def test_follow_private(self):
399418 # feed = getfeed('secret', 'py1')
@@ -842,18 +861,29 @@ def test_add_to_many(self):
842861 self .assertEqual (feed .get ()['results' ][0 ]['custom' ], 'data' )
843862
844863 def test_create_email_redirect (self ):
845- expected_parts = ['https://analytics.getstream.io/analytics/redirect/' ,
846- 'auth_type=jwt' ,
847- 'url=http%3A%2F%2Fgoogle.com%2F%3Fa%3Db%26c%3Dd' ,
848- 'api_key=%s' % self .c .api_key ,
849- 'events=%5B%7B%22foreign_ids%22%3A+%5B%22tweet%3A1%22%2C+%22tweet%3A2%22%2C+%22tweet%3A3%22%2C+%22tweet%3A4%22%2C+%22tweet%3A5%22%5D%2C+%22feed_id%22%3A+%22user%3Aglobal%22%2C+%22user_id%22%3A+%22tommaso%22%2C+%22location%22%3A+%22email%22%7D%2C+%7B%22user_id%22%3A+%22tommaso%22%2C+%22label%22%3A+%22click%22%2C+%22feed_id%22%3A+%22user%3Aglobal%22%2C+%22location%22%3A+%22email%22%2C+%22position%22%3A+3%2C+%22foreign_id%22%3A+%22tweet%3A1%22%7D%5D' ,
850- ]
851- engagement = {'foreign_id' : 'tweet:1' , 'label' : 'click' , 'position' : 3 , 'user_id' : 'tommaso' , 'location' : 'email' , 'feed_id' : 'user:global' }
852- impression = {'foreign_ids' : ['tweet:1' , 'tweet:2' , 'tweet:3' , 'tweet:4' , 'tweet:5' ], 'user_id' :
853- 'tommaso' , 'location' : 'email' , 'feed_id' : 'user:global' }
854- events = [impression , engagement ]
855864 target_url = 'http://google.com/?a=b&c=d'
856865 user_id = 'tommaso'
866+
867+ impression = {
868+ 'foreign_ids' : ['tweet:1' ,
869+ 'tweet:2' ,
870+ 'tweet:3' ,
871+ 'tweet:4' ,
872+ 'tweet:5' ],
873+ 'feed_id' : 'user:global' ,
874+ 'user_id' : user_id ,
875+ 'location' : 'email'
876+ }
877+ engagement = {
878+ 'user_id' : user_id ,
879+ 'label' : 'click' ,
880+ 'feed_id' : 'user:global' ,
881+ 'location' : 'email' ,
882+ 'position' : 3 ,
883+ 'foreign_id' : 'tweet:1'
884+ }
885+ events = [impression , engagement ]
886+
857887 redirect_url = self .c .create_redirect_url (target_url , user_id , events )
858888
859889 parsed_url = urlparse (redirect_url )
@@ -867,9 +897,18 @@ def test_create_email_redirect(self):
867897 'user_id' : 'tommaso'
868898 })
869899
870- for part in expected_parts :
871- if part not in redirect_url :
872- raise ValueError ('didnt find %s in url \n %s' % (part , redirect_url ))
900+ expected_params = {
901+ 'auth_type' : 'jwt' ,
902+ 'url' : target_url ,
903+ 'api_key' : self .c .api_key ,
904+ }
905+
906+ for k , v in expected_params .items ():
907+ self .assertEqual (qs [k ][0 ], v )
908+
909+ self .assertEqual (json .loads (qs ['events' ][0 ]), events )
910+
911+
873912
874913 def test_email_redirect_invalid_target (self ):
875914 engagement = {'foreign_id' : 'tweet:1' , 'label' : 'click' , 'position' : 3 , 'user_id' : 'tommaso' , 'location' : 'email' , 'feed_id' : 'user:global' }
0 commit comments