# MIT License
# Copyright (c) 2017 MassChallenge, Inc.

from django.urls import reverse

from impact.graphql.middleware import NOT_LOGGED_IN_MSG
from impact.tests.api_test_case import APITestCase
from impact.tests.factories import ExpertFactory, StartupMentorRelationshipFactory  # noqa: E501
from impact.tests.utils import capture_stderr


class TestGraphQL(APITestCase):
    url = reverse('graphql')
    auth_url = reverse('graphql-auth')

    def test_anonymous_user_cannot_access_main_graphql_view(self):
        user = ExpertFactory()
        query = """query {{ expertProfile(id: {id}) {{ title }} }}
            """.format(id=user.expertprofile.id)

        with capture_stderr(self.client.post,
                            self.url,
                            data={'query': query}) as (response, _):
            error_messages = [x['message'] for x in response.json()['errors']]

        self.assertIn(NOT_LOGGED_IN_MSG, error_messages)

    def test_anonymous_user_can_access_auth_graphql_view(self):
        query = """mutation {
                    verifyToken (token: "some-token") {
                        payload
                    }
                }"""
        response = self.client.post(self.url, data={'query': query})
        error_messages = [x['message'] for x in response.json()['errors']]
        self.assertNotIn(NOT_LOGGED_IN_MSG, error_messages)

    def test_query_with_expert(self):
        with self.login(email=self.basic_user().email):
            user = ExpertFactory()
            query = """
                query {{
                    expertProfile(id: {id}) {{
                        user {{ firstName }}
                        bio
                        currentMentees {{ startupName }}
                        previousMentees {{ startupName }}
                    }}
                }}
            """.format(id=user.expertprofile.id)
            response = self.client.post(self.url, data={'query': query})
            self.assertJSONEqual(
                str(response.content, encoding='utf8'),
                {
                    'data': {
                        'expertProfile': {
                            'user': {
                                'firstName': user.first_name,
                            },
                            'bio': user.expertprofile.bio,
                            'currentMentees': [],
                            'previousMentees': [],
                        }
                    }
                }
            )

    def test_requested_fields_for_startup_mentor_relationship_type(self):
        with self.login(email=self.basic_user().email):
            obj = StartupMentorRelationshipFactory()
            startup = obj.startup_mentor_tracking.startup
            program = obj.startup_mentor_tracking.program
            query = """
                query {{
                    expertProfile(id: {id}) {{
                        currentMentees {{
                            startupId
                            startupName
                            startupHighResolutionLogo
                            startupShortPitch
                            programLocation
                            programYear
                            programStatus
                        }}
                        previousMentees {{
                            startupId
                            startupName
                            startupHighResolutionLogo
                            startupShortPitch
                            programLocation
                            programYear
                            programStatus
                        }}
                    }}
                }}
            """.format(id=obj.mentor.expertprofile.id)
            response = self.client.post(self.url, data={'query': query})
            self.assertJSONEqual(
                str(response.content, encoding='utf8'),
                {
                    'data': {
                        'expertProfile': {
                            'currentMentees': [{
                                'startupId': str(startup.id),
                                'startupName': startup.name,
                                'startupHighResolutionLogo':
                                    str(startup.high_resolution_logo),
                                'startupShortPitch': startup.short_pitch,
                                'programLocation':
                                    program.program_family.name,
                                'programYear': str(program.start_date.year),
                                'programStatus': program.program_status
                            }],
                            'previousMentees': []
                        }
                    }
                }
            )

@@ -224,16 +224,22 @@
  import 
+(%0A    
 ExpertFa
@@ -244,16 +244,20 @@
 Factory,
+%0A   
  Startup
@@ -285,22 +285,11 @@
 tory
-  # noqa: E501
+,%0A)
 %0Afro
@@ -332,16 +332,177 @@
 stderr%0A%0A
+MENTEE_FIELDS = %22%22%22%0A    startupId%0A    startupName%0A    startupHighResolutionLogo%0A    startupShortPitch%0A    programLocation%0A    programYear%0A    programStatus%0A%22%22%22%0A%0A
 %0Aclass T
@@ -3130,657 +3130,137 @@
     
-startupId%0A                            startupName%0A                            startupHighResolutionLogo%0A                            startupShortPitch%0A                            programLocation%0A                            programYear%0A                            programStatus%0A                        %7D%7D%0A                        previousMentees %7B%7B%0A                            startupId%0A                            startupName%0A                            startupHighResolutionLogo%0A                            startupShortPitch%0A                            programLocation%0A                            programYear%0A                            programStatus
+%7BMENTEE_FIELDS%7D%0A                        %7D%7D%0A                        previousMentees %7B%7B%0A                            %7BMENTEE_FIELDS%7D
 %0A   
@@ -3370,32 +3370,84 @@
 expertprofile.id
+,%0A                       MENTEE_FIELDS=MENTEE_FIELDS
 )%0A            re

fix import and move mentee fields to a constant