1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/tests/unit/plugins/lookup/test_github_app_access_token.py
weisheng-p 3318034403
Add github_app_access_token lookup plugin (#7761)
* Add github_app_access_token lookup plugin

* Fix a typo in short_description

* Remove unused MockOpenUrl

* Fix MockJWT to be used on jwt_instance instead

* Fix a bunch of pep8 and pylint issue

* Remove JWT from requirements, also default jwt_instance and jwk_from_pem so they can be mocked

* Update version added

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update git reference in doc

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/lookup/github_app_access_token.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Expose token expiry as a configurable option

* Update BOTMETA.yml

* Update documentation

* Update example with var, so it is more readable

Co-authored-by: Felix Fontein <felix@fontein.de>

* Apply suggestions from code review

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
2023-12-31 15:20:57 +01:00

52 lines
1.7 KiB
Python

# -*- coding: utf-8 -*-
# Copyright (c) 2023, Poh Wei Sheng <weisheng-p@hotmail.sg>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import json
from ansible_collections.community.general.tests.unit.compat import unittest
from ansible_collections.community.general.tests.unit.compat.mock import (
patch,
MagicMock,
mock_open
)
from ansible.plugins.loader import lookup_loader
class MockJWT(MagicMock):
def encode(self, payload, key, alg):
return 'Foobar'
class MockResponse(MagicMock):
response_token = 'Bar'
def read(self):
return json.dumps({
"token": self.response_token,
}).encode('utf-8')
class TestLookupModule(unittest.TestCase):
def test_get_token(self):
with patch.multiple("ansible_collections.community.general.plugins.lookup.github_app_access_token",
open=mock_open(read_data="foo_bar"),
open_url=MagicMock(return_value=MockResponse()),
jwk_from_pem=MagicMock(return_value='private_key'),
jwt_instance=MockJWT(),
HAS_JWT=True):
lookup = lookup_loader.get('community.general.github_app_access_token')
self.assertListEqual(
[MockResponse.response_token],
lookup.run(
[],
key_path="key",
app_id="app_id",
installation_id="installation_id",
token_expiry=600
)
)