from django.test import TestCase

from core.factories import UserFactory
from users.tests.mock_hep_data_factory import ME_DATA
from users.models import User

TOKEN = 'abcd12345!'


class UserManagerTests(TestCase):
    def setUp(self):
        self.user = UserFactory(username=ME_DATA['email'], email=ME_DATA['email'], hep_id=ME_DATA['id'])

    def test_get_existing_user(self):

        user_data = {
            'id': ME_DATA['id']
        }

        user = User.objects.get_or_create_hep_user(user_data)
        self.assertEqual(user.id, self.user.id)

    def test_create_new_user(self):

        new_hep_id = ME_DATA['id'] + 1

        try:
            User.objects.get(hep_id=new_hep_id)
            self.fail('UserManagerTests.test_create_new_user failed: User already exists!')
        except User.DoesNotExist:
            pass

        user_data = {
            'id': new_hep_id,
            'first_name': ME_DATA['first_name'],
            'last_name': ME_DATA['last_name'],
            'email': ME_DATA['email'],
        }

        user = User.objects.get_or_create_hep_user(user_data)
        self.assertEqual(user.hep_id, new_hep_id)
