Files
octokit.py/tests/test_auth.py
T

160 lines
6.4 KiB
Python
Raw Normal View History

2018-02-07 15:15:51 -06:00
import os
2018-02-20 09:33:52 -06:00
from collections import ChainMap
2018-02-07 15:15:51 -06:00
from collections import namedtuple
import pytest
import requests
from octokit import Octokit
class TestAuth(object):
def test_can_set_basic_authentication(self):
2020-01-17 11:49:05 -06:00
sut = Octokit(auth="basic", username="myuser", password="mypassword")
assert sut.auth == "basic"
assert sut.username == "myuser"
assert sut.password == "mypassword"
2018-02-07 15:15:51 -06:00
def test_cannot_set_basic_authentication_with_out_required_data(self):
with pytest.raises(KeyError):
2020-01-17 11:49:05 -06:00
Octokit(auth="basic")
2018-02-07 15:15:51 -06:00
with pytest.raises(AssertionError):
2020-01-17 11:49:05 -06:00
Octokit(auth="basic", username="z", password="")
2018-02-07 15:15:51 -06:00
with pytest.raises(KeyError):
2020-01-17 11:49:05 -06:00
Octokit(auth="basic", password="mypassword")
2018-02-07 15:15:51 -06:00
with pytest.raises(AssertionError):
2020-01-17 11:49:05 -06:00
Octokit(auth="basic", username="")
2018-02-07 15:15:51 -06:00
with pytest.raises(KeyError):
2020-01-17 11:49:05 -06:00
Octokit(auth="basic", username="xyz")
2018-02-07 15:15:51 -06:00
def test_basic_auth_used_if_set(self, mocker):
2020-01-17 11:49:05 -06:00
mocker.patch("requests.get")
Octokit(
2020-01-17 11:49:05 -06:00
auth="basic", username="myuser", password="mypassword"
).oauth_authorizations.get_authorization(authorization_id=100)
2018-02-07 15:15:51 -06:00
requests.get.assert_called_once_with(
2020-01-17 11:49:05 -06:00
"https://api.github.com/authorizations/100",
2018-02-07 15:15:51 -06:00
params={},
headers=Octokit().headers,
2020-01-17 11:49:05 -06:00
auth=("myuser", "mypassword"),
2018-02-07 15:15:51 -06:00
)
def test_can_set_token_authentication(self):
2020-01-17 11:49:05 -06:00
sut = Octokit(auth="token", token="yippy")
assert sut.auth == "token"
assert sut.token == "yippy"
2018-02-07 15:15:51 -06:00
def test_cannot_set_token_authentication_with_out_required_data(self):
with pytest.raises(KeyError):
2020-01-17 11:49:05 -06:00
Octokit(auth="token")
2018-02-07 15:15:51 -06:00
with pytest.raises(AssertionError):
2020-01-17 11:49:05 -06:00
Octokit(auth="token", token="")
2018-02-07 15:15:51 -06:00
def test_token_auth_used_if_set(self, mocker):
2020-01-17 11:49:05 -06:00
mocker.patch("requests.get")
Octokit(auth="token", token="yak").oauth_authorizations.get_authorization(
authorization_id=100
)
headers = dict(ChainMap(Octokit().headers, {"Authorization": "token yak"}))
2018-02-07 15:15:51 -06:00
requests.get.assert_called_once_with(
2020-01-17 11:49:05 -06:00
"https://api.github.com/authorizations/100", params={}, headers=headers
2018-02-07 15:15:51 -06:00
)
def test_can_set_installation_authentication(self, mocker):
2020-01-17 11:49:05 -06:00
Request = namedtuple("Request", ["json"])
get = mocker.patch("requests.get")
get.return_value = Request(
json=lambda: [{"id": 13, "app_id": 1}, {"id": 37, "app_id": 42}]
)
post = mocker.patch("requests.post")
post.return_value = Request(
json=lambda: {
"token": "v1.1f699f1069f60",
"expires_at": "2016-07-11T22:14:10Z",
}
)
with open(os.path.join(os.path.dirname(__file__), "test.pem"), "r") as f:
2018-02-07 15:15:51 -06:00
private_key = f.read()
2020-01-17 11:49:05 -06:00
sut = Octokit(auth="installation", app_id="42", private_key=private_key)
assert sut.auth == "installation"
assert sut.token == "v1.1f699f1069f60"
assert sut.expires_at == "2016-07-11T22:14:10Z"
2018-02-07 15:15:51 -06:00
def test_cannot_set_installation_authentication_with_out_required_data(self):
2018-02-07 15:15:51 -06:00
with pytest.raises(KeyError):
2020-01-17 11:49:05 -06:00
Octokit(auth="installation")
2018-02-07 15:15:51 -06:00
with pytest.raises(AssertionError):
2020-01-17 11:49:05 -06:00
Octokit(auth="installation", app_id="")
2018-02-07 15:15:51 -06:00
with pytest.raises(KeyError):
2020-01-17 11:49:05 -06:00
Octokit(auth="installation", app_id="42")
2018-02-07 15:15:51 -06:00
with pytest.raises(AssertionError):
2020-01-17 11:49:05 -06:00
Octokit(auth="installation", app_id="42", private_key="")
2018-02-07 15:15:51 -06:00
def test_installation_token_is_used_if_set(self, mocker):
2020-01-17 11:49:05 -06:00
Request = namedtuple("Request", ["json"])
get = mocker.patch("requests.get")
get.return_value = Request(
json=lambda: [{"id": 13, "app_id": 1}, {"id": 37, "app_id": 42}]
)
post = mocker.patch("requests.post")
post.return_value = Request(
json=lambda: {
"token": "v1.1f699f1069f60",
"expires_at": "2016-07-11T22:14:10Z",
}
)
with open(os.path.join(os.path.dirname(__file__), "test.pem"), "r") as f:
2018-02-07 15:15:51 -06:00
private_key = f.read()
2020-01-17 11:49:05 -06:00
sut = Octokit(auth="installation", app_id="42", private_key=private_key)
assert sut.installation_id == 37
2020-01-17 11:49:05 -06:00
get = mocker.patch("requests.get")
sut.oauth_authorizations.get_authorization(authorization_id=100)
2018-02-07 15:15:51 -06:00
headers = {
2020-01-17 11:49:05 -06:00
"Content-Type": "application/json",
"Authorization": "token v1.1f699f1069f60",
"accept": "application/vnd.github.machine-man-preview+json",
2018-02-07 15:15:51 -06:00
}
requests.get.assert_called_once_with(
2020-01-17 11:49:05 -06:00
"https://api.github.com/authorizations/100", params={}, headers=headers
2018-02-07 15:15:51 -06:00
)
def test_cannot_set_app_authentication_with_out_required_data(self):
with pytest.raises(KeyError):
2020-01-17 11:49:05 -06:00
Octokit(auth="app")
with pytest.raises(AssertionError):
2020-01-17 11:49:05 -06:00
Octokit(auth="app", app_id="")
with pytest.raises(KeyError):
2020-01-17 11:49:05 -06:00
Octokit(auth="app", app_id=42)
with pytest.raises(AssertionError):
2020-01-17 11:49:05 -06:00
Octokit(auth="app", app_id=42, private_key="")
def test_can_set_app_authentication(self, mocker):
2020-01-17 11:49:05 -06:00
Request = namedtuple("Request", ["json"])
get = mocker.patch("requests.get")
get.return_value = Request(json=lambda: [{"id": 37}])
with open(os.path.join(os.path.dirname(__file__), "test.pem"), "r") as f:
private_key = f.read()
2020-01-17 11:49:05 -06:00
sut = Octokit(auth="app", app_id=42, private_key=private_key)
assert sut.auth == "app"
assert sut.jwt is not None
2018-06-01 13:34:58 -05:00
def test_can_get_with_app_authentication(self, mocker):
2020-01-17 11:49:05 -06:00
Request = namedtuple("Request", ["json"])
get = mocker.patch("requests.get")
get.return_value = Request(json=lambda: [{"id": 37}])
with open(os.path.join(os.path.dirname(__file__), "test.pem"), "r") as f:
2018-06-01 13:34:58 -05:00
private_key = f.read()
2020-01-17 11:49:05 -06:00
sut = Octokit(auth="app", app_id=42, private_key=private_key)
assert sut.apps.get_authenticated()
def test_can_make_unauthenticated_call(self, mocker):
2020-01-17 11:49:05 -06:00
mocker.patch("requests.get")
Octokit().users.list_followers_for_user(username="octokit")
requests.get.assert_called_once_with(
2020-01-17 11:49:05 -06:00
"https://api.github.com/users/octokit/followers",
2018-10-04 11:40:47 -05:00
headers={
2020-01-17 11:49:05 -06:00
"Content-Type": "application/json",
"accept": "application/vnd.github.v3+json",
2018-10-04 11:40:47 -05:00
},
2020-01-17 11:49:05 -06:00
params={"page": 1, "per_page": 30},
)