def test_not_modified_when_ref_not_master(self, mock): data = b'{"ref": "refs/heads/develop"}' request = self.FACTORY.post("/", data=data, content_type="application/json") request.META["HTTP_X_HUB_SIGNATURE"] = compute_signature(data, self.KEY) request.META["HTTP_X_GITHUB_EVENT"] = "push" response = webhook_handler(request) self.assertContains(response, "Event ignored") self.assertEqual(mock.call_count, 0)
def test_push_with_valid_signature(self, mock): data = b'{"ref": "refs/heads/master"}' request = self.FACTORY.post("/", data=data, content_type="application/json") request.META["HTTP_X_HUB_SIGNATURE"] = compute_signature(data, self.KEY) request.META["HTTP_X_GITHUB_EVENT"] = "push" response = webhook_handler(request) self.assertEqual(response.status_code, 200) self.assertEqual(mock.call_count, 1)
def test_not_modified_when_ref_not_master(self, mock): data = b'{"ref": "refs/heads/develop"}' request = self.FACTORY.post('/', data=data, content_type='application/json') request.META['HTTP_X_HUB_SIGNATURE'] = compute_signature(data, self.KEY) request.META['HTTP_X_GITHUB_EVENT'] = 'push' response = webhook_handler(request) self.assertContains(response, 'Event ignored') self.assertEqual(mock.call_count, 0)
def test_push_with_valid_signature(self, mock): data = b'{"ref": "refs/heads/master"}' request = self.FACTORY.post('/', data=data, content_type='application/json') request.META['HTTP_X_HUB_SIGNATURE'] = compute_signature(data, self.KEY) request.META['HTTP_X_GITHUB_EVENT'] = 'push' response = webhook_handler(request) self.assertEqual(response.status_code, 200) self.assertEqual(mock.call_count, 1)
def test_push_with_invalid_json(self, mock): data = b'<xml-is-not-json/>' request = self.FACTORY.post("/", data=data, content_type="application/json") request.META["HTTP_X_HUB_SIGNATURE"] = compute_signature(data, self.KEY) request.META["HTTP_X_GITHUB_EVENT"] = "push" response = webhook_handler(request) self.assertEqual(response.status_code, 400) self.assertTrue(b"malformed" in response.content.lower()) self.assertEqual(mock.call_count, 0)
def test_push_with_invalid_json(self, mock): data = b'<xml-is-not-json/>' request = self.FACTORY.post('/', data=data, content_type='application/json') request.META['HTTP_X_HUB_SIGNATURE'] = compute_signature(data, self.KEY) request.META['HTTP_X_GITHUB_EVENT'] = 'push' response = webhook_handler(request) self.assertEqual(response.status_code, 400) self.assertTrue(b'malformed' in response.content.lower()) self.assertEqual(mock.call_count, 0)
def test_not_modified_when_event_not_push(self, mock): data = b'{"ref": "refs/heads/master"}' request = self.FACTORY.post("/", data=data, content_type="application/json") request.META["HTTP_X_HUB_SIGNATURE"] = compute_signature( data, self.KEY) request.META["HTTP_X_GITHUB_EVENT"] = "ping" response = webhook_handler(request) self.assertContains(response, "Event ignored") self.assertEqual(mock.call_count, 0)
def test_push_with_invalid_signature(self, mock): data = b'{"ref": "refs/heads/master"}' request = self.FACTORY.post("/", data=data, content_type="application/json") request.META["HTTP_X_HUB_SIGNATURE"] = "invalid" request.META["HTTP_X_GITHUB_EVENT"] = "push" response = webhook_handler(request) self.assertEqual(response.status_code, 400) self.assertTrue(b"invalid" in response.content.lower()) self.assertEqual(mock.call_count, 0)
def test_get_not_allowed(self, mock): request = self.FACTORY.get("/") response = webhook_handler(request) self.assertEqual(response.status_code, 405) self.assertEqual(mock.call_count, 0)