# Path: tests/base.py
# class ServiceTest(ConfigTest):
#     GENERAL_CONFIG = {
#         'interactive': False,
#         'annotation_length': 100,
#         'description_length': 100,
#     }
#     SERVICE_CONFIG = {
#     }
# 
#     @classmethod
#     def setUpClass(cls):
#         cls.maxDiff = None
# 
#     def get_mock_service(
#         self, service_class, section='unspecified',
#         config_overrides=None, general_overrides=None
#     ):
#         options = {
#             'general': {**self.GENERAL_CONFIG, 'targets': section},
#             section: self.SERVICE_CONFIG.copy(),
#         }
#         if config_overrides:
#             options[section].update(config_overrides)
#         if general_overrides:
#             options['general'].update(general_overrides)
# 
#         service_config = service_class.CONFIG_SCHEMA(**options[section])
#         main_config = schema.MainSectionConfig(**options['general'])
#         main_config.data = data.BugwarriorData(self.lists_path)
# 
#         return service_class(service_config, main_config, section)
# 
#     @staticmethod
#     def add_response(url, **kwargs):
#         responses.add(responses.GET, url, match_querystring=True, **kwargs)
# 
# class AbstractServiceTest(abc.ABC):
#     """ Ensures that certain test methods are implemented for each service. """
#     @abc.abstractmethod
#     def test_to_taskwarrior(self):
#         """ Test Service.to_taskwarrior(). """
#         raise NotImplementedError
# 
#     @abc.abstractmethod
#     def test_issues(self):
#         """
#         Test Service.issues().
# 
#         - When the API is accessed via requests, use the responses library to
#         mock requests.
#         - When the API is accessed via a third party library, substitute a fake
#         implementation class for it.
#         """
#         raise NotImplementedError
# 
# Path: bugwarrior/services/teamwork_projects.py
# class TeamworkService(IssueService):
#     ISSUE_CLASS = TeamworkIssue
#     CONFIG_SCHEMA = TeamworkConfig
# 
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.client = TeamworkClient(self.config.host, self.config.token)
#         user = self.client.authenticate()
#         self.user_id = user["account"]["userId"]
#         self.name = user["account"]["firstname"] + " " + user["account"]["lastname"]
# 
#     def get_comments(self, issue):
#         if self.main_config.annotation_comments:
#             if issue.get("comments-count", 0) > 0:
#                 endpoint = "/tasks/{task_id}/comments.json".format(task_id=issue["id"])
#                 comments = self.client.call_api("GET", endpoint)
#                 comment_list = []
#                 for comment in comments["comments"]:
#                     url = self.config.host + "/#/tasks/" + str(issue["id"])
#                     author = "{first} {last}".format(
#                         first=comment["author-firstname"],
#                         last=comment["author-lastname"],
#                     )
#                     text = comment["body"]
#                     comment_list.append((author, text))
#                 return self.build_annotations(comment_list, None)
#         return []
# 
#     def get_owner(self, issue):
#         # TODO
#         raise NotImplementedError(
#             "This service has not implemented support for 'only_if_assigned'.")
# 
#     def issues(self):
#         response = self.client.call_api("GET", "/tasks.json")#, data= { "responsible-party-ids": self.user_id })
#         for issue in response["todo-items"]:
#             # Determine if issue is need by if following comments, changes or assigned
#             if issue["userFollowingComments"] or issue["userFollowingChanges"]\
#                     or (self.user_id in issue.get("responsible-party-ids", "")):
#                 issue_obj = self.get_issue_for_record(issue)
#                 extra = {
#                     "host": self.config.host,
#                     'annotations': self.get_comments(issue),
#                 }
#                 issue_obj.update_extra(extra)
#                 yield issue_obj
# 
# class TeamworkClient(ServiceClient):
# 
#     def __init__(self, host, token):
#         self.host = host
#         self.token = token
# 
#     def authenticate(self):
#         response = requests.get(self.host + "/authenticate.json", auth=(self.token, ""))
#         return self.json_response(response)
# 
#     def call_api(self, method, endpoint, data=None):
#         response = requests.get(self.host + endpoint, auth=(self.token, ""), params=data)
#         return self.json_response(response)


from .base import ServiceTest, AbstractServiceTest
from bugwarrior.services.teamwork_projects import TeamworkService, TeamworkClient
from dateutil.tz import tzutc
import responses
import datetime



class TestTeamworkIssue(AbstractServiceTest, ServiceTest):
    SERVICE_CONFIG = {
        'service': 'teamwork_projects',
        'teamwork_projects.host': 'https://test.teamwork_projects.com',
        'teamwork_projects.token': 'arbitrary_token',
    }

    @responses.activate
    def setUp(self):
        super().setUp()
        self.add_response(
            'https://test.teamwork_projects.com/authenticate.json',
            json={
                'account': {
                    'userId': 5,
                    'firstname': 'Greg',
                    'lastname': 'McCoy'
                }
            }
        )

