<cry>
u = request.environ.get('repoze.who.identity')['user']
TypeError: 'NoneType' object is unsubscriptable
My test:
class TestTemplateController(TestController):
def test_index(self):
"""Test Template index action"""
response = self.app.post(url(controller='template',
action='index', extra_environ={'REMOTE_USER':u'admin'}))
assert "Import File" in response
My controller:
class TemplateController(BaseController):
def index(self):
c.form = returnform()
c.user = request.environ.get('repoze.who.identity')['user']
return render('/derived/template/index.html')
I noticed while debugging my app the variable:
repoze.what.credentials {'repoze.what.userid': u'Joe Blow', 'groups':
(), 'permissions': ()}
I also tried making that up instead of the REMOTE_USER:
admin_user = { 'repoze.what.userid': 'admin',
'groups': ('Administrators'),
'permissions': ('')
}
and then: response = self.app.post(url(controller='template',
action='index', extra_environ={'repoze.what.credentials':admin_user}))
same thing happens...the only thing that I thought I was missing to
try was repoze.who.identity though I don't know how to mock that ....
(hidden dict?)
On Aug 12, 4:58 pm, Richard Harding <[email protected]> wrote:
> On Thu, Aug 12, 2010 at 3:49 PM, waugust <[email protected]> wrote:
> > This is most likely a very trivial question, but believe me I did my
> > due diligence and googled but alas I can't find the answer.
>
> > All I'm trying to do is write tests for my controllers but I fail to
> > see how to set an AUTH_USER environ variable.
>
> > I thought to just instantiate my User model and pass it in the headers
> > attribute of self.app.post though i get "AssertionError: Environmental
> > variable HTTP_AUTH_USER is not a string:"
>
> > What am I missing here? How do I mimic a logged in user when testing
> > my controllers?
>
> So you need to replicate the dictionary that repoze.what puts in place
> in the wsgi request.
>
> So what I do is have a series of functions in the test class like:
>
> admin_user = { 'user': object(),
> 'groups': ('admin'),
> 'permissions': ('add_job')
> }
>
> and then I will use it in a test like so:
>
> def test_list(self):
> # check that it's first permission locked down
> response = self.app.get(
> url=url(controller='accounts', action='list'),
> params={},
> status=302
> )
>
> response = self.app.get(
> url=url(controller='accounts', action='list'),
> params={},
> extra_environ={'repoze.what.credentials': self.admin_user},
> status=200
> )
>
> http://what.repoze.org/docs/1.0/Manual/InnerWorkings.html
>
> Rick Harding
>
> http://blog.mitechie.comhttp://lococast.net
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.