def secret_for_app1(self, environ, start_response): r"""This can be accessed by app1 consumer only""" if not is_consumer('app1').is_met(environ): start_response('401 ', [('Content-Type', 'text/plain')]) return HTTPUnauthorized() start_response('200 OK', [('Content-Type', 'text/plain')]) return ['This is a secret for app1 only']
def test_with_credentials(self): r"""Test how is_consumer handles credentials""" env = self._make_environ() # The consumer has to be defined in both # repoze.what.identity - repoze.what.userid (as consumer:...) and # repoze.who.identity - repoze.who.consumerkey # what.credentials - what.userid is not enough env['repoze.what.credentials']['repoze.what.userid'] = \ 'consumer:Some Consumer' p = is_consumer() self.eval_unmet_predicate(p, env, 'The current user must be a consumer') # who.identity - who.consumerkey alone is not enough either del env['repoze.what.credentials']['repoze.what.userid'] env['repoze.who.identity']['repoze.who.consumerkey'] = 'Some Consumer' self.eval_unmet_predicate(p, env, 'The current user must be a consumer') # what.credentials must have a 'consumer:' prefix env['repoze.what.credentials']['repoze.what.userid'] = 'Some Consumer' self.eval_unmet_predicate(p, env, 'The current user must be a consumer') # what.credentials after 'consumer:' prefix must match who.consumerid env['repoze.what.credentials']['repoze.what.userid'] = \ 'consumer:Some Other Consumer' self.eval_unmet_predicate(p, env, 'The current user must be a consumer') # Now make them match env['repoze.what.credentials']['repoze.what.userid'] = \ 'consumer:Some Consumer' # And all is ok now self.eval_met_predicate(p, env) # We can ask for a particular consumer p = is_consumer('Some Consumer') self.eval_met_predicate(p, env) # But not some other p = is_consumer('Some Other Consumer') self.eval_unmet_predicate(p, env, 'The current user must be a consumer')
def test_without_credentials(self): r"""Test how is_consumer behaves without credentials""" env = self._make_environ() p = is_consumer() self.eval_unmet_predicate(p, env, 'The current user must be a consumer')