Skip to content

Commit 7b0ce0e

Browse files
committed
Add integration test for passsing credentials via Python code
1 parent 71082cb commit 7b0ce0e

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

.github/workflows/integration.yml

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ jobs:
9191
_EOF_
9292
9393
make verify-integration
94+
make verify-credentials-via-python
9495
9596
- name: Verify tcms-api can communicate over Kerberos
9697
if: matrix.os == 'ubuntu-latest' && matrix.gssapi == 'with'

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ run-services:
4646
verify-integration:
4747
PYTHONPATH=. python -m coverage run --source tcms_api ./tests/krb5/integration_test.py
4848

49+
.PHONY: verify-credentials-via-python
50+
verify-credentials-via-python:
51+
PYTHONPATH=. python -m coverage run --source tcms_api ./tests/krb5/python_credentials_test.py
52+
4953
.PHONY: verify-curl-with-kerberos
5054
verify-curl-with-kerberos:
5155
# make sure curl supports Negotiate authentication

tests/krb5/kiwitcms_kerberos/db_init.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.contrib.auth.models import User
2+
from django.contrib.auth.models import Permission
23

34
from tcms.management.models import Classification
45
from tcms.utils.permissions import initiate_user_with_default_setups
@@ -16,3 +17,17 @@
1617

1718
# this is used inside integration test
1819
Classification.objects.create(name="test-products")
20+
21+
# account only to verify credentials passed
22+
# via Python source code, not config file
23+
developer = User.objects.create(
24+
username="kiwitcms-developer",
25+
26+
is_active=True,
27+
)
28+
developer.set_password("hack-me")
29+
developer.save()
30+
initiate_user_with_default_setups(developer)
31+
developer.user_permissions.add(
32+
Permission.objects.get(content_type__app_label="auth", codename="view_user")
33+
)

tests/krb5/python_credentials_test.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
3+
#
4+
# Copyright (c) 2024 Kiwi TCMS project. All rights reserved.
5+
# Author: Alexander Todorov <[email protected]>
6+
#
7+
8+
import ssl
9+
import unittest
10+
from unittest.mock import patch
11+
12+
import requests
13+
from tcms_api import TCMS
14+
15+
16+
try:
17+
_create_unverified_https_context = ssl._create_unverified_context
18+
except AttributeError:
19+
# Legacy Python that doesn't verify HTTPS certificates by default
20+
pass
21+
else:
22+
# Handle target environment that doesn't support HTTPS verification
23+
ssl._create_default_https_context = _create_unverified_https_context
24+
25+
26+
class DoNotVerifySSLSession(requests.sessions.Session):
27+
def __init__(self):
28+
super().__init__()
29+
self.verify = False
30+
31+
def get(self, url, **kwargs):
32+
kwargs.setdefault("verify", False)
33+
return super().get(url, **kwargs)
34+
35+
36+
class PythonCredentialsTestCase(unittest.TestCase):
37+
@classmethod
38+
def setUpClass(cls):
39+
cls.rpc = TCMS(
40+
url="https://web.kiwitcms.org:8443/json-rpc/",
41+
username="kiwitcms-developer",
42+
password="hack-me",
43+
).exec
44+
45+
def test_passing_credentials_via_python_works(self):
46+
with patch("requests.sessions.Session") as session:
47+
session.return_value = DoNotVerifySSLSession()
48+
49+
result = self.rpc.User.filter()[0]
50+
51+
# this is from config file
52+
self.assertNotEqual(result["username"], "kiwitcms-bot")
53+
54+
# this is specified in setUpClass() above
55+
self.assertEqual(result["username"], "kiwitcms-developer")
56+
57+
58+
if __name__ == "__main__":
59+
unittest.main()

0 commit comments

Comments
 (0)