<s> from datetime import date , timedelta <EOL> import mock <EOL> from nose . tools import eq_ <EOL> from kitsune . products . tests import ProductFactory <EOL> from kitsune . sumo . tests import TestCase <EOL> from kitsune . users . tests import UserFactory <EOL> from kitsune . wiki . tests import RevisionFactory , DocumentFactory <EOL> from kitsune . wiki . utils import ( <EOL> BitlyUnauthorizedException , BitlyRateLimitException , BitlyException , active_contributors , <EOL> num_active_contributors , generate_short_url ) <EOL> from django . test . utils import override_settings <EOL> class ActiveContributorsTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( ActiveContributorsTestCase , self ) . setUp ( ) <EOL> start_date = date . today ( ) - timedelta ( days = <NUM_LIT:10> ) <EOL> self . start_date = start_date <EOL> before_start = start_date - timedelta ( days = <NUM_LIT:1> ) <EOL> d = DocumentFactory ( locale = '<STR_LIT>' ) <EOL> u = UserFactory ( ) <EOL> self . user = u <EOL> RevisionFactory ( document = d , is_approved = True , reviewer = u ) <EOL> RevisionFactory ( document = d , creator = u ) <EOL> self . product = ProductFactory ( ) <EOL> RevisionFactory ( created = start_date , document__products = [ self . product ] ) <EOL> self . en_us_old = RevisionFactory ( document = d , created = before_start ) <EOL> d = DocumentFactory ( locale = '<STR_LIT>' ) <EOL> RevisionFactory ( document = d , is_approved = True , reviewer = u ) <EOL> RevisionFactory ( document = d , creator = u , reviewer = UserFactory ( ) ) <EOL> RevisionFactory ( document = d , created = start_date ) <EOL> RevisionFactory ( document = d ) <EOL> self . es_old = RevisionFactory ( document = d , created = before_start ) <EOL> def test_active_contributors ( self ) : <EOL> """<STR_LIT>""" <EOL> start_date = self . start_date <EOL> en_us_contributors = active_contributors ( <EOL> from_date = start_date , locale = '<STR_LIT>' ) <EOL> es_contributors = active_contributors ( <EOL> from_date = start_date , locale = '<STR_LIT>' ) <EOL> all_contributors = active_contributors ( from_date = start_date ) <EOL> eq_ ( <NUM_LIT:3> , len ( en_us_contributors ) ) <EOL> assert self . user in en_us_contributors <EOL> assert self . en_us_old . creator not in en_us_contributors <EOL> eq_ ( <NUM_LIT:4> , len ( es_contributors ) ) <EOL> assert self . user in es_contributors <EOL> assert self . es_old . creator not in es_contributors <EOL> eq_ ( <NUM_LIT:6> , len ( all_contributors ) ) <EOL> assert self . user in all_contributors <EOL> assert self . en_us_old . creator not in all_contributors <EOL> assert self . es_old . creator not in all_contributors <EOL> def test_num_active_contributors ( self ) : <EOL> """<STR_LIT>""" <EOL> start_date = self . start_date <EOL> eq_ ( <NUM_LIT:3> , num_active_contributors ( from_date = start_date , locale = '<STR_LIT>' ) ) <EOL> eq_ ( <NUM_LIT:4> , num_active_contributors ( from_date = start_date , locale = '<STR_LIT>' ) ) <EOL> eq_ ( <NUM_LIT:6> , num_active_contributors ( from_date = start_date ) ) <EOL> eq_ ( <NUM_LIT:1> , num_active_contributors ( from_date = start_date , product = self . product ) ) <EOL> eq_ ( <NUM_LIT:1> , num_active_contributors ( from_date = start_date , locale = '<STR_LIT>' , product = self . product ) ) <EOL> eq_ ( <NUM_LIT:0> , num_active_contributors ( from_date = start_date , locale = '<STR_LIT>' , product = self . product ) ) <EOL> @ override_settings ( BITLY_LOGIN = '<STR_LIT:test>' , BITLY_API_KEY = '<STR_LIT>' ) <EOL> class GenerateShortUrlTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . test_url = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_generate_short_url_200 ( self , mock_requests ) : <EOL> """<STR_LIT>""" <EOL> mock_json = mock . Mock ( ) <EOL> mock_json . json . return_value = { <EOL> '<STR_LIT>' : <NUM_LIT:200> , <EOL> '<STR_LIT:data>' : { <EOL> '<STR_LIT:url>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> mock_requests . post . return_value = mock_json <EOL> eq_ ( '<STR_LIT>' , generate_short_url ( self . test_url ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_generate_short_url_401 ( self , mock_requests ) : <EOL> """<STR_LIT>""" <EOL> mock_json = mock . Mock ( ) <EOL> mock_json . json . return_value = { '<STR_LIT>' : <NUM_LIT> } <EOL> mock_requests . post . return_value = mock_json <EOL> self . assertRaises ( BitlyUnauthorizedException , <EOL> generate_short_url , <EOL> self . test_url ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_generate_short_url_403 ( self , mock_requests ) : <EOL> """<STR_LIT>""" <EOL> mock_json = mock . Mock ( ) <EOL> mock_json . json . return_value = { '<STR_LIT>' : <NUM_LIT> } <EOL> mock_requests . post . return_value = mock_json <EOL> self . assertRaises ( BitlyRateLimitException , <EOL> generate_short_url , <EOL> self . test_url ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_generate_short_url_other ( self , mock_requests ) : <EOL> """<STR_LIT>""" <EOL> mock_json = mock . Mock ( ) <EOL> mock_json . json . return_value = { '<STR_LIT>' : <NUM_LIT> } <EOL> mock_requests . post . return_value = mock_json <EOL> self . assertRaises ( BitlyException , generate_short_url , self . test_url ) </s>
