Commit 44c88d77 authored by Naveen Kumar Sangi's avatar Naveen Kumar Sangi 👽

Jira: Add JiraMixin and JiraOAuth1Token

parent a2dba833
"""
This package contains the JIRA implementations of the interfaces in
server.git.Interfaces.
"""
from urllib.parse import urljoin
import logging
import os
from oauthlib.oauth1 import SIGNATURE_RSA
from requests_oauthlib import OAuth1
from IGitt.Interfaces import get
from IGitt.Interfaces import Token
from IGitt.Utils import CachedDataMixin
from tests import PRIVATE_KEY
JIRA_INSTANCE_URL = os.environ.get('JIRA_INSTANCE_URL',
'https://jira.atlassian.com')
BASE_URL = urljoin(JIRA_INSTANCE_URL, '/rest/api/2')
JIRA_RSA_PRIVATE_KEY_PATH = os.environ.get('JIRA_RSA_PRIVATE_KEY_PATH')
JIRA_RSA_PRIVATE_KEY = PRIVATE_KEY
try:
JIRA_RSA_PRIVATE_KEY = open(JIRA_RSA_PRIVATE_KEY_PATH, 'r').read().strip()
except (FileNotFoundError, TypeError):
logging.warning('JIRA REST APIs work only with key signing, please '
'include the correct path to your registered RSA private '
'key.')
class JiraMixin(CachedDataMixin):
"""
Base object for all things on Jira.
"""
def _get_data(self):
return get(self._token, self.url)
@staticmethod
def absolute_url(url):
"""
Builds an absolute URL from the base URL and specified url.
"""
return BASE_URL + url
@property
def hoster(self):
"""
Returns `jira`.
"""
return 'jira'
@property
def url(self):
"""
Returns JIRA API url.
"""
return self.absolute_url(self._url)
@property
def web_url(self):
"""
Returns the web link for the corresponding JIRA object.
"""
raise NotImplementedError
def __repr__(self): # dont cover
return '<{} object(url={}) at {}>'.format(self.__class__.__name__,
self.url,
hex(id(self)))
class JiraOAuth1Token(Token):
"""
Object representation of JIRA OAuth v1.0 token.
"""
def __init__(self, client_key, key, secret):
self.client_key = client_key
self.key = key
self.secret = secret
@property
def headers(self):
return {}
@property
def parameter(self):
return {}
@property
def value(self):
return {'client_key': self.client_key,
'oauth_token': self.key,
'oauth_token_secret': self.secret}
@property
def auth(self):
return OAuth1(self.client_key,
rsa_key=JIRA_RSA_PRIVATE_KEY,
resource_owner_key=self.key,
resource_owner_secret=self.secret,
signature_method=SIGNATURE_RSA,
signature_type='auth_header')
......@@ -20,6 +20,10 @@ env =
D:GITHUB_TEST_APP_ID=5408
D:GITHUB_APP_TEST_TOKEN=something
D:GITHUB_APP_USER_TEST_TOKEN=something
D:JIRA_CLIENT_KEY=OauthKey
D:JIRA_TEST_TOKEN=something
D:JIRA_TEST_SECRET=something
D:JIRA_INSTANCE_URL=https://jira.gitmate.io:443/
[coverage:report]
exclude_lines =
......
interactions:
- request:
body: '{}'
headers:
Accept:
- !!binary |
Ki8q
Accept-Encoding:
- !!binary |
Z3ppcCwgZGVmbGF0ZQ==
Connection:
- !!binary |
a2VlcC1hbGl2ZQ==
Content-Length: ['2']
Content-Type:
- !!binary |
YXBwbGljYXRpb24vanNvbg==
User-Agent:
- !!binary |
SUdpdHQ=
method: GET
uri: https://jira.gitmate.io/rest/api/2/myself?per_page=100
response:
body:
string: !!binary |
H4sIAAAAAAAAAKyRvW7CMBRGX6Xy0MmKEyekKRKizJUYilhaVdVtcpO6+E+2gVDEu9dphoJYWSzr
8/G5lr8j8ShbMiVfIVg/ZexbOEg6ERQETIRhDn1gYAXjbOvRzYdFg8KZ3lgndI1p+kAo2eAhSi6z
AbsKUYGQi6aJXj8cwg5RJx50J57+x0YQdhDArZ2M2JEUVV9UZ8/c7/dJ50YmqY1i45ZVKc8mj3kL
TTnhE6iysiiz9LNpq6zKW57Pm5lS934WZZTwoufFDaVRRklW9ll5Q2mUUZLzPuc3lEbZiZJGeCvh
sBx7Wv5Vcbcaqhj+vw5iF/PgtkhJEApfjR64hRfAno3cxImRk6YGOeSoP9arGHTObO1faV78xANO
iQioYvL2HoeCtVLUEITRL0biGZhdgthb0E0Uj0J6dfH0CwAA//8DANXwBOm9AgAA
headers:
Cache-Control: ['no-cache, no-store, no-transform']
Content-Encoding: [gzip]
Content-Length: ['333']
Content-Security-Policy: [frame-ancestors 'self']
Content-Type: [application/json;charset=UTF-8]
Date: ['Thu, 22 Mar 2018 08:36:55 GMT']
Set-Cookie: [JSESSIONID=DA11797C2DB2A7A7724A0A6854F2D270;path=/;HttpOnly, atlassian.xsrf.token=B15V-IRV7-Q0YS-ZCZZ|ecb5c93c2ccee5c93fae906fc1b02aeccc60954a|lin;path=/]
Strict-Transport-Security: [max-age=315569260; preload]
Vary: [User-Agent]
X-Arequestid: [516x7121x1]
X-Asen: [SEN-L11373089]
X-Asessionid: [j8bpb7]
X-Ausername: [nkprince007]
X-Content-Type-Options: [nosniff]
X-Frame-Options: [DENY, SAMEORIGIN]
X-Seraph-Loginreason: [OK]
X-Xss-Protection: [1; mode=block, 1; mode=block]
status: {code: 200, message: OK}
version: 1
from os import environ
from IGitt.Jira import JiraOAuth1Token, BASE_URL
from IGitt.Interfaces import get
from tests import IGittTestCase
class JiraInitTest(IGittTestCase):
def test_tokens(self):
token = JiraOAuth1Token(environ['JIRA_CLIENT_KEY'],
environ['JIRA_TEST_TOKEN'],
environ['JIRA_TEST_SECRET'])
self.assertEqual(token.parameter, {})
self.assertEqual(token.value, {
'client_key': environ['JIRA_CLIENT_KEY'],
'oauth_token': environ['JIRA_TEST_TOKEN'],
'oauth_token_secret': environ['JIRA_TEST_SECRET'],
})
self.assertEqual(get(token, BASE_URL + '/myself')['name'],
'nkprince007')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment