Commit a9185c80 authored by Flavio TARSETTI's avatar Flavio TARSETTI
Browse files

[accounts][tests] Adding tests for TemporaryUrl

parent 4ffd465f
......@@ -168,6 +168,7 @@ def generate_url_hash():
)
return url_hash
class TemporaryUrlManager(models.Manager):
def create_temporary_url(self, status, supervision_track):
# Actions that result creating the object
......
......@@ -35,38 +35,54 @@ from rest_framework.test import APITestCase
from django.contrib.auth.models import User
from django.conf import settings
from .models import SupervisionTrack, Profile
from .models import SupervisionTrack
from .models import Profile
from .models import TemporaryUrl
from datetime import datetime, timedelta
import datetime
import simplejson as json
#----------------------------------------------------------
# ----------------------------------------------------------
class AccountTestCase(APITestCase):
def setUp(self):
self.tearDown()
now = datetime.datetime.now()
expiration_date_delta = datetime.timedelta(days=settings.ACCOUNT_BLOCKAGE_AFTER_FIRST_REJECTION_DAYS)
expiration_date_delta = datetime.timedelta(
days=settings.ACCOUNT_BLOCKAGE_AFTER_FIRST_REJECTION_DAYS
)
# Create the users
self.password = '1234'
self.password = "1234"
self.firstsupervisor = User.objects.create_user('firstsupervisor', 'firstsupervisor@test.org', self.password)
self.secondsupervisor = User.objects.create_user('secondsupervisor', 'secondsupervisor@test.org', self.password)
self.invalidsupervisor = User.objects.create_user('invalidsupervisor', 'invalidsupervisor@test.org', self.password)
self.firstsupervisor = User.objects.create_user(
"firstsupervisor", "firstsupervisor@test.org", self.password
)
self.secondsupervisor = User.objects.create_user(
"secondsupervisor", "secondsupervisor@test.org", self.password
)
self.invalidsupervisor = User.objects.create_user(
"invalidsupervisor", "invalidsupervisor@test.org", self.password
)
self.accepteduser = User.objects.create_user('accepteduser', 'accepteduser@test.org', self.password)
self.rejecteduser = User.objects.create_user('rejecteduser', 'rejecteduser@test.org', self.password)
self.yearrevalidationuser = User.objects.create_user('yearrevalidationuser', 'yearrevalidationuser@test.org', self.password)
self.blockeduser = User.objects.create_user('blockeduser', 'blockeduser@test.org', self.password)
self.accepteduser = User.objects.create_user(
"accepteduser", "accepteduser@test.org", self.password
)
self.rejecteduser = User.objects.create_user(
"rejecteduser", "rejecteduser@test.org", self.password
)
self.yearrevalidationuser = User.objects.create_user(
"yearrevalidationuser", "yearrevalidationuser@test.org", self.password
)
self.blockeduser = User.objects.create_user(
"blockeduser", "blockeduser@test.org", self.password
)
# Update the profiles
#supervisors
# supervisors
self.firstsupervisor.profile.status = Profile.ACCEPTED
self.secondsupervisor.profile.status = Profile.ACCEPTED
self.invalidsupervisor.profile.status = Profile.BLOCKED
......@@ -82,134 +98,138 @@ class AccountTestCase(APITestCase):
self.secondsupervisor.profile.save()
self.invalidsupervisor.profile.save()
#users
# users
self.accepteduser.profile.status = Profile.ACCEPTED
self.rejecteduser.profile.status = Profile.REJECTED
self.yearrevalidationuser.profile.status = Profile.YEARREVALIDATION
self.blockeduser.profile.status = Profile.BLOCKED
# Create the supervision tracks
#Accepted user
#Create and assign key
self.accepteduser.profile.supervision_key = self.accepteduser.profile._generate_current_supervision_key()
# Accepted user
# Create and assign key
self.accepteduser.profile.supervision_key = (
self.accepteduser.profile._generate_current_supervision_key()
)
supervisiontrack = SupervisionTrack.objects.create(
supervisee = self.accepteduser,
supervisor = self.firstsupervisor,
is_valid = True,
start_date = now,
expiration_date = now + expiration_date_delta,
last_validation_date = now
supervisee=self.accepteduser,
supervisor=self.firstsupervisor,
is_valid=True,
start_date=now,
expiration_date=now + expiration_date_delta,
last_validation_date=now,
)
#Assign key to supervision track
# Assign key to supervision track
supervisiontrack.supervision_key = self.accepteduser.profile.supervision_key
supervisiontrack.save()
self.accepteduser.profile.supervision.add(supervisiontrack)
self.accepteduser.profile.save()
self.accepteduser.save()
#Rejected user
#No supervision key but a rejection date
# Rejected user
# No supervision key but a rejection date
self.rejecteduser.profile.supervision_key = None
self.rejecteduser.profile.rejection_date = now + expiration_date_delta
self.rejecteduser.profile.save()
self.rejecteduser.save()
#YearRevalidation user
#Create and assign key
self.yearrevalidationuser.profile.supervision_key = self.yearrevalidationuser.profile._generate_current_supervision_key()
# YearRevalidation user
# Create and assign key
self.yearrevalidationuser.profile.supervision_key = (
self.yearrevalidationuser.profile._generate_current_supervision_key()
)
supervisiontrack = SupervisionTrack.objects.create(
supervisee = self.yearrevalidationuser,
supervisor = self.firstsupervisor,
is_valid = True,
start_date = now,
expiration_date = now + expiration_date_delta,
last_validation_date = now
supervisee=self.yearrevalidationuser,
supervisor=self.firstsupervisor,
is_valid=True,
start_date=now,
expiration_date=now + expiration_date_delta,
last_validation_date=now,
)
#Assign key to supervision track
supervisiontrack.supervision_key = self.yearrevalidationuser.profile.supervision_key
# Assign key to supervision track
supervisiontrack.supervision_key = (
self.yearrevalidationuser.profile.supervision_key
)
supervisiontrack.save()
self.yearrevalidationuser.profile.supervision.add(supervisiontrack)
self.yearrevalidationuser.profile.save()
self.yearrevalidationuser.save()
#Blocked user
# Blocked user
self.blockeduser.profile.supervision_key = None
self.blockeduser.profile.rejection_date = None
self.blockeduser.is_active = False
self.blockeduser.profile.save()
self.blockeduser.save()
def tearDown(self):
user = None
pass
def test_check_access_and_setup(self):
#Supervisors
# Supervisors
user = self.firstsupervisor
logged_in = self.client.login(username=user.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(user.username, 'firstsupervisor')
self.assertEqual(user.username, "firstsupervisor")
logged_out = self.client.logout()
user = self.secondsupervisor
logged_in = self.client.login(username=user.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(user.username, 'secondsupervisor')
self.assertEqual(user.username, "secondsupervisor")
logged_out = self.client.logout()
user = self.invalidsupervisor
logged_in = self.client.login(username=user.username, password=self.password)
self.assertFalse(logged_in)
self.assertEqual(user.username, 'invalidsupervisor')
self.assertEqual(user.username, "invalidsupervisor")
logged_out = self.client.logout()
#Users
# Users
user = self.accepteduser
logged_in = self.client.login(username=user.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(user.username, 'accepteduser')
self.assertEqual(user.username, "accepteduser")
logged_out = self.client.logout()
user = self.rejecteduser
logged_in = self.client.login(username=user.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(user.username, 'rejecteduser')
self.assertEqual(user.username, "rejecteduser")
logged_out = self.client.logout()
user = self.yearrevalidationuser
logged_in = self.client.login(username=user.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(user.username, 'yearrevalidationuser')
self.assertEqual(user.username, "yearrevalidationuser")
logged_out = self.client.logout()
user = self.blockeduser
logged_in = self.client.login(username=user.username, password=self.password)
self.assertFalse(logged_in)
self.assertEqual(user.username, 'blockeduser')
self.assertEqual(user.username, "blockeduser")
logged_out = self.client.logout()
#----------------------------------------------------------
# ----------------------------------------------------------
class AccountListTestCase(AccountTestCase):
def setUp(self):
super(AccountListTestCase, self).setUp()
self.url = reverse('api_accounts:list_supervisee')
self.url = reverse("api_accounts:list_supervisee")
def test_anonymous_user(self):
response = self.client.get(self.url, format='json')
response = self.client.get(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
content = json.loads(response.content)
self.assertEqual(len(json.loads(response.content)), 1)
errormsg = content['detail']
self.assertEqual(errormsg, 'Authentication credentials were not provided.')
errormsg = content["detail"]
self.assertEqual(errormsg, "Authentication credentials were not provided.")
def test_logged_in_user_not_supervisor(self):
......@@ -217,14 +237,14 @@ class AccountListTestCase(AccountTestCase):
logged_in = self.client.login(username=client.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(client.username, 'accepteduser')
self.assertEqual(client.username, "accepteduser")
response = self.client.get(self.url, format='json')
response = self.client.get(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
content = json.loads(response.content)
self.assertEqual(len(json.loads(response.content)), 1)
errormsg = content['detail']
self.assertEqual(errormsg, 'Not a supervisor account')
errormsg = content["detail"]
self.assertEqual(errormsg, "Not a supervisor account")
logged_out = self.client.logout()
......@@ -234,49 +254,51 @@ class AccountListTestCase(AccountTestCase):
logged_in = self.client.login(username=client.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(client.username, 'firstsupervisor')
self.assertEqual(client.username, "firstsupervisor")
response = self.client.get(self.url, format='json')
response = self.client.get(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
content = json.loads(response.content)
self.assertEqual(len(json.loads(response.content)), 2)
self.assertEqual(content[0]['supervisee']['username'], 'accepteduser')
self.assertEqual(content[1]['supervisee']['username'], 'yearrevalidationuser')
self.assertEqual(content[0]["supervisee"]["username"], "accepteduser")
self.assertEqual(content[1]["supervisee"]["username"], "yearrevalidationuser")
logged_out = self.client.logout()
#----------------------------------------------------------
# ----------------------------------------------------------
class AccountRevalidationTestCase(AccountTestCase):
def setUp(self):
super(AccountRevalidationTestCase, self).setUp()
self.url = reverse('api_accounts:revalidate_account')
self.url = reverse("api_accounts:revalidate_account")
def test_anonymous_user(self):
response = self.client.put(self.url, format='json')
response = self.client.put(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
content = json.loads(response.content)
self.assertEqual(len(json.loads(response.content)), 1)
errormsg = content['detail']
self.assertEqual(errormsg, 'Authentication credentials were not provided.')
errormsg = content["detail"]
self.assertEqual(errormsg, "Authentication credentials were not provided.")
def test_logged_in_accepted_user(self):
client = self.accepteduser
logged_in = self.client.login(username=client.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(client.username, 'accepteduser')
self.assertEqual(client.username, "accepteduser")
response = self.client.put(self.url, format='json')
response = self.client.put(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
content = json.loads(response.content)
self.assertEqual(len(json.loads(response.content)), 1)
errormsg = content['error']
self.assertEqual(errormsg, "You don't need to revalidate at the moment, your supervision is still valid")
errormsg = content["error"]
self.assertEqual(
errormsg,
"You don't need to revalidate at the moment, your supervision is still valid",
)
logged_out = self.client.logout()
......@@ -285,14 +307,17 @@ class AccountRevalidationTestCase(AccountTestCase):
logged_in = self.client.login(username=client.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(client.username, 'rejecteduser')
self.assertEqual(client.username, "rejecteduser")
response = self.client.put(self.url, format='json')
response = self.client.put(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
content = json.loads(response.content)
self.assertEqual(len(json.loads(response.content)), 1)
errormsg = content['error']
self.assertEqual(errormsg, 'You are not allowed to perform this action, you first need to get a valid supervision')
errormsg = content["error"]
self.assertEqual(
errormsg,
"You are not allowed to perform this action, you first need to get a valid supervision",
)
logged_out = self.client.logout()
......@@ -302,9 +327,9 @@ class AccountRevalidationTestCase(AccountTestCase):
logged_in = self.client.login(username=client.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(client.username, 'yearrevalidationuser')
self.assertEqual(client.username, "yearrevalidationuser")
response = self.client.put(self.url, format='json')
response = self.client.put(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
logged_out = self.client.logout()
......@@ -315,17 +340,20 @@ class AccountRevalidationTestCase(AccountTestCase):
logged_in = self.client.login(username=client.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(client.username, 'yearrevalidationuser')
self.assertEqual(client.username, "yearrevalidationuser")
response = self.client.put(self.url, format='json')
response = self.client.put(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
response = self.client.put(self.url, format='json')
response = self.client.put(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
content = json.loads(response.content)
self.assertEqual(len(json.loads(response.content)), 1)
errormsg = content['error']
self.assertEqual(errormsg, "You don't need to revalidate at the moment, your supervision is still valid")
errormsg = content["error"]
self.assertEqual(
errormsg,
"You don't need to revalidate at the moment, your supervision is still valid",
)
logged_out = self.client.logout()
......@@ -335,14 +363,14 @@ class AccountRevalidationTestCase(AccountTestCase):
logged_in = self.client.login(username=client.username, password=self.password)
self.assertFalse(logged_in)
self.assertEqual(client.username, 'blockeduser')
self.assertEqual(client.username, "blockeduser")
response = self.client.put(self.url, format='json')
response = self.client.put(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
content = json.loads(response.content)
self.assertEqual(len(json.loads(response.content)), 1)
errormsg = content['detail']
self.assertEqual(errormsg, 'Authentication credentials were not provided.')
errormsg = content["detail"]
self.assertEqual(errormsg, "Authentication credentials were not provided.")
logged_out = self.client.logout()
......@@ -352,14 +380,17 @@ class AccountRevalidationTestCase(AccountTestCase):
logged_in = self.client.login(username=client.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(client.username, 'firstsupervisor')
self.assertEqual(client.username, "firstsupervisor")
response = self.client.put(self.url, format='json')
response = self.client.put(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
content = json.loads(response.content)
self.assertEqual(len(json.loads(response.content)), 1)
errormsg = content['error']
self.assertEqual(errormsg, 'You are not allowed to perform this action, you first need to get a valid supervision')
errormsg = content["error"]
self.assertEqual(
errormsg,
"You are not allowed to perform this action, you first need to get a valid supervision",
)
logged_out = self.client.logout()
......@@ -369,63 +400,74 @@ class AccountRevalidationTestCase(AccountTestCase):
logged_in = self.client.login(username=client.username, password=self.password)
self.assertFalse(logged_in)
self.assertEqual(client.username, 'invalidsupervisor')
self.assertEqual(client.username, "invalidsupervisor")
response = self.client.put(self.url, format='json')
response = self.client.put(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
content = json.loads(response.content)
self.assertEqual(len(json.loads(response.content)), 1)
errormsg = content['detail']
self.assertEqual(errormsg, 'Authentication credentials were not provided.')
errormsg = content["detail"]
self.assertEqual(errormsg, "Authentication credentials were not provided.")
logged_out = self.client.logout()
#----------------------------------------------------------
# ----------------------------------------------------------
class AccountAddSupervisorTestCase(AccountTestCase):
def setUp(self):
super(AccountAddSupervisorTestCase, self).setUp()
self.url = reverse('api_accounts:add_supervisor', kwargs={'supervisor_name': self.secondsupervisor.username})
self.url = reverse(
"api_accounts:add_supervisor",
kwargs={"supervisor_name": self.secondsupervisor.username},
)
def test_anonymous_user(self):
response = self.client.post(self.url, format='json')
response = self.client.post(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
content = json.loads(response.content)
self.assertEqual(len(json.loads(response.content)), 1)
errormsg = content['detail']
self.assertEqual(errormsg, 'Authentication credentials were not provided.')
errormsg = content["detail"]
self.assertEqual(errormsg, "Authentication credentials were not provided.")
def test_logged_in_accepted_user_supervision_request_to_invalid_supervisor(self):
client = self.accepteduser
logged_in = self.client.login(username=client.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(client.username, 'accepteduser')
self.assertEqual(client.username, "accepteduser")
previous_supervision_key = client.profile.supervision_key
previous_supervision_track = SupervisionTrack.objects.get(supervision_key = previous_supervision_key)
previous_supervision_track = SupervisionTrack.objects.get(
supervision_key=previous_supervision_key
)
self.assertEqual(previous_supervision_track.is_valid, True)
self.url = reverse('api_accounts:add_supervisor', kwargs={'supervisor_name': self.invalidsupervisor.username})
#change supervisor
response = self.client.post(self.url, format='json')
self.url = reverse(
"api_accounts:add_supervisor",
kwargs={"supervisor_name": self.invalidsupervisor.username},
)
# change supervisor
response = self.client.post(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
content = json.loads(response.content)
self.assertEqual(len(json.loads(response.content)), 1)
errormsg = content['error']
self.assertEqual(errormsg, 'Not a valid supervisor request')
errormsg = content["error"]
self.assertEqual(errormsg, "Not a valid supervisor request")
client = User.objects.get(username=client.username)
new_supervision_key = client.profile.supervision_key
new_supervision_track = SupervisionTrack.objects.get(supervision_key = new_supervision_key)
new_supervision_track = SupervisionTrack.objects.get(
supervision_key=new_supervision_key
)
#check track change and supervisor
previous_supervision_track = SupervisionTrack.objects.get(supervision_key = previous_supervision_key)
# check track change and supervisor
previous_supervision_track = SupervisionTrack.objects.get(
supervision_key=previous_supervision_key
)
self.assertEqual(previous_supervision_track, new_supervision_track)
self.assertEqual(previous_supervision_track.is_valid, True)
self.assertEqual(previous_supervision_key, new_supervision_key)
......@@ -438,26 +480,34 @@ class AccountAddSupervisorTestCase(AccountTestCase):
logged_in = self.client.login(username=client.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(client.username, 'accepteduser')
self.assertEqual(client.username, "accepteduser")
previous_supervision_key = client.profile.supervision_key
previous_supervision_track = SupervisionTrack.objects.get(supervision_key = previous_supervision_key)
previous_supervision_track = SupervisionTrack.objects.get(
supervision_key=previous_supervision_key
)
self.assertEqual(previous_supervision_track.is_valid, True)
#change supervisor
response = self.client.post(self.url, format='json')
# change supervisor
response = self.client.post(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
client = User.objects.get(username=client.username)
new_supervision_key = client.profile.supervision_key
new_supervision_track = SupervisionTrack.objects.get(supervision_key = new_supervision_key)
new_supervision_track = SupervisionTrack.objects.get(
supervision_key=new_supervision_key
)
#check track change and supervisor
previous_supervision_track = SupervisionTrack.objects.get(supervision_key = previous_supervision_key)
# check track change and supervisor
previous_supervision_track = SupervisionTrack.objects.get(
supervision_key=previous_supervision_key
)
self.assertEqual(previous_supervision_track.is_valid, False)
self.assertNotEqual(previous_supervision_key, new_supervision_key)
self.assertNotEqual(previous_supervision_track.supervisor, new_supervision_track.supervisor)
self.assertNotEqual(
previous_supervision_track.supervisor, new_supervision_track.supervisor
)
self.assertEqual(client.profile.status, Profile.REJECTED)
logged_out = self.client.logout()
......@@ -467,20 +517,22 @@ class AccountAddSupervisorTestCase(AccountTestCase):
logged_in = self.client.login(username=client.username, password=self.password)
self.assertTrue(logged_in)
self.assertEqual(client.username, 'rejecteduser')
self.assertEqual(client.username, "rejecteduser")
previous_supervision_key = client.profile.supervision_key
self.assertEqual(previous_supervision_key, None)
#change supervisor
response = self.client.post(self.url, format='json')
# change supervisor
response = self.client.post(self.url, format="json")