Skip to content

Commit

Permalink
Merge pull request #34 from moshthepitt/issue-18
Browse files Browse the repository at this point in the history
Add users app
  • Loading branch information
moshthepitt authored Mar 7, 2019
2 parents 654144f + f1ecb08 commit 6fec20b
Show file tree
Hide file tree
Showing 31 changed files with 783 additions and 88 deletions.
3 changes: 2 additions & 1 deletion example/example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'django_tables2',
'django_filters',
'vega_admin',
'vega_admin.contrib.users',
'artists',
]

Expand All @@ -56,7 +57,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand Down
8 changes: 7 additions & 1 deletion example/example/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
"""example URL Configuration"""
from django.conf.urls import include
from django.urls import path

from artists import views

urlpatterns = views.ArtistCRUD().url_patterns() +\

urlpatterns = [
path('', include('vega_admin.contrib.users.urls')),
] + views.ArtistCRUD().url_patterns() +\
views.SongCRUD().url_patterns()
21 changes: 21 additions & 0 deletions example/templates/vega_admin/badmin/sidebar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% load i18n %}
<div class="sidebar content-box" style="display: block;">
<ul class="nav">
<li class="submenu">
<a href="{% url 'auth.user-list' %}">
<i class="glyphicon glyphicon-list"></i> {% trans "Users" %}
<span class="caret pull-right"></span>
</a>
<!-- Sub menu -->
<ul>
<li><a href="{% url 'auth.user-list' %}">{% trans "View Users" %}</a></li>
<li><a href="{% url 'auth.user-create' %}">{% trans "Create User" %}</a></li>
</ul>
<!-- Sub menu -->
<ul>
<li><a href="{% url 'auth.group-list' %}">{% trans "View Groups" %}</a></li>
<li><a href="{% url 'auth.group-create' %}">{% trans "Create Group" %}</a></li>
</ul>
</li>
</ul>
</div>
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ django-braces==1.13.0
django-crispy-forms==1.7.2
django-filter==2.0.0
django-tables2==2.0.3
django==2.1.3
django==2.1.7
et-xmlfile==1.0.1 # via openpyxl
jdcal==1.4 # via openpyxl
odfpy==1.3.6 # via tablib
Expand Down
3 changes: 3 additions & 0 deletions requirements/dev.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# load from setup.py
-e .

# allauth
django-allauth

