<s> import hashlib <EOL> import hmac <EOL> import json <EOL> from base64 import b64encode <EOL> from django . conf import settings <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . test import TestCase <EOL> from django . test . utils import override_settings <EOL> from djrill . compat import b <EOL> from djrill . signals import webhook_event <EOL> class DjrillWebhookSecretMixinTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_missing_secret ( self ) : <EOL> with self . assertRaises ( ImproperlyConfigured ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> @ override_settings ( DJRILL_WEBHOOK_SECRET = '<STR_LIT>' ) <EOL> def test_incorrect_secret ( self ) : <EOL> response = self . client . head ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> @ override_settings ( DJRILL_WEBHOOK_SECRET = '<STR_LIT>' ) <EOL> def test_default_secret_name ( self ) : <EOL> response = self . client . head ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> @ override_settings ( DJRILL_WEBHOOK_SECRET = '<STR_LIT>' , DJRILL_WEBHOOK_SECRET_NAME = '<STR_LIT>' ) <EOL> def test_custom_secret_name ( self ) : <EOL> response = self . client . head ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> @ override_settings ( DJRILL_WEBHOOK_SECRET = '<STR_LIT>' , <EOL> DJRILL_WEBHOOK_SIGNATURE_KEY = "<STR_LIT>" ) <EOL> class DjrillWebhookSignatureMixinTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_incorrect_settings ( self ) : <EOL> with self . assertRaises ( ImproperlyConfigured ) : <EOL> self . client . post ( '<STR_LIT>' ) <EOL> @ override_settings ( DJRILL_WEBHOOK_URL = "<STR_LIT>" , <EOL> DJRILL_WEBHOOK_SIGNATURE_KEY = "<STR_LIT>" ) <EOL> def test_unauthorized ( self ) : <EOL> response = self . client . post ( settings . DJRILL_WEBHOOK_URL ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> @ override_settings ( DJRILL_WEBHOOK_URL = "<STR_LIT>" ) <EOL> def test_signature ( self ) : <EOL> signature = hmac . new ( key = b ( settings . DJRILL_WEBHOOK_SIGNATURE_KEY ) , <EOL> msg = b ( settings . DJRILL_WEBHOOK_URL + "<STR_LIT>" ) , <EOL> digestmod = hashlib . sha1 ) <EOL> hash_string = b64encode ( signature . digest ( ) ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT>" : "<STR_LIT>" } , <EOL> ** { "<STR_LIT>" : hash_string } ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> @ override_settings ( DJRILL_WEBHOOK_SECRET = '<STR_LIT>' ) <EOL> class DjrillWebhookViewTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_head_request ( self ) : <EOL> response = self . client . head ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_post_request_invalid_json ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_post_request_valid_json ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : json . dumps ( [ { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : { } } ] ) <EOL> } ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_webhook_send_signal ( self ) : <EOL> self . signal_received_count = <NUM_LIT:0> <EOL> test_event = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : { } } <EOL> def my_callback ( sender , event_type , data , ** kwargs ) : <EOL> self . signal_received_count += <NUM_LIT:1> <EOL> self . assertEqual ( event_type , '<STR_LIT>' ) <EOL> self . assertEqual ( data , test_event ) <EOL> try : <EOL> webhook_event . connect ( my_callback , weak = False ) <EOL> response = self . client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : json . dumps ( [ test_event ] ) <EOL> } ) <EOL> finally : <EOL> webhook_event . disconnect ( my_callback ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( self . signal_received_count , <NUM_LIT:1> ) <EOL> def test_webhook_sync_event ( self ) : <EOL> self . signal_received_count = <NUM_LIT:0> <EOL> test_event = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT:action>" : "<STR_LIT>" } <EOL> def my_callback ( sender , event_type , data , ** kwargs ) : <EOL> self . signal_received_count += <NUM_LIT:1> <EOL> self . assertEqual ( event_type , '<STR_LIT>' ) <EOL> self . assertEqual ( data , test_event ) <EOL> try : <EOL> webhook_event . connect ( my_callback , weak = False ) <EOL> response = self . client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : json . dumps ( [ test_event ] ) <EOL> } ) <EOL> finally : <EOL> webhook_event . disconnect ( my_callback ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( self . signal_received_count , <NUM_LIT:1> ) </s>
