def simulate_github_webhook_call(self, event: str, data: dict): request = self.factory.post(reverse('webhooks:github'), data, format='json') hashed = hmac.new(bytes(os.environ['WEBHOOK_SECRET'], 'utf-8'), request.body, sha1) signature = 'sha1=' + hashed.hexdigest() request.META.update({ 'HTTP_X_HUB_SIGNATURE': signature, 'HTTP_X_GITHUB_EVENT': event, }) return github_webhook_receiver(request)
def test_github_webhook_receiver_successful_pull_request_opened(self): data = { 'repository': {'full_name': environ['GITHUB_TEST_REPO'], 'id': 49558751}, 'pull_request': {'number': 0}, 'action': 'opened' } request = self.factory.post('/webhooks/github', data, format='json') hashed = hmac.new(bytes(self.key, 'utf-8'), request.body, sha1) signature = 'sha1=' + hashed.hexdigest() request.META.update({ 'HTTP_X_HUB_SIGNATURE': signature, 'HTTP_X_GITHUB_EVENT': 'pull_request' }) # Mocking the GitHubMergeRequest as no such pull request exists and # resetting all the responders to stop unnecessary actions during # testing phase. with patch.object(GitHubMergeRequest, '__init__', return_value=None): response = github_webhook_receiver(request) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_github_webhook_receiver_no_signature(self): data = {'some-random-key': 'some-random-value'} request = self.factory.post('/webhooks/github', data, format='json') response = github_webhook_receiver(request) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_github_webhook_receiver_no_data_received(self): request = self.factory.post('/webhooks/github', format='json') response = github_webhook_receiver(request) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_github_webhook_receiver_signature_match_failed(self): data = {'some-random-key': 'some-random-value'} request = self.factory.post('/webhooks/github', data, format='json') request.META.update({'HTTP_X_HUB_SIGNATURE': 'sha1=somerandomvalue'}) response = github_webhook_receiver(request) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)