from zerver.lib.test_classes import WebhookTestCase

TOPIC = "sandbox"
TOPIC_BRANCH_EVENTS = "sandbox / {branch}"


class Bitbucket3HookTests(WebhookTestCase):
    STREAM_NAME = "bitbucket3"
    URL_TEMPLATE = "/api/v1/external/bitbucket3?stream={stream}&api_key={api_key}"
    WEBHOOK_DIR_NAME = "bitbucket3"

    # Diagnostics events:
    def test_ping(self) -> None:
        expected_message = (
            "Congratulations! The Bitbucket Server webhook was configured successfully!"
        )
        self.check_webhook("diagnostics_ping", "Bitbucket Server Ping", expected_message)

    def test_ping_with_user_defined_topic(self) -> None:
        self.url = self.build_webhook_url(topic="my topic")
        expected_message = (
            "Congratulations! The Bitbucket Server webhook was configured successfully!"
        )
        self.check_webhook("diagnostics_ping", "my topic", expected_message)

    # Core repo events:
    def test_commit_comment_added(self) -> None:
        expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) commented on [508d1b6](http://139.59.64.214:7990/projects/SBOX/repos/sandbox/commits/508d1b67f1f8f3a25f543a030a7a178894aa9907):\n~~~ quote\nJust an arbitrary comment on a commit.\n~~~"""
        self.check_webhook("commit_comment_added", TOPIC, expected_message)

    def test_commit_comment_edited(self) -> None:
        expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) edited their comment on [508d1b6](http://139.59.64.214:7990/projects/SBOX/repos/sandbox/commits/508d1b67f1f8f3a25f543a030a7a178894aa9907):\n~~~ quote\nJust an arbitrary comment on a commit. Nothing to see here...\n~~~"""
        self.check_webhook("commit_comment_edited", TOPIC, expected_message)

    def test_commit_comment_deleted(self) -> None:
        expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) deleted their comment on [508d1b6](http://139.59.64.214:7990/projects/SBOX/repos/sandbox/commits/508d1b67f1f8f3a25f543a030a7a178894aa9907):\n~~~ quote\n~~Just an arbitrary comment on a commit. Nothing to see here...~~\n~~~"""
        self.check_webhook("commit_comment_deleted", TOPIC, expected_message)

    def test_bitbucket3_repo_forked(self) -> None:
        expected_message = """User Hemanth V. Alluri(login: [hypro999](http://139.59.64.214:7990/users/hypro999)) forked the repository into [sandbox fork](http://139.59.64.214:7990/users/hypro999/repos/sandbox-fork/browse)."""
        self.check_webhook("repo_forked", TOPIC, expected_message)

    def test_bitbucket3_repo_modified(self) -> None:
        expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) changed the name of the **sandbox** repo from **sandbox** to **sandbox v2**."""
        expected_topic = "sandbox v2"
        self.check_webhook("repo_modified", expected_topic, expected_message)

    # Repo push events:
    def test_push_add_branch(self) -> None:
        expected_message = (
            """[hypro999](http://139.59.64.214:7990/users/hypro999) created branch2 branch."""
        )
        expected_topic = TOPIC_BRANCH_EVENTS.format(branch="branch2")
        self.check_webhook("repo_push_add_branch", expected_topic, expected_message)

    def test_push_add_tag(self) -> None:
        expected_message = (
            """[hypro999](http://139.59.64.214:7990/users/hypro999) pushed tag newtag."""
        )
        self.check_webhook("repo_push_add_tag", TOPIC, expected_message)

    def test_push_delete_branch(self) -> None:
        expected_message = (
            """[hypro999](http://139.59.64.214:7990/users/hypro999) deleted branch branch2."""
        )
        expected_topic = TOPIC_BRANCH_EVENTS.format(branch="branch2")
        self.check_webhook("repo_push_delete_branch", expected_topic, expected_message)

    def test_push_delete_tag(self) -> None:
        expected_message = (
            """[hypro999](http://139.59.64.214:7990/users/hypro999) removed tag test-tag."""
        )
        self.check_webhook("repo_push_delete_tag", TOPIC, expected_message)

    def test_push_update_single_branch(self) -> None:
        expected_message = """[hypro999](http://139.59.64.214:7990/users/hypro999) pushed to branch master. Head is now e68c981ef53dbab0a5ca320a2d8d80e216c70528."""
        expected_topic = TOPIC_BRANCH_EVENTS.format(branch="master")
        self.check_webhook("repo_push_update_single_branch", expected_topic, expected_message)

    def test_push_update_multiple_branches(self) -> None:
        branch1_content = """[hypro999](http://139.59.64.214:7990/users/hypro999) pushed to branch branch1. Head is now 3980c2be32a7e23c795741d5dc1a2eecb9b85d6d."""
        master_content = """[hypro999](http://139.59.64.214:7990/users/hypro999) pushed to branch master. Head is now fc43d13cff1abb28631196944ba4fc4ad06a2cf2."""

        self.subscribe(self.test_user, self.STREAM_NAME)