Example #1
0
    def test__unit__OrAuthorizationChecker__err__exception(self):
        class CheckerFailed(TracimException):
            pass

        class OkChecker(AuthorizationChecker):
            def check(self, tracim_context: TracimContext):
                return True

        class ExceptionChecker(AuthorizationChecker):
            def check(self, tracim_context: TracimContext):
                raise CheckerFailed()

        class FakeTracimContext(TracimContext):
            pass

        with pytest.raises(CheckerFailed):
            ExceptionChecker().check(FakeTracimContext())

        with pytest.raises(CheckerFailed):
            OrAuthorizationChecker(ExceptionChecker(),
                                   ExceptionChecker()).check(
                                       FakeTracimContext())

        with pytest.raises(CheckerFailed):
            checkers = [ExceptionChecker()] * 5
            or_auth_checker = OrAuthorizationChecker(*checkers)
            assert list(or_auth_checker.authorization_checkers) == checkers
            or_auth_checker.check(FakeTracimContext())
    def test__unit__OrAuthorizationChecker__err__exception_order(self):

        class CheckerFailed(TracimException):
            pass

        class CheckerFailed2(TracimException):
            pass

        class ExceptionChecker(AuthorizationChecker):

            def check(
                self,
                tracim_context: TracimContext
            ):
                raise CheckerFailed()

        class Exception2Checker(AuthorizationChecker):

            def check(
                self,
                tracim_context: TracimContext
            ):
                raise CheckerFailed2()

        class FakeTracimContext(TracimContext):
            pass

        with pytest.raises(CheckerFailed):
            ExceptionChecker().check(FakeTracimContext())
        with pytest.raises(CheckerFailed2):
            Exception2Checker().check(FakeTracimContext())

        with pytest.raises(CheckerFailed2):
            OrAuthorizationChecker(ExceptionChecker(), Exception2Checker()).check(
                FakeTracimContext()
            )
        with pytest.raises(CheckerFailed):
            OrAuthorizationChecker(Exception2Checker(), ExceptionChecker()).check(
                FakeTracimContext()
            )


        with pytest.raises(CheckerFailed):
            checkers = [Exception2Checker()] * 5 + [ExceptionChecker()]
            or_auth_checker = OrAuthorizationChecker(*checkers)
            assert list(or_auth_checker.authorization_checkers) == checkers
            or_auth_checker.check(FakeTracimContext())
Example #3
0
    def test__unit__OrAuthorizationChecker__ok__nominal_case(self):
        class CheckerFailed(TracimException):
            pass

        class OkChecker(AuthorizationChecker):
            def check(self, tracim_context: TracimContext):
                return True

        class ExceptionChecker(AuthorizationChecker):
            def check(self, tracim_context: TracimContext):
                raise CheckerFailed()

        class FakeTracimContext(TracimContext):
            pass

        assert OkChecker().check(FakeTracimContext())
        assert OrAuthorizationChecker(OkChecker(), ExceptionChecker())
        assert OrAuthorizationChecker(ExceptionChecker(), OkChecker())
        assert OrAuthorizationChecker([ExceptionChecker()] * 5 +
                                      [OkChecker()] + [ExceptionChecker()] * 5)