<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from lino import AFTER17 <EOL> if AFTER17 : <EOL> import django <EOL> django . setup ( ) <EOL> from lino . api . shell import * <EOL> from django . utils import translation <EOL> from django . test import Client <EOL> import json <EOL> from bs4 import BeautifulSoup <EOL> from lino . utils import AttrDict <EOL> from lino . utils import i2d <EOL> from lino . utils . xmlgen . html import E <EOL> from lino . utils . diag import analyzer <EOL> from atelier . rstgen import attrtable <EOL> test_client = Client ( ) <EOL> import collections <EOL> HttpQuery = collections . namedtuple ( <EOL> '<STR_LIT>' , <EOL> [ '<STR_LIT:username>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def get_json_dict ( username , uri , an = '<STR_LIT>' ) : <EOL> url = '<STR_LIT>' . format ( uri , an ) <EOL> res = test_client . get ( url , REMOTE_USER = username ) <EOL> assert res . status_code == <NUM_LIT:200> <EOL> return json . loads ( res . content ) <EOL> def get_json_soup ( username , uri , fieldname , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> d = get_json_dict ( username , uri , ** kwargs ) <EOL> html = d [ '<STR_LIT:data>' ] [ fieldname ] <EOL> return BeautifulSoup ( html , '<STR_LIT>' ) <EOL> def post_json_dict ( username , url , data , ** extra ) : <EOL> """<STR_LIT>""" <EOL> res = test_client . post ( url , data , REMOTE_USER = username , ** extra ) <EOL> assert res . status_code == <NUM_LIT:200> <EOL> return AttrDict ( json . loads ( res . content ) ) <EOL> def check_json_result ( response , expected_keys = None , msg = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if response . status_code != <NUM_LIT:200> : <EOL> raise Exception ( <EOL> "<STR_LIT>" . format ( <EOL> msg , response . status_code ) ) <EOL> try : <EOL> result = json . loads ( response . content ) <EOL> except ValueError as e : <EOL> raise Exception ( "<STR_LIT>" . format ( e , response . content ) ) <EOL> if expected_keys is not None : <EOL> if set ( result . keys ( ) ) != set ( expected_keys . split ( ) ) : <EOL> raise Exception ( "<STR_LIT>" . format ( <EOL> '<STR_LIT:U+0020>' . join ( list ( result . keys ( ) ) ) , expected_keys ) ) <EOL> return result <EOL> def demo_get ( <EOL> username , url_base , json_fields , <EOL> expected_rows = None , ** kwargs ) : <EOL> from django . conf import settings <EOL> case = HttpQuery ( username , url_base , json_fields , <EOL> expected_rows , kwargs ) <EOL> url = str ( settings . SITE . buildurl ( case . url_base , ** case . kwargs ) ) <EOL> if True : <EOL> msg = '<STR_LIT>' <EOL> try : <EOL> response = self . client . get ( url ) <EOL> raise Exception ( "<STR_LIT>" % msg ) <EOL> except Exception : <EOL> pass <EOL> response = test_client . get ( url , REMOTE_USER = str ( '<STR_LIT:foo>' ) ) <EOL> if response . status_code != <NUM_LIT> : <EOL> raise Exception ( <EOL> "<STR_LIT>" % ( <EOL> response . status_code , url ) ) <EOL> response = test_client . get ( url , REMOTE_USER = str ( case . username ) ) <EOL> if True : <EOL> user = settings . SITE . user_model . objects . get ( <EOL> username = case . username ) <EOL> result = check_json_result ( <EOL> response , case . json_fields , <EOL> "<STR_LIT>" % ( url , user ) ) <EOL> num = case . expected_rows <EOL> if num is not None : <EOL> if not isinstance ( num , tuple ) : <EOL> num = [ num ] <EOL> if result [ '<STR_LIT:count>' ] not in num : <EOL> msg = "<STR_LIT>" % ( <EOL> url , result [ '<STR_LIT:count>' ] , num ) <EOL> raise Exception ( msg ) <EOL> def screenshot ( obj , filename , rstname , username = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> from lino . api . selenium import Album , runserver <EOL> assert filename . endswith ( '<STR_LIT>' ) <EOL> assert rstname . endswith ( '<STR_LIT>' ) <EOL> self = dd . plugins . extjs . renderer <EOL> uri = self . get_detail_url ( obj ) <EOL> print ( uri ) <EOL> def f ( driver ) : <EOL> app = Album ( driver ) <EOL> driver . get ( "<STR_LIT>" + uri ) <EOL> app . stabilize ( ) <EOL> if not driver . get_screenshot_as_file ( filename ) : <EOL> app . error ( "<STR_LIT>" . format ( filename ) ) <EOL> runserver ( settings . SETTINGS_MODULE , f ) </s>
