# Path: tests/conftest.py
# class FixtureServiceSet(object):
#     def __init__(self, admin: User):
#         self.test = ensure_service("test", "Test Service", admin, secret_key="SECRET__", scope="TEST")
#         self.sparcs = ensure_service("sparcs", "SPARCS Service", admin, secret_key="SPARCS__", scope="SPARCS")
#         self.public = ensure_service("public", "Public Service", admin, secret_key="PUBLIC__", scope="PUBLIC")
# 
# class FixtureUserSet(object):
#     def __init__(self):
#         self.admin = ensure_user("admin", ("Admin", "Admin"), "admin@sparcs.org", is_staff=True)
#         self.basic = ensure_user(
#             "user", ("User", "User"), "user@domain.com",
#             gender="*F", birthday=datetime.date(2000, 8, 1),
#         )
#         self.test = ensure_user("test", ("Test", "Test"), "test@domain.com", test_only=False, test_enabled=True)
#         self.test_only = ensure_user(
#             "test_only", ("Test", "Only"), "test-only@domain.com",
#             test_only=True, test_enabled=True,
#         )
#         self.sparcs = ensure_user("sparcs", ("SPARCS", "SPARCS"), "jungnoh@sparcs.org", sparcs_id="jungnoh")
#         self.email_unauthed = ensure_user("email_unauthed", ("No", "email"), "email@domain.com", email_authed=False)
#         self.kaist = ensure_user(
#             "kaist", ("KAIST", "user"), "jungnoh@kaist.ac.kr",
#             kaist_id="jungnoh", kaist_info=KAIST_USER_INFO, kaist_info_time=datetime.datetime(2018, 10, 29, 12, 1, 0),
#             birthday=datetime.date(2000, 9, 1),
#         )
# 
# class RequestSettingMixin:
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.api_client = APIClient()
# 
#     def http_request(self, user, method, path, data=None, querystring='', **kwargs):
#         self.api_client.force_authenticate(user=user)
#         request_func = {
#             'post': self.api_client.post,
#             'patch': self.api_client.patch,
#             'put': self.api_client.put,
#             'get': self.api_client.get,
#             'delete': self.api_client.delete,
#         }
#         url = f'/{path}/?{querystring}'
#         return request_func[method](url, data=data, format='json', **kwargs)
# 
# def ensure_user(username: str, name: Tuple[str, str], email: str, is_staff=False, password=None, **kwargs) -> User:
#     user_defaults = {
#         "username": username,
#         "first_name": name[0],
#         "last_name": name[1],
#         "email": email,
#         "is_staff": is_staff,
#     }
#     user, _ = User.objects.filter(Q(username=username) | Q(email=email)).update_or_create(defaults=user_defaults)
#     if password is None:
#         user.set_unusable_password()
#     else:
#         user.set_password(password)
#     user.save()
#     UserProfile.objects.update_or_create(defaults={
#         "gender": "*H",
#         "email_authed": True,
#         **kwargs,
#     }, user=user)
#     return user


from datetime import datetime
from django.test import TestCase
from django.utils import timezone
from rest_framework import status
from tests.conftest import FixtureServiceSet, FixtureUserSet, RequestSettingMixin, ensure_user



class ApiTestCase(TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.users = FixtureUserSet()
        cls.services = FixtureServiceSet(cls.users.admin)

    def tearDown(self):
        super().tearDown()


class TestWebSudo(RequestSettingMixin, TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.pw_user = ensure_user("foo", ("foo", "foo"), "foo@sparcs.org", password="123123")

# Path: tests/conftest.py
# class FixtureServiceSet(object):
#     def __init__(self, admin: User):
#         self.test = ensure_service("test", "Test Service", admin, secret_key="SECRET__", scope="TEST")
#         self.sparcs = ensure_service("sparcs", "SPARCS Service", admin, secret_key="SPARCS__", scope="SPARCS")
#         self.public = ensure_service("public", "Public Service", admin, secret_key="PUBLIC__", scope="PUBLIC")
# 
# class FixtureUserSet(object):
#     def __init__(self):
#         self.admin = ensure_user("admin", ("Admin", "Admin"), "admin@sparcs.org", is_staff=True)
#         self.basic = ensure_user(
#             "user", ("User", "User"), "user@domain.com",
#             gender="*F", birthday=datetime.date(2000, 8, 1),
#         )
#         self.test = ensure_user("test", ("Test", "Test"), "test@domain.com", test_only=False, test_enabled=True)
#         self.test_only = ensure_user(
#             "test_only", ("Test", "Only"), "test-only@domain.com",
#             test_only=True, test_enabled=True,
#         )
#         self.sparcs = ensure_user("sparcs", ("SPARCS", "SPARCS"), "jungnoh@sparcs.org", sparcs_id="jungnoh")
#         self.email_unauthed = ensure_user("email_unauthed", ("No", "email"), "email@domain.com", email_authed=False)
#         self.kaist = ensure_user(
#             "kaist", ("KAIST", "user"), "jungnoh@kaist.ac.kr",
#             kaist_id="jungnoh", kaist_info=KAIST_USER_INFO, kaist_info_time=datetime.datetime(2018, 10, 29, 12, 1, 0),
#             birthday=datetime.date(2000, 9, 1),
#         )
# 
# class RequestSettingMixin:
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.api_client = APIClient()
# 
#     def http_request(self, user, method, path, data=None, querystring='', **kwargs):
#         self.api_client.force_authenticate(user=user)
#         request_func = {
#             'post': self.api_client.post,
#             'patch': self.api_client.patch,
#             'put': self.api_client.put,
#             'get': self.api_client.get,
#             'delete': self.api_client.delete,
#         }
#         url = f'/{path}/?{querystring}'
#         return request_func[method](url, data=data, format='json', **kwargs)
# 
# def ensure_user(username: str, name: Tuple[str, str], email: str, is_staff=False, password=None, **kwargs) -> User:
#     user_defaults = {
#         "username": username,
#         "first_name": name[0],
#         "last_name": name[1],
#         "email": email,
#         "is_staff": is_staff,
#     }
#     user, _ = User.objects.filter(Q(username=username) | Q(email=email)).update_or_create(defaults=user_defaults)
#     if password is None:
#         user.set_unusable_password()
#     else:
#         user.set_password(password)
#     user.save()
#     UserProfile.objects.update_or_create(defaults={
#         "gender": "*H",
#         "email_authed": True,
#         **kwargs,
#     }, user=user)
#     return user


# Path: tests/test_web_sudo.py
from datetime import datetime
from django.test import TestCase
from django.utils import timezone
from rest_framework import status
from tests.conftest import FixtureServiceSet, FixtureUserSet, RequestSettingMixin, ensure_user




class ApiTestCase(TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.users = FixtureUserSet()
        cls.services = FixtureServiceSet(cls.users.admin)

    def tearDown(self):
        super().tearDown()


class TestWebSudo(RequestSettingMixin, TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.pw_user = ensure_user("foo", ("foo", "foo"), "foo@sparcs.org", password="123123")

        cls.no_pw_user = ensure_user("bar", ("bar", "bar"), "bar@sparcs.org")