mirror of
https://github.com/wassname/flask-security.git
synced 2026-08-12 12:00:37 +08:00
Convert all tests to use pytest. Phew!
This commit is contained in:
+2
-60
@@ -10,8 +10,6 @@
|
||||
"""
|
||||
|
||||
import base64
|
||||
import blinker
|
||||
import functools
|
||||
import hashlib
|
||||
import hmac
|
||||
import sys
|
||||
@@ -20,16 +18,13 @@ from contextlib import contextmanager
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from flask import url_for, flash, current_app, request, session, render_template
|
||||
from flask.ext.login import login_user as _login_user, \
|
||||
logout_user as _logout_user
|
||||
from flask.ext.login import login_user as _login_user, logout_user as _logout_user
|
||||
from flask.ext.mail import Message
|
||||
from flask.ext.principal import Identity, AnonymousIdentity, identity_changed
|
||||
from itsdangerous import BadSignature, SignatureExpired
|
||||
from werkzeug.local import LocalProxy
|
||||
|
||||
from .signals import user_registered, user_confirmed, \
|
||||
confirm_instructions_sent, login_instructions_sent, \
|
||||
password_reset, password_changed, reset_password_instructions_sent
|
||||
from .signals import user_registered, login_instructions_sent, reset_password_instructions_sent
|
||||
|
||||
# Convenient references
|
||||
_security = LocalProxy(lambda: current_app.extensions['security'])
|
||||
@@ -396,56 +391,3 @@ def capture_reset_password_requests(reset_password_sent_at=None):
|
||||
yield reset_requests
|
||||
finally:
|
||||
reset_password_instructions_sent.disconnect(_on)
|
||||
|
||||
|
||||
class CaptureSignals(object):
|
||||
"""Testing utility for capturing blinker signals.
|
||||
|
||||
Context manager which mocks out selected signals and registers which are `sent` on and what
|
||||
arguments were sent. Instantiate with a list of blinker `NamedSignals` to patch. Each signal
|
||||
has it's `send` mocked out.
|
||||
"""
|
||||
def __init__(self, signals):
|
||||
"""Patch all given signals and make them available as attributes.
|
||||
|
||||
:param signals: list of signals
|
||||
"""
|
||||
self._records = {}
|
||||
self._receivers = {}
|
||||
for signal in signals:
|
||||
self._records[signal] = []
|
||||
self._receivers[signal] = functools.partial(self._record, signal)
|
||||
|
||||
def __getitem__(self, signal):
|
||||
"""All captured signals are available via `ctxt[signal]`.
|
||||
"""
|
||||
if isinstance(signal, blinker.base.NamedSignal):
|
||||
return self._records[signal]
|
||||
else:
|
||||
super(CaptureSignals, self).__setitem__(signal)
|
||||
|
||||
def _record(self, signal, *args, **kwargs):
|
||||
self._records[signal].append((args, kwargs))
|
||||
|
||||
def __enter__(self):
|
||||
for signal, receiver in self._receivers.items():
|
||||
signal.connect(receiver)
|
||||
return self
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
for signal, receiver in self._receivers.items():
|
||||
signal.disconnect(receiver)
|
||||
|
||||
def signals_sent(self):
|
||||
"""Return a set of the signals sent.
|
||||
:rtype: list of blinker `NamedSignals`.
|
||||
"""
|
||||
return set([signal for signal, _ in self._records.items() if self._records[signal]])
|
||||
|
||||
|
||||
def capture_signals():
|
||||
"""Factory method that creates a `CaptureSignals` with all the flask_security signals."""
|
||||
return CaptureSignals([user_registered, user_confirmed,
|
||||
confirm_instructions_sent, login_instructions_sent,
|
||||
password_reset, password_changed,
|
||||
reset_password_instructions_sent])
|
||||
|
||||
Reference in New Issue
Block a user