Skip to content

Instantly share code, notes, and snippets.

@btbytes
Created March 7, 2009 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btbytes/75207 to your computer and use it in GitHub Desktop.
Save btbytes/75207 to your computer and use it in GitHub Desktop.
import os, sys
from base64 import encodestring
from ZPublisher.Request import Request
from ZPublisher.Response import Response
from ZPublisher.BaseRequest import RequestContainer
from AccessControl.SecurityManagement import newSecurityManager
e=os.environ
e['SERVER_NAME']= 'localhost'
e['SERVER_PORT']= '8080'
e['SCRIPT_NAME']= ''
def wrap(obj, user=None, password=None, method=''):
'''wrap *obj* in a request.'''
e['REQUEST_METHOD'] = method
r = Request(sys.stdin, e, Response())
r['PARENTS'] = [obj]
if user is not None and password is not None:
r._auth = 'basic ' + encodestring('%s:%s' % (user,password))
UF = obj.acl_users
u = UF.getUser(user)
if u is not None:
u = u.__of__(UF)
newSecurityManager(r, u)
r['AUTHENTICATED_USER'] = u
return obj.__of__(RequestContainer(REQUEST=r))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment