mirror of
https://github.com/haiwen/seahub.git
synced 2025-04-27 11:01:14 +00:00
Add copyright header
This commit is contained in:
parent
527449d7ab
commit
5a2af77d44
@ -1 +1,2 @@
|
||||
from . import locale
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from . import locale, copyright
|
||||
|
92
fabfile/copyright.py
Normal file
92
fabfile/copyright.py
Normal file
@ -0,0 +1,92 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
|
||||
import os
|
||||
from fabric.api import task
|
||||
|
||||
@task
|
||||
def update(path):
|
||||
"""Add copyright stuff to the begining of files.
|
||||
"""
|
||||
for filename in path_to_pyfile_list(path):
|
||||
do_update(filename)
|
||||
|
||||
@task
|
||||
def check(path):
|
||||
"""Check copyright stuff for files.
|
||||
"""
|
||||
for filename in path_to_pyfile_list(path):
|
||||
do_check(filename)
|
||||
|
||||
def do_update(filename):
|
||||
if 'migrations' in filename:
|
||||
print 'skip migration file: %s' % filename
|
||||
return
|
||||
|
||||
with open(filename) as f:
|
||||
# try read first line of file
|
||||
try:
|
||||
head = [next(f) for x in xrange(1)]
|
||||
except StopIteration:
|
||||
print '%s is empty, skip' % filename
|
||||
return
|
||||
|
||||
copy_str = '# Copyright (c) 2012-2016 Seafile Ltd.'
|
||||
|
||||
need_update = True
|
||||
for line in head:
|
||||
line = line.lower()
|
||||
if 'seafile ltd.' in line:
|
||||
need_update = False
|
||||
|
||||
if not need_update:
|
||||
print '%s is ok.' % filename
|
||||
return
|
||||
|
||||
line_prepender(filename, copy_str)
|
||||
print '%s Done.' % filename
|
||||
|
||||
def path_to_pyfile_list(path):
|
||||
is_dir = False
|
||||
if os.path.isdir(path):
|
||||
is_dir = True
|
||||
|
||||
if not is_dir:
|
||||
py_files = [path]
|
||||
else:
|
||||
py_files = []
|
||||
for root, directories, filenames in os.walk(path):
|
||||
for directory in directories:
|
||||
f = os.path.join(root, directory)
|
||||
if f.endswith('.py'):
|
||||
py_files.append(f)
|
||||
for filename in filenames:
|
||||
if filename.endswith('.py'):
|
||||
py_files.append(os.path.join(root, filename))
|
||||
return py_files
|
||||
|
||||
|
||||
def line_prepender(filename, line):
|
||||
with open(filename, 'r+') as f:
|
||||
content = f.read()
|
||||
f.seek(0, 0)
|
||||
f.write(line.rstrip('\r\n') + '\n' + content)
|
||||
|
||||
def do_check(filename):
|
||||
if 'migrations' in filename:
|
||||
return
|
||||
|
||||
with open(filename) as f:
|
||||
# try read first line of file
|
||||
try:
|
||||
head = [next(f) for x in xrange(1)]
|
||||
except StopIteration:
|
||||
return
|
||||
|
||||
need_update = True
|
||||
for line in head:
|
||||
line = line.lower()
|
||||
if 'seafile ltd.' in line:
|
||||
need_update = False
|
||||
|
||||
if need_update:
|
||||
print 'No copyright info in %s.' % filename
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
"""
|
||||
Tools for i18n.
|
||||
"""
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from signals import repo_created, repo_deleted
|
||||
from handlers import repo_created_cb, repo_deleted_cb
|
||||
|
||||
|
@ -1 +1,2 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import datetime
|
||||
import logging
|
||||
from rest_framework import status
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
#coding: UTF-8
|
||||
|
||||
from rest_framework.views import APIView as RestFrameworkAPIView
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
from rest_framework.permissions import IsAdminUser
|
||||
from rest_framework.response import Response
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
from rest_framework.permissions import IsAdminUser
|
||||
from rest_framework.response import Response
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import os
|
||||
import stat
|
||||
import logging
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
from rest_framework.permissions import IsAdminUser
|
||||
from rest_framework.response import Response
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
import os
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import re
|
||||
import datetime
|
||||
import time
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import os
|
||||
import logging
|
||||
import posixpath
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
import json
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import stat
|
||||
import logging
|
||||
import json
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import os
|
||||
import logging
|
||||
import posixpath
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from rest_framework import status
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from rest_framework import status
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from django.db.models import Count
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from rest_framework import status
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import json
|
||||
|
||||
from django.core.paginator import EmptyPage, InvalidPage
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework import status
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from django.utils.translation import ugettext as _
|
||||
from rest_framework import status
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
import json
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import json
|
||||
|
||||
from django.db.models import Q
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import os
|
||||
import logging
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
import os
|
||||
import json
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import os
|
||||
import logging
|
||||
from constance import config
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import json
|
||||
import os
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import os
|
||||
import logging
|
||||
from constance import config
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from rest_framework import status
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
import json
|
||||
import stat
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import uuid
|
||||
import hmac
|
||||
import datetime
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
"""
|
||||
Provides a set of pluggable permission policies.
|
||||
"""
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from rest_framework import serializers
|
||||
|
||||
from seahub.auth import authenticate
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
# Define custom HTTP status code. 4xx starts from 440, 5xx starts from 520.
|
||||
HTTP_440_REPO_PASSWD_REQUIRED = 440
|
||||
HTTP_441_REPO_PASSWD_MAGIC_REQUIRED = 441
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
"""
|
||||
This file demonstrates writing tests using the unittest module. These will pass
|
||||
when you run "manage.py test".
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
"""
|
||||
Provides various throttling policies.
|
||||
"""
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from django.conf.urls import patterns, url, include
|
||||
|
||||
from .views import *
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
# encoding: utf-8
|
||||
# Utility functions for api2
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
# encoding: utf-8
|
||||
import logging
|
||||
import os
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from rest_framework import status
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from seahub.api2.base import APIView
|
||||
from seahub.api2.utils import json_response, is_seafile_pro
|
||||
from seahub import settings
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import datetime
|
||||
from warnings import warn
|
||||
from django.conf import settings
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from django import template
|
||||
from django.db import transaction
|
||||
from django.conf import settings
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
try:
|
||||
from functools import update_wrapper, wraps
|
||||
except ImportError:
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from django.contrib.sites.models import Site
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from mod_python import apache
|
||||
import os
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
"""
|
||||
Creates permissions for all installed apps that need permissions.
|
||||
"""
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.contrib.auth.models import User
|
||||
import getpass
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
"""
|
||||
Management utility to create superusers.
|
||||
"""
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from django.contrib import auth
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import datetime
|
||||
import hashlib
|
||||
import urllib
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from django.dispatch import Signal
|
||||
|
||||
user_logged_in = Signal(providing_args=['request', 'user'])
|
||||
|
@ -1,17 +0,0 @@
|
||||
from auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest
|
||||
from auth.tests.basic import BASIC_TESTS
|
||||
from auth.tests.decorators import LoginRequiredTestCase
|
||||
from auth.tests.forms import UserCreationFormTest, AuthenticationFormTest, SetPasswordFormTest, PasswordChangeFormTest, UserChangeFormTest, PasswordResetFormTest
|
||||
from auth.tests.remote_user \
|
||||
import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest
|
||||
from auth.tests.models import ProfileTestCase
|
||||
from auth.tests.tokens import TOKEN_GENERATOR_TESTS
|
||||
from auth.tests.views \
|
||||
import PasswordResetTest, ChangePasswordTest, LoginTest, LogoutTest
|
||||
|
||||
# The password for the fixture data users is 'password'
|
||||
|
||||
__test__ = {
|
||||
'BASIC_TESTS': BASIC_TESTS,
|
||||
'TOKEN_GENERATOR_TESTS': TOKEN_GENERATOR_TESTS,
|
||||
}
|
@ -1,247 +0,0 @@
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User, Group, Permission, AnonymousUser
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
class BackendTest(TestCase):
|
||||
|
||||
backend = 'django.contrib.auth.backends.ModelBackend'
|
||||
|
||||
def setUp(self):
|
||||
self.curr_auth = settings.AUTHENTICATION_BACKENDS
|
||||
settings.AUTHENTICATION_BACKENDS = (self.backend,)
|
||||
User.objects.create_user('test', 'test@example.com', 'test')
|
||||
|
||||
def tearDown(self):
|
||||
settings.AUTHENTICATION_BACKENDS = self.curr_auth
|
||||
|
||||
def test_has_perm(self):
|
||||
user = User.objects.get(username='test')
|
||||
self.assertEqual(user.has_perm('auth.test'), False)
|
||||
user.is_staff = True
|
||||
user.save()
|
||||
self.assertEqual(user.has_perm('auth.test'), False)
|
||||
user.is_superuser = True
|
||||
user.save()
|
||||
self.assertEqual(user.has_perm('auth.test'), True)
|
||||
user.is_staff = False
|
||||
user.is_superuser = False
|
||||
user.save()
|
||||
self.assertEqual(user.has_perm('auth.test'), False)
|
||||
user.is_staff = True
|
||||
user.is_superuser = True
|
||||
user.is_active = False
|
||||
user.save()
|
||||
self.assertEqual(user.has_perm('auth.test'), False)
|
||||
|
||||
def test_custom_perms(self):
|
||||
user = User.objects.get(username='test')
|
||||
content_type=ContentType.objects.get_for_model(Group)
|
||||
perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
|
||||
user.user_permissions.add(perm)
|
||||
user.save()
|
||||
|
||||
# reloading user to purge the _perm_cache
|
||||
user = User.objects.get(username='test')
|
||||
self.assertEqual(user.get_all_permissions() == set([u'auth.test']), True)
|
||||
self.assertEqual(user.get_group_permissions(), set([]))
|
||||
self.assertEqual(user.has_module_perms('Group'), False)
|
||||
self.assertEqual(user.has_module_perms('auth'), True)
|
||||
perm = Permission.objects.create(name='test2', content_type=content_type, codename='test2')
|
||||
user.user_permissions.add(perm)
|
||||
user.save()
|
||||
perm = Permission.objects.create(name='test3', content_type=content_type, codename='test3')
|
||||
user.user_permissions.add(perm)
|
||||
user.save()
|
||||
user = User.objects.get(username='test')
|
||||
self.assertEqual(user.get_all_permissions(), set([u'auth.test2', u'auth.test', u'auth.test3']))
|
||||
self.assertEqual(user.has_perm('test'), False)
|
||||
self.assertEqual(user.has_perm('auth.test'), True)
|
||||
self.assertEqual(user.has_perms(['auth.test2', 'auth.test3']), True)
|
||||
perm = Permission.objects.create(name='test_group', content_type=content_type, codename='test_group')
|
||||
group = Group.objects.create(name='test_group')
|
||||
group.permissions.add(perm)
|
||||
group.save()
|
||||
user.groups.add(group)
|
||||
user = User.objects.get(username='test')
|
||||
exp = set([u'auth.test2', u'auth.test', u'auth.test3', u'auth.test_group'])
|
||||
self.assertEqual(user.get_all_permissions(), exp)
|
||||
self.assertEqual(user.get_group_permissions(), set([u'auth.test_group']))
|
||||
self.assertEqual(user.has_perms(['auth.test3', 'auth.test_group']), True)
|
||||
|
||||
user = AnonymousUser()
|
||||
self.assertEqual(user.has_perm('test'), False)
|
||||
self.assertEqual(user.has_perms(['auth.test2', 'auth.test3']), False)
|
||||
|
||||
def test_has_no_object_perm(self):
|
||||
"""Regressiontest for #12462"""
|
||||
user = User.objects.get(username='test')
|
||||
content_type=ContentType.objects.get_for_model(Group)
|
||||
perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
|
||||
user.user_permissions.add(perm)
|
||||
user.save()
|
||||
|
||||
self.assertEqual(user.has_perm('auth.test', 'object'), False)
|
||||
self.assertEqual(user.get_all_permissions('object'), set([]))
|
||||
self.assertEqual(user.has_perm('auth.test'), True)
|
||||
self.assertEqual(user.get_all_permissions(), set(['auth.test']))
|
||||
|
||||
|
||||
class TestObj(object):
|
||||
pass
|
||||
|
||||
|
||||
class SimpleRowlevelBackend(object):
|
||||
supports_object_permissions = True
|
||||
|
||||
# This class also supports tests for anonymous user permissions,
|
||||
# via subclasses which just set the 'supports_anonymous_user' attribute.
|
||||
|
||||
def has_perm(self, user, perm, obj=None):
|
||||
if not obj:
|
||||
return # We only support row level perms
|
||||
|
||||
if isinstance(obj, TestObj):
|
||||
if user.username == 'test2':
|
||||
return True
|
||||
elif user.is_anonymous() and perm == 'anon':
|
||||
# not reached due to supports_anonymous_user = False
|
||||
return True
|
||||
return False
|
||||
|
||||
def has_module_perms(self, user, app_label):
|
||||
return app_label == "app1"
|
||||
|
||||
def get_all_permissions(self, user, obj=None):
|
||||
if not obj:
|
||||
return [] # We only support row level perms
|
||||
|
||||
if not isinstance(obj, TestObj):
|
||||
return ['none']
|
||||
|
||||
if user.is_anonymous():
|
||||
return ['anon']
|
||||
if user.username == 'test2':
|
||||
return ['simple', 'advanced']
|
||||
else:
|
||||
return ['simple']
|
||||
|
||||
def get_group_permissions(self, user, obj=None):
|
||||
if not obj:
|
||||
return # We only support row level perms
|
||||
|
||||
if not isinstance(obj, TestObj):
|
||||
return ['none']
|
||||
|
||||
if 'test_group' in [group.name for group in user.groups.all()]:
|
||||
return ['group_perm']
|
||||
else:
|
||||
return ['none']
|
||||
|
||||
|
||||
class RowlevelBackendTest(TestCase):
|
||||
"""
|
||||
Tests for auth backend that supports object level permissions
|
||||
"""
|
||||
backend = 'django.contrib.auth.tests.auth_backends.SimpleRowlevelBackend'
|
||||
|
||||
def setUp(self):
|
||||
self.curr_auth = settings.AUTHENTICATION_BACKENDS
|
||||
settings.AUTHENTICATION_BACKENDS = self.curr_auth + (self.backend,)
|
||||
self.user1 = User.objects.create_user('test', 'test@example.com', 'test')
|
||||
self.user2 = User.objects.create_user('test2', 'test2@example.com', 'test')
|
||||
self.user3 = User.objects.create_user('test3', 'test3@example.com', 'test')
|
||||
|
||||
def tearDown(self):
|
||||
settings.AUTHENTICATION_BACKENDS = self.curr_auth
|
||||
|
||||
def test_has_perm(self):
|
||||
self.assertEqual(self.user1.has_perm('perm', TestObj()), False)
|
||||
self.assertEqual(self.user2.has_perm('perm', TestObj()), True)
|
||||
self.assertEqual(self.user2.has_perm('perm'), False)
|
||||
self.assertEqual(self.user2.has_perms(['simple', 'advanced'], TestObj()), True)
|
||||
self.assertEqual(self.user3.has_perm('perm', TestObj()), False)
|
||||
self.assertEqual(self.user3.has_perm('anon', TestObj()), False)
|
||||
self.assertEqual(self.user3.has_perms(['simple', 'advanced'], TestObj()), False)
|
||||
|
||||
def test_get_all_permissions(self):
|
||||
self.assertEqual(self.user1.get_all_permissions(TestObj()), set(['simple']))
|
||||
self.assertEqual(self.user2.get_all_permissions(TestObj()), set(['simple', 'advanced']))
|
||||
self.assertEqual(self.user2.get_all_permissions(), set([]))
|
||||
|
||||
def test_get_group_permissions(self):
|
||||
content_type=ContentType.objects.get_for_model(Group)
|
||||
group = Group.objects.create(name='test_group')
|
||||
self.user3.groups.add(group)
|
||||
self.assertEqual(self.user3.get_group_permissions(TestObj()), set(['group_perm']))
|
||||
|
||||
|
||||
class AnonymousUserBackend(SimpleRowlevelBackend):
|
||||
|
||||
supports_anonymous_user = True
|
||||
|
||||
|
||||
class NoAnonymousUserBackend(SimpleRowlevelBackend):
|
||||
|
||||
supports_anonymous_user = False
|
||||
|
||||
|
||||
class AnonymousUserBackendTest(TestCase):
|
||||
"""
|
||||
Tests for AnonymousUser delegating to backend if it has 'supports_anonymous_user' = True
|
||||
"""
|
||||
|
||||
backend = 'django.contrib.auth.tests.auth_backends.AnonymousUserBackend'
|
||||
|
||||
def setUp(self):
|
||||
self.curr_auth = settings.AUTHENTICATION_BACKENDS
|
||||
settings.AUTHENTICATION_BACKENDS = (self.backend,)
|
||||
self.user1 = AnonymousUser()
|
||||
|
||||
def tearDown(self):
|
||||
settings.AUTHENTICATION_BACKENDS = self.curr_auth
|
||||
|
||||
def test_has_perm(self):
|
||||
self.assertEqual(self.user1.has_perm('perm', TestObj()), False)
|
||||
self.assertEqual(self.user1.has_perm('anon', TestObj()), True)
|
||||
|
||||
def test_has_perms(self):
|
||||
self.assertEqual(self.user1.has_perms(['anon'], TestObj()), True)
|
||||
self.assertEqual(self.user1.has_perms(['anon', 'perm'], TestObj()), False)
|
||||
|
||||
def test_has_module_perms(self):
|
||||
self.assertEqual(self.user1.has_module_perms("app1"), True)
|
||||
self.assertEqual(self.user1.has_module_perms("app2"), False)
|
||||
|
||||
def test_get_all_permissions(self):
|
||||
self.assertEqual(self.user1.get_all_permissions(TestObj()), set(['anon']))
|
||||
|
||||
|
||||
class NoAnonymousUserBackendTest(TestCase):
|
||||
"""
|
||||
Tests that AnonymousUser does not delegate to backend if it has 'supports_anonymous_user' = False
|
||||
"""
|
||||
backend = 'django.contrib.auth.tests.auth_backends.NoAnonymousUserBackend'
|
||||
|
||||
def setUp(self):
|
||||
self.curr_auth = settings.AUTHENTICATION_BACKENDS
|
||||
settings.AUTHENTICATION_BACKENDS = self.curr_auth + (self.backend,)
|
||||
self.user1 = AnonymousUser()
|
||||
|
||||
def tearDown(self):
|
||||
settings.AUTHENTICATION_BACKENDS = self.curr_auth
|
||||
|
||||
def test_has_perm(self):
|
||||
self.assertEqual(self.user1.has_perm('perm', TestObj()), False)
|
||||
self.assertEqual(self.user1.has_perm('anon', TestObj()), False)
|
||||
|
||||
def test_has_perms(self):
|
||||
self.assertEqual(self.user1.has_perms(['anon'], TestObj()), False)
|
||||
|
||||
def test_has_module_perms(self):
|
||||
self.assertEqual(self.user1.has_module_perms("app1"), False)
|
||||
self.assertEqual(self.user1.has_module_perms("app2"), False)
|
||||
|
||||
def test_get_all_permissions(self):
|
||||
self.assertEqual(self.user1.get_all_permissions(TestObj()), set())
|
@ -1,77 +0,0 @@
|
||||
|
||||
BASIC_TESTS = """
|
||||
>>> from django.contrib.auth.models import User, AnonymousUser
|
||||
>>> u = User.objects.create_user('testuser', 'test@example.com', 'testpw')
|
||||
>>> u.has_usable_password()
|
||||
True
|
||||
>>> u.check_password('bad')
|
||||
False
|
||||
>>> u.check_password('testpw')
|
||||
True
|
||||
>>> u.set_unusable_password()
|
||||
>>> u.save()
|
||||
>>> u.check_password('testpw')
|
||||
False
|
||||
>>> u.has_usable_password()
|
||||
False
|
||||
>>> u2 = User.objects.create_user('testuser2', 'test2@example.com')
|
||||
>>> u2.has_usable_password()
|
||||
False
|
||||
|
||||
>>> u.is_authenticated()
|
||||
True
|
||||
>>> u.is_staff
|
||||
False
|
||||
>>> u.is_active
|
||||
True
|
||||
>>> u.is_superuser
|
||||
False
|
||||
|
||||
>>> a = AnonymousUser()
|
||||
>>> a.is_authenticated()
|
||||
False
|
||||
>>> a.is_staff
|
||||
False
|
||||
>>> a.is_active
|
||||
False
|
||||
>>> a.is_superuser
|
||||
False
|
||||
>>> a.groups.all()
|
||||
[]
|
||||
>>> a.user_permissions.all()
|
||||
[]
|
||||
|
||||
# superuser tests.
|
||||
>>> super = User.objects.create_superuser('super', 'super@example.com', 'super')
|
||||
>>> super.is_superuser
|
||||
True
|
||||
>>> super.is_active
|
||||
True
|
||||
>>> super.is_staff
|
||||
True
|
||||
|
||||
#
|
||||
# Tests for createsuperuser management command.
|
||||
# It's nearly impossible to test the interactive mode -- a command test helper
|
||||
# would be needed (and *awesome*) -- so just test the non-interactive mode.
|
||||
# This covers most of the important validation, but not all.
|
||||
#
|
||||
>>> from django.core.management import call_command
|
||||
|
||||
>>> call_command("createsuperuser", interactive=False, username="joe", email="joe@somewhere.org")
|
||||
Superuser created successfully.
|
||||
|
||||
>>> u = User.objects.get(username="joe")
|
||||
>>> u.email
|
||||
u'joe@somewhere.org'
|
||||
>>> u.password
|
||||
u'!'
|
||||
>>> call_command("createsuperuser", interactive=False, username="joe+admin@somewhere.org", email="joe@somewhere.org")
|
||||
Superuser created successfully.
|
||||
|
||||
>>> u = User.objects.get(username="joe+admin@somewhere.org")
|
||||
>>> u.email
|
||||
u'joe@somewhere.org'
|
||||
>>> u.password
|
||||
u'!'
|
||||
"""
|
@ -1,25 +0,0 @@
|
||||
from unittest import TestCase
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
|
||||
|
||||
class LoginRequiredTestCase(TestCase):
|
||||
"""
|
||||
Tests the login_required decorators
|
||||
"""
|
||||
def testCallable(self):
|
||||
"""
|
||||
Check that login_required is assignable to callable objects.
|
||||
"""
|
||||
class CallableView(object):
|
||||
def __call__(self, *args, **kwargs):
|
||||
pass
|
||||
login_required(CallableView())
|
||||
|
||||
def testView(self):
|
||||
"""
|
||||
Check that login_required is assignable to normal views.
|
||||
"""
|
||||
def normal_view(request):
|
||||
pass
|
||||
login_required(normal_view)
|
@ -1,252 +0,0 @@
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
class UserCreationFormTest(TestCase):
|
||||
|
||||
fixtures = ['authtestdata.json']
|
||||
|
||||
def test_user_already_exists(self):
|
||||
data = {
|
||||
'username': 'testclient',
|
||||
'password1': 'test123',
|
||||
'password2': 'test123',
|
||||
}
|
||||
form = UserCreationForm(data)
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form["username"].errors,
|
||||
[u'A user with that username already exists.'])
|
||||
|
||||
def test_invalid_data(self):
|
||||
data = {
|
||||
'username': 'jsmith!',
|
||||
'password1': 'test123',
|
||||
'password2': 'test123',
|
||||
}
|
||||
form = UserCreationForm(data)
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form["username"].errors,
|
||||
[u'This value may contain only letters, numbers and @/./+/-/_ characters.'])
|
||||
|
||||
|
||||
def test_password_verification(self):
|
||||
# The verification password is incorrect.
|
||||
data = {
|
||||
'username': 'jsmith',
|
||||
'password1': 'test123',
|
||||
'password2': 'test',
|
||||
}
|
||||
form = UserCreationForm(data)
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form["password2"].errors,
|
||||
[u"The two password fields didn't match."])
|
||||
|
||||
|
||||
def test_both_passwords(self):
|
||||
# One (or both) passwords weren't given
|
||||
data = {'username': 'jsmith'}
|
||||
form = UserCreationForm(data)
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form['password1'].errors,
|
||||
[u'This field is required.'])
|
||||
self.assertEqual(form['password2'].errors,
|
||||
[u'This field is required.'])
|
||||
|
||||
|
||||
data['password2'] = 'test123'
|
||||
form = UserCreationForm(data)
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form['password1'].errors,
|
||||
[u'This field is required.'])
|
||||
|
||||
def test_success(self):
|
||||
# The success case.
|
||||
|
||||
data = {
|
||||
'username': 'jsmith@example.com',
|
||||
'password1': 'test123',
|
||||
'password2': 'test123',
|
||||
}
|
||||
form = UserCreationForm(data)
|
||||
self.assertTrue(form.is_valid())
|
||||
u = form.save()
|
||||
self.assertEqual(repr(u), '<User: jsmith@example.com>')
|
||||
|
||||
|
||||
class AuthenticationFormTest(TestCase):
|
||||
|
||||
fixtures = ['authtestdata.json']
|
||||
|
||||
def test_invalid_username(self):
|
||||
# The user submits an invalid username.
|
||||
|
||||
data = {
|
||||
'username': 'jsmith_does_not_exist',
|
||||
'password': 'test123',
|
||||
}
|
||||
form = AuthenticationForm(None, data)
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form.non_field_errors(),
|
||||
[u'Please enter a correct username and password. Note that both fields are case-sensitive.'])
|
||||
|
||||
def test_inactive_user(self):
|
||||
# The user is inactive.
|
||||
data = {
|
||||
'username': 'inactive',
|
||||
'password': 'password',
|
||||
}
|
||||
form = AuthenticationForm(None, data)
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form.non_field_errors(),
|
||||
[u'This account is inactive.'])
|
||||
|
||||
|
||||
def test_success(self):
|
||||
# The success case
|
||||
data = {
|
||||
'username': 'testclient',
|
||||
'password': 'password',
|
||||
}
|
||||
form = AuthenticationForm(None, data)
|
||||
self.assertTrue(form.is_valid())
|
||||
self.assertEqual(form.non_field_errors(), [])
|
||||
|
||||
|
||||
class SetPasswordFormTest(TestCase):
|
||||
|
||||
fixtures = ['authtestdata.json']
|
||||
|
||||
def test_password_verification(self):
|
||||
# The two new passwords do not match.
|
||||
user = User.objects.get(username='testclient')
|
||||
data = {
|
||||
'new_password1': 'abc123',
|
||||
'new_password2': 'abc',
|
||||
}
|
||||
form = SetPasswordForm(user, data)
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form["new_password2"].errors,
|
||||
[u"The two password fields didn't match."])
|
||||
|
||||
def test_success(self):
|
||||
user = User.objects.get(username='testclient')
|
||||
data = {
|
||||
'new_password1': 'abc123',
|
||||
'new_password2': 'abc123',
|
||||
}
|
||||
form = SetPasswordForm(user, data)
|
||||
self.assertTrue(form.is_valid())
|
||||
|
||||
|
||||
class PasswordChangeFormTest(TestCase):
|
||||
|
||||
fixtures = ['authtestdata.json']
|
||||
|
||||
def test_incorrect_password(self):
|
||||
user = User.objects.get(username='testclient')
|
||||
data = {
|
||||
'old_password': 'test',
|
||||
'new_password1': 'abc123',
|
||||
'new_password2': 'abc123',
|
||||
}
|
||||
form = PasswordChangeForm(user, data)
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form["old_password"].errors,
|
||||
[u'Your old password was entered incorrectly. Please enter it again.'])
|
||||
|
||||
|
||||
def test_password_verification(self):
|
||||
# The two new passwords do not match.
|
||||
user = User.objects.get(username='testclient')
|
||||
data = {
|
||||
'old_password': 'password',
|
||||
'new_password1': 'abc123',
|
||||
'new_password2': 'abc',
|
||||
}
|
||||
form = PasswordChangeForm(user, data)
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form["new_password2"].errors,
|
||||
[u"The two password fields didn't match."])
|
||||
|
||||
|
||||
def test_success(self):
|
||||
# The success case.
|
||||
user = User.objects.get(username='testclient')
|
||||
data = {
|
||||
'old_password': 'password',
|
||||
'new_password1': 'abc123',
|
||||
'new_password2': 'abc123',
|
||||
}
|
||||
form = PasswordChangeForm(user, data)
|
||||
self.assertTrue(form.is_valid())
|
||||
|
||||
def test_field_order(self):
|
||||
# Regression test - check the order of fields:
|
||||
user = User.objects.get(username='testclient')
|
||||
self.assertEqual(PasswordChangeForm(user, {}).fields.keys(),
|
||||
['old_password', 'new_password1', 'new_password2'])
|
||||
|
||||
class UserChangeFormTest(TestCase):
|
||||
|
||||
fixtures = ['authtestdata.json']
|
||||
|
||||
def test_username_validity(self):
|
||||
user = User.objects.get(username='testclient')
|
||||
data = {'username': 'not valid'}
|
||||
form = UserChangeForm(data, instance=user)
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form['username'].errors,
|
||||
[u'This value may contain only letters, numbers and @/./+/-/_ characters.'])
|
||||
|
||||
def test_bug_14242(self):
|
||||
# A regression test, introduce by adding an optimization for the
|
||||
# UserChangeForm.
|
||||
|
||||
class MyUserForm(UserChangeForm):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(MyUserForm, self).__init__(*args, **kwargs)
|
||||
self.fields['groups'].help_text = 'These groups give users different permissions'
|
||||
|
||||
class Meta(UserChangeForm.Meta):
|
||||
fields = ('groups',)
|
||||
|
||||
# Just check we can create it
|
||||
form = MyUserForm({})
|
||||
|
||||
|
||||
class PasswordResetFormTest(TestCase):
|
||||
|
||||
fixtures = ['authtestdata.json']
|
||||
|
||||
def test_invalid_email(self):
|
||||
data = {'email':'not valid'}
|
||||
form = PasswordResetForm(data)
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form['email'].errors,
|
||||
[u'Enter a valid e-mail address.'])
|
||||
|
||||
def test_nonexistant_email(self):
|
||||
# Test nonexistant email address
|
||||
data = {'email':'foo@bar.com'}
|
||||
form = PasswordResetForm(data)
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form.errors,
|
||||
{'email': [u"That e-mail address doesn't have an associated user account. Are you sure you've registered?"]})
|
||||
|
||||
def test_cleaned_data(self):
|
||||
# Regression test
|
||||
user = User.objects.create_user("jsmith3", "jsmith3@example.com", "test123")
|
||||
data = {'email':'jsmith3@example.com'}
|
||||
form = PasswordResetForm(data)
|
||||
self.assertTrue(form.is_valid())
|
||||
self.assertEqual(form.cleaned_data['email'], u'jsmith3@example.com')
|
||||
|
||||
|
||||
def test_bug_5605(self):
|
||||
# bug #5605, preserve the case of the user name (before the @ in the
|
||||
# email address) when creating a user.
|
||||
user = User.objects.create_user('forms_test2', 'tesT@EXAMple.com', 'test')
|
||||
self.assertEqual(user.email, 'tesT@example.com')
|
||||
user = User.objects.create_user('forms_test3', 'tesT', 'test')
|
||||
self.assertEqual(user.email, 'tesT')
|
@ -1,35 +0,0 @@
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
from django.contrib.auth.models import User, SiteProfileNotAvailable
|
||||
|
||||
class ProfileTestCase(TestCase):
|
||||
fixtures = ['authtestdata.json']
|
||||
def setUp(self):
|
||||
"""Backs up the AUTH_PROFILE_MODULE"""
|
||||
self.old_AUTH_PROFILE_MODULE = getattr(settings,
|
||||
'AUTH_PROFILE_MODULE', None)
|
||||
|
||||
def tearDown(self):
|
||||
"""Restores the AUTH_PROFILE_MODULE -- if it was not set it is deleted,
|
||||
otherwise the old value is restored"""
|
||||
if self.old_AUTH_PROFILE_MODULE is None and \
|
||||
hasattr(settings, 'AUTH_PROFILE_MODULE'):
|
||||
del settings.AUTH_PROFILE_MODULE
|
||||
|
||||
if self.old_AUTH_PROFILE_MODULE is not None:
|
||||
settings.AUTH_PROFILE_MODULE = self.old_AUTH_PROFILE_MODULE
|
||||
|
||||
def test_site_profile_not_available(self):
|
||||
# calling get_profile without AUTH_PROFILE_MODULE set
|
||||
if hasattr(settings, 'AUTH_PROFILE_MODULE'):
|
||||
del settings.AUTH_PROFILE_MODULE
|
||||
user = User.objects.get(username='testclient')
|
||||
self.assertRaises(SiteProfileNotAvailable, user.get_profile)
|
||||
|
||||
# Bad syntax in AUTH_PROFILE_MODULE:
|
||||
settings.AUTH_PROFILE_MODULE = 'foobar'
|
||||
self.assertRaises(SiteProfileNotAvailable, user.get_profile)
|
||||
|
||||
# module that doesn't exist
|
||||
settings.AUTH_PROFILE_MODULE = 'foo.bar'
|
||||
self.assertRaises(SiteProfileNotAvailable, user.get_profile)
|
@ -1,170 +0,0 @@
|
||||
from datetime import datetime
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.backends import RemoteUserBackend
|
||||
from django.contrib.auth.models import User
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
class RemoteUserTest(TestCase):
|
||||
|
||||
urls = 'django.contrib.auth.tests.urls'
|
||||
middleware = 'django.contrib.auth.middleware.RemoteUserMiddleware'
|
||||
backend = 'django.contrib.auth.backends.RemoteUserBackend'
|
||||
|
||||
# Usernames to be passed in REMOTE_USER for the test_known_user test case.
|
||||
known_user = 'knownuser'
|
||||
known_user2 = 'knownuser2'
|
||||
|
||||
def setUp(self):
|
||||
self.curr_middleware = settings.MIDDLEWARE_CLASSES
|
||||
self.curr_auth = settings.AUTHENTICATION_BACKENDS
|
||||
settings.MIDDLEWARE_CLASSES += (self.middleware,)
|
||||
settings.AUTHENTICATION_BACKENDS = (self.backend,)
|
||||
|
||||
def test_no_remote_user(self):
|
||||
"""
|
||||
Tests requests where no remote user is specified and insures that no
|
||||
users get created.
|
||||
"""
|
||||
num_users = User.objects.count()
|
||||
|
||||
response = self.client.get('/remote_user/')
|
||||
self.assert_(response.context['user'].is_anonymous())
|
||||
self.assertEqual(User.objects.count(), num_users)
|
||||
|
||||
response = self.client.get('/remote_user/', REMOTE_USER=None)
|
||||
self.assert_(response.context['user'].is_anonymous())
|
||||
self.assertEqual(User.objects.count(), num_users)
|
||||
|
||||
response = self.client.get('/remote_user/', REMOTE_USER='')
|
||||
self.assert_(response.context['user'].is_anonymous())
|
||||
self.assertEqual(User.objects.count(), num_users)
|
||||
|
||||
def test_unknown_user(self):
|
||||
"""
|
||||
Tests the case where the username passed in the header does not exist
|
||||
as a User.
|
||||
"""
|
||||
num_users = User.objects.count()
|
||||
response = self.client.get('/remote_user/', REMOTE_USER='newuser')
|
||||
self.assertEqual(response.context['user'].username, 'newuser')
|
||||
self.assertEqual(User.objects.count(), num_users + 1)
|
||||
User.objects.get(username='newuser')
|
||||
|
||||
# Another request with same user should not create any new users.
|
||||
response = self.client.get('/remote_user/', REMOTE_USER='newuser')
|
||||
self.assertEqual(User.objects.count(), num_users + 1)
|
||||
|
||||
def test_known_user(self):
|
||||
"""
|
||||
Tests the case where the username passed in the header is a valid User.
|
||||
"""
|
||||
User.objects.create(username='knownuser')
|
||||
User.objects.create(username='knownuser2')
|
||||
num_users = User.objects.count()
|
||||
response = self.client.get('/remote_user/', REMOTE_USER=self.known_user)
|
||||
self.assertEqual(response.context['user'].username, 'knownuser')
|
||||
self.assertEqual(User.objects.count(), num_users)
|
||||
# Test that a different user passed in the headers causes the new user
|
||||
# to be logged in.
|
||||
response = self.client.get('/remote_user/', REMOTE_USER=self.known_user2)
|
||||
self.assertEqual(response.context['user'].username, 'knownuser2')
|
||||
self.assertEqual(User.objects.count(), num_users)
|
||||
|
||||
def test_last_login(self):
|
||||
"""
|
||||
Tests that a user's last_login is set the first time they make a
|
||||
request but not updated in subsequent requests with the same session.
|
||||
"""
|
||||
user = User.objects.create(username='knownuser')
|
||||
# Set last_login to something so we can determine if it changes.
|
||||
default_login = datetime(2000, 1, 1)
|
||||
user.last_login = default_login
|
||||
user.save()
|
||||
|
||||
response = self.client.get('/remote_user/', REMOTE_USER=self.known_user)
|
||||
self.assertNotEqual(default_login, response.context['user'].last_login)
|
||||
|
||||
user = User.objects.get(username='knownuser')
|
||||
user.last_login = default_login
|
||||
user.save()
|
||||
response = self.client.get('/remote_user/', REMOTE_USER=self.known_user)
|
||||
self.assertEqual(default_login, response.context['user'].last_login)
|
||||
|
||||
def tearDown(self):
|
||||
"""Restores settings to avoid breaking other tests."""
|
||||
settings.MIDDLEWARE_CLASSES = self.curr_middleware
|
||||
settings.AUTHENTICATION_BACKENDS = self.curr_auth
|
||||
|
||||
|
||||
class RemoteUserNoCreateBackend(RemoteUserBackend):
|
||||
"""Backend that doesn't create unknown users."""
|
||||
create_unknown_user = False
|
||||
|
||||
|
||||
class RemoteUserNoCreateTest(RemoteUserTest):
|
||||
"""
|
||||
Contains the same tests as RemoteUserTest, but using a custom auth backend
|
||||
class that doesn't create unknown users.
|
||||
"""
|
||||
|
||||
backend =\
|
||||
'django.contrib.auth.tests.remote_user.RemoteUserNoCreateBackend'
|
||||
|
||||
def test_unknown_user(self):
|
||||
num_users = User.objects.count()
|
||||
response = self.client.get('/remote_user/', REMOTE_USER='newuser')
|
||||
self.assert_(response.context['user'].is_anonymous())
|
||||
self.assertEqual(User.objects.count(), num_users)
|
||||
|
||||
|
||||
class CustomRemoteUserBackend(RemoteUserBackend):
|
||||
"""
|
||||
Backend that overrides RemoteUserBackend methods.
|
||||
"""
|
||||
|
||||
def clean_username(self, username):
|
||||
"""
|
||||
Grabs username before the @ character.
|
||||
"""
|
||||
return username.split('@')[0]
|
||||
|
||||
def configure_user(self, user):
|
||||
"""
|
||||
Sets user's email address.
|
||||
"""
|
||||
user.email = 'user@example.com'
|
||||
user.save()
|
||||
return user
|
||||
|
||||
|
||||
class RemoteUserCustomTest(RemoteUserTest):
|
||||
"""
|
||||
Tests a custom RemoteUserBackend subclass that overrides the clean_username
|
||||
and configure_user methods.
|
||||
"""
|
||||
|
||||
backend =\
|
||||
'django.contrib.auth.tests.remote_user.CustomRemoteUserBackend'
|
||||
# REMOTE_USER strings with e-mail addresses for the custom backend to
|
||||
# clean.
|
||||
known_user = 'knownuser@example.com'
|
||||
known_user2 = 'knownuser2@example.com'
|
||||
|
||||
def test_known_user(self):
|
||||
"""
|
||||
The strings passed in REMOTE_USER should be cleaned and the known users
|
||||
should not have been configured with an email address.
|
||||
"""
|
||||
super(RemoteUserCustomTest, self).test_known_user()
|
||||
self.assertEqual(User.objects.get(username='knownuser').email, '')
|
||||
self.assertEqual(User.objects.get(username='knownuser2').email, '')
|
||||
|
||||
def test_unknown_user(self):
|
||||
"""
|
||||
The unknown user created should be configured with an email address.
|
||||
"""
|
||||
super(RemoteUserCustomTest, self).test_unknown_user()
|
||||
newuser = User.objects.get(username='newuser')
|
||||
self.assertEqual(newuser.email, 'user@example.com')
|
@ -1 +0,0 @@
|
||||
{{ form.as_ul }}
|
@ -1 +0,0 @@
|
||||
Password reset successfully
|
@ -1,5 +0,0 @@
|
||||
{% if validlink %}
|
||||
Please enter your new password: {{ form }}
|
||||
{% else %}
|
||||
The password reset link was invalid
|
||||
{% endif %}
|
@ -1 +0,0 @@
|
||||
E-mail sent
|
@ -1 +0,0 @@
|
||||
{{ protocol }}://{{ domain }}/reset/{{ uid }}-{{ token }}/
|
@ -1 +0,0 @@
|
||||
{{ form }}
|
@ -1,42 +0,0 @@
|
||||
TOKEN_GENERATOR_TESTS = """
|
||||
>>> from django.contrib.auth.models import User, AnonymousUser
|
||||
>>> from django.contrib.auth.tokens import PasswordResetTokenGenerator
|
||||
>>> from django.conf import settings
|
||||
>>> u = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw')
|
||||
>>> p0 = PasswordResetTokenGenerator()
|
||||
>>> tk1 = p0.make_token(u)
|
||||
>>> p0.check_token(u, tk1)
|
||||
True
|
||||
|
||||
>>> u = User.objects.create_user('comebackkid', 'test3@example.com', 'testpw')
|
||||
>>> p0 = PasswordResetTokenGenerator()
|
||||
>>> tk1 = p0.make_token(u)
|
||||
>>> reload = User.objects.get(username='comebackkid')
|
||||
>>> tk2 = p0.make_token(reload)
|
||||
>>> tk1 == tk2
|
||||
True
|
||||
|
||||
Tests to ensure we can use the token after n days, but no greater.
|
||||
Use a mocked version of PasswordResetTokenGenerator so we can change
|
||||
the value of 'today'
|
||||
|
||||
>>> class Mocked(PasswordResetTokenGenerator):
|
||||
... def __init__(self, today):
|
||||
... self._today_val = today
|
||||
... def _today(self):
|
||||
... return self._today_val
|
||||
|
||||
>>> from datetime import date, timedelta
|
||||
>>> p1 = Mocked(date.today() + timedelta(settings.PASSWORD_RESET_TIMEOUT_DAYS))
|
||||
>>> p1.check_token(u, tk1)
|
||||
True
|
||||
>>> p2 = Mocked(date.today() + timedelta(settings.PASSWORD_RESET_TIMEOUT_DAYS + 1))
|
||||
>>> p2.check_token(u, tk1)
|
||||
False
|
||||
|
||||
This will put a 14-digit base36 timestamp into the token, which is too large.
|
||||
>>> tk1 = p0._make_token_with_timestamp(u, 175455491841851871349)
|
||||
>>> p0.check_token(u, tk1)
|
||||
False
|
||||
|
||||
"""
|
@ -1,18 +0,0 @@
|
||||
from django.conf.urls.defaults import patterns
|
||||
from django.contrib.auth.urls import urlpatterns
|
||||
from django.http import HttpResponse
|
||||
from django.template import Template, RequestContext
|
||||
|
||||
def remote_user_auth_view(request):
|
||||
"Dummy view for remote user tests"
|
||||
t = Template("Username is {{ user }}.")
|
||||
c = RequestContext(request, {})
|
||||
return HttpResponse(t.render(c))
|
||||
|
||||
# special urls for auth test cases
|
||||
urlpatterns += patterns('',
|
||||
(r'^logout/custom_query/$', 'django.contrib.auth.views.logout', dict(redirect_field_name='follow')),
|
||||
(r'^logout/next_page/$', 'django.contrib.auth.views.logout', dict(next_page='/somewhere/')),
|
||||
(r'^remote_user/$', remote_user_auth_view),
|
||||
)
|
||||
|
@ -1,274 +0,0 @@
|
||||
import os
|
||||
import re
|
||||
import urllib
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import SESSION_KEY, REDIRECT_FIELD_NAME
|
||||
from django.contrib.auth.forms import AuthenticationForm
|
||||
from django.contrib.sites.models import Site, RequestSite
|
||||
from django.contrib.auth.models import User
|
||||
from django.test import TestCase
|
||||
from django.core import mail
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
class AuthViewsTestCase(TestCase):
|
||||
"""
|
||||
Helper base class for all the follow test cases.
|
||||
"""
|
||||
fixtures = ['authtestdata.json']
|
||||
urls = 'django.contrib.auth.urls'
|
||||
|
||||
def setUp(self):
|
||||
self.old_LANGUAGES = settings.LANGUAGES
|
||||
self.old_LANGUAGE_CODE = settings.LANGUAGE_CODE
|
||||
settings.LANGUAGES = (('en', 'English'),)
|
||||
settings.LANGUAGE_CODE = 'en'
|
||||
self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
|
||||
settings.TEMPLATE_DIRS = (
|
||||
os.path.join(
|
||||
os.path.dirname(__file__),
|
||||
'templates'
|
||||
)
|
||||
,)
|
||||
|
||||
def tearDown(self):
|
||||
settings.LANGUAGES = self.old_LANGUAGES
|
||||
settings.LANGUAGE_CODE = self.old_LANGUAGE_CODE
|
||||
settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
|
||||
|
||||
class PasswordResetTest(AuthViewsTestCase):
|
||||
|
||||
def test_email_not_found(self):
|
||||
"Error is raised if the provided email address isn't currently registered"
|
||||
response = self.client.get('/password_reset/')
|
||||
self.assertEquals(response.status_code, 200)
|
||||
response = self.client.post('/password_reset/', {'email': 'not_a_real_email@email.com'})
|
||||
self.assertContains(response, "That e-mail address doesn't have an associated user account")
|
||||
self.assertEquals(len(mail.outbox), 0)
|
||||
|
||||
def test_email_found(self):
|
||||
"Email is sent if a valid email address is provided for password reset"
|
||||
response = self.client.post('/password_reset/', {'email': 'staffmember@example.com'})
|
||||
self.assertEquals(response.status_code, 302)
|
||||
self.assertEquals(len(mail.outbox), 1)
|
||||
self.assert_("http://" in mail.outbox[0].body)
|
||||
|
||||
def _test_confirm_start(self):
|
||||
# Start by creating the email
|
||||
response = self.client.post('/password_reset/', {'email': 'staffmember@example.com'})
|
||||
self.assertEquals(response.status_code, 302)
|
||||
self.assertEquals(len(mail.outbox), 1)
|
||||
return self._read_signup_email(mail.outbox[0])
|
||||
|
||||
def _read_signup_email(self, email):
|
||||
urlmatch = re.search(r"https?://[^/]*(/.*reset/\S*)", email.body)
|
||||
self.assert_(urlmatch is not None, "No URL found in sent email")
|
||||
return urlmatch.group(), urlmatch.groups()[0]
|
||||
|
||||
def test_confirm_valid(self):
|
||||
url, path = self._test_confirm_start()
|
||||
response = self.client.get(path)
|
||||
# redirect to a 'complete' page:
|
||||
self.assertEquals(response.status_code, 200)
|
||||
self.assert_("Please enter your new password" in response.content)
|
||||
|
||||
def test_confirm_invalid(self):
|
||||
url, path = self._test_confirm_start()
|
||||
# Let's munge the token in the path, but keep the same length,
|
||||
# in case the URLconf will reject a different length.
|
||||
path = path[:-5] + ("0"*4) + path[-1]
|
||||
|
||||
response = self.client.get(path)
|
||||
self.assertEquals(response.status_code, 200)
|
||||
self.assert_("The password reset link was invalid" in response.content)
|
||||
|
||||
def test_confirm_invalid_post(self):
|
||||
# Same as test_confirm_invalid, but trying
|
||||
# to do a POST instead.
|
||||
url, path = self._test_confirm_start()
|
||||
path = path[:-5] + ("0"*4) + path[-1]
|
||||
|
||||
response = self.client.post(path, {'new_password1': 'anewpassword',
|
||||
'new_password2':' anewpassword'})
|
||||
# Check the password has not been changed
|
||||
u = User.objects.get(email='staffmember@example.com')
|
||||
self.assert_(not u.check_password("anewpassword"))
|
||||
|
||||
def test_confirm_complete(self):
|
||||
url, path = self._test_confirm_start()
|
||||
response = self.client.post(path, {'new_password1': 'anewpassword',
|
||||
'new_password2': 'anewpassword'})
|
||||
# It redirects us to a 'complete' page:
|
||||
self.assertEquals(response.status_code, 302)
|
||||
# Check the password has been changed
|
||||
u = User.objects.get(email='staffmember@example.com')
|
||||
self.assert_(u.check_password("anewpassword"))
|
||||
|
||||
# Check we can't use the link again
|
||||
response = self.client.get(path)
|
||||
self.assertEquals(response.status_code, 200)
|
||||
self.assert_("The password reset link was invalid" in response.content)
|
||||
|
||||
def test_confirm_different_passwords(self):
|
||||
url, path = self._test_confirm_start()
|
||||
response = self.client.post(path, {'new_password1': 'anewpassword',
|
||||
'new_password2':' x'})
|
||||
self.assertEquals(response.status_code, 200)
|
||||
self.assert_("The two password fields didn't match" in response.content)
|
||||
|
||||
class ChangePasswordTest(AuthViewsTestCase):
|
||||
|
||||
def login(self, password='password'):
|
||||
response = self.client.post('/login/', {
|
||||
'username': 'testclient',
|
||||
'password': password
|
||||
}
|
||||
)
|
||||
self.assertEquals(response.status_code, 302)
|
||||
self.assert_(response['Location'].endswith(settings.LOGIN_REDIRECT_URL))
|
||||
|
||||
def fail_login(self, password='password'):
|
||||
response = self.client.post('/login/', {
|
||||
'username': 'testclient',
|
||||
'password': password
|
||||
}
|
||||
)
|
||||
self.assertEquals(response.status_code, 200)
|
||||
self.assert_("Please enter a correct username and password. Note that both fields are case-sensitive." in response.content)
|
||||
|
||||
def logout(self):
|
||||
response = self.client.get('/logout/')
|
||||
|
||||
def test_password_change_fails_with_invalid_old_password(self):
|
||||
self.login()
|
||||
response = self.client.post('/password_change/', {
|
||||
'old_password': 'donuts',
|
||||
'new_password1': 'password1',
|
||||
'new_password2': 'password1',
|
||||
}
|
||||
)
|
||||
self.assertEquals(response.status_code, 200)
|
||||
self.assert_("Your old password was entered incorrectly. Please enter it again." in response.content)
|
||||
|
||||
def test_password_change_fails_with_mismatched_passwords(self):
|
||||
self.login()
|
||||
response = self.client.post('/password_change/', {
|
||||
'old_password': 'password',
|
||||
'new_password1': 'password1',
|
||||
'new_password2': 'donuts',
|
||||
}
|
||||
)
|
||||
self.assertEquals(response.status_code, 200)
|
||||
self.assert_("The two password fields didn't match." in response.content)
|
||||
|
||||
def test_password_change_succeeds(self):
|
||||
self.login()
|
||||
response = self.client.post('/password_change/', {
|
||||
'old_password': 'password',
|
||||
'new_password1': 'password1',
|
||||
'new_password2': 'password1',
|
||||
}
|
||||
)
|
||||
self.assertEquals(response.status_code, 302)
|
||||
self.assert_(response['Location'].endswith('/password_change/done/'))
|
||||
self.fail_login()
|
||||
self.login(password='password1')
|
||||
|
||||
class LoginTest(AuthViewsTestCase):
|
||||
|
||||
def test_current_site_in_context_after_login(self):
|
||||
response = self.client.get(reverse('django.contrib.auth.views.login'))
|
||||
self.assertEquals(response.status_code, 200)
|
||||
site = Site.objects.get_current()
|
||||
self.assertEquals(response.context['site'], site)
|
||||
self.assertEquals(response.context['site_name'], site.name)
|
||||
self.assert_(isinstance(response.context['form'], AuthenticationForm),
|
||||
'Login form is not an AuthenticationForm')
|
||||
|
||||
def test_security_check(self, password='password'):
|
||||
login_url = reverse('django.contrib.auth.views.login')
|
||||
|
||||
# Those URLs should not pass the security check
|
||||
for bad_url in ('http://example.com',
|
||||
'https://example.com',
|
||||
'ftp://exampel.com',
|
||||
'//example.com'):
|
||||
|
||||
nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
|
||||
'url': login_url,
|
||||
'next': REDIRECT_FIELD_NAME,
|
||||
'bad_url': urllib.quote(bad_url)
|
||||
}
|
||||
response = self.client.post(nasty_url, {
|
||||
'username': 'testclient',
|
||||
'password': password,
|
||||
}
|
||||
)
|
||||
self.assertEquals(response.status_code, 302)
|
||||
self.assertFalse(bad_url in response['Location'], "%s should be blocked" % bad_url)
|
||||
|
||||
# Now, these URLs have an other URL as a GET parameter and therefore
|
||||
# should be allowed
|
||||
for url_ in ('http://example.com', 'https://example.com',
|
||||
'ftp://exampel.com', '//example.com'):
|
||||
safe_url = '%(url)s?%(next)s=/view/?param=%(safe_param)s' % {
|
||||
'url': login_url,
|
||||
'next': REDIRECT_FIELD_NAME,
|
||||
'safe_param': urllib.quote(url_)
|
||||
}
|
||||
response = self.client.post(safe_url, {
|
||||
'username': 'testclient',
|
||||
'password': password,
|
||||
}
|
||||
)
|
||||
self.assertEquals(response.status_code, 302)
|
||||
self.assertTrue('/view/?param=%s' % url_ in response['Location'], "/view/?param=%s should be allowed" % url_)
|
||||
|
||||
|
||||
class LogoutTest(AuthViewsTestCase):
|
||||
urls = 'django.contrib.auth.tests.urls'
|
||||
|
||||
def login(self, password='password'):
|
||||
response = self.client.post('/login/', {
|
||||
'username': 'testclient',
|
||||
'password': password
|
||||
}
|
||||
)
|
||||
self.assertEquals(response.status_code, 302)
|
||||
self.assert_(response['Location'].endswith(settings.LOGIN_REDIRECT_URL))
|
||||
self.assert_(SESSION_KEY in self.client.session)
|
||||
|
||||
def confirm_logged_out(self):
|
||||
self.assert_(SESSION_KEY not in self.client.session)
|
||||
|
||||
def test_logout_default(self):
|
||||
"Logout without next_page option renders the default template"
|
||||
self.login()
|
||||
response = self.client.get('/logout/')
|
||||
self.assertEquals(200, response.status_code)
|
||||
self.assert_('Logged out' in response.content)
|
||||
self.confirm_logged_out()
|
||||
|
||||
def test_logout_with_next_page_specified(self):
|
||||
"Logout with next_page option given redirects to specified resource"
|
||||
self.login()
|
||||
response = self.client.get('/logout/next_page/')
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assert_(response['Location'].endswith('/somewhere/'))
|
||||
self.confirm_logged_out()
|
||||
|
||||
def test_logout_with_redirect_argument(self):
|
||||
"Logout with query string redirects to specified resource"
|
||||
self.login()
|
||||
response = self.client.get('/logout/?next=/login/')
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assert_(response['Location'].endswith('/login/'))
|
||||
self.confirm_logged_out()
|
||||
|
||||
def test_logout_with_custom_redirect_argument(self):
|
||||
"Logout with custom query string redirects to specified resource"
|
||||
self.login()
|
||||
response = self.client.get('/logout/custom_query/?follow=/somewhere/')
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assert_(response['Location'].endswith('/somewhere/'))
|
||||
self.confirm_logged_out()
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from datetime import date
|
||||
from django.conf import settings
|
||||
from django.utils.http import int_to_base36, base36_to_int
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
# These URLs are normally mapped to /admin/urls.py. This URLs file is
|
||||
# provided as a convenience to those who want to deploy these URLs elsewhere.
|
||||
# This file is also used to provide a reliable view deployment for test purposes.
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import hashlib
|
||||
import re
|
||||
import logging
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from django.contrib import admin
|
||||
from seahub.avatar.models import Avatar
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import os
|
||||
|
||||
from django import forms
|
||||
|
@ -1 +1,2 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from django.core.management.base import NoArgsCommand
|
||||
|
||||
from seahub.avatar.models import Avatar
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from abc import abstractmethod
|
||||
import datetime
|
||||
import hashlib
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from django.conf import settings
|
||||
|
||||
try:
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import django.dispatch
|
||||
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
import urllib
|
||||
import hashlib
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import os.path
|
||||
|
||||
from django.test import TestCase
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
urlpatterns = patterns('seahub.avatar.views',
|
||||
|
@ -1,3 +1,4 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.core.files.storage import default_storage, get_storage_class
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user