# others
pylint
pylint-django
Expand Down
14 changes: 12 additions & 2 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --output-file requirements/dev.txt requirements/dev.in
# pip-compile requirements/dev.in
#
appdirs==1.4.3 # via black
astroid==2.0.4 # via pylint
attrs==18.2.0 # via black
backcall==0.1.0 # via ipython
black==18.9b0
certifi==2018.11.29 # via requests
chardet==3.0.4 # via requests
click==7.0 # via black
coverage==4.5.2
decorator==4.3.0 # via ipython, traitlets
defusedxml==0.5.0 # via python3-openid
django-allauth==0.39.1
django-braces==1.13.0
django-crispy-forms==1.7.2
django-filter==2.0.0
django-tables2==2.0.3
django==2.1.3 # via django-filter, django-tables2, model-mommy
django==2.1.7 # via django-allauth, django-filter, django-tables2, model-mommy
et-xmlfile==1.0.1 # via openpyxl
filelock==3.0.10 # via tox
flake8==3.6.0
idna==2.8 # via requests
ipdb==0.11
ipython-genutils==0.2.0 # via traitlets
ipython==7.1.1 # via ipdb
Expand All @@ -29,6 +34,7 @@ jedi==0.13.1 # via ipython
lazy-object-proxy==1.3.1 # via astroid
mccabe==0.6.1 # via flake8, pylint
model-mommy==1.6.0
oauthlib==3.0.1 # via requests-oauthlib
odfpy==1.3.6 # via tablib
openpyxl==2.5.10 # via tablib
parso==0.3.1 # via jedi
Expand All @@ -46,15 +52,19 @@ pygments==2.2.0 # via ipython
pylint-django==2.0.2
pylint-plugin-utils==0.4 # via pylint-django
pylint==2.1.1
python3-openid==3.1.0 # via django-allauth
pytz==2018.7 # via django
pyyaml==4.2b4 # via tablib
requests-oauthlib==1.2.0 # via django-allauth
requests==2.21.0 # via django-allauth, requests-oauthlib
six==1.11.0 # via astroid, model-mommy, prompt-toolkit, tox, traitlets
tablib==0.12.1
toml==0.10.0 # via black, tox
tox==3.5.3
traitlets==4.3.2 # via ipython
typed-ast==1.1.0 # via astroid
unicodecsv==0.14.1 # via tablib
urllib3==1.24.1 # via requests
virtualenv==16.1.0 # via tox
wcwidth==0.1.7 # via prompt-toolkit
wrapt==1.10.11 # via astroid
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
url='https://github.com/moshthepitt/django-vega-admin',
packages=find_packages(exclude=['docs', 'tests']),
install_requires=[
'Django >= 2.0',
'Django >=2.0.11',
'django-crispy-forms',
'django-braces',
'django-filter',
Expand Down
Empty file added tests/contrib/__init__.py
Empty file.
Empty file added tests/contrib/users/__init__.py
Empty file.
172 changes: 172 additions & 0 deletions tests/contrib/users/test_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
"""Test vega_admin.contrib.users.forms module"""

from django.test import TestCase, override_settings
from unittest.mock import patch

from model_mommy import mommy

from vega_admin.contrib.users.forms import (AddUserForm, EditUserForm,
PasswordChangeForm)


@override_settings(
ROOT_URLCONF="vega_admin.contrib.users.urls", VEGA_TEMPLATE="basic")
class TestForms(TestCase):
"""
Test class for vega_admin.contrib.users.forms
"""

def test_adduserform(self):
"""
Test AddUserForm
"""
good_data = {
"first_name": "mosh",
"last_name": "pitt",
"username": "moshthepitt",
"email": "[email protected]",
"password": "Miserable-Water-9",
}

form = AddUserForm(data=good_data)
self.assertTrue(form.is_valid())
user = form.save()
self.assertEqual("mosh", user.first_name)
self.assertEqual("pitt", user.last_name)
self.assertEqual("moshthepitt", user.username)
self.assertEqual("[email protected]", user.email)
self.assertTrue(
self.client.login(
username="moshthepitt", password="Miserable-Water-9"))

# good data no username
good_data = {
"first_name": "mosh",
"last_name": "pitt",
"email": "[email protected]",
"password": "Bean-Quarrel-0",
}

form = AddUserForm(data=good_data)
self.assertTrue(form.is_valid())
user = form.save()
self.assertEqual("mosh", user.username) # was generated

# weak password
bad_data = {
"first_name": "mosh",
"last_name": "pitt",
"username": "moshthepitt2",
"email": "[email protected]",
"password": "[email protected]",
}
form = AddUserForm(data=bad_data)
self.assertFalse(form.is_valid())
self.assertEqual(1, len(form.errors.keys()))
self.assertEqual(
"The password is too similar to the email address.",
form.errors["password"][0],
)

# email not unique
bad_data = {
"first_name": "mosh",
"last_name": "pitt",
"username": "moshthepitt3",
"email": "[email protected]",
"password": "Every-Actor-1",
}
form = AddUserForm(data=bad_data)
self.assertFalse(form.is_valid())
self.assertEqual(1, len(form.errors.keys()))
self.assertEqual(
"A user is already registered with this e-mail address.",
form.errors["email"][0],
)

# missing email and username
bad_data = {
"first_name": "mosh",
"last_name": "pitt",
"password": "TestAddUserForm",
}
form = AddUserForm(data=bad_data)
self.assertFalse(form.is_valid())
self.assertEqual(1, len(form.errors.keys()))
self.assertEqual("You must provide one of email or username",
form.errors["__all__"][0])

def test_edituserform(self):
"""
Test EditUserForm
"""
user = mommy.make("auth.User")

good_data = {
"first_name": "mosh",
"last_name": "pitt",
"username": "moshthepitt22",
"email": "[email protected]",
}
form = EditUserForm(instance=user, data=good_data)
self.assertTrue(form.is_valid())
user = form.save()
self.assertEqual("mosh", user.first_name)
self.assertEqual("pitt", user.last_name)
self.assertEqual("moshthepitt22", user.username)
self.assertEqual("[email protected]", user.email)

def test_password_change_form(self):
"""
Test PasswordChangeForm
"""
user = mommy.make("auth.User", username="softie")

good_data = {
"password1": "PasswordChangeForm",
"password2": "PasswordChangeForm",
}
form = PasswordChangeForm(instance=user, data=good_data)
self.assertTrue(form.is_valid())
form.save()
self.assertTrue(
self.client.login(
username="softie", password="PasswordChangeForm"))

# weak password
bad_data = {"password1": "123456789", "password2": "123456789"}
form = PasswordChangeForm(instance=user, data=bad_data)
self.assertFalse(form.is_valid())
self.assertEqual(1, len(form.errors.keys()))
self.assertEqual("This password is too common.",
form.errors["password2"][0])
self.assertEqual("This password is entirely numeric.",
form.errors["password2"][1])

# different passwords
bad_data = {
"password1": "PasswordChangeForm",
"password2": "123456789"
}
form = PasswordChangeForm(instance=user, data=bad_data)
self.assertFalse(form.is_valid())
self.assertEqual(1, len(form.errors.keys()))
self.assertEqual("The two password fields didn't match.",
form.errors["password2"][0])

@patch('vega_admin.contrib.users.forms.get_form_actions')
def test_password_change_form_cancel_url(self, mock):
"""Test cancel url on password change form"""
user = mommy.make("auth.User")

good_data = {
"password1": "PasswordChangeForm",
"password2": "PasswordChangeForm",
}
form = PasswordChangeForm(instance=user, data=good_data)
self.assertTrue(form.is_valid())
form.save()

mock.assert_called_with(cancel_url="/auth.user/list/")
65 changes: 65 additions & 0 deletions tests/contrib/users/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""Test vega_admin.contrib.users.forms module"""

from django.test import TestCase, override_settings

from model_mommy import mommy

from vega_admin.contrib.users.forms import PasswordChangeForm
from vega_admin.contrib.users.views import ChangePassword
from vega_admin.views import VegaUpdateView


@override_settings(
ROOT_URLCONF="vega_admin.contrib.users.urls", VEGA_TEMPLATE="basic")
class TestViews(TestCase):
"""
Test class for vega_admin.contrib.users.views
"""

def test_changepassword(self):
"""
Test ChangePassword
"""
user = mommy.make("auth.User", username="TestChangePasswordView")
data = {
"password1": "Extension-I-School-5",
"password2": "Extension-I-School-5",
}

res = self.client.get(f"/auth.user/change%20password/{user.id}/")
self.assertEqual(res.status_code, 200)
self.assertIsInstance(res.context["form"], PasswordChangeForm)
self.assertIsInstance(res.context["view"], ChangePassword)
self.assertIsInstance(res.context["view"], VegaUpdateView)
self.assertTemplateUsed(res, "vega_admin/basic/update.html")
res = self.client.post(f"/auth.user/change%20password/{user.id}/",
data)
self.assertEqual(res.status_code, 302)
self.assertRedirects(res, "/auth.user/list/")
user.refresh_from_db()
self.assertTrue(
self.client.login(
username="TestChangePasswordView",
password="Extension-I-School-5"))

@override_settings(VEGA_FORCE_ORDERING=True)
def test_list_view_ordering(self):
"""
Test VEGA_FORCE_ORDERING=True
"""
mommy.make("auth.User", _quantity=7)
res = self.client.get("/auth.user/list/")
self.assertTrue(res.context["object_list"].ordered)
self.assertEqual("-last_login",
res.context["object_list"].query.order_by[0])
self.assertEqual("first_name",
res.context["object_list"].query.order_by[1])

@override_settings(VEGA_FORCE_ORDERING=False)
def test_list_view_ordering_off(self):
"""
Test VEGA_FORCE_ORDERING=False
"""
res = self.client.get("/auth.user/list/")
self.assertFalse(res.context["object_list"].ordered)
Loading

0 comments on commit 6fec20b

Please sign in to comment.