Commit a0362a11 authored by Håvard Farestveit's avatar Håvard Farestveit
Browse files

#28 fixed pylint errors in /users


#28
parent efa8a837
Pipeline #125309 passed with stage
in 27 seconds
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import Offer, AthleteFile
from django.contrib.auth import get_user_model
from .models import Offer, AthleteFile
from .forms import CustomUserChangeForm, CustomUserCreationForm
# Register your models here.
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
......
......@@ -4,13 +4,11 @@ from django.contrib.auth import get_user_model
class CustomUserCreationForm(UserCreationForm):
phone_number = forms.CharField(max_length=50)
country = forms.CharField(max_length=50)
city = forms.CharField(max_length=50)
street_address = forms.CharField(max_length=50)
class Meta(UserCreationForm):
model = get_user_model()
fields = ("username", "coach", "phone_number", "country", "city", "street_address")
......
......@@ -18,7 +18,7 @@ class User(AbstractUser):
country = models.TextField(max_length=50, blank=True)
city = models.TextField(max_length=50, blank=True)
street_address = models.TextField(max_length=50, blank=True)
def athlete_directory_path(instance, filename):
"""
......
from rest_framework import serializers
from django.contrib.auth import get_user_model, password_validation
from users.models import Offer, AthleteFile
from django import forms
from users.models import Offer, AthleteFile
class UserSerializer(serializers.HyperlinkedModelSerializer):
......@@ -30,9 +30,7 @@ class UserSerializer(serializers.HyperlinkedModelSerializer):
def validate_password(self, value):
data = self.get_initial()
password = data.get("password")
password1 = data.get("password1")
try:
password_validation.validate_password(password)
......@@ -49,7 +47,14 @@ class UserSerializer(serializers.HyperlinkedModelSerializer):
country = validated_data["country"]
city = validated_data["city"]
street_address = validated_data["street_address"]
user_obj = get_user_model()(username=username, email=email, phone_number=phone_number, country=country, city=city, street_address=street_address)
user_obj = get_user_model()(
username=username,
email=email,
phone_number=phone_number,
country=country,
city=city,
street_address=street_address
)
user_obj.set_password(password)
user_obj.save()
......
from django.test import TestCase, Client
import uuid
variables = [
'username',
'email',
'password',
'password1',
'phone_number',
'country',
'city',
'street_address'
]
variable_values = {
'valid':
{
'username': 'Testuser',
'email': 'test@test.te',
'password': 'password',
'password1': 'password',
'phone_number': '1234567890',
'country': 'Testonia',
'city': 'Testheim',
'street_address': 'Test',
},
'invalid': {
'username': 'Test user',
'email': 'test',
'password': None,
'password1': None,
'phone_number': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'country': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'city': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'street_address': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
},
'blank': {
'username': '',
'email': '',
'password': '',
'password1': '',
'phone_number': '',
'country': '',
'city': '',
'street_address': '',
}
}
# Test cases generated by https://pairwise.teremokgames.com
test_cases = [
{
"username": "invalid", "password": "blank", "password1": "valid", "email": "invalid", "phone_number": "valid", "country": "blank", "city": "blank", "street_address": "invalid"},{
"username": "invalid", "password": "valid", "password1": "blank", "email": "valid", "phone_number": "blank", "country": "blank", "city": "invalid", "street_address": "valid"},{
"username": "invalid", "password": "valid", "password1": "blank", "email": "blank", "phone_number": "invalid", "country": "valid", "city": "blank", "street_address": "invalid"},{
"username": "invalid", "password": "blank", "password1": "blank", "email": "invalid", "phone_number": "valid", "country": "blank", "city": "invalid", "street_address": "valid"},{
"username": "invalid", "password": "blank", "password1": "valid", "email": "valid", "phone_number": "blank", "country": "invalid", "city": "valid", "street_address": "blank"},{
"username": "valid", "password": "blank", "password1": "blank", "email": "blank", "phone_number": "invalid", "country": "blank", "city": "valid", "street_address": "blank"},{
"username": "valid", "password": "blank", "password1": "blank", "email": "invalid", "phone_number": "blank", "country": "valid", "city": "blank", "street_address": "valid"},{
"username": "valid", "password": "valid", "password1": "valid", "email": "valid", "phone_number": "blank", "country": "valid", "city": "invalid", "street_address": "blank"},{
"username": "blank", "password": "valid", "password1": "blank", "email": "valid", "phone_number": "valid", "country": "invalid", "city": "invalid", "street_address": "blank"},{
"username": "blank", "password": "blank", "password1": "blank", "email": "blank", "phone_number": "blank", "country": "valid", "city": "valid", "street_address": "invalid"},{
"username": "blank", "password": "blank", "password1": "blank", "email": "valid", "phone_number": "invalid", "country": "invalid", "city": "blank", "street_address": "blank"},{
"username": "blank", "password": "blank", "password1": "valid", "email": "blank", "phone_number": "valid", "country": "valid", "city": "invalid", "street_address": "invalid"},{
"username": "blank", "password": "valid", "password1": "blank", "email": "blank", "phone_number": "blank", "country": "blank", "city": "valid", "street_address": "valid"},{
"username": "blank", "password": "blank", "password1": "valid", "email": "invalid", "phone_number": "invalid", "country": "blank", "city": "blank", "street_address": "blank"},{
"username": "invalid", "password": "blank", "password1": "valid", "email": "blank", "phone_number": "invalid", "country": "invalid", "city": "valid", "street_address": "valid"},{
"username": "invalid", "password": "valid", "password1": "valid", "email": "valid", "phone_number": "blank", "country": "blank", "city": "blank", "street_address": "invalid"},{
"username": "invalid", "password": "blank", "password1": "blank", "email": "blank", "phone_number": "blank", "country": "invalid", "city": "invalid", "street_address": "valid"},{
"username": "invalid", "password": "valid", "password1": "blank", "email": "invalid", "phone_number": "invalid", "country": "valid", "city": "valid", "street_address": "blank"},{
"username": "valid", "password": "valid", "password1": "valid", "email": "invalid", "phone_number": "blank", "country": "invalid", "city": "valid", "street_address": "blank"},{
"username": "valid", "password": "blank", "password1": "blank", "email": "valid", "phone_number": "blank", "country": "valid", "city": "blank", "street_address": "invalid"},{
"username": "valid", "password": "blank", "password1": "valid", "email": "blank", "phone_number": "invalid", "country": "blank", "city": "invalid", "street_address": "valid"},{
"username": "valid", "password": "valid", "password1": "blank", "email": "invalid", "phone_number": "valid", "country": "blank", "city": "valid", "street_address": "blank"},{
"username": "valid", "password": "blank", "password1": "blank", "email": "valid", "phone_number": "blank", "country": "invalid", "city": "blank", "street_address": "invalid"},{
"username": "valid", "password": "valid", "password1": "valid", "email": "blank", "phone_number": "invalid", "country": "valid", "city": "blank", "street_address": "valid"},{
"username": "valid", "password": "blank", "password1": "blank", "email": "blank", "phone_number": "valid", "country": "blank", "city": "invalid", "street_address": "blank"},{
"username": "blank", "password": "blank", "password1": "valid", "email": "invalid", "phone_number": "blank", "country": "valid", "city": "invalid", "street_address": "blank"},{
"username": "blank", "password": "blank", "password1": "blank", "email": "valid", "phone_number": "invalid", "country": "blank", "city": "valid", "street_address": "invalid"},{
"username": "blank", "password": "valid", "password1": "blank", "email": "blank", "phone_number": "valid", "country": "invalid", "city": "blank", "street_address": "valid"},{
"username": "blank", "password": "blank", "password1": "valid", "email": "blank", "phone_number": "blank", "country": "valid", "city": "invalid", "street_address": "blank"},{
"username": "blank", "password": "valid", "password1": "blank", "email": "invalid", "phone_number": "blank", "country": "blank", "city": "valid", "street_address": "invalid"},{
"username": "blank", "password": "blank", "password1": "valid", "email": "valid", "phone_number": "invalid", "country": "blank", "city": "blank", "street_address": "valid"},{
"username": "blank", "password": "valid", "password1": "blank", "email": "blank", "phone_number": "valid", "country": "invalid", "city": "blank", "street_address": "blank"}
]
class UserRegistration2WayTestCase(TestCase):
def setUp(self):
print("Running two way user registration")
self.client = Client()
def test_two_way(self):
for testcase in test_cases:
request = {}
invalid_request = False
for key, value in testcase.items():
request[key] = variable_values[value][key]
if key == 'username' and value == 'valid':
# Add a uid to username to avoid "user already exists error"
request['username'] = request['username'] + str(uuid.uuid4())
if (
value == 'invalid'
or (key == 'username' and value == 'blank')
or (key == 'password' and value == 'blank')
or (key == 'password1' and value == 'blank')
):
invalid_request = True
response_code = 400 if invalid_request else 201
response = self.client.post('/api/users/', request)
self.assertEqual(response.status_code, response_code)
from users.tests.UserRegistration import *
from users.tests.UserSerializer import *
\ No newline at end of file
from users.tests.user_registration import *
from users.tests.user_serializer import *
variables = [
'username',
'email',
'password',
'password1',
'phone_number',
'country',
'city',
'street_address'
]
variable_values = {
'valid':
{
'username': 'Testuser',
'email': 'test@test.te',
'password': 'password',
'password1': 'password',
'phone_number': '1234567890',
'country': 'Testonia',
'city': 'Testheim',
'street_address': 'Test',
},
'invalid': {
'username': 'Test user',
'email': 'test',
'password': None,
'password1': None,
'phone_number': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'country': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'city': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'street_address': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
},
'blank': {
'username': '',
'email': '',
'password': '',
'password1': '',
'phone_number': '',
'country': '',
'city': '',
'street_address': '',
}
}
test_cases = [
{
"username": "invalid",
"password": "blank",
"password1": "valid",
"email": "invalid",
"phone_number": "valid",
"country": "blank",
"city": "blank",
"street_address": "invalid"
},
{
"username": "invalid",
"password": "valid",
"password1": "blank",
"email": "valid",
"phone_number": "blank",
"country": "blank",
"city": "invalid",
"street_address": "valid"
},
{
"username": "invalid",
"password": "valid",
"password1": "blank",
"email": "blank",
"phone_number": "invalid",
"country": "valid",
"city": "blank",
"street_address": "invalid"
},
{
"username": "invalid",
"password": "blank",
"password1": "blank",
"email": "invalid",
"phone_number": "valid",
"country": "blank",
"city": "invalid",
"street_address": "valid"
},
{
"username": "invalid",
"password": "blank",
"password1": "valid",
"email": "valid",
"phone_number": "blank",
"country": "invalid",
"city": "valid",
"street_address": "blank"
},
{
"username": "valid",
"password": "blank",
"password1": "blank",
"email": "blank",
"phone_number": "invalid",
"country": "blank",
"city": "valid",
"street_address": "blank"
},
{
"username": "valid",
"password": "blank",
"password1": "blank",
"email": "invalid",
"phone_number": "blank",
"country": "valid",
"city": "blank",
"street_address": "valid"
},
{
"username": "valid",
"password": "valid",
"password1": "valid",
"email": "valid",
"phone_number": "blank",
"country": "valid",
"city": "invalid",
"street_address": "blank"
},
{
"username": "blank",
"password": "valid",
"password1": "blank",
"email": "valid",
"phone_number": "valid",
"country": "invalid",
"city": "invalid",
"street_address": "blank"
},
{
"username": "blank",
"password": "blank",
"password1": "blank",
"email": "blank",
"phone_number": "blank",
"country": "valid",
"city": "valid",
"street_address": "invalid"
},
{
"username": "blank",
"password": "blank",
"password1": "blank",
"email": "valid",
"phone_number": "invalid",
"country": "invalid",
"city": "blank",
"street_address": "blank"
},
{
"username": "blank",
"password": "blank",
"password1": "valid",
"email": "blank",
"phone_number": "valid",
"country": "valid",
"city": "invalid",
"street_address": "invalid"
},
{
"username": "blank",
"password": "valid",
"password1": "blank",
"email": "blank",
"phone_number": "blank",
"country": "blank",
"city": "valid",
"street_address": "valid"
},
{
"username": "blank",
"password": "blank",
"password1": "valid",
"email": "invalid",
"phone_number": "invalid",
"country": "blank",
"city": "blank",
"street_address": "blank"
},
{
"username": "invalid",
"password": "blank",
"password1": "valid",
"email": "blank",
"phone_number": "invalid",
"country": "invalid",
"city": "valid",
"street_address": "valid"
},
{
"username": "invalid",
"password": "valid",
"password1": "valid",
"email": "valid",
"phone_number": "blank",
"country": "blank",
"city": "blank",
"street_address": "invalid"
},
{
"username": "invalid",
"password": "blank",
"password1": "blank",
"email": "blank",
"phone_number": "blank",
"country": "invalid",
"city": "invalid",
"street_address": "valid"
},
{
"username": "invalid",
"password": "valid",
"password1": "blank",
"email": "invalid",
"phone_number": "invalid",
"country": "valid",
"city": "valid",
"street_address": "blank"
},
{
"username": "valid",
"password": "valid",
"password1": "valid",
"email": "invalid",
"phone_number": "blank",
"country": "invalid",
"city": "valid",
"street_address": "blank"
},
{
"username": "valid",
"password": "blank",
"password1": "blank",
"email": "valid",
"phone_number": "blank",
"country": "valid",
"city": "blank",
"street_address": "invalid"
},
{
"username": "valid",
"password": "blank",
"password1": "valid",
"email": "blank",
"phone_number": "invalid",
"country": "blank",
"city": "invalid",
"street_address": "valid"
},
{
"username": "valid",
"password": "valid",
"password1": "blank",
"email": "invalid",
"phone_number": "valid",
"country": "blank",
"city": "valid",
"street_address": "blank"
},
{
"username": "valid",
"password": "blank",
"password1": "blank",
"email": "valid",
"phone_number": "blank",
"country": "invalid",
"city": "blank",
"street_address": "invalid"
},
{
"username": "valid",
"password": "valid",
"password1": "valid",
"email": "blank",
"phone_number": "invalid",
"country": "valid",
"city": "blank",
"street_address": "valid"
},
{
"username": "valid",
"password": "blank",
"password1": "blank",
"email": "blank",
"phone_number": "valid",
"country": "blank",
"city": "invalid",
"street_address": "blank"
},
{
"username": "blank",
"password": "blank",
"password1": "valid",
"email": "invalid",
"phone_number": "blank",
"country": "valid",
"city": "invalid",
"street_address": "blank"
},
{
"username": "blank",
"password": "blank",
"password1": "blank",
"email": "valid",
"phone_number": "invalid",
"country": "blank",
"city": "valid",
"street_address": "invalid"
},
{
"username": "blank",
"password": "valid",
"password1": "blank",
"email": "blank",
"phone_number": "valid",
"country": "invalid",
"city": "blank",
"street_address": "valid"
},
{
"username": "blank",
"password": "blank",
"password1": "valid",
"email": "blank",
"phone_number": "blank",
"country": "valid",
"city": "invalid",
"street_address": "blank"
},
{
"username": "blank",
"password": "valid",
"password1": "blank",
"email": "invalid",
"phone_number": "blank",
"country": "blank",
"city": "valid",
"street_address": "invalid"
},
{
"username": "blank",
"password": "blank",
"password1": "valid",
"email": "valid",
"phone_number": "invalid",
"country": "blank",
"city": "blank",
"street_address": "valid"
},
{
"username": "blank",
"password": "valid",
"password1": "blank",
"email": "blank",
"phone_number": "valid",
"country": "invalid",
"city": "blank",
"street_address": "blank"
}
]
import uuid
from django.test import TestCase, Client
from users.tests.test_data import variable_values, test_cases
class UserRegistration2WayTestCase(TestCase):
def setUp(self):
print("Running two way user registration")
self.client = Client()
def test_two_way(self):
for testcase in test_cases:
request = {}
invalid_request = False
for key, value in testcase.items():
request[key] = variable_values[value][key]
if key == 'username' and value == 'valid':
# Add a uid to username to avoid "user already exists error"
request['username'] = request['username'] + str(uuid.uuid4())
if value == 'invalid':
invalid_request = True
elif key == ('username' or 'password' or 'password1') and value == 'blank':
invalid_request = True
response_code = 400 if invalid_request else 201
response = self.client.post('/api/users/', request)
self.assertEqual(response.status_code, response_code)