89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
from django.test import TestCase, RequestFactory
|
|
from graphene.test import Client
|
|
from graphql_relay import to_global_id
|
|
|
|
from api.schema import schema
|
|
from portfolio.factories import ProjectFactory
|
|
from users.factories import SchoolClassFactory
|
|
from users.models import User
|
|
from users.services import create_users
|
|
from api.test_utils import create_client, DefaultUserTestCase
|
|
from portfolio.models import Project
|
|
|
|
class ProjectQuery(TestCase):
|
|
def setUp(self):
|
|
create_users()
|
|
self.teacher = User.objects.get(username='teacher')
|
|
self.teacher2 = User.objects.get(username='teacher2')
|
|
self.student = User.objects.get(username='student1')
|
|
self.student2 = User.objects.get(username='student2')
|
|
school_class1 = SchoolClassFactory(users=[self.teacher, self.student])
|
|
school_class2 = SchoolClassFactory(users=[self.teacher2, self.student2])
|
|
self.project1 = ProjectFactory(student=self.student)
|
|
|
|
self.mutation = '''
|
|
mutation DeleteProject($input: DeleteProjectInput!) {
|
|
deleteProject(input: $input) {
|
|
success
|
|
errors
|
|
}
|
|
}
|
|
'''
|
|
|
|
self.variables = {
|
|
'input': {
|
|
'id': to_global_id('ProjectNode', self.project1.id)
|
|
}
|
|
}
|
|
|
|
def test_should_be_able_to_delete_own_projects(self):
|
|
|
|
self.assertEqual(Project.objects.count(), 1)
|
|
request = RequestFactory().get('/')
|
|
request.user = self.student
|
|
self.client = Client(schema=schema, context_value=request)
|
|
|
|
result = self.client.execute(self.mutation, variables=self.variables)
|
|
|
|
self.assertIsNone(result.get('errors'))
|
|
self.assertEqual(Project.objects.count(), 0)
|
|
|
|
def test_should_not_be_able_to_delete_other_projects(self):
|
|
|
|
self.assertEqual(Project.objects.count(), 1)
|
|
request = RequestFactory().get('/')
|
|
request.user = self.student2
|
|
self.client = Client(schema=schema, context_value=request)
|
|
|
|
result = self.client.execute(self.mutation, variables=self.variables)
|
|
self.assertEqual(result.get('errors')[0]['message'], 'Permission denied: Incorrect project')
|
|
|
|
|
|
class ProjectMutationsTestCase(DefaultUserTestCase):
|
|
def test_add_project(self):
|
|
client = create_client(self.student1)
|
|
mutation = """
|
|
mutation AddProjectMutation($input: AddProjectInput!){
|
|
addProject(input: $input){
|
|
project {
|
|
id
|
|
}
|
|
errors
|
|
}
|
|
}
|
|
"""
|
|
|
|
result = client.execute(mutation, variables={
|
|
'input': {
|
|
"project": {
|
|
"title": "Rick Astley",
|
|
"description": "She wants to dance with me",
|
|
"objectives": "Dance with me",
|
|
"appearance": "green"
|
|
}
|
|
}
|
|
})
|
|
self.assertIsNone(result.get('errors'))
|
|
self.assertEqual(Project.objects.count(), 1)
|
|
|