<s> from __future__ import absolute_import <EOL> import json <EOL> from contextlib import contextmanager <EOL> from django . test import TestCase <EOL> from django . contrib . auth . models import User , Permission <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from . . models import Layout , RawTextWidget , Bucket <EOL> from . . widgy_config import widgy_site <EOL> class RootNodeTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( RootNodeTestCase , self ) . setUp ( ) <EOL> self . root_node = Layout . add_root ( widgy_site ) . node <EOL> class HttpTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( HttpTestCase , self ) . setUp ( ) <EOL> u = User . objects . create_superuser ( <EOL> username = '<STR_LIT>' , <EOL> email = '<STR_LIT>' , <EOL> password = '<STR_LIT>' , <EOL> ) <EOL> u . save ( ) <EOL> self . client . login ( username = u . username , password = '<STR_LIT>' ) <EOL> self . user = u <EOL> def json_request ( self , method , url , data = None , * args , ** kwargs ) : <EOL> method = getattr ( self . client , method ) <EOL> if method == self . client . get : <EOL> encode = lambda x : x <EOL> else : <EOL> encode = json . dumps <EOL> if data : <EOL> resp = method ( url , encode ( data ) , content_type = '<STR_LIT:application/json>' , * args , ** kwargs ) <EOL> else : <EOL> resp = method ( url , content_type = '<STR_LIT:application/json>' , * args , ** kwargs ) <EOL> self . assertEqual ( resp [ '<STR_LIT:Content-Type>' ] , '<STR_LIT:application/json>' ) <EOL> return resp <EOL> def __getattr__ ( self , attr ) : <EOL> if attr in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return lambda * args , ** kwargs : self . json_request ( attr , * args , ** kwargs ) <EOL> else : <EOL> return getattr ( super ( HttpTestCase , self ) , attr ) <EOL> class SwitchUserTestCase ( TestCase ) : <EOL> username = '<STR_LIT:username>' <EOL> password = '<STR_LIT:password>' <EOL> def setUp ( self ) : <EOL> super ( SwitchUserTestCase , self ) . setUp ( ) <EOL> self . user = user = User . objects . create_user ( <EOL> username = self . username , <EOL> password = self . password , <EOL> ) <EOL> user . save ( ) <EOL> self . client . logout ( ) <EOL> @ contextmanager <EOL> def as_superuser ( self ) : <EOL> self . user . is_superuser = True <EOL> self . user . save ( ) <EOL> with self . as_staffuser ( ) as user : <EOL> yield user <EOL> self . user . is_superuser = False <EOL> self . user . save ( ) <EOL> @ contextmanager <EOL> def as_staffuser ( self ) : <EOL> self . user . is_staff = True <EOL> self . user . save ( ) <EOL> with self . logged_in ( ) as user : <EOL> yield user <EOL> self . user . is_staff = False <EOL> self . user . save ( ) <EOL> @ contextmanager <EOL> def logged_in ( self ) : <EOL> self . client . login ( username = self . username , password = self . password ) <EOL> yield self . user <EOL> self . client . logout ( ) <EOL> @ contextmanager <EOL> def with_permission ( self , user , name , model ) : <EOL> contenttype = ContentType . objects . get_for_model ( model ) <EOL> permission , _ = Permission . objects . get_or_create ( <EOL> codename = '<STR_LIT>' % ( name , model . _meta . model_name ) , <EOL> defaults = { <EOL> '<STR_LIT>' : contenttype , <EOL> } <EOL> ) <EOL> user . user_permissions . add ( permission ) <EOL> yield user <EOL> user . user_permissions . remove ( permission ) <EOL> def tree_to_dot ( node ) : <EOL> output = [ ] <EOL> output . append ( '<STR_LIT>' ) <EOL> for i in node . depth_first_order ( ) : <EOL> output . append ( '<STR_LIT>' % ( i . id , i . id , repr ( i . content ) ) ) <EOL> if i != node : <EOL> output . append ( '<STR_LIT>' % ( i . get_parent ( ) . id , i . id ) ) <EOL> output . append ( '<STR_LIT:}>' ) <EOL> return '<STR_LIT:\n>' . join ( output ) <EOL> def display_node ( node ) : <EOL> import subprocess <EOL> proc = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' ] , stdin = subprocess . PIPE ) <EOL> proc . communicate ( tree_to_dot ( node ) ) <EOL> def make_a_nice_tree ( root_node , widgy_site = widgy_site ) : <EOL> left , right = root_node . content . get_children ( ) <EOL> left . add_child ( widgy_site , <EOL> RawTextWidget , <EOL> text = '<STR_LIT>' ) <EOL> left . add_child ( widgy_site , <EOL> RawTextWidget , <EOL> text = '<STR_LIT>' ) <EOL> subbucket = left . add_child ( widgy_site , <EOL> Bucket ) <EOL> subbucket . add_child ( widgy_site , <EOL> RawTextWidget , <EOL> text = '<STR_LIT>' ) <EOL> subbucket . add_child ( widgy_site , <EOL> RawTextWidget , <EOL> text = '<STR_LIT>' ) <EOL> right . add_child ( widgy_site , <EOL> RawTextWidget , <EOL> text = '<STR_LIT>' ) <EOL> right . add_child ( widgy_site , <EOL> RawTextWidget , <EOL> text = '<STR_LIT>' ) <EOL> return left . node , right . node <EOL> def refetch ( obj ) : <EOL> return obj . __class__ . objects . get ( pk = obj . pk ) </s>
