import unittest
import os

from jira.client import JIRA
from jira.exceptions import JIRAError
from jira.resources import Resource, cls_for_resource, Issue, Project, Role

TEST_ROOT = os.path.dirname(__file__)
TEST_ICON_PATH = os.path.join(TEST_ROOT, 'icon.png')
TEST_ATTACH_PATH = os.path.join(TEST_ROOT, '__init__.py')

OAUTH = True
CONSUMER_KEY = 'oauth-consumer'
KEY_CERT_FILE = '/home/bspeakmon/src/atlassian-oauth-examples/rsa.pem'
KEY_CERT_DATA = None
with open(KEY_CERT_FILE, 'r') as cert:
    KEY_CERT_DATA = cert.read()

def get_jira_admin_auth():
    if OAUTH:
        return JIRA(oauth={
            'access_token': 'hTxcwsbUQiFuFALf7KZHDaeAJIo3tLUK',
            'access_token_secret': 'aNCLQFP3ORNU6WY7HQISbqbhf0UudDAf',
            'consumer_key': CONSUMER_KEY,
            'key_cert': KEY_CERT_DATA,
        })
    else:
        return JIRA(basic_auth=('admin', 'admin'))

def get_jira_sysadmin_auth():
    if OAUTH:
        return JIRA(oauth={
            'access_token': '4ul1ETSFo7ybbIxAxzyRal39cTrwEGFv',
            'access_token_secret': 'K83jBZnjnuVRcfjBflrKyThJa0KSjSs2',
            'consumer_key': CONSUMER_KEY,
            'key_cert': KEY_CERT_DATA,
        })
    else:
        return JIRA(basic_auth=('eviladmin', 'eviladmin'))

def get_jira_schlub_auth():
    if OAUTH:
        return JIRA(oauth={
            'access_token': 'ZVDgYDyIQqJY8IFlQ446jZaURIz5ECiB',
            'access_token_secret': '5WbLBybPDg1lqqyFjyXSCsCtAWTwz1eD',
            'consumer_key': CONSUMER_KEY,
            'key_cert': KEY_CERT_DATA,
        })
    else:
        return JIRA(basic_auth=('fred', 'fred'))

def find_by_key(seq, key):
    for seq_item in seq:
        if seq_item['key'] == key:
            return seq_item

def find_by_id(seq, id):
    for seq_item in seq:
        if seq_item.id == id:
            return seq_item


class UniversalResourceTests(unittest.TestCase):

    def setUp(self):
        self.jira = get_jira_admin_auth()

    def test_universal_find_existing_resource(self):
        resource = self.jira.find('issue/{0}', 'BULK-1')
        issue = self.jira.issue('BULK-1')
        self.assertEqual(resource.self, issue.self)
        self.assertEqual(resource.key, issue.key)

    def test_universal_find_custom_resource(self):
        resource = Resource('nope/{0}', self.jira._options, None)  # don't need an actual session
        self.assertEqual('http://localhost:2990/jira/rest/api/2/nope/666', resource._url(('666',)))

    def test_find_invalid_resource_raises_exception(self):
        with self.assertRaises(JIRAError) as cm:
            self.jira.find('woopsydoodle/{0}', '666')

        ex = cm.exception
        self.assertEqual(ex.status_code, 404)
        self.assertIsNotNone(ex.text)
        self.assertEqual(ex.url, 'http://localhost:2990/jira/rest/api/2/woopsydoodle/666')

    def test_verify_works_with_https(self):
        self.jira = JIRA(options={'server': 'https://jira.atlassian.com'})

    def test_verify_fails_without_https(self):
        # we need a server that doesn't do https
        self.jira = JIRA(options={'server': 'https://www.yahoo.com'})
        self.assertRaises(JIRAError, self.jira.issue, 'BULK-1')


class ResourceTests(unittest.TestCase):

    def setUp(self):
        pass

    def test_cls_for_resource(self):
        self.assertEqual(cls_for_resource('https://jira.atlassian.com/rest/api/2/issue/JRA-1330'), Issue)
        self.assertEqual(cls_for_resource('http://localhost:2990/jira/rest/api/2/project/BULK'), Project)
        self.assertEqual(cls_for_resource('http://imaginary-jira.com/rest/api/2/project/IMG/role/10002'), Role)
        self.assertEqual(cls_for_resource('http://customized-jira.com/rest/plugin-resource/4.5/json/getMyObject'), Resource)


class ApplicationPropertiesTests(unittest.TestCase):

    def setUp(self):