Ejemplo n.º 1
0
    def test_success(self, password, prefix, expected, dataset):
        response = pretend.stub(text=dataset, raise_for_status=lambda: None)
        session = pretend.stub(get=pretend.call_recorder(lambda url: response))

        svc = services.HaveIBeenPwnedPasswordBreachedService(session=session)

        assert svc.check_password(password) == expected
        assert session.get.calls == [
            pretend.call(f"https://api.pwnedpasswords.com/range/{prefix}")
        ]
Ejemplo n.º 2
0
    def test_http_failure(self):
        @pretend.call_recorder
        def raiser():
            raise requests.RequestException()

        response = pretend.stub(raise_for_status=raiser)
        session = pretend.stub(get=lambda url: response)

        svc = services.HaveIBeenPwnedPasswordBreachedService(session=session)
        assert not svc.check_password("my password")
        assert raiser.calls
Ejemplo n.º 3
0
    def test_failure(self):
        class AnError(Exception):
            pass

        def raiser():
            raise AnError

        response = pretend.stub(raise_for_status=raiser)
        session = pretend.stub(get=lambda url: response)

        svc = services.HaveIBeenPwnedPasswordBreachedService(session=session)

        with pytest.raises(AnError):
            svc.check_password("my password")
Ejemplo n.º 4
0
    def test_metrics_increments(self):
        class Metrics:
            def __init__(self):
                self.values = collections.Counter()

            def increment(self, metric):
                self.values[metric] += 1

        metrics = Metrics()

        svc = services.HaveIBeenPwnedPasswordBreachedService(
            session=pretend.stub(), metrics=metrics)

        svc._metrics_increment("something")
        svc._metrics_increment("another_thing")
        svc._metrics_increment("something")

        assert metrics.values == {"something": 2, "another_thing": 1}