from django.test import TestCase

from vbv_lernwelt.core.models import User
from vbv_lernwelt.importer.services import create_or_update_user


class CreateOrUpdateUserTestCase(TestCase):
    def test_create_user(self):
        u = create_or_update_user(
            email="daniel@example.com",
            first_name="Daniel",
            last_name="Egger",
            sso_id="12229620-81ea-483d-8d96-6ba8be5f9eb7",
        )

        saved_user = User.objects.get(id=u.id)

        self.assertEqual(saved_user.email, "daniel@example.com")
        self.assertEqual(saved_user.username, "daniel@example.com")
        self.assertEqual(saved_user.first_name, "Daniel")
        self.assertEqual(saved_user.last_name, "Egger")
        self.assertEqual(str(saved_user.sso_id), "12229620-81ea-483d-8d96-6ba8be5f9eb7")

    def test_update_existing_user_with_oid(self):
        User.objects.create(
            email="daniel@example.com",
            username="daniel@example.com",
            first_name="Daniel",
            last_name="Egger",
        )

        create_or_update_user(
            email="daniel@example.com",
            last_name="Egger",
            sso_id="12229620-81ea-483d-8d96-6ba8be5f9eb7",
        )

        self.assertEqual(1, User.objects.count())
        user = User.objects.first()

        self.assertEqual(user.email, "daniel@example.com")
        self.assertEqual(user.username, "daniel@example.com")
        self.assertEqual(user.first_name, "Daniel")
        self.assertEqual(user.last_name, "Egger")
        self.assertEqual(str(user.sso_id), "12229620-81ea-483d-8d96-6ba8be5f9eb7")

    def test_update_existing_user_with_new_last_name(self):
        User.objects.create(
            email="daniel@example.com",
            username="daniel@example.com",
            first_name="Daniel",
            last_name="Egger",
            sso_id="12229620-81ea-483d-8d96-6ba8be5f9eb7",
        )

        create_or_update_user(
            email="daniel@example.com",
            first_name="Daniel",
            last_name="Marro",
        )

        self.assertEqual(1, User.objects.count())
        user = User.objects.first()

        self.assertEqual(user.email, "daniel@example.com")
        self.assertEqual(user.username, "daniel@example.com")
        self.assertEqual(user.first_name, "Daniel")
        self.assertEqual(user.last_name, "Marro")
        self.assertEqual(str(user.sso_id), "12229620-81ea-483d-8d96-6ba8be5f9eb7")

    def test_update_existing_user_with_new_email(self):
        User.objects.create(
            email="daniel@example.com",
            username="daniel@example.com",
            first_name="Daniel",
            last_name="Egger",
            sso_id="12229620-81ea-483d-8d96-6ba8be5f9eb7",
        )

        create_or_update_user(
            email="danu@example.com",
            first_name="Daniel",
            last_name="Egger",
            sso_id="12229620-81ea-483d-8d96-6ba8be5f9eb7",
        )

        self.assertEqual(1, User.objects.count())
        user = User.objects.first()

        self.assertEqual(user.email, "danu@example.com")
        self.assertEqual(user.username, "danu@example.com")
        self.assertEqual(user.first_name, "Daniel")
        self.assertEqual(user.last_name, "Egger")
        self.assertEqual(str(user.sso_id), "12229620-81ea-483d-8d96-6ba8be5f9eb7")
