summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Gerus <ar@bash.org.pl>2020-03-07 08:39:18 +0100
committerRobert Gerus <ar@bash.org.pl>2020-03-07 08:39:18 +0100
commit4615d3a41751ec0f82502084cd0769221460c28f (patch)
tree9ec421e7e494bbad98bf9c5a5a8dd61e316f2737
parentcd283b1423eb850838cd9ac6c66cbf8d0a7467a7 (diff)
downloadspejstore-4615d3a41751ec0f82502084cd0769221460c28f.tar.gz
spejstore-4615d3a41751ec0f82502084cd0769221460c28f.tar.bz2
spejstore-4615d3a41751ec0f82502084cd0769221460c28f.zip
Mock authentication for development/testing
-rw-r--r--auth/mock_auth.py28
-rw-r--r--spejstore/settings.py3
2 files changed, 30 insertions, 1 deletions
diff --git a/auth/mock_auth.py b/auth/mock_auth.py
new file mode 100644
index 0000000..ff39317
--- /dev/null
+++ b/auth/mock_auth.py
@@ -0,0 +1,28 @@
+from django.contrib.auth.models import User
+from django.utils.deprecation import MiddlewareMixin
+from django.utils.functional import SimpleLazyObject
+
+class MockUser(object):
+ is_active = True
+ is_staff = True
+ pk = True
+
+ def has_module_perms(*args):
+ return True
+ def has_perm(*args):
+ return True
+ def __init__(self, name="mock"):
+ if len(User.objects.filter()) < 1:
+ user = User.objects.create_user(name, name + "@hackerspace.pl", name + "pass")
+ user.first_name = 'John'
+ user.last_name = 'Doe'
+ user.save()
+
+
+def get_user(request):
+ user = MockUser()
+ return user
+
+class Middleware(MiddlewareMixin):
+ def process_request(self, request):
+ request.user = SimpleLazyObject(lambda: get_user(request))
diff --git a/spejstore/settings.py b/spejstore/settings.py
index 796e784..07cc79f 100644
--- a/spejstore/settings.py
+++ b/spejstore/settings.py
@@ -33,6 +33,7 @@ DEBUG = not PROD
ALLOWED_HOSTS = env('ALLOWED_HOSTS', 'devinventory,inventory.waw.hackerspace.pl,i,inventory').split(',')
LOGIN_REDIRECT_URL = '/admin/'
+auth_middleware = 'django.contrib.auth.middleware.AuthenticationMiddleware' if PROD else 'auth.mock_auth.Middleware'
# Application definition
@@ -63,7 +64,7 @@ MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
- 'django.contrib.auth.middleware.AuthenticationMiddleware',
+ auth_middleware,
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'social_django.middleware.SocialAuthExceptionMiddleware',