-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
209 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import random | ||
|
||
from django.utils.translation import gettext as _ | ||
|
||
from ..models import User | ||
|
@@ -55,6 +57,22 @@ def test_signup_with_existing_email(self): | |
in form.errors.get('email')) | ||
assert User.objects.count() == 1 | ||
|
||
def test_signup_with_restricted_username(self): | ||
invalid_usernames = ('add', 'ADD', 'Add', 'new', 'NEW', 'New') | ||
data = { | ||
'username': random.choice(invalid_usernames), | ||
'email': '[email protected]', | ||
'password1': 'iloveyoko79', | ||
'password2': 'iloveyoko68', | ||
'full_name': 'John Lennon', | ||
} | ||
form = RegisterForm(data) | ||
|
||
assert form.is_valid() is False | ||
assert (_("Username cannot be “add” or “new”.") | ||
in form.errors.get('username')) | ||
assert User.objects.count() == 0 | ||
|
||
|
||
class ProfileFormTest(UserTestCase): | ||
def test_update_user(self): | ||
|
@@ -94,3 +112,15 @@ def test_update_user_with_existing_email(self): | |
} | ||
form = ProfileForm(data, instance=user) | ||
assert form.is_valid() is False | ||
|
||
def test_update_user_with_restricted_username(self): | ||
user = UserFactory.create(username='imagine71', | ||
email='[email protected]') | ||
invalid_usernames = ('add', 'ADD', 'Add', 'new', 'NEW', 'New') | ||
data = { | ||
'username': random.choice(invalid_usernames), | ||
'email': '[email protected]', | ||
'full_name': 'John Lennon', | ||
} | ||
form = ProfileForm(data, instance=user) | ||
assert form.is_valid() is False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import random | ||
import pytest | ||
from datetime import datetime | ||
from django.utils.translation import gettext as _ | ||
|
@@ -73,6 +74,21 @@ def test_create_with_existing_email(self): | |
assert (_("Another user is already registered with this email address") | ||
in serializer._errors['email']) | ||
|
||
def test_create_with_restricted_username(self): | ||
invalid_usernames = ('add', 'ADD', 'Add', 'new', 'NEW', 'New') | ||
data = { | ||
'username': random.choice(invalid_usernames), | ||
'email': '[email protected]', | ||
'password': 'iloveyoko79', | ||
'password_repeat': 'iloveyoko79', | ||
'full_name': 'John Lennon', | ||
} | ||
|
||
serializer = RegistrationSerializer(data=data) | ||
assert not serializer.is_valid() | ||
assert (_("Username cannot be “add” or “new”.") | ||
in serializer._errors['username']) | ||
|
||
|
||
class UserSerializerTest(UserTestCase): | ||
def test_field_serialization(self): | ||
|
@@ -102,8 +118,7 @@ def test_create_with_valid_data(self): | |
def test_update_username_fails(self): | ||
serializer = UserSerializer(data=BASIC_TEST_DATA) | ||
assert serializer.is_valid() | ||
serializer.save() | ||
user = User.objects.first() | ||
user = serializer.save() | ||
other_user = UserFactory.create() | ||
update_data = {'username': 'bad-update'} | ||
request = APIRequestFactory().patch('/user/imagine71', update_data) | ||
|
@@ -125,6 +140,25 @@ def test_update_last_login_fails(self): | |
assert not serializer2.is_valid() | ||
assert serializer2.errors['last_login'] == ['Cannot update last_login'] | ||
|
||
def test_update_with_restricted_username(self): | ||
serializer = UserSerializer(data=BASIC_TEST_DATA) | ||
assert serializer.is_valid() | ||
user = serializer.save() | ||
invalid_usernames = ('add', 'ADD', 'Add', 'new', 'NEW', 'New') | ||
data = { | ||
'username': random.choice(invalid_usernames), | ||
'email': '[email protected]', | ||
'full_name': 'John Lennon', | ||
} | ||
request = APIRequestFactory().patch('/user/imagine71', data) | ||
force_authenticate(request, user=user) | ||
serializer2 = UserSerializer( | ||
user, data=data, context={'request': Request(request)} | ||
) | ||
assert not serializer2.is_valid() | ||
assert serializer2.errors['username'] == [ | ||
_("Username cannot be “add” or “new”.")] | ||
|
||
|
||
class AccountLoginSerializerTest(UserTestCase): | ||
def test_unverified_account(self): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import os | ||
import random | ||
from pytest import raises | ||
|
||
from django.conf import settings | ||
|
@@ -18,7 +19,7 @@ | |
from tutelary.models import Policy | ||
|
||
|
||
class OrganzationTest(UserTestCase): | ||
class OrganizationTest(UserTestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.data = { | ||
|
@@ -77,6 +78,23 @@ def test_add_organization_with_contact(self): | |
'email': '[email protected]' | ||
}] | ||
|
||
def test_add_organization_with_restricted_name(self): | ||
invalid_names = ('add', 'ADD', 'Add', 'new', 'NEW', 'New') | ||
data = { | ||
'name': random.choice(invalid_names), | ||
'contacts-TOTAL_FORMS': 1, | ||
'contacts-INITIAL_FORMS': 0, | ||
'contacts-0-name': '', | ||
'contacts-0-email': '', | ||
'contacts-0-tel': '' | ||
} | ||
form = forms.OrganizationForm(data, user=UserFactory.create()) | ||
assert not form.is_valid() | ||
assert form.errors == { | ||
'name': ["Organization name cannot be “Add” or “New”."] | ||
} | ||
assert not Organization.objects.exists() | ||
|
||
def test_update_organization(self): | ||
org = OrganizationFactory.create(slug='some-org') | ||
self.data['description'] = 'Org description' | ||
|
@@ -216,6 +234,26 @@ def test_edit_project_roles(self): | |
is False) | ||
|
||
|
||
class ProjectAddDetailsTest(UserTestCase): | ||
def test_add_new_project_with_restricted_name(self): | ||
org = OrganizationFactory.create() | ||
invalid_names = ('add', 'ADD', 'Add', 'new', 'NEW', 'New') | ||
data = { | ||
'organization': org.slug, | ||
'name': random.choice(invalid_names), | ||
'contacts-TOTAL_FORMS': 1, | ||
'contacts-INITIAL_FORMS': 0, | ||
'contacts-0-name': '', | ||
'contacts-0-email': '', | ||
'contacts-0-tel': '' | ||
} | ||
form = forms.ProjectAddDetails(data=data) | ||
assert not form.is_valid() | ||
assert form.errors == { | ||
'name': ["Project name cannot be “Add” or “New”."] | ||
} | ||
|
||
|
||
class ProjectEditDetailsTest(UserTestCase): | ||
def _get_form(self, form_name): | ||
path = os.path.dirname(settings.BASE_DIR) | ||
|
Oops, something went wrong.