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 portfolio.models import Project
from rooms.models import Room
from users.factories import SchoolClassFactory
from users.models import User, SchoolClass
from users.services import create_users


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.query = '''
            query ProjectsQuery {
              projects {
                edges {
                  node {
                    ...ProjectParts
                    __typename
                  }
                  __typename
                }
                __typename
              }
            }
            
            fragment ProjectParts on ProjectNode {
              id
              title
              appearance
              description
              slug
              objectives
              __typename
            }
            
        '''

    def test_should_see_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.query)

        self.assertIsNone(result.get('errors'))
        self.assertEqual(result.get('data').get('projects').get('edges')[0].get('node').get('title'), self.project1.title)

    def test_should_not_see_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.query)

        self.assertIsNone(result.get('errors'))
        self.assertEqual(len(result.get('data').get('projects').get('edges')), 0)

    def test_teacher_should_not_see_unfinished_projects(self):
        request = RequestFactory().get('/')
        request.user = self.teacher
        self.client = Client(schema=schema, context_value=request)

        result = self.client.execute(self.query)

        self.assertIsNone(result.get('errors'))
        self.assertEqual(len(result.get('data').get('projects').get('edges')), 0)

    def test_teacher_should_only_see_finished_projects(self):
        self.project1.final = True
        self.assertEqual(Project.objects.count(), 1)
        request = RequestFactory().get('/')
        request.user = self.teacher
        self.client = Client(schema=schema, context_value=request)

        result = self.client.execute(self.query)

        self.assertIsNone(result.get('errors'))
        self.assertEqual(result.get('data').get('projects').get('edges')[0].get('node').get('title'),
                         self.project1.title)

    def test_teacher_should_only_see_finished_projects(self):
        self.project1.final = True
        self.project1.save()
        self.assertEqual(Project.objects.count(), 1)
        request = RequestFactory().get('/')
        request.user = self.teacher
        self.client = Client(schema=schema, context_value=request)

        result = self.client.execute(self.query)

        self.assertIsNone(result.get('errors'))
        self.assertEqual(result.get('data').get('projects').get('edges')[0].get('node').get('title'),
                         self.project1.title)

    def test_other_teacher_should_not_see_projects(self):
        self.project1.final = True
        self.project1.save()
        self.assertEqual(Project.objects.count(), 1)
        request = RequestFactory().get('/')
        request.user = self.teacher2
        self.client = Client(schema=schema, context_value=request)

        result = self.client.execute(self.query)

        self.assertIsNone(result.get('errors'))
        self.assertEqual(len(result.get('data').get('projects').get('edges')), 0)
