Skip to content

Commit e510c9a

Browse files
committed
test: add and update tests for webhook and error handling
- Add tests for invalid and missing signatures in webhook requests - Add tests for method not allowed errors on webhook endpoint - Update integration.json structure test to match actual response - Ensure comprehensive test coverage for error handling and webhook processing
1 parent 168533a commit e510c9a

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

tests/__init__.py

Whitespace-only changes.

tests/test_config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
import os
3+
from dotenv import load_dotenv
4+
5+
class TestEnvironmentConfig(unittest.TestCase):
6+
def setUp(self):
7+
load_dotenv()
8+
9+
def test_environment_variables_exist(self):
10+
"""Test that required environment variables are set"""
11+
self.assertIsNotNone(os.getenv('MY_GITHUB_SECRET'))
12+
self.assertIsNotNone(os.getenv('CHANNEL_ID'))

tests/test_error_handling.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import unittest
2+
import json
3+
from app import app
4+
5+
class TestErrorHandling(unittest.TestCase):
6+
def setUp(self):
7+
self.app = app.test_client()
8+
9+
def test_invalid_signature(self):
10+
"""Test webhook request with invalid signature"""
11+
payload = {
12+
"head_commit": {
13+
"message": "test commit",
14+
"author": {"name": "Test User"}
15+
}
16+
}
17+
headers = {
18+
'X-Hub-Signature-256': 'invalid_signature',
19+
'Content-Type': 'application/json'
20+
}
21+
response = self.app.post(
22+
'/github-webhook',
23+
data=json.dumps(payload),
24+
headers=headers
25+
)
26+
self.assertEqual(response.status_code, 401)
27+
self.assertIn('Invalid signature', response.get_json()['error'])
28+
29+
def test_missing_signature(self):
30+
"""Test webhook request without signature header"""
31+
response = self.app.post(
32+
'/github-webhook',
33+
data=json.dumps({}),
34+
headers={'Content-Type': 'application/json'}
35+
)
36+
self.assertEqual(response.status_code, 401)
37+
self.assertIn('No signature provided', response.get_json()['error'])
38+
39+
def test_method_not_allowed(self):
40+
"""Test GET request to webhook endpoint"""
41+
response = self.app.get('/github-webhook')
42+
self.assertEqual(response.status_code, 405)

tests/test_intergration_json.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import unittest
2+
from app import app
3+
4+
class TestIntegrationJSON(unittest.TestCase):
5+
def setUp(self):
6+
self.app = app.test_client()
7+
8+
def test_integration_json_structure(self):
9+
"""Test integration.json returns correct structure"""
10+
response = self.app.get('/integration.json')
11+
data = response.get_json()
12+
13+
14+
self.assertEqual(response.status_code, 200)
15+
self.assertIn('data', data)
16+
self.assertIn('descriptions', data['data'])
17+
self.assertIn('settings', data['data'])
18+
self.assertIn('target_url', data['data'])
19+
20+
# More specific assertions
21+
descriptions = data['data']['descriptions']
22+
self.assertIn('app_name', descriptions)
23+
self.assertIn('app_description', descriptions)
24+
self.assertIn('app_logo', descriptions)
25+
self.assertIn('app_url', descriptions)

tests/test_load.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
import concurrent.futures
3+
import requests
4+
5+
class TestLoad(unittest.TestCase):
6+
def setUp(self):
7+
self.base_url = "https://github-commit-monitor-4a53c549b932.herokuapp.com"
8+
9+
def test_concurrent_webhook_requests(self):
10+
"""Test handling multiple concurrent webhook requests"""
11+
def make_request():
12+
return requests.get(f"{self.base_url}/integration.json")
13+
14+
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
15+
futures = [executor.submit(make_request) for _ in range(10)]
16+
for future in concurrent.futures.as_completed(futures):
17+
self.assertEqual(future.result().status_code, 200)

tests/test_webhook.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import unittest
2+
import json
3+
from app import app
4+
import hmac
5+
import hashlib
6+
import os
7+
from dotenv import load_dotenv
8+
9+
class TestWebhook(unittest.TestCase):
10+
def setUp(self):
11+
self.app = app.test_client()
12+
load_dotenv()
13+
self.github_secret = os.getenv('MY_GITHUB_SECRET')
14+
15+
def generate_signature(self, payload):
16+
"""Generate GitHub signature for payload"""
17+
return 'sha256=' + hmac.new(
18+
self.github_secret.encode(),
19+
payload.encode(),
20+
hashlib.sha256
21+
).hexdigest()
22+
23+
def test_valid_webhook_request(self):
24+
"""Test successful webhook processing"""
25+
payload = {
26+
"head_commit": {
27+
"message": "test: add new feature",
28+
"author": {"name": "Test User"},
29+
},
30+
"repository": {"name": "test-repo"}
31+
}
32+
33+
headers = {
34+
'X-Hub-Signature-256': self.generate_signature(json.dumps(payload)),
35+
'Content-Type': 'application/json'
36+
}
37+
38+
response = self.app.post(
39+
'/github-webhook',
40+
data=json.dumps(payload),
41+
headers=headers
42+
)
43+
44+
self.assertEqual(response.status_code, 200)

0 commit comments

Comments
 (0)