"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 13.09.23

"""
from konova.settings import ZB_GROUP, DEFAULT_GROUP, ETS_GROUP
from konova.tests.test_views import BaseTestCase
from user.enums import UserNotificationEnum
from user.models import UserNotification


class UserTestCase(BaseTestCase):
    def test_is_notification_setting_set(self):
        notification = UserNotification.objects.create(
            id=UserNotificationEnum.NOTIFY_ON_DEDUCTION_CHANGES.name,
            name=UserNotificationEnum.NOTIFY_ON_DEDUCTION_CHANGES.value,
        )
        self.assertFalse(self.user.is_notification_setting_set(UserNotificationEnum.NOTIFY_ON_DEDUCTION_CHANGES))
        self.user.notifications.add(notification)
        self.assertTrue(self.user.is_notification_setting_set(UserNotificationEnum.NOTIFY_ON_DEDUCTION_CHANGES))

    def test_is_group_member(self):
        zb_group = self.groups.get(name=ZB_GROUP)
        ets_group = self.groups.get(name=ETS_GROUP)
        default_group = self.groups.get(name=DEFAULT_GROUP)

        self.user.groups.set([])
        self.assertFalse(self.user.is_zb_user())
        self.assertFalse(self.user.is_ets_user())
        self.assertFalse(self.user.is_default_user())

        self.user.groups.add(zb_group)
        self.assertTrue(self.user.is_zb_user())

        self.user.groups.add(ets_group)
        self.assertTrue(self.user.is_ets_user())

        self.user.groups.add(default_group)
        self.assertTrue(self.user.is_default_user())

    def test_get_API_token(self):
        self.assertIsNone(self.user.api_token)
        token = self.user.get_API_token()
        self.assertIsNotNone(self.user.api_token)
        self.assertEqual(self.user.api_token, token)

        # Make sure the same token is returned if command is called twice
        token = self.user.get_API_token()
        self.assertEqual(self.user.api_token, token)

    def test_shared_teams_property(self):
        shared_teams = self.user.shared_teams
        self.assertEqual(shared_teams.count(), 0)

        self.team.users.add(self.user)
        shared_teams = self.user.shared_teams
        self.assertEqual(shared_teams.count(), 1)
        self.assertIn(self.team, shared_teams)

