Commit 97f35dd1 authored by Ola Hermann Opheim's avatar Ola Hermann Opheim
Browse files

refact project tests

parent 83081505
......@@ -69,3 +69,40 @@ def get_project_participants(project):
participants.add(participant.user.username)
return participants
def get_user_task_permissions(user, task):
if user == task.project.user.user:
return {
'write': True,
'read': True,
'modify': True,
'owner': True,
'upload': True,
}
if task.accepted_task_offer() and task.accepted_task_offer().offerer == user.profile:
return {
'write': True,
'read': True,
'modify': True,
'owner': False,
'upload': True,
}
user_permissions = {
'write': False,
'read': False,
'modify': False,
'owner': False,
'view_task': False,
'upload': False,
}
user_permissions['read'] = user_permissions['read'] or user.profile.task_participants_read.filter(id=task.id).exists()
# Team members can view its teams tasks
user_permissions['upload'] = user_permissions['upload'] or user.profile.teams.filter(task__id=task.id, write=True).exists()
user_permissions['view_task'] = user_permissions['view_task'] or user.profile.teams.filter(task__id=task.id).exists()
user_permissions['write'] = user_permissions['write'] or user.profile.task_participants_write.filter(id=task.id).exists()
user_permissions['modify'] = user_permissions['modify'] or user.profile.task_participants_modify.filter(id=task.id).exists()
return user_permissions
\ No newline at end of file
import unittest
from django.test import Client
from user.models import User
from projects.models import Task
from projects.templatetags.project_extras import get_user_task_permissions
from test_helpers import create_multiple_test_data, delete_test_data
class UserTaskPermissionStatementTest(unittest.TestCase):
def setUp(self):
self.client = Client()
create_multiple_test_data()
self.task = Task.objects.get(title="Task2")
def tearDown(self):
delete_test_data()
def test_owner_task_permissions(self):
user = User.objects.get(username="User1")
get_user_task_permissions(user, self.task)
def test_unrelated_task_permissions(self):
user = User.objects.get(username="User3")
get_user_task_permissions(user, self.task)
def test_offerer_task_permissions(self):
user = User.objects.get(username="User2")
get_user_task_permissions(user, self.task)
......@@ -29,7 +29,7 @@ class NewProjectRegressionTest(unittest.TestCase):
new_project = Project.objects.get(pk=new_project_id)
self.assertEquals(new_project.user.user, self.user)
self.assertEquals(new_project.category.name, "Category")
new_task = Task.objects.get(title="Task1")
self.assertEquals(new_task.description, "TaskDescription")
self.assertEquals(new_task.budget, 123)
......
......@@ -159,3 +159,143 @@ class ProjectViewRegressionTest(unittest.TestCase):
self.assertEquals(task_offer.description, "Edited")
self.assertEquals(task_offer.price, 30)
self.assertFalse(task_offer.edit)
class AcceptingOfferOutputTest(unittest.TestCase):
def setUp(self):
self.client = Client()
create_multiple_test_data()
self.project = Project.objects.get(title="Project1")
def tearDown(self):
delete_test_data()
def test_post_offer_accept(self):
self.client.login(username= 'User1', password= 'top_secret')
response = self.client.post(f'/projects/{self.project.pk}/', {
'offer_response': True,
'taskofferid': TaskOffer.objects.get(title="TaskOffer2").id,
'status': 'a',
'feedback': 'feedback',
})
task = Task.objects.get(title="Task2")
task_offer = TaskOffer.objects.get(title="TaskOffer2")
offerer = User.objects.get(username="User2")
# Checks output project relevant data
self.assertEqual(response.context['project'], self.project)
self.assertTrue(offerer.profile in response.context['project'].participants.all())
# self.assertEqual(project.status, 'i')
# Checks output task relevant data
self.assertTrue(task in response.context['tasks'])
self.assertEqual(task.accepted_task_offer(), task_offer)
self.assertTrue(offerer.profile in task.read.all())
self.assertTrue(offerer.profile in task.write.all())
self.assertFalse(offerer.profile in task.modify.all())
# Checks output offer relevant data
self.assertEqual(task_offer.status, 'a')
self.assertEqual(task_offer.feedback, 'feedback')
# Checks other output data
self.assertIsNotNone(response.context['total_budget'])
self.assertIsNotNone(response.context['status_form'])
self.assertIsNotNone(response.context['offer_response_form'])
class GiveTaskOfferBoundaryTest(unittest.TestCase):
chars_200 = 'NNEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNNFRLRGi2'\
'KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOtEcOLJyGWwOaXC'\
'hNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOtEcOLJyGWwOaXChNue'
chars_500 = 'NNEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNNFRLRGi2KoOtEcOLJyGW'\
'wOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPGWwOaXChNueq2bq12qrlS6S0YPHkku3JsfYo2BPWOhkNNFRLRGi2'\
'KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOtEcOLJyEcOLJyGWwOaXChNue'\
'GWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOtEcOLJyGWwOaXChNueNNEcOLJyGWwOaXC'\
'hNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNNFRLRGi2KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3Js'\
'fYo2BPWOhkNFRLRGi2KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOt'
def setUp(self):
self.client = Client()
create_multiple_test_data()
self.project_id = Project.objects.get(title="Project2").pk
self.task = Task.objects.get(title="Task3")
self.nr_of_offers = TaskOffer.objects.count()
self.chars_200 = 'NNEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNNFRLRGi2'\
'KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOtEcOLJyGWwOaXC'\
'hNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOtEcOLJyGWwOaXChNue'
self.chars_500 = 'NNEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNNFRLRGi2KoOtEcOLJyGW'\
'wOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPGWwOaXChNueq2bq12qrlS6S0YPHkku3JsfYo2BPWOhkNNFRLRGi2'\
'KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOtEcOLJyEcOLJyGWwOaXChNue'\
'GWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOtEcOLJyGWwOaXChNueNNEcOLJyGWwOaXC'\
'hNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNNFRLRGi2KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3Js'\
'fYo2BPWOhkNFRLRGi2KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOt'
self.client.login(username= 'User3', password= 'top_secret')
self.offer_test_data = {
'title': "TaskOffer5",
'description': 'description',
'price': 123,
'taskvalue': self.task.id,
'offer_submit': True
}
def tearDown(self):
delete_test_data()
def test_give_offer_title_0(self):
self.offer_test_data['title'] = ''
self.client.post(f'/projects/{self.project_id}/', self.offer_test_data)
self.assertEqual(TaskOffer.objects.count(), self.nr_of_offers)
def test_give_offer_title_1(self):
self.offer_test_data['title'] = 'A'
self.client.post(f'/projects/{self.project_id}/', self.offer_test_data)
self.assertEqual(TaskOffer.objects.count(), self.nr_of_offers+1)
def test_give_offer_title_200(self):
self.offer_test_data['title'] = self.chars_200
self.client.post(f'/projects/{self.project_id}/', self.offer_test_data)
self.assertEqual(TaskOffer.objects.count(), self.nr_of_offers+1)
def test_give_offer_title_201(self):
self.offer_test_data['title'] = self.chars_200 + 'A'
self.client.post(f'/projects/{self.project_id}/', self.offer_test_data)
self.assertEqual(TaskOffer.objects.count(), self.nr_of_offers)
def test_give_offer_description_0(self):
self.offer_test_data['description'] = ''
self.client.post(f'/projects/{self.project_id}/', self.offer_test_data)
self.assertEqual(TaskOffer.objects.count(), self.nr_of_offers)
def test_give_offer_description_1(self):
self.offer_test_data['description'] = 'A'
self.client.post(f'/projects/{self.project_id}/', self.offer_test_data)
self.assertEqual(TaskOffer.objects.count(), self.nr_of_offers+1)
def test_give_offer_description_500(self):
self.offer_test_data['description'] = self.chars_500
self.client.post(f'/projects/{self.project_id}/', self.offer_test_data)
self.assertEqual(TaskOffer.objects.count(), self.nr_of_offers+1)
def test_give_offer_description_501(self):
self.offer_test_data['description'] = self.chars_500 + 'A'
self.client.post(f'/projects/{self.project_id}/', self.offer_test_data)
self.assertEqual(TaskOffer.objects.count(), self.nr_of_offers)
@unittest.skip("Should fail - doesn't")
def test_give_offer_price_minus_1(self):
self.offer_test_data['price'] = -1
self.client.post(f'/projects/{self.project_id}/', self.offer_test_data)
self.assertEqual(TaskOffer.objects.count(), self.nr_of_offers)
def test_give_offer_price_0(self):
self.offer_test_data['price'] = 0
self.client.post(f'/projects/{self.project_id}/', self.offer_test_data)
self.assertEqual(TaskOffer.objects.count(), self.nr_of_offers+1)
def test_give_offer_price_1(self):
self.offer_test_data['price'] = 1
self.client.post(f'/projects/{self.project_id}/', self.offer_test_data)
self.assertEqual(TaskOffer.objects.count(), self.nr_of_offers+1)
"""
import unittest
from django.test import Client
from django.contrib.auth.models import User
from projects.templatetags.project_extras import get_all_taskoffers
from projects.models import Project, ProjectCategory, Task, TaskOffer
from projects.forms import ProjectStatusForm, TaskOfferResponseForm
from projects.views import get_user_task_permissions
# Task 2 - II
class UserTaskPermissionStatementTest(unittest.TestCase):
def setUp(self):
self.client = Client()
self.userOwner2 = User.objects.create_user(
username='Owner2',
email='test@test.com',
password='top_secret'
)
self.userHired = User.objects.create_user(
username='Hired',
email='test2@test.com',
password='top_secret'
)
self.testCategory = ProjectCategory.objects.create(name='test2')
self.testProject2 = Project.objects.create(
user=self.userOwner2.profile,
title="Test prosjekt",
description='',
category=self.testCategory,
status='o'
)
self.testTask2 = Task.objects.create(
project=self.testProject2,
title='Test task',
description='',
budget=20,
status='ad',
feedback=''
)
self.test_permission_offer = TaskOffer.objects.create(
task=self.testTask2,
title='Test offer 2',
description='original',
price=20,
offerer=self.userHired.profile,
)
def test_get_user_task_permissions(self):
# Test route of project owner
get_user_task_permissions(self.userOwner2, self.testTask2)
# Test route of user unrelated to task
get_user_task_permissions(self.userHired, self.testTask2)
# Test route of user hired to do the task
self.client.login(username= 'Owner2', password= 'top_secret')
response = self.client.post('/projects/' + str(Project.objects.last().pk) + '/', {
'offer_response': True,
'taskofferid': self.test_permission_offer.id,
'status': 'a',
'feedback': 'ja',
})
get_user_task_permissions(self.userHired, response.context['tasks'][0])
# Task 3 - I
class GiveTaskOfferBoundaryTest(unittest.TestCase):
def setUp(self):
self.client = Client()
self.testCategory = ProjectCategory.objects.create(name='test1')
def test_boundary_give_offer(self):
self.userBoundaryOwner = User.objects.create_user(
username='BoundOw',
email='test@test.com',
password='top_secret'
)
self.userBoundaryOfferer = User.objects.create_user(
username='BoundOff',
email='test@test.com',
password='top_secret'
)
self.testBoundaryProject = Project.objects.create(
user=self.userBoundaryOwner.profile,
title="Test project",
description='',
category=self.testCategory,
status='o'
)
self.testBoundaryTask = Task.objects.create(
project=self.testBoundaryProject,
title='Test task',
description='',
budget=20,
status='ad',
feedback=''
)
title_200char = "NNEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNNFRLRGi2KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOtEcOLJyGWwOaXChNue"
des_500char = "NNEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNNFRLRGi2KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPGWwOaXChNueq2bq12qrlS6S0YPHkku3JsfYo2BPWOhkNNFRLRGi2KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOtEcOLJyGWwOaXChNueNNEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNNFRLRGi2KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOtEcOLJyGWwOaXChNueq2bq12qrlS6S0YPkku3JsfYo2BPWOhkNFRLRGi2KoOtEcOLJyGWwOaXChNue"
self.client.login(username= 'BoundOff', password= 'top_secret')
# Test title boundary values by check if list of offers extends
# title 0 chars
nr_of_offers = TaskOffer.objects.count()
self.client.post(f'/projects/{self.testBoundaryProject.pk}/', {
'title': "",
'description': 'max 500',
'price': 123,
'taskvalue': self.testBoundaryTask.id,
'offer_submit': True
})
self.assertEqual(TaskOffer.objects.count(), nr_of_offers)
# title 1 chars
nr_of_offers = TaskOffer.objects.count()
self.client.post(f'/projects/{self.testBoundaryProject.pk}/', {
'title': "A",
'description': 'max 500',
'price': 123,
'taskvalue': self.testBoundaryTask.id,
'offer_submit': True
})
self.assertEqual(TaskOffer.objects.count(), nr_of_offers+1)
# title 199 chars
nr_of_offers = TaskOffer.objects.count()
self.client.post(f'/projects/{self.testBoundaryProject.pk}/', {
'title': title_200char[:-1],
'description': 'max 500',
'price': 123,
'taskvalue': self.testBoundaryTask.id,
'offer_submit': True
})
self.assertEqual(TaskOffer.objects.count(), nr_of_offers+1)
# title 200 chars
nr_of_offers = TaskOffer.objects.count()
self.client.post(f'/projects/{self.testBoundaryProject.pk}/', {
'title': title_200char,
'description': 'max 500',
'price': 123,
'taskvalue': self.testBoundaryTask.id,
'offer_submit': True
})
self.assertEqual(TaskOffer.objects.count(), nr_of_offers+1)
# title 201 chars
nr_of_offers = TaskOffer.objects.count()
self.client.post(f'/projects/{self.testBoundaryProject.pk}/', {
'title': (title_200char + "A"),
'description': 'max 500',
'price': 123,
'taskvalue': self.testBoundaryTask.id,
'offer_submit': True
})
self.assertEqual(TaskOffer.objects.count(), nr_of_offers)
# Test description boundary values by check if list of offers extends
# description 0 chars
nr_of_offers = TaskOffer.objects.count()
self.client.post(f'/projects/{self.testBoundaryProject.pk}/', {
'title': 'title',
'description': '',
'price': 123,
'taskvalue': self.testBoundaryTask.id,
'offer_submit': True
})
self.assertEqual(TaskOffer.objects.count(), nr_of_offers)
# description 1 chars
nr_of_offers = TaskOffer.objects.count()
self.client.post(f'/projects/{self.testBoundaryProject.pk}/', {
'title': 'title',
'description': 'A',
'price': 123,
'taskvalue': self.testBoundaryTask.id,
'offer_submit': True
})
self.assertEqual(TaskOffer.objects.count(), nr_of_offers+1)
# description 499 chars
nr_of_offers = TaskOffer.objects.count()
self.client.post(f'/projects/{self.testBoundaryProject.pk}/', {
'title': 'title',
'description': des_500char[:-1],
'price': 123,
'taskvalue': self.testBoundaryTask.id,
'offer_submit': True
})
self.assertEqual(TaskOffer.objects.count(), nr_of_offers+1)
# description 500 chars
nr_of_offers = TaskOffer.objects.count()
self.client.post(f'/projects/{self.testBoundaryProject.pk}/', {
'title': 'title',
'description': des_500char,
'price': 123,
'taskvalue': self.testBoundaryTask.id,
'offer_submit': True
})
self.assertEqual(TaskOffer.objects.count(), nr_of_offers+1)
# description 501 chars
nr_of_offers = TaskOffer.objects.count()
self.client.post(f'/projects/{self.testBoundaryProject.pk}/', {
'title': 'title',
'description': des_500char + "A",
'price': 123,
'taskvalue': self.testBoundaryTask.id,
'offer_submit': True
})
self.assertEqual(TaskOffer.objects.count(), nr_of_offers)
# Test price boundary values by check if list of offers extends
# price -1
nr_of_offers = TaskOffer.objects.count()
self.client.post(f'/projects/{self.testBoundaryProject.pk}/', {
'title': 'title',
'description': 'description',
'price': -1,
'taskvalue': self.testBoundaryTask.id,
'offer_submit': True
})
# self.assertEqual(TaskOffer.objects.count(), nr_of_offers)
# price 0
nr_of_offers = TaskOffer.objects.count()
self.client.post(f'/projects/{self.testBoundaryProject.pk}/', {
'title': 'title',
'description': 'description',
'price': 0,
'taskvalue': self.testBoundaryTask.id,
'offer_submit': True
})
self.assertEqual(TaskOffer.objects.count(), nr_of_offers+1)
# price 1
nr_of_offers = TaskOffer.objects.count()
self.client.post(f'/projects/{self.testBoundaryProject.pk}/', {
'title': 'title',
'description': 'description',
'price': 1,
'taskvalue': self.testBoundaryTask.id,
'offer_submit': True
})
self.assertEqual(TaskOffer.objects.count(), nr_of_offers+1)
# Task 3 - II
class AcceptingOfferOutputTest(unittest.TestCase):
def setUp(self):
self.client = Client()
def test_project_view(self):
self.userAccepter = User.objects.create_user(
username='Accepter',
email='test@test.com',
password='top_secret'
)
self.userOfferer = User.objects.create_user(
username='Offerer3',
email='test@test.com',
password='top_secret'
)
self.testCategory = ProjectCategory.objects.create(name='accept_category')
self.testAcceptProject = Project.objects.create(
user=self.userAccepter.profile,
title="Test prosjekt",
description='',
category=self.testCategory,
status='o'
)
self.testAcceptTask = Task.objects.create(
project=self.testAcceptProject,
title='Test task',
description='',
budget=20,
status='ad',
feedback=''
)
self.accepting_offer_test_offer = TaskOffer.objects.create(
task=self.testAcceptTask,
title='Test offer 1',
description='original',
price=20,
offerer=self.userOfferer.profile,
)
# Test output on POST route offer_response with accepting
self.client.login(username= 'Accepter', password= 'top_secret')
response = self.client.post('/projects/' + str(Project.objects.last().pk) + '/', {
'offer_response': True,
'taskofferid': self.accepting_offer_test_offer.id,
'status': 'a',
'feedback': 'ja',
})
# Checks output project relevant data
project = response.context['project']
self.assertEqual(project, self.testAcceptProject)
self.assertTrue(self.userOfferer.profile in project.participants.all())
# self.assertEqual(project.status, 'i')
# Checks output task relevant data
task = Task.objects.get(id=self.testAcceptTask.id)
self.assertEqual(response.context['tasks'][0], self.testAcceptTask)
self.assertEqual(task.accepted_task_offer(), self.accepting_offer_test_offer)
self.assertTrue(self.userOfferer.profile in task.read.all())
self.assertTrue(self.userOfferer.profile in task.write.all())
self.assertFalse(self.userOfferer.profile in task.modify.all())
# Checks output offer relevant data
offer = TaskOffer.objects.get(id=self.accepting_offer_test_offer.id)
self.assertEqual(offer, self.accepting_offer_test_offer)
self.assertEqual(offer.status, 'a')
self.assertEqual(offer.feedback, 'ja')
# Checks other output data
self.assertEqual(response.context['total_budget'], 20)
self.assertIsNotNone(response.context['status_form'])
self.assertIsNotNone(response.context['offer_response_form'])
# Task 3 - IV
class EditTaskOfferTest(unittest.TestCase):
def setUp(self):
self.client = Client()
self.edit_offer_category = ProjectCategory.objects.create(name='edit_offer_category')
self.project_owner = User.objects.create_user(
username='POwner',
email='test@test.com',
password='top_secret'
)
self.offerer = User.objects.create_user(
username='Offerer',
email='test@test.com',
password='top_secret'
)
self.edit_offer_test_project = Project.objects.create(
user=self.project_owner.profile,
title='Edit offer test project',
description='',
category=self.edit_offer_category,
status='o'
)
self.edit_offer_test_task = Task.objects.create(
project=self.edit_offer_test_project,
title='Test task 1',
description='',
budget=20,
status='o',
feedback=''
)
self.edit_offer_test_offer = TaskOffer.objects.create(
task=self.edit_offer_test_task,
title='Test offer 1',
description='original',
price=20,
offerer=self.offerer.profile,
edit=True
)
def test_edit_offer(self):
# Test init
self.assertEqual(self.edit_offer_test_offer.description, 'original')
self.assertEqual(self.edit_offer_test_offer.price, 20)