# Path: tests/base.py
# class ServiceTest(ConfigTest):
#     GENERAL_CONFIG = {
#         'interactive': False,
#         'annotation_length': 100,
#         'description_length': 100,
#     }
#     SERVICE_CONFIG = {
#     }
# 
#     @classmethod
#     def setUpClass(cls):
#         cls.maxDiff = None
# 
#     def get_mock_service(
#         self, service_class, section='unspecified',
#         config_overrides=None, general_overrides=None
#     ):
#         options = {
#             'general': {**self.GENERAL_CONFIG, 'targets': section},
#             section: self.SERVICE_CONFIG.copy(),
#         }
#         if config_overrides:
#             options[section].update(config_overrides)
#         if general_overrides:
#             options['general'].update(general_overrides)
# 
#         service_config = service_class.CONFIG_SCHEMA(**options[section])
#         main_config = schema.MainSectionConfig(**options['general'])
#         main_config.data = data.BugwarriorData(self.lists_path)
# 
#         return service_class(service_config, main_config, section)
# 
#     @staticmethod
#     def add_response(url, **kwargs):
#         responses.add(responses.GET, url, match_querystring=True, **kwargs)
# 
# class AbstractServiceTest(abc.ABC):
#     """ Ensures that certain test methods are implemented for each service. """
#     @abc.abstractmethod
#     def test_to_taskwarrior(self):
#         """ Test Service.to_taskwarrior(). """
#         raise NotImplementedError
# 
#     @abc.abstractmethod
#     def test_issues(self):
#         """
#         Test Service.issues().
# 
#         - When the API is accessed via requests, use the responses library to
#         mock requests.
#         - When the API is accessed via a third party library, substitute a fake
#         implementation class for it.
#         """
#         raise NotImplementedError
# 
# Path: bugwarrior/services/teamwork_projects.py
# class TeamworkService(IssueService):
#     ISSUE_CLASS = TeamworkIssue
#     CONFIG_SCHEMA = TeamworkConfig
# 
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.client = TeamworkClient(self.config.host, self.config.token)
#         user = self.client.authenticate()
#         self.user_id = user["account"]["userId"]
#         self.name = user["account"]["firstname"] + " " + user["account"]["lastname"]
# 
#     def get_comments(self, issue):
#         if self.main_config.annotation_comments:
#             if issue.get("comments-count", 0) > 0:
#                 endpoint = "/tasks/{task_id}/comments.json".format(task_id=issue["id"])
#                 comments = self.client.call_api("GET", endpoint)
#                 comment_list = []
#                 for comment in comments["comments"]:
#                     url = self.config.host + "/#/tasks/" + str(issue["id"])
#                     author = "{first} {last}".format(
#                         first=comment["author-firstname"],
#                         last=comment["author-lastname"],
#                     )
#                     text = comment["body"]
#                     comment_list.append((author, text))
#                 return self.build_annotations(comment_list, None)
#         return []
# 
#     def get_owner(self, issue):
#         # TODO
#         raise NotImplementedError(
#             "This service has not implemented support for 'only_if_assigned'.")
# 
#     def issues(self):
#         response = self.client.call_api("GET", "/tasks.json")#, data= { "responsible-party-ids": self.user_id })
#         for issue in response["todo-items"]:
#             # Determine if issue is need by if following comments, changes or assigned
#             if issue["userFollowingComments"] or issue["userFollowingChanges"]\
#                     or (self.user_id in issue.get("responsible-party-ids", "")):
#                 issue_obj = self.get_issue_for_record(issue)
#                 extra = {
#                     "host": self.config.host,
#                     'annotations': self.get_comments(issue),
#                 }
#                 issue_obj.update_extra(extra)
#                 yield issue_obj
# 
# class TeamworkClient(ServiceClient):
# 
#     def __init__(self, host, token):
#         self.host = host
#         self.token = token
# 
#     def authenticate(self):
#         response = requests.get(self.host + "/authenticate.json", auth=(self.token, ""))
#         return self.json_response(response)
# 
#     def call_api(self, method, endpoint, data=None):
#         response = requests.get(self.host + endpoint, auth=(self.token, ""), params=data)
#         return self.json_response(response)


# Path: tests/test_teamwork_projects.py
from .base import ServiceTest, AbstractServiceTest
from bugwarrior.services.teamwork_projects import TeamworkService, TeamworkClient
from dateutil.tz import tzutc
import responses
import datetime




class TestTeamworkIssue(AbstractServiceTest, ServiceTest):
    SERVICE_CONFIG = {
        'service': 'teamwork_projects',
        'teamwork_projects.host': 'https://test.teamwork_projects.com',
        'teamwork_projects.token': 'arbitrary_token',
    }

    @responses.activate
    def setUp(self):
        super().setUp()
        self.add_response(
            'https://test.teamwork_projects.com/authenticate.json',
            json={
                'account': {
                    'userId': 5,
                    'firstname': 'Greg',
                    'lastname': 'McCoy'
                }
            }
        )

        self.service = self.get_mock_service(TeamworkService)