Skip to content

Commit

Permalink
Add minimal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed May 17, 2017
1 parent 72ab4b4 commit 1a9e2b0
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 4 deletions.
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# top-most EditorConfig file
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

# HTML: 2 spaces
[*.html]
indent_size = 2

# SCSS: 2 spaces
[*.scss]
indent_size = 2

# JS : 2 spaces
[*.js]
indent_size = 2
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ _build
build
dist
venv
htmlcov
.coverage
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
language: python
sudo: false
python:
- "3.4"
- "2.7"
- "3.5"
env:
- REQ="Django>=1.8,<1.9"
- REQ="Django>=1.9,<1.10"
- REQ="Django>=1.10,<1.11"
- REQ="Django>=1.11,<2.0"
# command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
install:
- pip install -q $REQ towel==0.8.0 --use-mirrors
- python setup.py -q install
- pip install $REQ pytz requests_oauthlib
- python setup.py install
# command to run tests, e.g. python setup.py test
script: "cd tests && ./manage.py test testapp"
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
django-authlib - Authentication utils for Django
================================================

.. image:: https://travis-ci.org/matthiask/django-authlib.svg?branch=master
:target: https://travis-ci.org/matthiask/django-authlib

authlib is a collection of authentication utilities for implementing
passwordless authentication. This is achieved by either sending
cryptographically signed links by email, or by fetching the email
Expand Down
3 changes: 3 additions & 0 deletions tests/cov.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
venv/bin/coverage run --branch --include="*authlib/*" --omit="*tests*" ./manage.py test -v 2 testapp
venv/bin/coverage html
14 changes: 14 additions & 0 deletions tests/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env python
import os
import sys
from os.path import abspath, dirname


if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testapp.settings")

sys.path.insert(0, dirname(dirname(abspath(__file__))))

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
4 changes: 4 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Django
pytz
coverage
requests_oauthlib
Empty file added tests/testapp/__init__.py
Empty file.
83 changes: 83 additions & 0 deletions tests/testapp/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os
import warnings

SITE_ID = 1

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
}
}

INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.staticfiles',
'django.contrib.messages',
'testapp',
'authlib',
'authlib.admin_oauth',
'authlib.little_auth',
'django.contrib.admin',
]

MEDIA_ROOT = '/media/'
STATIC_URL = '/static/'
BASEDIR = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(BASEDIR, 'media/')
STATIC_ROOT = os.path.join(BASEDIR, 'static/')
SECRET_KEY = 'supersikret'
LOGIN_REDIRECT_URL = '/?login=1'

ROOT_URLCONF = 'testapp.urls'
LANGUAGES = (('en', 'English'),)

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

MIDDLEWARE_CLASSES = MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
# Do not warn about MIDDLEWARE_CLASSES
SILENCED_SYSTEM_CHECKS = ['1_10.W001']

try:
# We do not yet care about those.
from django.utils.deprecation import RemovedInDjango21Warning

warnings.simplefilter('ignore', RemovedInDjango21Warning)
except ImportError: # pragma: no cover
pass

AUTH_USER_MODEL = 'little_auth.User'
GOOGLE_CLIENT_ID = 'empty'
GOOGLE_CLIENT_SECRET = 'empty'
TWITTER_CLIENT_ID = 'empty'
TWITTER_CLIENT_SECRET = 'empty'
FACEBOOK_CLIENT_ID = 'empty'
FACEBOOK_CLIENT_SECRET = 'empty'
ADMIN_OAUTH_PATTERNS = [
(r'@example\.com', 'admin@example.com'),
]
5 changes: 5 additions & 0 deletions tests/testapp/templates/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends "base.html" %}

{% block content %}
<h1>Page not found</h1>
{% endblock %}
16 changes: 16 additions & 0 deletions tests/testapp/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE HTML>
<html>
<head>
<title>{% block title %}testapp{% endblock %}</title>
</head>
<body>
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% block content %}{% endblock %}
</body>
</html>
54 changes: 54 additions & 0 deletions tests/testapp/test_authlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from django.test import Client, TestCase
from django.utils.translation import deactivate_all

from authlib.little_auth.models import User


def zero_management_form_data(prefix):
return {
'%s-TOTAL_FORMS' % prefix: 0,
'%s-INITIAL_FORMS' % prefix: 0,
'%s-MIN_NUM_FORMS' % prefix: 0,
'%s-MAX_NUM_FORMS' % prefix: 1000,
}


def merge_dicts(*dicts):
res = {}
for d in dicts:
res.update(d)
return res


class Test(TestCase):
def setUp(self):
self.user = User.objects.create_superuser(
'admin@example.com', 'blabla')
deactivate_all()

def login(self):
client = Client()
client.force_login(self.user)
return client

def test_admin_oauth(self):
client = Client()

response = client.get('/admin/login/?next=/admin/little_auth/')
self.assertContains(
response,
'<a href="/admin/__oauth__/?next=/admin/little_auth/">'
'Log in using SSO</a>'
)

response = client.get('/admin/__oauth__/?next=/admin/little_auth/')
self.assertEqual(response.status_code, 302)
self.assertIn(
'https://accounts.google.com/o/oauth2/v2/auth?response_type=code'
'&client_id=empty&redirect_uri=',
response['Location'],
)
self.assertEqual(
client.session['admin-oauth-next'],
'/admin/little_auth/',
)
10 changes: 10 additions & 0 deletions tests/testapp/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.conf.urls import include, url
from django.contrib import admin
from django.shortcuts import render


urlpatterns = [
url(r'', include('authlib.admin_oauth.urls')),
url(r'^admin/', admin.site.urls),
url(r'^404/$', lambda request: render(request, '404.html')),
]

0 comments on commit 1a9e2b0

Please sign in to comment.