Skip to content
Snippets Groups Projects
Commit 2b38f73c authored by Vegard Murvold Sporstøl's avatar Vegard Murvold Sporstøl
Browse files

working test on FR5

parent 88e5fa71
No related branches found
No related tags found
1 merge request!7Task3
from rest_framework import status import json
from rest_framework.test import APITestCase
# Tests new registration, boundary values and 2-way domain from django.test import TestCase, RequestFactory, Client
class RegistrationTestCase(APITestCase): from users.serializers import UserSerializer
def test_boundaryValuesOfRegistrationInvalid(self): from users.models import User
from django.core.exceptions import ValidationError
class RegistrationTestCase(TestCase):
def setUp(self):
self.client = Client()
def test_registration(self):
data = {"username": "Test", "email": "test@test.no",
"password": "strong_pwd", "password1": "strong_pwd",
"phone_number": "12345678", "country": "Norway",
"city": "Trondheim", "street_address": "Trondheimsvegen 1"}
response = self.client.post('/api/users/', data)
self.assertEqual(response.status_code, 201)
### Test boundary values of registration ###
class BoundaryValuesOfRegistrationTestCase(TestCase):
def setUp(self):
self.client = Client()
def test_minimumData(self):
data = {"username": "t", "email": "",
"password": "1", "password1": "1",
"phone_number": "", "country": "",
"city": "", "street_address": ""}
response = self.client.post("/api/users/", data)
self.assertEqual(response.status_code, 201) #should work at one char pwd and username
def test_blankPassword(self):
data = {"username": "test", "email": "", data = {"username": "test", "email": "",
"password": "", "password1": "", "password": "", "password1": "",
"phone_number": "", "country": "", "phone_number": "", "country": "",
"city": "", "street_address": ""} "city": "", "street_address": ""}
response = self.client.post("/api/users/", data) response = self.client.post("/api/users/", data)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) #should fail at blank pwd self.assertEqual(response.status_code, 400) #should fail at blank pwd
def test_blankUsername(self):
data = {"username": "", "email": "", data = {"username": "", "email": "",
"password": "1", "password1": "1", "password": "1", "password1": "1",
"phone_number": "", "country": "", "phone_number": "", "country": "",
"city": "", "street_address": ""} "city": "", "street_address": ""}
response = self.client.post("/api/users/", data) response = self.client.post("/api/users/", data)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) #should fail at blank username self.assertEqual(response.status_code, 400) #should fail at blank username
# Seem to be no upper value and no restrictions to the other fields def test_invalidEmail(self):
data = {"username": "Test", "email": "test.no",
def test_boundaryValuesOfRegistrationValid(self):
data = {"username": "t", "email": "",
"password": "1", "password1": "1", "password": "1", "password1": "1",
"phone_number": "", "country": "", "phone_number": "", "country": "",
"city": "", "street_address": ""} "city": "", "street_address": ""}
response = self.client.post("/api/users/", data) response = self.client.post("/api/users/", data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED) #should work at one char pwd and username self.assertEqual(response.status_code, 400) #should fail if email does not conatin @
### 2-way domain testing of registration ###
class TwoWayDomainTestOfRegistrationTestCase(TestCase):
def setUp(self):
self.client = Client()
def test_domainRegistration(self): def test_domainRegistration(self):
"2-way domain tests to test the register page" "2-way domain tests to test the register page"
# Tests new functionality in UC-2
class Statistics(APITestCase):
def test_renderStatistics(self):
"test that the statistics are rendered"
def test_correctStatistics(self): ### Test the class UserSerializer ###
"Check that statistics are correctly calculated"
class UserSerializerTestCase(TestCase):
def setUp(self):
self.user1 = User.objects.create(username="user1")
self.user2 = User.objects.create(username="user2", coach=self.user1)
self.factory = RequestFactory()
def test_validate_passwordValid(self):
password = "some_very_strong_pwd"
serializer = UserSerializer(data = {'password': password,'password1': password})
validated_password = serializer.validate_password(password)
self.assertEqual(validated_password, password)
def test_validate_passwordInValid(self):
password = "some_very_strong_pwd"
password1 = "Some_wrong_pwd"
serializer = UserSerializer(data = {'password': password,'password1': password})
validated_password = serializer.validate_password(password1)
self.assertRaises(ValidationError)
def test_create(self):
data = {"username": "Test", "email": "test@test.no",
"password": "strong_pwd", "password1": "strong_pwd",
"phone_number": "12345678", "country": "Norway",
"city": "Trondheim", "street_address": "Trondheimsvegen 1"}
serializer = UserSerializer(data)
user = serializer.create(data)
self.assertEqual(user.username, "Test")
self.assertEqual(user.email, "test@test.no")
self.assertEqual(user.phone_number, "12345678")
self.assertEqual(user.country, "Norway")
self.assertEqual(user.city, "Trondheim")
self.assertEqual(user.street_address, "Trondheimsvegen 1")
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment