コード例 #1
0
    def test_subscription_delivery_scheduling(
        self,
        mock_deliver_task: MagicMock,
        mock_gen_assets: MagicMock,
        mock_send_email: MagicMock,
        mock_send_slack: MagicMock,
    ) -> None:
        subscriptions = [
            create_subscription(team=self.team,
                                insight=self.insight,
                                created_by=self.user),
            create_subscription(team=self.team,
                                insight=self.insight,
                                created_by=self.user),
            create_subscription(team=self.team,
                                dashboard=self.dashboard,
                                created_by=self.user),
            create_subscription(team=self.team,
                                dashboard=self.dashboard,
                                created_by=self.user,
                                deleted=True),
        ]
        # Modify a subscription to have its target time at least an hour ahead
        subscriptions[2].start_date = datetime(2022, 1, 1, 10,
                                               0).replace(tzinfo=pytz.UTC)
        subscriptions[2].save()
        assert subscriptions[2].next_delivery_date == datetime(
            2022, 2, 2, 10, 0).replace(tzinfo=pytz.UTC)

        schedule_all_subscriptions()

        assert mock_deliver_task.delay.mock_calls == [
            call(subscriptions[0].id),
            call(subscriptions[1].id)
        ]
コード例 #2
0
    def test_deliver_subscription_report_email(
        self,
        mock_gen_assets: MagicMock,
        mock_send_email: MagicMock,
        mock_send_slack: MagicMock,
    ) -> None:
        subscription = create_subscription(team=self.team,
                                           insight=self.insight,
                                           created_by=self.user)
        mock_gen_assets.return_value = [self.insight], [self.asset]

        deliver_subscription_report(subscription.id)

        assert mock_send_email.call_count == 2

        assert mock_send_email.call_args_list == [
            call("*****@*****.**",
                 subscription, [self.asset],
                 invite_message=None,
                 total_asset_count=1),
            call("*****@*****.**",
                 subscription, [self.asset],
                 invite_message=None,
                 total_asset_count=1),
        ]
コード例 #3
0
    def test_handle_subscription_value_change_email(
        self,
        mock_gen_assets: MagicMock,
        mock_send_email: MagicMock,
        mock_send_slack: MagicMock,
    ) -> None:
        subscription = create_subscription(
            team=self.team,
            insight=self.insight,
            created_by=self.user,
            target_value="[email protected],[email protected]",
        )
        mock_gen_assets.return_value = [self.insight], [self.asset]

        handle_subscription_value_change(
            subscription.id,
            previous_value="*****@*****.**",
            invite_message="My invite message")

        assert mock_send_email.call_count == 1

        assert mock_send_email.call_args_list == [
            call(
                "*****@*****.**",
                subscription,
                [self.asset],
                invite_message="My invite message",
                total_asset_count=1,
            ),
        ]
コード例 #4
0
    def test_raises_if_missing_resource(self, mock_export_task: MagicMock,
                                        mock_group: MagicMock) -> None:
        subscription = create_subscription(team=self.team,
                                           created_by=self.user)

        with pytest.raises(Exception) as e:
            generate_assets(subscription)

        assert str(
            e.value
        ) == "There are no insights to be sent for this Subscription"
コード例 #5
0
    def test_generate_assets_for_dashboard(self, mock_export_task: MagicMock,
                                           mock_group: MagicMock) -> None:
        subscription = create_subscription(team=self.team,
                                           dashboard=self.dashboard,
                                           created_by=self.user)

        insights, assets = generate_assets(subscription)

        assert len(insights) == len(self.tiles)
        assert len(assets) == DEFAULT_MAX_ASSET_COUNT
        assert mock_export_task.s.call_count == DEFAULT_MAX_ASSET_COUNT
コード例 #6
0
    def setUp(self) -> None:
        self.dashboard = Dashboard.objects.create(team=self.team,
                                                  name="private dashboard",
                                                  created_by=self.user)
        self.insight = Insight.objects.create(team=self.team,
                                              short_id="123456",
                                              name="My Test subscription")
        self.tiles = []
        for _ in range(10):
            self.tiles.append(
                DashboardTile.objects.create(dashboard=self.dashboard,
                                             insight=self.insight))

        self.subscription = create_subscription(team=self.team,
                                                insight=self.insight,
                                                created_by=self.user)
コード例 #7
0
    def setUp(self) -> None:
        self.dashboard = Dashboard.objects.create(team=self.team,
                                                  name="private dashboard",
                                                  created_by=self.user)
        self.insight = Insight.objects.create(team=self.team,
                                              short_id="123456",
                                              name="My Test subscription")

        set_instance_setting("EMAIL_HOST", "fake_host")
        set_instance_setting("EMAIL_ENABLED", True)

        self.asset = ExportedAsset.objects.create(team=self.team,
                                                  insight_id=self.insight.id,
                                                  export_format="image/png")
        self.subscription = create_subscription(team=self.team,
                                                insight=self.insight,
                                                created_by=self.user)
コード例 #8
0
    def setUp(self) -> None:
        self.dashboard = Dashboard.objects.create(team=self.team,
                                                  name="private dashboard",
                                                  created_by=self.user)
        self.insight = Insight.objects.create(team=self.team,
                                              short_id="123456",
                                              name="My Test subscription")
        self.asset = ExportedAsset.objects.create(team=self.team,
                                                  insight_id=self.insight.id,
                                                  export_format="image/png")
        self.subscription = create_subscription(
            team=self.team,
            insight=self.insight,
            created_by=self.user,
            target_type="slack",
            target_value="C12345|#test-channel",
        )

        self.integration = Integration.objects.create(team=self.team,
                                                      kind="slack")
コード例 #9
0
    def test_sends_dashboard_subscription(
        self,
        MockEmailMessage: MagicMock,
    ) -> None:
        mocked_email_messages = mock_email_messages(MockEmailMessage)

        subscription = create_subscription(team=self.team,
                                           dashboard=self.dashboard,
                                           created_by=self.user)

        send_email_subscription_report(self.user.email,
                                       subscription, [self.asset],
                                       invite_message="My invite message",
                                       total_asset_count=10)

        assert len(mocked_email_messages) == 1
        assert mocked_email_messages[0].send.call_count == 1
        assert "You have been subscribed" in mocked_email_messages[0].html_body
        assert "You have been subscribed to a PostHog Dashboard" == mocked_email_messages[
            0].subject
        assert f"SHOWING 1 OF 10 DASHBOARD INSIGHTS" in mocked_email_messages[
            0].html_body
コード例 #10
0
    def test_deliver_subscription_report_slack(
        self,
        mock_gen_assets: MagicMock,
        mock_send_email: MagicMock,
        mock_send_slack: MagicMock,
    ) -> None:
        subscription = create_subscription(
            team=self.team,
            insight=self.insight,
            created_by=self.user,
            target_type="slack",
            target_value="C12345|#test-channel",
        )
        mock_gen_assets.return_value = [self.insight], [self.asset]

        deliver_subscription_report(subscription.id)

        assert mock_send_slack.call_count == 1
        assert mock_send_slack.call_args_list == [
            call(subscription, [self.asset],
                 total_asset_count=1,
                 is_new_subscription=False),
        ]
コード例 #11
0
    def test_subscription_dashboard_delivery(
            self, MockSlackIntegration: MagicMock) -> None:
        mock_slack_integration = MagicMock()
        MockSlackIntegration.return_value = mock_slack_integration
        mock_slack_integration.client.chat_postMessage.return_value = {
            "ts": "1.234"
        }

        self.subscription = create_subscription(
            team=self.team,
            dashboard=self.dashboard,
            created_by=self.user,
            target_type="slack",
            target_value="C12345|#test-channel",
        )

        send_slack_subscription_report(self.subscription,
                                       [self.asset, self.asset, self.asset],
                                       10)

        assert mock_slack_integration.client.chat_postMessage.call_count == 4
        post_message_calls = mock_slack_integration.client.chat_postMessage.call_args_list
        first_call = post_message_calls[0].kwargs

        assert first_call["channel"] == "C12345"
        assert first_call[
            "text"] == "Your subscription to the Dashboard *private dashboard* is ready! 🎉"

        assert first_call["blocks"] == [
            {
                "type": "section",
                "text": {
                    "type":
                    "mrkdwn",
                    "text":
                    "Your subscription to the Dashboard *private dashboard* is ready! 🎉",
                },
            },
            {
                "type": "image",
                "image_url":
                post_message_calls[0].kwargs["blocks"][1]["image_url"],
                "alt_text": "My Test subscription",
            },
            {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "_See 🧵 for more Insights_"
                }
            },
            {
                "type": "divider"
            },
            {
                "type":
                "actions",
                "elements": [
                    {
                        "type":
                        "button",
                        "text": {
                            "type": "plain_text",
                            "text": "View in PostHog"
                        },
                        "url":
                        f"http://localhost:8000/dashboard/{self.dashboard.id}?utm_source=posthog&utm_campaign=subscription_report&utm_medium=slack",
                    },
                    {
                        "type":
                        "button",
                        "text": {
                            "type": "plain_text",
                            "text": "Manage Subscription"
                        },
                        "url":
                        f"http://localhost:8000/dashboard/{self.dashboard.id}/subscriptions/{self.subscription.id}?utm_source=posthog&utm_campaign=subscription_report&utm_medium=slack",
                    },
                ],
            },
        ]

        # Second call - other asset
        second_call = post_message_calls[1].kwargs
        assert second_call["channel"] == "C12345"
        assert second_call["thread_ts"] == "1.234"
        assert second_call["blocks"] == [{
            "type":
            "image",
            "image_url":
            second_call["blocks"][0]["image_url"],
            "alt_text":
            "My Test subscription",
        }]

        # Third call - other asset
        third_call = post_message_calls[2].kwargs
        assert third_call["blocks"] == [{
            "type":
            "image",
            "image_url":
            third_call["blocks"][0]["image_url"],
            "alt_text":
            "My Test subscription",
        }]

        # Fourth call - notice that more exists
        fourth_call = post_message_calls[3].kwargs
        assert fourth_call["blocks"] == [{
            "type": "section",
            "text": {
                "type":
                "mrkdwn",
                "text":
                f"Showing 3 of 10 Insights. <http://localhost:8000/dashboard/{self.dashboard.id}?utm_source=posthog&utm_campaign=subscription_report&utm_medium=slack|View the rest in PostHog>",
            },
        }]