forked from kiwitcms/Kiwi
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes kiwitcms#1612 - Add test for tcms.utils.github
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# Copyright (c) 2019 Alexander Todorov <[email protected]> | ||
|
||
import hmac | ||
import hashlib | ||
|
||
from http import HTTPStatus | ||
|
||
from django import test | ||
|
@@ -8,6 +11,19 @@ | |
from tcms.utils import github | ||
|
||
|
||
class CalculateSignatureTestCase(test.TestCase): | ||
def test_string_arguments(self): | ||
with self.assertRaisesRegex(TypeError, 'key: expected bytes or bytearray, but got \'str\''): | ||
github.calculate_signature('secret', 'content') | ||
|
||
def test_return_format(self): | ||
sha1 = hmac.new(key=b'secret', | ||
msg=b'content', | ||
digestmod=hashlib.sha1).hexdigest() | ||
self.assertEqual(github.calculate_signature(b'secret', b'content'), | ||
f'sha1={sha1}') | ||
|
||
|
||
class VerifySignatureTestCase(test.TestCase): | ||
def setUp(self): | ||
self.factory = test.RequestFactory() | ||
|
@@ -27,3 +43,13 @@ def test_invalid_signature_header(self): | |
|
||
self.assertIsInstance(result, HttpResponseForbidden) | ||
self.assertEqual(HTTPStatus.FORBIDDEN, result.status_code) | ||
|
||
def test_valid_signature_header(self): | ||
request = self.factory.post(self.url) | ||
sha1 = hmac.new(key=b'secret', | ||
msg=request.body, | ||
digestmod=hashlib.sha1).hexdigest() | ||
request.META['HTTP_X_HUB_SIGNATURE'] = f'sha1={sha1}' | ||
result = github.verify_signature(request, b'secret') | ||
self.assertIsInstance(result, bool) | ||
self.assertTrue(result) |