from django.test import TestCase
from graphene import Context
from graphene.test import Client

from core.factories import UserFactory
from users.factories import SchoolClassFactory
from users.models import SchoolClass, SchoolClassMember
from api.schema import schema


class OnboardingTest(TestCase):
    def setUp(self):
        self.client = Client(schema=schema)
        self.user = UserFactory(username='ueli')
        SchoolClassFactory(users=[self.user], name='Klasse 1A', code='XXXX')
        self.context = Context(user=self.user)
        self.me_query = """
        query MeQuery {
          me {
            onboardingVisited
          }
        }
        """

        self.mutation = """
        mutation UpdateOnboardingProgress {
          updateOnboardingProgress {
            success
          }
        }
        """

    def test_show_onboarding(self):
        executed = self.client.execute(self.me_query, context=self.context)
        self.assertIsNone(executed.get('errors', None))
        result = executed['data']['me']
        self.assertEqual(result['onboardingVisited'], False)

    def test_update_onboarding(self):
        executed = self.client.execute(self.mutation, context=self.context)
        self.assertIsNone(executed.get('errors', None))
        result = executed['data']['updateOnboardingProgress']
        self.assertEqual(result['success'], True)

        executed = self.client.execute(self.me_query, context=self.context)
        self.assertIsNone(executed.get('errors', None))
        result = executed['data']['me']
        self.assertEqual(result['onboardingVisited'], True)

