Exemple #1
0
    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_payload_sent_to_rabbitmq(self, mocked_rabbit):
        """
        Tests that a payload is sent to rabbitmq and that it contains the
        expected payload.
        """

        instance_rabbit = mocked_rabbit.return_value
        instance_rabbit.__enter__.return_value = instance_rabbit
        instance_rabbit.__exit__.return_value = None
        instance_rabbit.publish.side_effect = None

        payload = {
            "exchange": "test",
            "route": "test",
            "repository": "important-service",
            "commit": "da89fuhds",
            "environment": "staging",
            "author": "author",
            "tag": "da89fuhds",
        }
        payload_copy = payload.copy()

        GithubListener.push_rabbitmq(payload_copy)

        self.assertTrue(mocked_rabbit.called)

        c = instance_rabbit.publish.call_args[1]

        self.assertEqual(c["route"], payload.pop("route"))
        self.assertEqual(c["exchange"], payload.pop("exchange"))

        p = json.loads(c["payload"])
        for key in payload:
            self.assertEqual(payload[key], p.get(key, None), msg='key "{}" not found in call {}'.format(key, p))
Exemple #3
0
    def test_payload_sent_to_rabbitmq(self, mocked_rabbit):
        """
        Tests that a payload is sent to rabbitmq and that it contains the
        expected payload.
        """

        instance_rabbit = mocked_rabbit.return_value
        instance_rabbit.__enter__.return_value = instance_rabbit
        instance_rabbit.__exit__.return_value = None
        instance_rabbit.publish.side_effect = None

        payload = OrderedDict([
            ('application', 'important-service'),
            ('commit', 'd8fgdfgdf'),
            ('environment', 'staging'),
            ('author', 'author'),
            ('tag', 'dsfdsf')
        ])

        GithubListener.push_rabbitmq(payload=payload, exchange='test', route='test')

        self.assertTrue(mocked_rabbit.called)

        instance_rabbit.publish.assert_has_calls(
            [mock.call(payload=json.dumps(payload), exchange='test', route='test')]
        )
Exemple #4
0
    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

        c = GithubListener.parse_github_payload(r)
        self.assertEqual(
            c['url'],
            'https://github.com/adsabs/adsws'
        )
        self.assertEqual(
            c['tag'],
            'v1.0.0'
        )

        for key in ['url', 'commit', 'author', 'tag']:
            self.assertIn(
                key,
                c,
                msg='Key "{}" not found in "{}"'.format(key, c)
            )
Exemple #5
0
    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 = github_payload.replace('"name": "adsws"', '"name": "mission-control"')

        # Modify the data such that the payload refers to a known repo,
        # assert that the returned models.Commit contains the expected data
        r.data = github_payload
        c = GithubListener.parse_github_payload(r)
        self.assertEqual(
            c['url'],
            u'https://github.com/adsabs/adsws'
        )
        self.assertEqual(
            c['tag'],
            None
        )

        for key in ['url', 'commit', 'author', 'tag']:
            self.assertIn(
                key,
                c,
                msg='Key "{}" not found in "{}"'.format(key, c)
            )
    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

        c = GithubListener.parse_github_payload(r)
        self.assertEqual(c["repository"], "adsws")
        self.assertEqual(c["tag"], "v1.0.0")

        for key in ["repository", "environment", "commit", "author", "tag"]:
            self.assertIn(key, c, msg='Key "{}" not found in "{}"'.format(key, c))
    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 = github_payload.replace('"name": "adsws"', '"name": "mission-control"')

        # Modify the data such that the payload refers to a known repo,
        # assert that the returned models.Commit contains the expected data
        r.data = github_payload
        c = GithubListener.parse_github_payload(r)
        self.assertEqual(c["repository"], "adsws")
        self.assertEqual(c["tag"], None)

        for key in ["repository", "environment", "commit", "author", "tag"]:
            self.assertIn(key, c, msg='Key "{}" not found in "{}"'.format(key, c))