11import pytest
2- import json
32import time
4- from unittest .mock import patch , Mock , mock_open
5- from fastapi import APIRouter , Request , FastAPI
3+ from unittest .mock import patch , Mock
4+ from fastapi import FastAPI
65from fastapi .testclient import TestClient
76from githubapp import GitHubApp
87from githubapp .core import (
@@ -352,7 +351,7 @@ def test_client_with_payload_installation(self, mock_ghapi, mock_get_token):
352351
353352 github_app = GitHubApp ()
354353 github_app .payload = {"installation" : {"id" : 456 }}
355- result = github_app .client ()
354+ github_app .client ()
356355
357356 mock_get_token .assert_called_once_with (456 )
358357
@@ -363,27 +362,25 @@ def test_extract_payload_valid_json(self):
363362 github_app = GitHubApp (app )
364363 github_app .init_app (app )
365364
366- with TestClient (app ) as client :
365+ with TestClient (app ):
367366 # simplified; no real assertion here
368367 pass
369368
370369 def test_handle_request_missing_content_type (self ):
371370 app = FastAPI ()
372- github_app = GitHubApp (app )
373- github_app .init_app (app )
371+ GitHubApp (app )
374372
375373 with TestClient (app ) as client :
376- response = client .post ("/" , json = {"test" : "data" })
374+ response = client .post ("/webhooks/github/ " , json = {"test" : "data" })
377375 assert response .status_code == 400
378376
379377 def test_handle_request_missing_github_event_header (self ):
380378 app = FastAPI ()
381- github_app = GitHubApp (app )
382- github_app .init_app (app )
379+ GitHubApp (app )
383380
384381 with TestClient (app ) as client :
385382 response = client .post (
386- "/" ,
383+ "/webhooks/github/ " ,
387384 json = {"installation" : {"id" : 123 }},
388385 headers = {"Content-Type" : "application/json" },
389386 )
@@ -395,17 +392,16 @@ def test_handle_request_valid_webhook(self):
395392 app ,
396393 github_app_id = 123 ,
397394 github_app_key = b"test_key" ,
398- github_app_secret = False , # Disable signature verification for testing
395+ github_app_secret = False ,
399396 )
400- github_app .init_app (app )
401397
402398 @github_app .on ("issues.opened" )
403399 def test_handler ():
404400 return "handled"
405401
406402 with TestClient (app ) as client :
407403 response = client .post (
408- "/" ,
404+ "/webhooks/github/ " ,
409405 json = {
410406 "action" : "opened" ,
411407 "installation" : {"id" : 123 },
@@ -427,17 +423,16 @@ def test_handle_request_call_async_hook_function(self):
427423 app ,
428424 github_app_id = 123 ,
429425 github_app_key = b"test_key" ,
430- github_app_secret = False , # Disable signature verification for testing
426+ github_app_secret = False ,
431427 )
432- github_app .init_app (app )
433428
434429 @github_app .on ("issues.opened" )
435430 async def async_test_handler ():
436431 return "handled"
437432
438433 with TestClient (app ) as client :
439434 response = client .post (
440- "/" ,
435+ "/webhooks/github/ " ,
441436 json = {
442437 "action" : "opened" ,
443438 "installation" : {"id" : 123 },
@@ -457,35 +452,35 @@ async def async_test_handler():
457452class TestGitHubAppWebhookSignatureVerification :
458453 def test_signature_verification_disabled (self ):
459454 app = FastAPI ()
460- github_app = GitHubApp (
461- app , github_app_secret = False # Explicitly disable verification
462- )
463- # Test that webhooks work without signature headers when verification is disabled
455+ GitHubApp (app , github_app_secret = False )
456+ # Webhooks work without signature headers when verification is disabled
464457
465458 def test_signature_verification_sha256_valid (self ):
466459 app = FastAPI ()
467- github_app = GitHubApp (app , github_app_secret = b"test_secret" )
468- # Test that valid SHA256 signatures are accepted
460+ GitHubApp (app , github_app_secret = b"test_secret" )
461+ # Valid SHA256 signatures are accepted
469462
470463 def test_signature_verification_sha256_invalid (self ):
471464 app = FastAPI ()
472- github_app = GitHubApp (app , github_app_secret = b"test_secret" )
473- # Test that invalid SHA256 signatures are rejected
465+ GitHubApp (app , github_app_secret = b"test_secret" )
466+ # Invalid SHA256 signatures are rejected
474467
475468 def test_signature_verification_sha1_fallback (self ):
476469 app = FastAPI ()
477- github_app = GitHubApp (app , github_app_secret = b"test_secret" )
478- # Test that SHA1 signatures work when SHA256 is not present
470+ GitHubApp (app , github_app_secret = b"test_secret" )
471+ # SHA1 signatures work when SHA256 is not present
479472
480473
481474class TestGitHubAppIntegration :
482475 def test_full_webhook_flow (self ):
483476 """Test complete webhook handling flow"""
484477 app = FastAPI ()
485478 github_app = GitHubApp (
486- app , github_app_id = 123 , github_app_key = b"test_key" , github_app_secret = False
479+ app ,
480+ github_app_id = 123 ,
481+ github_app_key = b"test_key" ,
482+ github_app_secret = False ,
487483 )
488- github_app .init_app (app )
489484
490485 results = []
491486
@@ -501,7 +496,7 @@ def handle_opened_issue():
501496
502497 with TestClient (app ) as client :
503498 response = client .post (
504- "/" ,
499+ "/webhooks/github/ " ,
505500 json = {
506501 "action" : "opened" ,
507502 "installation" : {"id" : 123 },
@@ -523,14 +518,16 @@ def handle_opened_issue():
523518 def test_no_matching_handlers (self ):
524519 """Test webhook with no matching handlers"""
525520 app = FastAPI ()
526- github_app = GitHubApp (
527- app , github_app_id = 123 , github_app_key = b"test_key" , github_app_secret = False
521+ GitHubApp (
522+ app ,
523+ github_app_id = 123 ,
524+ github_app_key = b"test_key" ,
525+ github_app_secret = False ,
528526 )
529- github_app .init_app (app )
530527
531528 with TestClient (app ) as client :
532529 response = client .post (
533- "/" ,
530+ "/webhooks/github/ " ,
534531 json = {
535532 "action" : "closed" ,
536533 "installation" : {"id" : 123 },
@@ -551,17 +548,19 @@ def test_handler_exception_returns_500(self):
551548 """Test that exceptions in handlers return 500"""
552549 app = FastAPI ()
553550 github_app = GitHubApp (
554- app , github_app_id = 123 , github_app_key = b"test_key" , github_app_secret = False
551+ app ,
552+ github_app_id = 123 ,
553+ github_app_key = b"test_key" ,
554+ github_app_secret = False ,
555555 )
556- github_app .init_app (app )
557556
558557 @github_app .on ("issues.opened" )
559558 def failing_handler ():
560559 raise ValueError ("Something went wrong" )
561560
562561 with TestClient (app ) as client :
563562 response = client .post (
564- "/" ,
563+ "/webhooks/github/ " ,
565564 json = {
566565 "action" : "opened" ,
567566 "installation" : {"id" : 123 },
0 commit comments