예제 #1
0
 def setUp(self):
     # Let's set up the environment for this mock request:
     mw = RepozeWhatMiddleware()
     self.request = Request({'PATH_INFO': "/"}, make_user(None))
     mw.process_view(self.request, None, None, None)
     # Let's enabled logging after the middleware has been set:
     self.log_fixture = LoggingHandlerFixture()
예제 #2
0
 def test_no_authz_decision_made(self):
     """Nothing must be done if no decision was made."""
     environ = {'PATH_INFO': "/app2/nothing"}
     request = Request(environ, make_user(None))
     response = self.middleware.process_view(request, object(), (), {})
     eq_(response, None)
     eq_(len(self.log_fixture.handler.messages['debug']), 1)
     eq_(self.log_fixture.handler.messages['debug'][0],
         "No authorization decision made on ingress at /app2/nothing")
예제 #3
0
 def test_user_with_permissions_and_no_groups(self):
     environ = {}
     request = Request(environ, make_user("foo", [], ("p1", "p2", "p3")))
     # Running the middleware and checking the resulting environ:
     self.middleware._set_request_up(request)
     eq_(len(request.environ['repoze.what.credentials']['groups']), 0)
     eq_(request.environ['repoze.what.credentials']['permissions'],
         set(["p1", "p2", "p3"]))
     eq_(request.environ['repoze.what.credentials']['repoze.what.userid'],
         "foo")\
예제 #4
0
 def test_anonymous_user(self):
     """Anonymous users shouldn't have groups nor permissions."""
     environ = {}
     request = Request(environ, make_user(None))
     # Running the middleware and checking the resulting environ:
     self.middleware._set_request_up(request)
     eq_(len(request.environ['repoze.what.credentials']['groups']), 0)
     eq_(len(request.environ['repoze.what.credentials']['permissions']), 0)
     eq_(request.environ['repoze.what.credentials']["repoze.what.userid"],
         None)
예제 #5
0
 def test_authz_granted(self):
     """When authorization is granted nothing must be done."""
     environ = {'PATH_INFO': "/app1/blog"}
     request = Request(environ, make_user(None))
     response = self.middleware.process_view(request, object(), (), {})
     eq_(response, None)
     eq_(len(self.log_fixture.handler.messages['info']), 1)
     eq_(
         self.log_fixture.handler.messages['info'][0],
         "Authorization granted to %s on ingress at /app1/blog" %
         repr(request.user))
예제 #6
0
 def test_middleware_skips_media_dir(self):
     """The middleware must do nothing in the media directory."""
     environ = {'PATH_INFO': "/media/photo.jpg"}
     request = Request(environ, make_user(None))
     response = self.middleware.process_view(request, object(), (), {})
     eq_(response, None)
     # The environment must not have been set up for repoze.what:
     ok_("repoze.what.credentials" not in request.environ)
     # Checking the logs:
     eq_(len(self.log_fixture.handler.messages['info']), 0)
     eq_(len(self.log_fixture.handler.messages['warning']), 0)
     eq_(len(self.log_fixture.handler.messages['debug']), 1)
     eq_(
         self.log_fixture.handler.messages['debug'][0],
         "Authorization checks disabled for media file at /media/photo.jpg")
예제 #7
0
 def test_authorization_denied_with_custom_denial_handler(self):
     """
     Authorization must be denied with a custom denial handler, if available.
     
     """
     environ = {'PATH_INFO': "/app1/admin"}
     request = Request(environ, make_user(None))
     response = self.middleware.process_view(request, object(), (), {})
     eq_(response, "No! Get out!")
     # Checking the logs:
     eq_(len(self.log_fixture.handler.messages['warning']), 1)
     eq_(
         self.log_fixture.handler.messages['warning'][0],
         "Authorization denied on ingress to %s at /app1/admin: Get out!" %
         repr(request.user))
     eq_(len(self.log_fixture.handler.messages['debug']), 0)
예제 #8
0
 def test_authorization_denied_with_custom_handler(self):
     """
     When authorization is denied with a custom handler, it must be used
     
     """
     request = Request({'PATH_INFO': "/"}, make_user(None))
     exception = _AuthorizationDenial(
         "Nothing", lambda request, message: "No! %s" % message)
     response = self.middleware.process_exception(request, exception)
     eq_(response, "No! Nothing")
     # Checking the logs:
     eq_(len(self.log_fixture.handler.messages['warning']), 1)
     eq_(
         self.log_fixture.handler.messages['warning'][0],
         "Authorization denied to %s in the view at /: Nothing" %
         repr(request.user))
     eq_(len(self.log_fixture.handler.messages['debug']), 0)
