"""Authorization helper tests."""

from django.contrib.auth.models import AnonymousUser, User
from django.test import TestCase

from allianceauth.eveonline.models import EveCorporationInfo
from allianceauth.tests.auth_utils import AuthUtils

from shop.models import Shop
from shop.permissions import can_manage, corp_shops_for, personal_shops_for


class PersonalShopAuthTests(TestCase):
    def setUp(self):
        self.owner = AuthUtils.create_user("owner")
        self.other = AuthUtils.create_user("other")
        self.shop = Shop.objects.create(
            slug="p", name="P", created_by=self.owner,
            shop_type=Shop.ShopType.PERSONAL,
        )

    def test_owner_can_manage(self):
        self.assertTrue(can_manage(self.owner, self.shop))

    def test_other_cannot_manage(self):
        self.assertFalse(can_manage(self.other, self.shop))

    def test_anonymous_cannot_manage(self):
        self.assertFalse(can_manage(AnonymousUser(), self.shop))

    def test_superuser_can_manage(self):
        su = AuthUtils.create_user("su")
        su.is_superuser = True
        su.save()
        self.assertTrue(can_manage(su, self.shop))

    def test_personal_shops_for_lists_only_owned(self):
        self.assertEqual(list(personal_shops_for(self.owner)), [self.shop])
        self.assertEqual(list(personal_shops_for(self.other)), [])


class CorporationShopAuthTests(TestCase):
    def setUp(self):
        self.corp = EveCorporationInfo.objects.create(
            corporation_id=2001, corporation_name="Corp",
            corporation_ticker="CORP", member_count=1,
        )
        self.member = AuthUtils.create_user("corpmember")
        AuthUtils.add_main_character_2(
            self.member, "Main", 1001, corp_id=2001, disconnect_signals=True
        )
        AuthUtils.add_permission_to_user_by_name(
            "shop.manage_corporation_shops", self.member
        )
        self.member = User.objects.get(pk=self.member.pk)  # refresh perm cache
        self.shop = Shop.objects.create(
            slug="c", name="C", created_by=self.member,
            shop_type=Shop.ShopType.CORPORATION, corporation=self.corp,
        )

    def test_member_with_perm_and_corp_can_manage(self):
        self.assertTrue(can_manage(self.member, self.shop))

    def test_member_without_perm_cannot_manage(self):
        u = AuthUtils.create_user("noperm")
        AuthUtils.add_main_character_2(u, "NP", 1002, corp_id=2001, disconnect_signals=True)
        self.assertFalse(can_manage(u, self.shop))

    def test_member_of_other_corp_cannot_manage(self):
        u = AuthUtils.create_user("othercorp")
        AuthUtils.add_main_character_2(u, "OC", 1003, corp_id=9999, disconnect_signals=True)
        AuthUtils.add_permission_to_user_by_name("shop.manage_corporation_shops", u)
        u = User.objects.get(pk=u.pk)
        self.assertFalse(can_manage(u, self.shop))

    def test_corp_shops_for_returns_own_corp_shop(self):
        self.assertIn(self.shop, corp_shops_for(self.member))
