From 64c521f55bede063a8a50cfaaa11569a08bc42f8 Mon Sep 17 00:00:00 2001 From: Marvin Pohl Date: Fri, 20 Dec 2019 10:44:52 +0100 Subject: [PATCH] Added minimal unittests for GiteaAuth. --- buildbot_gitea/test/test_auth.py | 88 ++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 buildbot_gitea/test/test_auth.py diff --git a/buildbot_gitea/test/test_auth.py b/buildbot_gitea/test/test_auth.py new file mode 100644 index 0000000..d285895 --- /dev/null +++ b/buildbot_gitea/test/test_auth.py @@ -0,0 +1,88 @@ +import json +import mock +from buildbot.process.properties import Secret +from buildbot.test.util.config import ConfigErrorsMixin +from buildbot.test.util.misc import TestReactorMixin +from buildbot.test.util import www +from twisted.internet import defer +from twisted.trial import unittest +from buildbot.secrets.manager import SecretManager +from buildbot.test.fake.secrets import FakeSecretStorage + +from buildbot_gitea.auth import GiteaAuth + +try: + import requests +except ImportError: + requests = None + + +class FakeResponse: + + def __init__(self, _json): + self.json = lambda: _json + self.content = json.dumps(_json) + + def raise_for_status(self): + pass + + +class TestGiteaAuth(TestReactorMixin, www.WwwTestMixin, ConfigErrorsMixin, + unittest.TestCase): + def setUp(self): + self.setUpTestReactor() + if requests is None: + raise unittest.SkipTest("Need to install requests to test oauth2") + + self.patch(requests, 'request', mock.Mock(spec=requests.request)) + self.patch(requests, 'post', mock.Mock(spec=requests.post)) + self.patch(requests, 'get', mock.Mock(spec=requests.get)) + + self.giteaAuth = GiteaAuth( + 'https://gitea.test', + 'client-id', + 'client-secret') + self._master = master = self.make_master( + url='h:/a/b/', auth=self.giteaAuth) + self.giteaAuth.reconfigAuth(master, master.config) + + self.giteaAuth_secret = GiteaAuth( + 'https://gitea.test', + Secret("client-id"), + Secret("client-secret")) + self._master = master = self.make_master( + url='h:/a/b/', auth=self.giteaAuth_secret) + fake_storage_service = FakeSecretStorage() + fake_storage_service.reconfigService( + secretdict={ + "client-id": "secretClientId", + "client-secret": "secretClientSecret" + }) + secret_service = SecretManager() + secret_service.services = [fake_storage_service] + secret_service.setServiceParent(self._master) + self.giteaAuth_secret.reconfigAuth(master, master.config) + + @defer.inlineCallbacks + def test_getGiteaLoginURL(self): + res = yield self.giteaAuth.getLoginURL('http://redir') + exp = ("https://gitea.test/login/oauth/authorize?client_id=client-id&" + "redirect_uri=h%3A%2Fa%2Fb%2Fauth%2Flogin&response_type=code&" + "state=redirect%3Dhttp%253A%252F%252Fredir") + self.assertEqual(res, exp) + res = yield self.giteaAuth.getLoginURL(None) + exp = ("https://gitea.test/login/oauth/authorize?client_id=client-id&" + "redirect_uri=h%3A%2Fa%2Fb%2Fauth%2Flogin&response_type=code") + self.assertEqual(res, exp) + + @defer.inlineCallbacks + def test_getGiteaLoginURL_with_secret(self): + res = yield self.giteaAuth_secret.getLoginURL('http://redir') + exp = ("https://gitea.test/login/oauth/authorize?client_id=secretClientId&" + "redirect_uri=h%3A%2Fa%2Fb%2Fauth%2Flogin&response_type=code&" + "state=redirect%3Dhttp%253A%252F%252Fredir") + self.assertEqual(res, exp) + res = yield self.giteaAuth_secret.getLoginURL(None) + exp = ("https://gitea.test/login/oauth/authorize?client_id=secretClientId&" + "redirect_uri=h%3A%2Fa%2Fb%2Fauth%2Flogin&response_type=code") + self.assertEqual(res, exp)