Example #1
0
    def test_send_notification_for_event(self, codepipeline_mock, urlopen_mock):
        cm = mock.MagicMock()
        cm.getcode.return_value = 200
        cm.read.return_value = b"ok"
        urlopen_mock.return_value = cm

        codepipeline_mock.return_value = boto3.client("codepipeline")
        with Stubber(codepipeline_mock.return_value) as stubber:
            stubber.add_response("get_pipeline_execution", **PIPELINE_EXECUTION)
            notifier.handler(event=TEST_EVENT, context={})

        urlopen_mock.assert_called_once()
        request = urlopen_mock.call_args[0][0]
        self.assertEqual(request.full_url, ENVIRONMENT["SLACK_WEBHOOK_URL"])
        self.assertEqual(request.get_header("Content-type"), "application/json")
        self.assertEqual(
            json.loads(request.data),
            {
                "channel": "#notifications",
                "username": "******",
                "icon_emoji": ":rocket:",
                "text": "*Deployment* of *kjagiello-qa-homepage* has started.",
                "attachments": [
                    {
                        "color": "#1a9edb",
                        "fallback": "`kjagiello-qa-homepage` has `STARTED`",
                        "fields": [
                            {"title": "Pipeline", "value": "kjagiello-qa-homepage"},
                            {
                                "title": "Execution ID",
                                "value": (
                                    "<https://eu-west-1.console.aws.amazon.com/"
                                    "codesuite/codepipeline/pipelines/kjagiello-"
                                    "qa-homepage/executions/0aeb1e48-4de1-4d3c-"
                                    "8815-4a8a701b1fcc/timeline?region=eu-west-1|"
                                    "0aeb1e48-4de1-4d3c-8815-4a8a701b1fcc>"
                                ),
                            },
                            {"title": "Environment", "value": "QA", "short": True},
                            {"title": "Region", "value": "eu-west-1", "short": True},
                            {
                                "title": "Code revision",
                                "value": (
                                    "commit message\n\n"
                                    "<http://foo.bar|View the changeset>"
                                ),
                            },
                        ],
                    }
                ],
            },
        )
Example #2
0
    def test_handles_missing_revision_gracefully(self, codepipeline_mock, urlopen_mock):
        # Get rid of the revision summary
        pipeline_execution = deepcopy(PIPELINE_EXECUTION)
        service_response = pipeline_execution["service_response"]
        del service_response["pipelineExecution"]["artifactRevisions"][0][
            "revisionSummary"
        ]

        codepipeline_mock.return_value = boto3.client("codepipeline")
        with Stubber(codepipeline_mock.return_value) as stubber:
            stubber.add_response("get_pipeline_execution", **pipeline_execution)
            notifier.handler(event=TEST_EVENT, context={})

        urlopen_mock.assert_called_once()
        request = urlopen_mock.call_args[0][0]
        payload = json.loads(request.data)
        revision_field = [
            field
            for field in payload["attachments"][0]["fields"]
            if field["title"] == "Code revision"
        ][0]
        self.assertEqual(revision_field["value"], "Unknown")