Commit ad915aa4 authored by Morten Nordseth's avatar Morten Nordseth
Browse files

userSerializer tests done

parent 3c69313b
Pipeline #116151 passed with stages
in 1 minute and 11 seconds
# Use the official Python image from the Docker Hub
FROM johanhef/secfit_backend_base_multiarch:v1
# These two environment variables prevent __pycache__/ files.
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
# Make a new directory to put our code in.
# RUN mkdir /code
# Change the working directory.
# Every command after this will be run from the /code directory.
WORKDIR /code
# Copy the of the code.
COPY . /code/
# Add sqlite3 to enable dbshell command for managing the database
# RUN apt-get update -y && apt-get install sqlite3 -y
# Upgrade pip
# RUN pip install --upgrade pip
# Install the requirements.
RUN pip install -r requirements.txt
# Import groupid environment variable
ENV GROUPID=${GROUPID}
#RUN python manage.py test
# Initialize Django
RUN python manage.py migrate
# Import credential variables
ARG DJANGO_SUPERUSER_USERNAME
ARG DJANGO_SUPERUSER_PASSWORD
ARG DJANGO_SUPERUSER_EMAIL
# Create superuser
RUN DJANGO_SUPERUSER_USERNAME=${DJANGO_SUPERUSER_USERNAME} \
DJANGO_SUPERUSER_PASSWORD=${DJANGO_SUPERUSER_PASSWORD} \
DJANGO_SUPERUSER_EMAIL=${DJANGO_SUPERUSER_EMAIL} \
python manage.py createsuperuser --noinput || \
echo "WARNING: This error is ignored as it most likely is 'That username is already taken.'" \
&& echo "If you wish to alter the user credentials, then delete the user first."
# Create some exercises from seed data
# RUN python manage.py loaddata seed.json
RUN python manage.py loaddata seed-with-workouts.json
# Run wsgi server with gunicorn
CMD ["python", "manage.py", "test"]
#CMD ["gunicorn", "secfit.wsgi", "--log-file", "-", "-b", "0.0.0.0:8000"]
......@@ -143,6 +143,15 @@ REST_FRAMEWORK = {
),
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS': {
'min_length': 4,
}
}
]
AUTH_USER_MODEL = "users.User"
DEBUG = True
docker run --platform linux/amd64 -v `pwd`:`pwd` -w `pwd` -i -t secfit_backend_local python manage.py test
\ No newline at end of file
docker build -f Dockerfile_test -t backend_test .
docker run backend_test
#docker run --platform linux/amd64 -v `pwd`:`pwd` -w `pwd` -i -t secfit_backend_local python manage.py test
\ No newline at end of file
......@@ -30,7 +30,7 @@ class UserSerializer(serializers.HyperlinkedModelSerializer):
def validate_password(self, value):
data = self.get_initial()
password = data.get("password")
password1 = data.get("password1")
......
from django.test import TestCase
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from users.models import User
# Create your tests here.
#from secfit.models import User
from users.serializers import UserSerializer
class UserSerializerTestCase(TestCase):
def test_get_user(self):
validated_data = {
def setUp(self):
self.data = {
"username": "testuser",
"email": "testuser@mail.com",
"password": "password",
"phone_number": 12345678,
"password1": "password",
"phone_number": "12345678",
"country": "Norway",
"city": "Trondheim",
"street_address": "Add"
"street_address": "Address"
}
user = UserSerializer.create(self, validated_data)
print(user)
print(user.password)
self.assertEqual(user.username, 'testuser')
def test_git_good(self):
print("Checking if good")
self.assertEqual("good", "good")
def test_create_user(self):
serializer = UserSerializer()
user = serializer.create(validated_data=self.data)
self.assertEqual(user.username, self.data['username'])
self.assertEqual(user.email, self.data['email'])
self.assertEqual(user.phone_number, self.data['phone_number'])
self.assertEqual(user.country, self.data['country'])
self.assertEqual(user.city, self.data['city'])
self.assertEqual(user.street_address, self.data['street_address'])
storedUser = User.objects.get(username=self.data['username'])
self.assertEqual(storedUser.username, self.data['username'])
self.assertEqual(storedUser.email, self.data['email'])
self.assertEqual(storedUser.phone_number, self.data['phone_number'])
self.assertEqual(storedUser.country, self.data['country'])
self.assertEqual(storedUser.city, self.data['city'])
self.assertEqual(storedUser.street_address, self.data['street_address'])
def test_validate_password(self):
serializer = UserSerializer(data={"password": "aa"})
with self.assertRaises(serializers.ValidationError):
# "value" is not used, but will raise error if not present
serializer.validate_password(value="a")
def test_validate_password_valid(self):
pw = "avalidpassword"
serializer = UserSerializer(data={"password": pw})
try:
# "value" is not used, but will raise error if not present
serializer.validate_password(value="b")
except serializers.ValidationError:
self.fail("validate_password() raised ValidationError")
\ No newline at end of file
......@@ -13,9 +13,9 @@ from PIL import Image
class WorkoutPermissionTest(TestCase):
def setUp(self):
self.unauthenticatedClient = Client()
user1 = {"username": "dev", "password": "dev", "password1": "dev", "phone_number": 12312312,
user1 = {"username": "dev", "password": "devpassword", "password1": "devpassword", "phone_number": 12312312,
"email": f"dev@dev.dev", "country": "", "city": "", "street_address": ""}
user2 = {"username": "user", "password": "user", "password1": "user", "phone_number": 21321321,
user2 = {"username": "user", "password": "userpassword", "password1": "userpassword", "phone_number": 21321321,
"email": f"user@user.user", "country": "", "city": "", "street_address": ""}
user3 = {"username": "idiot", "password": "hello", "password1": "hello", "phone_number": 21321321,
"email": f"aaa@aaa.aaa", "country": "", "city": "", "street_address": ""}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment