Skip to content

Commit

Permalink
Fixes kiwitcms#1612 - Add test for tcms.utils.github
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed Nov 12, 2020
1 parent 54fcd23 commit e70af65
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tcms/utils/tests/test_github.py
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
Expand All @@ -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()
Expand All @@ -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)

0 comments on commit e70af65

Please sign in to comment.