<s> import json <EOL> import os <EOL> from django . conf import settings <EOL> from django . core . files . storage import default_storage as storage <EOL> import mock <EOL> import pytest <EOL> from PIL import Image <EOL> from olympia import amo , paypal <EOL> from olympia . amo . tests import TestCase <EOL> from olympia . amo . tests . test_helpers import get_image_path <EOL> from olympia . amo . urlresolvers import reverse <EOL> from olympia . amo . helpers import user_media_path <EOL> from olympia . applications . models import AppVersion <EOL> from olympia . addons . forms import EditThemeForm , EditThemeOwnerForm , ThemeForm <EOL> from olympia . addons . models import Addon , Category , Charity , Persona <EOL> from olympia . devhub import forms <EOL> from olympia . editors . models import RereviewQueueTheme <EOL> from olympia . files . helpers import copyfileobj <EOL> from olympia . files . models import FileUpload <EOL> from olympia . tags . models import Tag <EOL> from olympia . users . models import UserProfile <EOL> from olympia . versions . models import ApplicationsVersions , License , Version <EOL> class TestNewAddonForm ( TestCase ) : <EOL> def test_only_valid_uploads ( self ) : <EOL> upload = FileUpload . objects . create ( valid = False ) <EOL> form = forms . NewAddonForm ( <EOL> { '<STR_LIT>' : upload . uuid , '<STR_LIT>' : [ <NUM_LIT:1> ] } , <EOL> request = mock . Mock ( ) ) <EOL> assert ( '<STR_LIT>' in <EOL> form . errors . get ( '<STR_LIT>' ) ) , form . errors <EOL> upload . validation = '<STR_LIT>' <EOL> upload . save ( ) <EOL> form = forms . NewAddonForm ( <EOL> { '<STR_LIT>' : upload . uuid , '<STR_LIT>' : [ <NUM_LIT:1> ] } , <EOL> request = mock . Mock ( ) ) <EOL> assert ( '<STR_LIT>' not in <EOL> form . errors . get ( '<STR_LIT>' ) ) , form . errors <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , lambda xpi , path : None ) <EOL> @ mock . patch ( '<STR_LIT>' , lambda xpi : None ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_check_xpi_called ( self , mock_check_xpi_info , mock_parse ) : <EOL> """<STR_LIT>""" <EOL> mock_parse . return_value = None <EOL> mock_check_xpi_info . return_value = { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:type>' : <NUM_LIT:2> } <EOL> upload = FileUpload . objects . create ( valid = True ) <EOL> form = forms . NewAddonForm ( <EOL> { '<STR_LIT>' : upload . uuid , '<STR_LIT>' : [ <NUM_LIT:1> ] } , <EOL> request = mock . Mock ( ) ) <EOL> form . clean ( ) <EOL> assert mock_check_xpi_info . called <EOL> class TestNewVersionForm ( TestCase ) : <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , lambda xpi , path : None ) <EOL> @ mock . patch ( '<STR_LIT>' , lambda xpi : None ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_check_xpi_called ( self , mock_check_xpi_info , mock_parse ) : <EOL> """<STR_LIT>""" <EOL> mock_parse . return_value = None <EOL> mock_check_xpi_info . return_value = { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:type>' : <NUM_LIT:2> } <EOL> upload = FileUpload . objects . create ( valid = True ) <EOL> addon = Addon . objects . create ( ) <EOL> form = forms . NewVersionForm ( <EOL> { '<STR_LIT>' : upload . uuid , '<STR_LIT>' : [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : amo . STATUS_NOMINATED } , <EOL> addon = addon , <EOL> request = mock . Mock ( ) ) <EOL> form . clean ( ) <EOL> assert mock_check_xpi_info . called <EOL> class TestNewFileForm ( TestCase ) : <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , lambda xpi , path : None ) <EOL> @ mock . patch ( '<STR_LIT>' , lambda xpi : None ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_check_xpi_called ( self , mock_check_xpi_info , mock_parse ) : <EOL> """<STR_LIT>""" <EOL> mock_parse . return_value = None <EOL> mock_check_xpi_info . return_value = { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:type>' : <NUM_LIT:2> } <EOL> upload = FileUpload . objects . create ( valid = True ) <EOL> addon = Addon . objects . create ( ) <EOL> version = Version . objects . create ( addon = addon ) <EOL> version . compatible_platforms = mock . Mock ( ) <EOL> version . compatible_platforms . return_value = amo . SUPPORTED_PLATFORMS <EOL> form = forms . NewFileForm ( <EOL> { '<STR_LIT>' : upload . uuid , '<STR_LIT>' : [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : amo . STATUS_NOMINATED , <EOL> '<STR_LIT>' : '<STR_LIT:1>' } , <EOL> addon = addon , <EOL> version = version , <EOL> request = mock . Mock ( ) ) <EOL> form . clean ( ) <EOL> assert mock_check_xpi_info . called <EOL> class TestContribForm ( TestCase ) : <EOL> def test_neg_suggested_amount ( self ) : <EOL> form = forms . ContribForm ( { '<STR_LIT>' : - <NUM_LIT:10> } ) <EOL> assert not form . is_valid ( ) <EOL> assert form . errors [ '<STR_LIT>' ] [ <NUM_LIT:0> ] == ( <EOL> '<STR_LIT>' ) <EOL> def test_max_suggested_amount ( self ) : <EOL> form = forms . ContribForm ( <EOL> { '<STR_LIT>' : settings . MAX_CONTRIBUTION + <NUM_LIT:10> } ) <EOL> assert not form . is_valid ( ) <EOL> assert form . errors [ '<STR_LIT>' ] [ <NUM_LIT:0> ] == ( <EOL> '<STR_LIT>' % <EOL> settings . MAX_CONTRIBUTION ) <EOL> class TestCharityForm ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestCharityForm , self ) . setUp ( ) <EOL> self . paypal_mock = mock . Mock ( ) <EOL> self . paypal_mock . return_value = ( True , None ) <EOL> paypal . check_paypal_id = self . paypal_mock <EOL> def test_always_new ( self ) : <EOL> params = dict ( name = '<STR_LIT:name>' , url = '<STR_LIT>' , paypal = '<STR_LIT>' ) <EOL> charity = forms . CharityForm ( params ) . save ( ) <EOL> for k , v in params . items ( ) : <EOL> assert getattr ( charity , k ) == v <EOL> assert charity . id <EOL> instance = Charity . objects . get ( id = charity . id ) <EOL> params [ '<STR_LIT:name>' ] = '<STR_LIT>' <EOL> new_charity = forms . CharityForm ( params , instance = instance ) . save ( ) <EOL> for k , v in params . items ( ) : <EOL> assert getattr ( new_charity , k ) == v <EOL> assert new_charity . id != charity . id <EOL> class TestCompatForm ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def test_mozilla_app ( self ) : <EOL> moz = amo . MOZILLA <EOL> appver = AppVersion . objects . create ( application = moz . id ) <EOL> v = Addon . objects . get ( id = <NUM_LIT> ) . current_version <EOL> ApplicationsVersions ( application = moz . id , version = v , <EOL> min = appver , max = appver ) . save ( ) <EOL> fs = forms . CompatFormSet ( None , queryset = v . apps . all ( ) ) <EOL> apps = [ f . app for f in fs . forms ] <EOL> assert moz in apps <EOL> class TestPreviewForm ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> super ( TestPreviewForm , self ) . setUp ( ) <EOL> self . dest = os . path . join ( settings . TMP_PATH , '<STR_LIT>' ) <EOL> if not os . path . exists ( self . dest ) : <EOL> os . makedirs ( self . dest ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_preview_modified ( self , update_mock ) : <EOL> addon = Addon . objects . get ( pk = <NUM_LIT> ) <EOL> name = '<STR_LIT>' <EOL> form = forms . PreviewForm ( { '<STR_LIT>' : '<STR_LIT:test>' , '<STR_LIT>' : name , <EOL> '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> with storage . open ( os . path . join ( self . dest , name ) , '<STR_LIT:w>' ) as f : <EOL> copyfileobj ( open ( get_image_path ( name ) ) , f ) <EOL> assert form . is_valid ( ) <EOL> form . save ( addon ) <EOL> assert update_mock . called <EOL> def test_preview_size ( self ) : <EOL> addon = Addon . objects . get ( pk = <NUM_LIT> ) <EOL> name = '<STR_LIT>' <EOL> form = forms . PreviewForm ( { '<STR_LIT>' : '<STR_LIT:test>' , '<STR_LIT>' : name , <EOL> '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> with storage . open ( os . path . join ( self . dest , name ) , '<STR_LIT:w>' ) as f : <EOL> copyfileobj ( open ( get_image_path ( name ) ) , f ) <EOL> assert form . is_valid ( ) <EOL> form . save ( addon ) <EOL> assert addon . previews . all ( ) [ <NUM_LIT:0> ] . sizes == ( <EOL> { u'<STR_LIT:image>' : [ <NUM_LIT> , <NUM_LIT> ] , u'<STR_LIT>' : [ <NUM_LIT> , <NUM_LIT> ] } ) <EOL> class TestThemeForm ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> super ( TestThemeForm , self ) . setUp ( ) <EOL> self . populate ( ) <EOL> self . request = mock . Mock ( ) <EOL> self . request . user = mock . Mock ( ) <EOL> self . request . user . groups_list = [ ] <EOL> self . request . user . is_authenticated . return_value = True <EOL> def populate ( self ) : <EOL> self . cat = Category . objects . create ( application = amo . FIREFOX . id , <EOL> type = amo . ADDON_PERSONA , name = '<STR_LIT>' ) <EOL> License . objects . create ( id = amo . LICENSE_CC_BY . id ) <EOL> def get_dict ( self , ** kw ) : <EOL> data = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . cat . id , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : amo . LICENSE_CC_BY . id , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> data . update ( ** kw ) <EOL> return data <EOL> def post ( self , ** kw ) : <EOL> self . form = ThemeForm ( self . get_dict ( ** kw ) , request = self . request ) <EOL> return self . form <EOL> def test_name_unique ( self ) : <EOL> Addon . objects . create ( type = amo . ADDON_PERSONA , name = '<STR_LIT>' ) <EOL> for name in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . post ( name = name ) <EOL> assert not self . form . is_valid ( ) <EOL> assert self . form . errors == ( <EOL> { '<STR_LIT:name>' : [ '<STR_LIT>' <EOL> '<STR_LIT>' ] } ) <EOL> def test_name_required ( self ) : <EOL> self . post ( name = '<STR_LIT>' ) <EOL> assert not self . form . is_valid ( ) <EOL> assert self . form . errors == { '<STR_LIT:name>' : [ '<STR_LIT>' ] } <EOL> def test_name_length ( self ) : <EOL> self . post ( name = '<STR_LIT:a>' * <NUM_LIT> ) <EOL> assert not self . form . is_valid ( ) <EOL> assert self . form . errors == { <EOL> '<STR_LIT:name>' : [ '<STR_LIT>' <EOL> '<STR_LIT>' ] } <EOL> def test_slug_unique ( self ) : <EOL> Addon . objects . create ( type = amo . ADDON_PERSONA , slug = '<STR_LIT>' ) <EOL> for slug in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . post ( slug = slug ) <EOL> assert not self . form . is_valid ( ) <EOL> assert self . form . errors == { <EOL> '<STR_LIT>' : [ '<STR_LIT>' <EOL> '<STR_LIT>' ] } <EOL> def test_slug_required ( self ) : <EOL> self . post ( slug = '<STR_LIT>' ) <EOL> assert not self . form . is_valid ( ) <EOL> assert self . form . errors == { '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> def test_slug_length ( self ) : <EOL> self . post ( slug = '<STR_LIT:a>' * <NUM_LIT> ) <EOL> assert not self . form . is_valid ( ) <EOL> assert self . form . errors == { <EOL> '<STR_LIT>' : [ '<STR_LIT>' <EOL> '<STR_LIT>' ] } <EOL> def test_description_optional ( self ) : <EOL> self . post ( description = '<STR_LIT>' ) <EOL> assert self . form . is_valid ( ) <EOL> def test_description_length ( self ) : <EOL> self . post ( description = '<STR_LIT:a>' * <NUM_LIT> ) <EOL> assert not self . form . is_valid ( ) <EOL> assert self . form . errors == ( <EOL> { '<STR_LIT:description>' : [ '<STR_LIT>' <EOL> '<STR_LIT>' ] } ) <EOL> def test_categories_required ( self ) : <EOL> self . post ( category = '<STR_LIT>' ) <EOL> assert not self . form . is_valid ( ) <EOL> assert self . form . errors == { '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> def test_license_required ( self ) : <EOL> self . post ( license = '<STR_LIT>' ) <EOL> assert not self . form . is_valid ( ) <EOL> assert self . form . errors == { '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> def test_header_hash_required ( self ) : <EOL> self . post ( header_hash = '<STR_LIT>' ) <EOL> assert not self . form . is_valid ( ) <EOL> assert self . form . errors == { '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> def test_footer_hash_optional ( self ) : <EOL> self . post ( footer_hash = '<STR_LIT>' ) <EOL> assert self . form . is_valid ( ) <EOL> def test_accentcolor_optional ( self ) : <EOL> self . post ( accentcolor = '<STR_LIT>' ) <EOL> assert self . form . is_valid ( ) <EOL> def test_accentcolor_invalid ( self ) : <EOL> self . post ( accentcolor = '<STR_LIT>' ) <EOL> assert not self . form . is_valid ( ) <EOL> assert self . form . errors == ( <EOL> { '<STR_LIT>' : [ '<STR_LIT>' <EOL> '<STR_LIT>' ] } ) <EOL> def test_textcolor_optional ( self ) : <EOL> self . post ( textcolor = '<STR_LIT>' ) <EOL> assert self . form . is_valid ( ) , self . form . errors <EOL> def test_textcolor_invalid ( self ) : <EOL> self . post ( textcolor = '<STR_LIT>' ) <EOL> assert not self . form . is_valid ( ) <EOL> assert self . form . errors == ( <EOL> { '<STR_LIT>' : [ '<STR_LIT>' <EOL> '<STR_LIT>' ] } ) <EOL> def get_img_urls ( self ) : <EOL> return ( <EOL> reverse ( '<STR_LIT>' , args = [ '<STR_LIT>' ] ) , <EOL> reverse ( '<STR_LIT>' , args = [ '<STR_LIT>' ] ) <EOL> ) <EOL> def test_img_attrs ( self ) : <EOL> header_url , footer_url = self . get_img_urls ( ) <EOL> self . post ( ) <EOL> assert self . form . fields [ '<STR_LIT>' ] . widget . attrs == ( <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : header_url } ) <EOL> assert self . form . fields [ '<STR_LIT>' ] . widget . attrs == ( <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : footer_url } ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ pytest . mark . skipif ( not hasattr ( Image . core , '<STR_LIT>' ) , <EOL> reason = '<STR_LIT>' ) <EOL> def test_success ( self , save_persona_image_mock , <EOL> create_persona_preview_images_mock , make_checksum_mock ) : <EOL> make_checksum_mock . return_value = '<STR_LIT>' <EOL> self . request . user = UserProfile . objects . get ( pk = <NUM_LIT> ) <EOL> data = self . get_dict ( ) <EOL> header_url , footer_url = self . get_img_urls ( ) <EOL> img = open ( get_image_path ( '<STR_LIT>' ) , '<STR_LIT:rb>' ) <EOL> r_ajax = self . client . post ( header_url , { '<STR_LIT>' : img } ) <EOL> data . update ( header_hash = json . loads ( r_ajax . content ) [ '<STR_LIT>' ] ) <EOL> img = open ( get_image_path ( '<STR_LIT>' ) , '<STR_LIT:rb>' ) <EOL> r_ajax = self . client . post ( footer_url , { '<STR_LIT>' : img } ) <EOL> data . update ( footer_hash = json . loads ( r_ajax . content ) [ '<STR_LIT>' ] ) <EOL> self . post ( ) <EOL> assert self . form . is_valid ( ) , self . form . errors <EOL> self . form . save ( ) <EOL> addon = Addon . objects . filter ( type = amo . ADDON_PERSONA ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> persona = addon . persona <EOL> assert unicode ( addon . name ) == data [ '<STR_LIT:name>' ] <EOL> assert addon . slug == data [ '<STR_LIT>' ] <EOL> self . assertSetEqual ( addon . categories . values_list ( '<STR_LIT:id>' , flat = True ) , <EOL> [ self . cat . id ] ) <EOL> self . assertSetEqual ( addon . tags . values_list ( '<STR_LIT>' , flat = True ) , <EOL> data [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002CU+0020>' ) ) <EOL> assert persona . persona_id == <NUM_LIT:0> <EOL> assert persona . license == data [ '<STR_LIT>' ] <EOL> assert persona . accentcolor == data [ '<STR_LIT>' ] . lstrip ( '<STR_LIT:#>' ) <EOL> assert persona . textcolor == data [ '<STR_LIT>' ] . lstrip ( '<STR_LIT:#>' ) <EOL> assert persona . author == self . request . user . username <EOL> assert persona . display_username == self . request . user . name <EOL> assert not persona . dupe_persona <EOL> v = addon . versions . all ( ) <EOL> assert len ( v ) == <NUM_LIT:1> <EOL> assert v [ <NUM_LIT:0> ] . version == '<STR_LIT:0>' <EOL> dst = os . path . join ( user_media_path ( '<STR_LIT>' ) , str ( addon . id ) ) <EOL> header_src = os . path . join ( settings . TMP_PATH , '<STR_LIT>' , <EOL> u'<STR_LIT>' ) <EOL> footer_src = os . path . join ( settings . TMP_PATH , '<STR_LIT>' , <EOL> u'<STR_LIT>' ) <EOL> assert save_persona_image_mock . mock_calls == ( <EOL> [ mock . call ( src = header_src , <EOL> full_dst = os . path . join ( dst , '<STR_LIT>' ) ) , <EOL> mock . call ( src = footer_src , <EOL> full_dst = os . path . join ( dst , '<STR_LIT>' ) ) ] ) <EOL> create_persona_preview_images_mock . assert_called_with ( <EOL> src = header_src , <EOL> full_dst = [ os . path . join ( dst , '<STR_LIT>' ) , <EOL> os . path . join ( dst , '<STR_LIT>' ) ] , <EOL> set_modified_on = [ addon ] ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_dupe_persona ( self , make_checksum_mock , mock1 , mock2 ) : <EOL> """<STR_LIT>""" <EOL> make_checksum_mock . return_value = '<STR_LIT>' <EOL> self . request . user = UserProfile . objects . get ( pk = <NUM_LIT> ) <EOL> self . post ( ) <EOL> assert self . form . is_valid ( ) , self . form . errors <EOL> self . form . save ( ) <EOL> self . post ( name = '<STR_LIT>' , slug = '<STR_LIT>' ) <EOL> assert self . form . is_valid ( ) , self . form . errors <EOL> self . form . save ( ) <EOL> personas = Persona . objects . order_by ( '<STR_LIT>' ) <EOL> assert personas [ <NUM_LIT:0> ] . checksum == personas [ <NUM_LIT:1> ] . checksum <EOL> assert personas [ <NUM_LIT:1> ] . dupe_persona == personas [ <NUM_LIT:0> ] <EOL> assert personas [ <NUM_LIT:0> ] . dupe_persona is None <EOL> class TestEditThemeForm ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> super ( TestEditThemeForm , self ) . setUp ( ) <EOL> self . populate ( ) <EOL> self . request = mock . Mock ( ) <EOL> self . request . user = mock . Mock ( ) <EOL> self . request . user . groups_list = [ ] <EOL> self . request . user . username = '<STR_LIT>' <EOL> self . request . user . name = '<STR_LIT>' <EOL> self . request . user . is_authenticated . return_value = True <EOL> def populate ( self ) : <EOL> self . instance = Addon . objects . create ( <EOL> type = amo . ADDON_PERSONA , status = amo . STATUS_PUBLIC , <EOL> slug = '<STR_LIT>' , name = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) <EOL> self . cat = Category . objects . create ( <EOL> type = amo . ADDON_PERSONA , name = '<STR_LIT>' ) <EOL> self . instance . addoncategory_set . create ( category = self . cat ) <EOL> self . license = amo . LICENSE_CC_BY . id <EOL> self . theme = Persona . objects . create ( <EOL> persona_id = <NUM_LIT:0> , addon_id = self . instance . id , license = self . license , <EOL> accentcolor = '<STR_LIT>' , textcolor = '<STR_LIT>' ) <EOL> Tag ( tag_text = '<STR_LIT>' ) . save_tag ( self . instance ) <EOL> Tag ( tag_text = '<STR_LIT>' ) . save_tag ( self . instance ) <EOL> def get_dict ( self , ** kw ) : <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . cat . id , <EOL> '<STR_LIT>' : self . license , <EOL> '<STR_LIT>' : self . instance . slug , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : unicode ( self . instance . name ) , <EOL> '<STR_LIT>' : unicode ( self . instance . description ) , <EOL> } <EOL> data . update ( ** kw ) <EOL> return data <EOL> def test_initial ( self ) : <EOL> self . form = EditThemeForm ( None , request = self . request , <EOL> instance = self . instance ) <EOL> eq_data = self . get_dict ( ) <EOL> for k in [ k for k in self . form . initial . keys ( ) <EOL> if k not in [ '<STR_LIT:name>' , '<STR_LIT:description>' ] ] : <EOL> assert self . form . initial [ k ] == eq_data [ k ] <EOL> def save_success ( self ) : <EOL> other_cat = Category . objects . create ( type = amo . ADDON_PERSONA ) <EOL> self . data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : other_cat . id , <EOL> '<STR_LIT>' : amo . LICENSE_CC_BY_NC_SA . id , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . form = EditThemeForm ( self . data , request = self . request , <EOL> instance = self . instance ) <EOL> eq_data = self . get_dict ( ) <EOL> for k in [ k for k in self . form . initial . keys ( ) <EOL> if k not in [ '<STR_LIT:name>' , '<STR_LIT:description>' ] ] : <EOL> assert self . form . initial [ k ] == eq_data [ k ] <EOL> assert self . form . data == self . data <EOL> assert self . form . is_valid ( ) , self . form . errors <EOL> self . form . save ( ) <EOL> def test_success ( self ) : <EOL> self . save_success ( ) <EOL> self . instance = self . instance . reload ( ) <EOL> assert unicode ( self . instance . persona . accentcolor ) == ( <EOL> self . data [ '<STR_LIT>' ] . lstrip ( '<STR_LIT:#>' ) ) <EOL> assert self . instance . categories . all ( ) [ <NUM_LIT:0> ] . id == self . data [ '<STR_LIT>' ] <EOL> assert self . instance . persona . license == self . data [ '<STR_LIT>' ] <EOL> assert unicode ( self . instance . name ) == self . data [ '<STR_LIT>' ] <EOL> assert unicode ( self . instance . description ) == ( <EOL> self . data [ '<STR_LIT>' ] ) <EOL> self . assertSetEqual ( <EOL> self . instance . tags . values_list ( '<STR_LIT>' , flat = True ) , <EOL> [ self . data [ '<STR_LIT>' ] ] ) <EOL> assert unicode ( self . instance . persona . textcolor ) == ( <EOL> self . data [ '<STR_LIT>' ] . lstrip ( '<STR_LIT:#>' ) ) <EOL> def test_success_twice ( self ) : <EOL> """<STR_LIT>""" <EOL> self . save_success ( ) <EOL> self . form . save ( ) <EOL> def test_name_unique ( self ) : <EOL> data = self . get_dict ( ** { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> Addon . objects . create ( type = amo . ADDON_PERSONA , status = amo . STATUS_PUBLIC , <EOL> name = data [ '<STR_LIT>' ] ) <EOL> self . form = EditThemeForm ( data , request = self . request , <EOL> instance = self . instance ) <EOL> assert not self . form . is_valid ( ) <EOL> assert self . form . errors == { <EOL> '<STR_LIT:name>' : <EOL> [ ( '<STR_LIT>' , '<STR_LIT>' ) ] } <EOL> def test_localize_name_description ( self ) : <EOL> data = self . get_dict ( name_de = '<STR_LIT>' , <EOL> description_de = '<STR_LIT>' ) <EOL> self . form = EditThemeForm ( data , request = self . request , <EOL> instance = self . instance ) <EOL> assert self . form . is_valid ( ) , self . form . errors <EOL> self . form . save ( ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_reupload ( self , save_persona_image_mock , <EOL> create_persona_preview_images_mock , <EOL> make_checksum_mock ) : <EOL> make_checksum_mock . return_value = '<STR_LIT>' <EOL> data = self . get_dict ( header_hash = '<STR_LIT>' , footer_hash = '<STR_LIT>' ) <EOL> self . form = EditThemeForm ( data , request = self . request , <EOL> instance = self . instance ) <EOL> assert self . form . is_valid ( ) <EOL> self . form . save ( ) <EOL> dst = os . path . join ( user_media_path ( '<STR_LIT>' ) , str ( self . instance . id ) ) <EOL> header_src = os . path . join ( settings . TMP_PATH , '<STR_LIT>' , <EOL> u'<STR_LIT>' ) <EOL> footer_src = os . path . join ( settings . TMP_PATH , '<STR_LIT>' , <EOL> u'<STR_LIT>' ) <EOL> assert save_persona_image_mock . mock_calls == ( <EOL> [ mock . call ( src = header_src , <EOL> full_dst = os . path . join ( dst , '<STR_LIT>' ) ) , <EOL> mock . call ( src = footer_src , <EOL> full_dst = os . path . join ( dst , '<STR_LIT>' ) ) ] ) <EOL> rqt = RereviewQueueTheme . objects . filter ( theme = self . instance . persona ) <EOL> assert rqt . count ( ) == <NUM_LIT:1> <EOL> assert rqt [ <NUM_LIT:0> ] . header == '<STR_LIT>' <EOL> assert rqt [ <NUM_LIT:0> ] . footer == '<STR_LIT>' <EOL> assert not rqt [ <NUM_LIT:0> ] . dupe_persona <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> new = mock . Mock ) <EOL> @ mock . patch ( '<STR_LIT>' , new = mock . Mock ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_reupload_duplicate ( self , make_checksum_mock ) : <EOL> make_checksum_mock . return_value = '<STR_LIT>' <EOL> theme = amo . tests . addon_factory ( type = amo . ADDON_PERSONA ) <EOL> theme . persona . checksum = '<STR_LIT>' <EOL> theme . persona . save ( ) <EOL> data = self . get_dict ( header_hash = '<STR_LIT>' , footer_hash = '<STR_LIT>' ) <EOL> self . form = EditThemeForm ( data , request = self . request , <EOL> instance = self . instance ) <EOL> assert self . form . is_valid ( ) <EOL> self . form . save ( ) <EOL> rqt = RereviewQueueTheme . objects . get ( theme = self . instance . persona ) <EOL> assert rqt . dupe_persona == theme . persona <EOL> @ mock . patch ( '<STR_LIT>' , new = mock . Mock ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> new = mock . Mock ) <EOL> @ mock . patch ( '<STR_LIT>' , new = mock . Mock ) <EOL> def test_reupload_legacy_header_only ( self ) : <EOL> """<STR_LIT>""" <EOL> self . theme . header = '<STR_LIT>' <EOL> self . theme . footer = '<STR_LIT>' <EOL> self . theme . save ( ) <EOL> data = self . get_dict ( header_hash = '<STR_LIT>' ) <EOL> self . form = EditThemeForm ( data , request = self . request , <EOL> instance = self . instance ) <EOL> assert self . form . is_valid ( ) <EOL> self . form . save ( ) <EOL> rqt = RereviewQueueTheme . objects . get ( ) <EOL> assert rqt . header == '<STR_LIT>' <EOL> assert rqt . footer == '<STR_LIT>' <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_reupload_no_footer ( self , save_persona_image_mock , <EOL> create_persona_preview_images_mock , <EOL> make_checksum_mock ) : <EOL> make_checksum_mock . return_value = '<STR_LIT>' <EOL> data = self . get_dict ( header_hash = '<STR_LIT>' , footer_hash = '<STR_LIT>' ) <EOL> self . form = EditThemeForm ( data , request = self . request , <EOL> instance = self . instance ) <EOL> assert self . form . is_valid ( ) <EOL> self . form . save ( ) <EOL> dst = os . path . join ( user_media_path ( '<STR_LIT>' ) , str ( self . instance . id ) ) <EOL> header_src = os . path . join ( settings . TMP_PATH , '<STR_LIT>' , <EOL> u'<STR_LIT>' ) <EOL> assert save_persona_image_mock . mock_calls == ( <EOL> [ mock . call ( src = header_src , <EOL> full_dst = os . path . join ( dst , '<STR_LIT>' ) ) ] ) <EOL> rqt = RereviewQueueTheme . objects . filter ( theme = self . instance . persona ) <EOL> assert rqt . count ( ) == <NUM_LIT:1> <EOL> assert rqt [ <NUM_LIT:0> ] . header == '<STR_LIT>' <EOL> assert rqt [ <NUM_LIT:0> ] . footer == '<STR_LIT>' <EOL> assert not rqt [ <NUM_LIT:0> ] . dupe_persona <EOL> class TestEditThemeOwnerForm ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> super ( TestEditThemeOwnerForm , self ) . setUp ( ) <EOL> self . instance = Addon . objects . create ( <EOL> type = amo . ADDON_PERSONA , <EOL> status = amo . STATUS_PUBLIC , slug = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , description = '<STR_LIT>' ) <EOL> Persona . objects . create ( <EOL> persona_id = <NUM_LIT:0> , addon_id = self . instance . id , <EOL> license = amo . LICENSE_CC_BY . id , accentcolor = '<STR_LIT>' , <EOL> textcolor = '<STR_LIT>' ) <EOL> def test_initial ( self ) : <EOL> self . form = EditThemeOwnerForm ( None , instance = self . instance ) <EOL> assert self . form . initial == { } <EOL> self . instance . addonuser_set . create ( user_id = <NUM_LIT> ) <EOL> assert self . instance . addonuser_set . all ( ) [ <NUM_LIT:0> ] . user . email == ( <EOL> '<STR_LIT>' ) <EOL> self . form = EditThemeOwnerForm ( None , instance = self . instance ) <EOL> assert self . form . initial == { '<STR_LIT>' : '<STR_LIT>' } <EOL> def test_success_change_from_no_owner ( self ) : <EOL> self . form = EditThemeOwnerForm ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> instance = self . instance ) <EOL> assert self . form . is_valid ( ) , self . form . errors <EOL> self . form . save ( ) <EOL> assert self . instance . addonuser_set . all ( ) [ <NUM_LIT:0> ] . user . email == ( <EOL> '<STR_LIT>' ) <EOL> def test_success_replace_owner ( self ) : <EOL> self . instance . addonuser_set . create ( user_id = <NUM_LIT> ) <EOL> self . form = EditThemeOwnerForm ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> instance = self . instance ) <EOL> assert self . form . is_valid ( ) , self . form . errors <EOL> self . form . save ( ) <EOL> assert self . instance . addonuser_set . all ( ) [ <NUM_LIT:0> ] . user . email == ( <EOL> '<STR_LIT>' ) <EOL> def test_error_invalid_user ( self ) : <EOL> self . form = EditThemeOwnerForm ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> instance = self . instance ) <EOL> assert not self . form . is_valid ( ) </s>
