def test_parse_github_payload_tag(self): """ Tests that a db.Commit object is created when passed a create event example github webhook payload """ # Set up fake payload r = FakeRequest() r.data = payload_tag # Unknown repos should raise UnknownRepoError with self.assertRaises(UnknownRepoError): GithubListener.parse_github_payload(r) # There should be not tag added to the commit, but we should expect # the returned model.Commit to be correct r.data = r.data.replace('"name": "governor"', '"name": "adsws"') r.data = r.data.replace('refs/tags', 'heads/commits') print r.data c = GithubListener.parse_github_payload(r) self.assertEqual( c.commit_hash, '2a047ead58a3a87b46388ac67fe08c944c3230e0' ) self.assertIsNone(c.tag) self.assertEqual(c.author, 'adsabs') self.assertEqual(c.repository, 'adsws') self.assertEqual( c.message, "First commit." ) self.assertEqual( c.timestamp, datetime.datetime(2015, 8, 12, 16, 18, 57, tzinfo=tzoffset(None, -4*60*60)) ) db.session.add(c) db.session.commit() # Now put the tag, the tag should be added to the commit r.data = r.data.replace('heads/commits', 'refs/tags') c2 = GithubListener.parse_github_payload(r) self.assertEqual( c2.commit_hash, '2a047ead58a3a87b46388ac67fe08c944c3230e0' ) self.assertEqual( c2.tag, 'v1.0.0' ) self.assertEqual(c.id, c2.id) # Re-sending a previously saved commit payload should return that # previously saved commit db.session.add(c) db.session.commit() self.assertEqual(len(db.session.query(Commit).all()), 1) c3 = GithubListener.parse_github_payload(r) db.session.add(c3) db.session.commit() self.assertEqual(len(db.session.query(Commit).all()), 1)
def test_verify_signature(self): """ Ensures that the signature is validated against the github algorithim found at https://github.com/github/github-services/blob/f3bb3dd780feb6318c42b2db064ed6d481b70a1f/lib/service/http_helper.rb#L77 """ r = FakeRequest() r.data = '''{"payload": "unittest"}''' h = hmac.new( self.app.config['GITHUB_SECRET'], msg=r.data, digestmod=hashlib.sha1, ).hexdigest() r.headers = { 'content-type': 'application/json', self.app.config['GITHUB_SIGNATURE_HEADER']: "sha1={}".format(h) } self.assertTrue(GithubListener.verify_github_signature(r)) with self.assertRaises(InvalidSignature): r.data = '' GithubListener.verify_github_signature(r) with self.assertRaises(NoSignatureInfo): r.headers = {} GithubListener.verify_github_signature(r)
def test_parse_github_payload(self): """ Tests that a db.Commit object is created when passed an example github webhook payload """ # Set up fake payload r = FakeRequest() r.data = payload # Unknown repos should raise UnknownRepoError with self.assertRaises(UnknownRepoError): GithubListener.parse_github_payload(r) # Modify the data such that the payload refers to a known repo, # assert that the returned models.Commit contains the expected data r.data = r.data.replace('"name": "mission-control"', '"name": "adsws"') c = GithubListener.parse_github_payload(r) self.assertEqual( c.commit_hash, 'bcdf7771aa10d78d865c61e5336145e335e30427' ) self.assertEqual( c.tag, None ) self.assertEqual(c.author, 'vsudilov') self.assertEqual(c.repository, 'adsws') self.assertEqual( c.message, "config: 's/SECRET_KEY/GITHUB_SECRET/g' for webhook secret" ) self.assertEqual( c.timestamp, datetime.datetime(2015, 6, 3, 12, 26, 57, tzinfo=tzutc()) ) # Assert that a different timeformat returns the expected # models.Commit.timestamp value r.data = r.data.replace( "2015-06-03T12:26:57Z", "2015-06-09T18:19:39+02:00" ) c = GithubListener.parse_github_payload(r) self.assertEqual( c.timestamp, datetime.datetime(2015, 6, 9, 18, 19, 39, tzinfo=tzoffset(None, 7200)) ) # Re-sending a previously saved commit payload should return that # previously saved commit db.session.add(c) db.session.commit() self.assertEqual(len(db.session.query(Commit).all()), 1) c2 = GithubListener.parse_github_payload(r) db.session.add(c2) db.session.commit() self.assertEqual(len(db.session.query(Commit).all()), 1)