<s> from __future__ import absolute_import <EOL> import hmac <EOL> from django . core . urlresolvers import reverse <EOL> from exam import fixture <EOL> from hashlib import sha256 <EOL> from mock import patch <EOL> from sentry . models import ProjectOption <EOL> from sentry . testutils import TestCase <EOL> from sentry . utils import json <EOL> class ReleaseWebhookTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( ReleaseWebhookTest , self ) . setUp ( ) <EOL> self . organization = self . create_organization ( ) <EOL> self . team = self . create_team ( organization = self . organization ) <EOL> self . project = self . create_project ( team = self . team ) <EOL> self . token = '<STR_LIT>' <EOL> self . signature = hmac . new ( <EOL> key = self . token , <EOL> msg = '<STR_LIT>' . format ( self . project . id ) , <EOL> digestmod = sha256 , <EOL> ) . hexdigest ( ) <EOL> ProjectOption . objects . set_value ( <EOL> self . project , '<STR_LIT>' , self . token ) <EOL> @ fixture <EOL> def path ( self ) : <EOL> return reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . signature , <EOL> } ) <EOL> def test_invalid_signature ( self ) : <EOL> path = reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> resp = self . client . post ( path ) <EOL> assert resp . status_code == <NUM_LIT> <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_valid_signature ( self , mock_plugin_get ) : <EOL> MockPlugin = mock_plugin_get . return_value <EOL> MockPlugin . is_enabled . return_value = True <EOL> MockReleaseHook = MockPlugin . get_release_hook . return_value <EOL> resp = self . client . post ( self . path ) <EOL> assert resp . status_code == <NUM_LIT> <EOL> mock_plugin_get . assert_called_once_with ( '<STR_LIT>' ) <EOL> MockPlugin . get_release_hook . assert_called_once_with ( ) <EOL> MockReleaseHook . assert_called_once_with ( self . project ) <EOL> assert MockReleaseHook . return_value . handle . call_count is <NUM_LIT:1> <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_disabled_plugin ( self , mock_plugin_get ) : <EOL> MockPlugin = mock_plugin_get . return_value <EOL> MockPlugin . is_enabled . return_value = False <EOL> resp = self . client . post ( self . path ) <EOL> assert resp . status_code == <NUM_LIT> <EOL> mock_plugin_get . assert_called_once_with ( '<STR_LIT>' ) <EOL> assert not MockPlugin . get_release_hook . called <EOL> class BuiltinReleaseWebhookTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( BuiltinReleaseWebhookTest , self ) . setUp ( ) <EOL> self . organization = self . create_organization ( ) <EOL> self . team = self . create_team ( organization = self . organization ) <EOL> self . project = self . create_project ( team = self . team ) <EOL> self . token = '<STR_LIT>' <EOL> self . signature = hmac . new ( <EOL> key = self . token , <EOL> msg = '<STR_LIT>' . format ( self . project . id ) , <EOL> digestmod = sha256 , <EOL> ) . hexdigest ( ) <EOL> ProjectOption . objects . set_value ( <EOL> self . project , '<STR_LIT>' , self . token ) <EOL> @ fixture <EOL> def path ( self ) : <EOL> return reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : self . project . id , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . signature , <EOL> } ) <EOL> def test_invalid_params ( self ) : <EOL> resp = self . client . post ( self . path , content_type = '<STR_LIT:application/json>' ) <EOL> assert resp . status_code == <NUM_LIT> <EOL> def test_valid_params ( self ) : <EOL> resp = self . client . post ( self . path , data = json . dumps ( { <EOL> '<STR_LIT:version>' : '<STR_LIT:a>' , <EOL> } ) , content_type = '<STR_LIT:application/json>' ) <EOL> assert resp . status_code == <NUM_LIT> , resp . content <EOL> data = json . loads ( resp . content ) <EOL> assert data [ '<STR_LIT:version>' ] == '<STR_LIT:a>' </s>
