<s> from __future__ import print_function , unicode_literals <EOL> from datetime import datetime <EOL> from django . core import mail <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase <EOL> from django . test . client import Client <EOL> from corehq import toggles <EOL> from corehq . apps . users . models import WebUser <EOL> from corehq . apps . domain . models import Domain , TransferDomainRequest <EOL> from corehq . apps . domain . forms import TransferDomainForm , TransferDomainFormErrors <EOL> from corehq . apps . domain . views import TransferDomainView <EOL> from corehq . apps . domain . exceptions import InactiveTransferDomainException <EOL> class BaseDomainTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . client = Client ( ) <EOL> self . domain = Domain ( name = "<STR_LIT>" , is_active = True ) <EOL> self . domain . save ( ) <EOL> toggles . TRANSFER_DOMAIN . set ( "<STR_LIT>" . format ( domain = self . domain . name ) , True ) <EOL> self . username = '<STR_LIT>' <EOL> self . password = '<STR_LIT>' <EOL> self . user = WebUser . create ( self . domain . name , self . username , self . password ) <EOL> self . user . set_role ( self . domain . name , '<STR_LIT>' ) <EOL> self . user . save ( ) <EOL> self . another_domain = Domain ( name = '<STR_LIT>' , is_active = True ) <EOL> self . another_domain . save ( ) <EOL> self . mugglename = '<STR_LIT>' <EOL> self . muggle = WebUser . create ( self . another_domain . name , self . mugglename , self . password ) <EOL> self . muggle . save ( ) <EOL> def tearDown ( self ) : <EOL> self . user . delete ( ) <EOL> self . domain . delete ( ) <EOL> self . muggle . delete ( ) <EOL> self . another_domain . delete ( ) <EOL> class TestTransferDomainForm ( BaseDomainTest ) : <EOL> def test_valid_data ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : self . mugglename , <EOL> '<STR_LIT>' : self . domain . name , <EOL> } <EOL> form = TransferDomainForm ( self . domain . name , self . user . username , data ) <EOL> self . assertTrue ( form . is_valid ( ) , form . errors ) <EOL> transfer = form . save ( ) <EOL> self . assertEqual ( transfer . to_username , self . mugglename ) <EOL> self . assertEqual ( transfer . domain , self . domain . name ) <EOL> self . assertEqual ( transfer . from_username , self . username ) <EOL> def test_invalid_user_data ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> form = TransferDomainForm ( self . domain . name , self . user . username , data ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( form . errors , { <EOL> '<STR_LIT>' : [ TransferDomainFormErrors . USER_DNE ] , <EOL> '<STR_LIT>' : [ TransferDomainFormErrors . DOMAIN_MISMATCH ] , <EOL> } ) <EOL> class TestTransferDomainModel ( BaseDomainTest ) : <EOL> def setUp ( self ) : <EOL> super ( TestTransferDomainModel , self ) . setUp ( ) <EOL> self . transfer = TransferDomainRequest ( <EOL> to_username = self . mugglename , <EOL> from_username = self . username , <EOL> domain = self . domain . name ) <EOL> self . transfer . save ( ) <EOL> def tearDown ( self ) : <EOL> super ( TestTransferDomainModel , self ) . tearDown ( ) <EOL> self . transfer . delete ( ) <EOL> def test_domain_transfer_inactive ( self ) : <EOL> self . transfer . active = False <EOL> self . transfer . save ( ) <EOL> with self . assertRaises ( InactiveTransferDomainException ) : <EOL> self . transfer . transfer_domain ( ) <EOL> with self . assertRaises ( InactiveTransferDomainException ) : <EOL> self . transfer . send_transfer_request ( ) <EOL> def test_domain_transfer ( self ) : <EOL> self . transfer . transfer_domain ( ) <EOL> self . assertFalse ( self . transfer . active ) <EOL> self . assertFalse ( self . transfer . from_user . is_member_of ( self . domain ) ) <EOL> self . assertTrue ( self . transfer . to_user . is_member_of ( self . domain ) ) <EOL> def test_send_transfer_request ( self ) : <EOL> self . transfer . send_transfer_request ( ) <EOL> self . assertIsNotNone ( self . transfer . transfer_guid ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:2> , <EOL> "<STR_LIT>" ) <EOL> def test_get_active_transfer ( self ) : <EOL> res = TransferDomainRequest . get_active_transfer ( self . domain , self . user . username ) <EOL> self . assertIsNotNone ( res ) <EOL> newer = TransferDomainRequest ( <EOL> to_username = self . mugglename , <EOL> from_username = self . username , <EOL> request_time = datetime . utcnow ( ) , <EOL> domain = self . domain . name ) <EOL> newer . save ( ) <EOL> res = TransferDomainRequest . get_active_transfer ( self . domain , self . user . username ) <EOL> self . assertEqual ( res . pk , newer . pk ) <EOL> self . assertFalse ( TransferDomainRequest . objects . get ( pk = self . transfer . pk ) . active ) <EOL> class TestTransferDomainViews ( BaseDomainTest ) : <EOL> def setUp ( self ) : <EOL> super ( TestTransferDomainViews , self ) . setUp ( ) <EOL> self . transfer = TransferDomainRequest ( <EOL> to_username = self . mugglename , <EOL> from_username = self . username , <EOL> domain = self . domain . name ) <EOL> self . transfer . save ( ) <EOL> self . transfer . send_transfer_request ( ) <EOL> self . rando = WebUser . create ( self . domain . name , '<STR_LIT>' , self . password ) <EOL> def tearDown ( self ) : <EOL> super ( TestTransferDomainViews , self ) . tearDown ( ) <EOL> self . transfer . delete ( ) <EOL> self . rando . delete ( ) <EOL> def test_permissions_for_activation ( self ) : <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' , <EOL> args = [ self . transfer . transfer_guid ] ) , follow = True ) <EOL> self . assertEqual ( resp . template_name , [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> self . client . login ( username = self . username , password = self . password ) <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' , <EOL> args = [ self . transfer . transfer_guid ] ) , follow = True ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . client . login ( username = self . mugglename , password = self . password ) <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' , <EOL> args = [ self . transfer . transfer_guid ] ) , follow = True ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> def test_permissions_for_deactivation ( self ) : <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' , <EOL> args = [ self . transfer . transfer_guid ] ) , follow = True ) <EOL> self . assertEqual ( resp . template_name , [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> self . client . login ( username = self . rando . username , password = self . password ) <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' , <EOL> args = [ self . transfer . transfer_guid ] ) , follow = True ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . client . login ( username = self . mugglename , password = self . password ) <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' , <EOL> args = [ self . transfer . transfer_guid ] ) , follow = True ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> def test_permissions_for_transfer_domain_view ( self ) : <EOL> resp = self . client . get ( reverse ( '<STR_LIT>' , <EOL> args = [ self . domain . name ] ) , follow = True ) <EOL> self . assertRedirects ( resp , reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . domain . name } ) + <EOL> '<STR_LIT>' + reverse ( '<STR_LIT>' , args = [ self . domain . name ] ) ) <EOL> self . client . login ( username = self . rando . username , password = self . password ) <EOL> resp = self . client . get ( reverse ( '<STR_LIT>' , <EOL> args = [ self . domain . name ] ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> , '<STR_LIT>' ) <EOL> self . client . login ( username = self . user . username , password = self . password ) <EOL> resp = self . client . get ( reverse ( '<STR_LIT>' , <EOL> args = [ self . domain . name ] ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> class TestTransferDomainIntegration ( BaseDomainTest ) : <EOL> def setUp ( self ) : <EOL> super ( TestTransferDomainIntegration , self ) . setUp ( ) <EOL> self . client . login ( username = self . username , password = self . password ) <EOL> def test_basic_workflow ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = self . client . get ( reverse ( TransferDomainView . urlname , args = [ self . domain . name ] ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( type ( resp . context [ '<STR_LIT>' ] ) , TransferDomainForm , <EOL> "<STR_LIT>" ) <EOL> form = resp . context [ '<STR_LIT>' ] <EOL> form . data [ '<STR_LIT>' ] = self . domain . name <EOL> form . data [ '<STR_LIT>' ] = self . muggle . username <EOL> resp = self . client . post ( reverse ( TransferDomainView . urlname , args = [ self . domain . name ] ) , <EOL> form . data , follow = True ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:2> , <EOL> "<STR_LIT>" ) <EOL> transfer = TransferDomainRequest . objects . get ( to_username = self . muggle . username , <EOL> from_username = self . user . username , <EOL> domain = self . domain . name ) <EOL> self . assertIsNotNone ( transfer ) <EOL> self . assertIsNone ( transfer . confirm_time ) <EOL> self . assertTrue ( transfer . active ) <EOL> self . client . login ( username = self . muggle . username , password = self . password ) <EOL> resp = self . client . get ( reverse ( '<STR_LIT>' , args = [ transfer . transfer_guid ] ) , follow = True ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertIsNotNone ( resp . context [ '<STR_LIT>' ] ) <EOL> mail . outbox = [ ] <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' , args = [ transfer . transfer_guid ] ) , follow = True ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> , "<STR_LIT>" ) <EOL> user = WebUser . get_by_username ( self . user . username ) <EOL> muggle = WebUser . get_by_username ( self . muggle . username ) <EOL> transfer = TransferDomainRequest . objects . get ( to_username = self . muggle . username , <EOL> from_username = self . user . username , <EOL> domain = self . domain . name ) <EOL> self . assertFalse ( user . is_member_of ( self . domain . name ) ) <EOL> self . assertTrue ( muggle . is_member_of ( self . domain . name ) ) <EOL> self . assertTrue ( muggle . get_domain_membership ( self . domain . name ) . is_admin ) <EOL> self . assertIsNotNone ( transfer . confirm_time ) <EOL> def test_transfer_cancel_workflow ( self ) : <EOL> """<STR_LIT>""" <EOL> data = { <EOL> '<STR_LIT>' : self . domain . name , <EOL> '<STR_LIT>' : self . muggle . username , <EOL> } <EOL> resp = self . client . post ( reverse ( TransferDomainView . urlname , args = [ self . domain . name ] ) , data , follow = True ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> transfer = TransferDomainRequest . get_active_transfer ( self . domain . name , self . user . username ) <EOL> self . assertIsNotNone ( transfer ) <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' , args = [ transfer . transfer_guid ] ) ) <EOL> updated_transfer = TransferDomainRequest . get_active_transfer ( self . domain . name , self . user . username ) <EOL> self . assertIsNone ( updated_transfer ) <EOL> self . client . login ( username = self . muggle . username , password = self . password ) <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' , args = [ transfer . transfer_guid ] ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) </s>