예제 #9
0
 def test_authorization_denied_without_custom_denial_handler(self):
     """
     When authorization is denied without a custom denial handler, the
     default one must be user.
     
     """
     environ = {'PATH_INFO': "/app2/secret"}
     request = Request(environ, make_user(None))
     response = self.middleware.process_view(request, object(), (), {})
     ok_(isinstance(response, HttpResponse))
     # Checking the logs:
     eq_(len(self.log_fixture.handler.messages['warning']), 1)
     eq_(
         self.log_fixture.handler.messages['warning'][0],
         "Authorization denied on ingress to %s at /app2/secret: %s" %
         (repr(request.user), "This is a secret"))
     eq_(len(self.log_fixture.handler.messages['debug']), 1)
     eq_(self.log_fixture.handler.messages['debug'][0],
         "No custom denial handler defined; using the default one")
예제 #10
0
 def test_authorization_denied_without_custom_handler(self):
     """
     When authorization is denied with no custom handler, the default one
     must be used.
     
     """
     request = Request({}, make_user(None))
     exception = _AuthorizationDenial("You can't be here", None)
     response = self.middleware.process_exception(request, exception)
     eq_(response.status_code, 401)
     # Checking the logs:
     eq_(len(self.log_fixture.handler.messages['warning']), 1)
     eq_(
         self.log_fixture.handler.messages['warning'][0],
         "Authorization denied to %s in the view at /: You can't be here" %
         repr(request.user))
     eq_(len(self.log_fixture.handler.messages['debug']), 1)
     eq_(self.log_fixture.handler.messages['debug'][0],
         "Using the default denial handler")
예제 #11
0
 def test_with_predicate_met(self):
     req = Request({}, make_user(None))
     assert_false(not_met(MockPredicate(), req))
예제 #12
0
 def test_with_predicate_unmet(self):
     req = Request({}, make_user(None))
     ok_(not_met(MockPredicate(False), req))
예제 #13
0
 def test_no_message_set(self):
     """No message should be shown to the user if there's none set."""
     req = Request({}, make_user(None))
     response = default_denial_handler(req, None)
     eq_(response.status_code, 401)
예제 #14
0
 def test_with_predicate_met(self):
     req = Request({}, make_user(None))
     ok_(is_met(MockPredicate(), req))
예제 #15
0
 def setUp(self):
     """Set up the request."""
     mw = RepozeWhatMiddleware()
     self.request = Request({}, make_user("foo"))
     mw._set_request_up(self.request)
예제 #16
0
 def setUp(self):
     self.req = Request({}, make_user(None))
예제 #17
0
 def test_no_response_returned(self):
     """The middleware's _set_request_up() shouldn't return a response."""
     request = Request({}, make_user(None))
     eq_(self.middleware._set_request_up(request), None)
예제 #18
0
 def test_environment_is_setup(self):
     """The WSGI environment must be set up before processing the view."""
     environ = {'PATH_INFO': "/app2/nothing"}
     request = Request(environ, make_user(None))
     self.middleware.process_view(request, object(), (), {})
     ok_("repoze.what.credentials" in request.environ)
예제 #19
0
 def test_user_is_authenticated(self):
     req = Request({}, make_user("Foo"))
     response = default_denial_handler(req, "What are you doing here?")
     eq_(response.status_code, 403)
     eq_(len(req.user.message_set.messages), 1)
     eq_(req.user.message_set.messages[0], "What are you doing here?")
예제 #20
0
 def test_with_predicate_unmet_with_message(self):
     req = Request({}, make_user(None))
     self._check_enforcement(MockPredicate(False),
                             req,
                             expected_message="Go away")
예제 #21
0
 def test_with_predicate_met(self):
     req = Request({}, make_user(None))
     enforce(MockPredicate(), req)
예제 #22
0
 def test_with_predicate_unmet(self):
     req = Request({}, make_user(None))
     self._check_enforcement(MockPredicate(False), req)
예제 #23
0
 def test_another_exception(self):
     """process_exception() must do nothing if not given a denial handler."""
     request = Request({}, make_user(None))
     exception = Exception()
     response = self.middleware.process_exception(request, exception)
     eq_(response, None)
예제 #24
0
 def test_with_predicate_unmet_with_message_and_denial_handler(self):
     req = Request({}, make_user(None))
     self._check_enforcement(MockPredicate(False),
                             req,
                             expected_message="Go away",
                             expected_denial_handler=object())
예제 #25
0
 def test_user_is_anonymous(self):
     req = Request({}, make_user(None))
     response = default_denial_handler(req, "You can't be here")
     eq_(response.status_code, 401)