<commit_before>import responses
import unittest
from cumulusci.tasks.salesforce import PublishCommunity
from cumulusci.core.exceptions import SalesforceException
from cumulusci.core.exceptions import TaskOptionsError
from simple_salesforce.exceptions import SalesforceResourceNotFound
from .util import create_task


task_options = {"name": "Test Community"}
task_options_with_id = {"name": "Test Community", "community_id": "000000000000000000"}


class test_PublishCommunity(unittest.TestCase):
    @responses.activate
    def test_publishes_community(self):
        cc_task = create_task(PublishCommunity, task_options)
        communities_url = "{}/services/data/v46.0/connect/communities".format(
            cc_task.org_config.instance_url
        )
        community_publish_url = "{}/services/data/v46.0/connect/communities/{}/publish".format(
            cc_task.org_config.instance_url, task_options_with_id["community_id"]
        )

        responses.add(
            method=responses.GET,
            url=communities_url,
            status=200,
            json={
                "communities": [
                    {
                        "allowChatterAccessWithoutLogin": "false",
                        "allowMembersToFlag": "false",
                        "description": "This is a test community",
                        "id": "{}".format(task_options_with_id["community_id"]),
                        "invitationsEnabled": "false",
                        "knowledgeableEnabled": "false",
                        "loginUrl": "https://mydomain.force.com/test/s/login",
                        "memberVisibilityEnabled": "true",
                        "name": "{}".format(task_options["name"]),
                        "nicknameDisplayEnabled": "false",
                        "privateMessagesEnabled": "false",
                        "reputationEnabled": "false",
                        "sendWelcomeEmail": "true",
                        "siteAsContainerEnabled": "true",
                        "siteUrl": "https://mydomain.force.com/test",
                        "status": "Live",
                        "templateName": "VF Template",
                        "url": "/services/data/v46.0/connect/communities/{}".format(
                            task_options_with_id["community_id"]
                        ),
                        "urlPathPrefix": "test",
                    }
                ],
                "total": "1",
            },
        )

        responses.add(
            method=responses.POST,
            url=community_publish_url,
            status=200,
            json={
                "id": "{}".format(task_options_with_id["community_id"]),
                "message": "We are publishing your changes now. You will receive an email confirmation when your changes are live.",
                "name": "{}".format(task_options["name"]),
                "url": "https://mydomain.force.com/test",
            },
        )

        cc_task()

        self.assertEqual(2, len(responses.calls))
        self.assertEqual(communities_url, responses.calls[0].request.url)
        self.assertEqual(community_publish_url, responses.calls[1].request.url)

    @responses.activate
    def test_publishes_community_with_id(self):
        cc_task = create_task(PublishCommunity, task_options_with_id)
        community_publish_url = "{}/services/data/v46.0/connect/communities/{}/publish".format(
            cc_task.org_config.instance_url, task_options_with_id["community_id"]
        )

        responses.add(
            method=responses.POST,
            url=community_publish_url,
            status=200,
            json={
                "id": "{}".format(task_options_with_id["community_id"]),
                "message": "We are publishing your changes now. You will receive an email confirmation when your changes are live.",
                "name": "{}".format(task_options_with_id["name"]),
                "url": "https://mydomain.force.com/test",
            },
        )

        cc_task()

        self.assertEqual(1, len(responses.calls))
        self.assertEqual(community_publish_url, responses.calls[0].request.url)

    @responses.activate
    def test_throws_exception_for_bad_name(self):
        cc_task = create_task(PublishCommunity, task_options)
        communities_url = "{}/services/data/v46.0/connect/communities".format(
            cc_task.org_config.instance_url
        )

        responses.add(
            method=responses.GET,
            url=communities_url,
            status=200,
            json={
                "communities": [
                    {
                        "allowChatterAccessWithoutLogin": "false",
                        "allowMembersToFlag": "false",
                        "description": "This is a test community",
                        "id": "{}".format(task_options_with_id["community_id"]),
                        "invitationsEnabled": "false",
                        "knowledgeableEnabled": "false",
                        "loginUrl": "https://mydomain.force.com/test/s/login",
                        "memberVisibilityEnabled": "true",
                        "name": "Not {}".format(task_options["name"]),
                        "nicknameDisplayEnabled": "false",
                        "privateMessagesEnabled": "false",
                        "reputationEnabled": "false",
                        "sendWelcomeEmail": "true",
                        "siteAsContainerEnabled": "true",
                        "siteUrl": "https://mydomain.force.com/test",
                        "status": "Live",
                        "templateName": "VF Template",
                        "url": "/services/data/v46.0/connect/communities/{}".format(
                            task_options_with_id["community_id"]
                        ),
                        "urlPathPrefix": "test",
                    }
                ],
                "total": "1",
            },
        )

        cc_task._init_task()
        with self.assertRaises(SalesforceException):
            cc_task._run_task()

    @responses.activate
    def test_throws_exception_for_bad_id(self):
        cc_task = create_task(PublishCommunity, task_options_with_id)
        community_publish_url = "{}/services/data/v46.0/connect/communities/{}/publish".format(
            cc_task.org_config.instance_url, task_options_with_id["community_id"]
        )

        responses.add(
            method=responses.POST,
            url=community_publish_url,
            status=404,
            json=[
                {
                    "errorCode": "NOT_FOUND",
                    "message": "The requested resource does not exist",
                }
            ],
        )

        with self.assertRaises(SalesforceResourceNotFound):
            cc_task._init_task()
            cc_task._run_task()

    @responses.activate
    def test_throws_exeption_for_unset_name_and_id(self):
        cc_task = create_task(PublishCommunity, {})

        cc_task._init_task()
        with self.assertRaises(TaskOptionsError):
            cc_task._run_task()<commit_msg>Fix typo in test name<commit_after>import responses
import unittest
from cumulusci.tasks.salesforce import PublishCommunity
from cumulusci.core.exceptions import SalesforceException
from cumulusci.core.exceptions import TaskOptionsError
from simple_salesforce.exceptions import SalesforceResourceNotFound
from .util import create_task


task_options = {"name": "Test Community"}
task_options_with_id = {"name": "Test Community", "community_id": "000000000000000000"}


class test_PublishCommunity(unittest.TestCase):
    @responses.activate
    def test_publishes_community(self):
        cc_task = create_task(PublishCommunity, task_options)
        communities_url = "{}/services/data/v46.0/connect/communities".format(
            cc_task.org_config.instance_url
        )
        community_publish_url = "{}/services/data/v46.0/connect/communities/{}/publish".format(
            cc_task.org_config.instance_url, task_options_with_id["community_id"]
        )

        responses.add(
            method=responses.GET,
            url=communities_url,
            status=200,
            json={
                "communities": [
                    {
                        "allowChatterAccessWithoutLogin": "false",
                        "allowMembersToFlag": "false",
                        "description": "This is a test community",
                        "id": "{}".format(task_options_with_id["community_id"]),
                        "invitationsEnabled": "false",
                        "knowledgeableEnabled": "false",
                        "loginUrl": "https://mydomain.force.com/test/s/login",
                        "memberVisibilityEnabled": "true",
                        "name": "{}".format(task_options["name"]),
                        "nicknameDisplayEnabled": "false",
                        "privateMessagesEnabled": "false",
                        "reputationEnabled": "false",
                        "sendWelcomeEmail": "true",
                        "siteAsContainerEnabled": "true",
                        "siteUrl": "https://mydomain.force.com/test",
                        "status": "Live",
                        "templateName": "VF Template",
                        "url": "/services/data/v46.0/connect/communities/{}".format(
                            task_options_with_id["community_id"]
                        ),
                        "urlPathPrefix": "test",
                    }
                ],
                "total": "1",
            },
        )

        responses.add(
            method=responses.POST,
            url=community_publish_url,
            status=200,
            json={
                "id": "{}".format(task_options_with_id["community_id"]),
                "message": "We are publishing your changes now. You will receive an email confirmation when your changes are live.",
                "name": "{}".format(task_options["name"]),
                "url": "https://mydomain.force.com/test",
            },
        )

        cc_task()

        self.assertEqual(2, len(responses.calls))
        self.assertEqual(communities_url, responses.calls[0].request.url)
        self.assertEqual(community_publish_url, responses.calls[1].request.url)

    @responses.activate
    def test_publishes_community_with_id(self):
        cc_task = create_task(PublishCommunity, task_options_with_id)
        community_publish_url = "{}/services/data/v46.0/connect/communities/{}/publish".format(
            cc_task.org_config.instance_url, task_options_with_id["community_id"]
        )

        responses.add(
            method=responses.POST,
            url=community_publish_url,
            status=200,
            json={
                "id": "{}".format(task_options_with_id["community_id"]),
                "message": "We are publishing your changes now. You will receive an email confirmation when your changes are live.",
                "name": "{}".format(task_options_with_id["name"]),
                "url": "https://mydomain.force.com/test",
            },
        )

        cc_task()

        self.assertEqual(1, len(responses.calls))
        self.assertEqual(community_publish_url, responses.calls[0].request.url)

    @responses.activate
    def test_throws_exception_for_bad_name(self):
        cc_task = create_task(PublishCommunity, task_options)
        communities_url = "{}/services/data/v46.0/connect/communities".format(
            cc_task.org_config.instance_url
        )

        responses.add(
            method=responses.GET,
            url=communities_url,
            status=200,
            json={
                "communities": [
                    {
                        "allowChatterAccessWithoutLogin": "false",
                        "allowMembersToFlag": "false",
                        "description": "This is a test community",
                        "id": "{}".format(task_options_with_id["community_id"]),
                        "invitationsEnabled": "false",
                        "knowledgeableEnabled": "false",
                        "loginUrl": "https://mydomain.force.com/test/s/login",
                        "memberVisibilityEnabled": "true",
                        "name": "Not {}".format(task_options["name"]),
                        "nicknameDisplayEnabled": "false",
                        "privateMessagesEnabled": "false",
                        "reputationEnabled": "false",
                        "sendWelcomeEmail": "true",
                        "siteAsContainerEnabled": "true",
                        "siteUrl": "https://mydomain.force.com/test",
                        "status": "Live",
                        "templateName": "VF Template",
                        "url": "/services/data/v46.0/connect/communities/{}".format(
                            task_options_with_id["community_id"]
                        ),
                        "urlPathPrefix": "test",
                    }
                ],
                "total": "1",
            },
        )

        cc_task._init_task()
        with self.assertRaises(SalesforceException):
            cc_task._run_task()

    @responses.activate
    def test_throws_exception_for_bad_id(self):
        cc_task = create_task(PublishCommunity, task_options_with_id)
        community_publish_url = "{}/services/data/v46.0/connect/communities/{}/publish".format(
            cc_task.org_config.instance_url, task_options_with_id["community_id"]
        )

        responses.add(
            method=responses.POST,
            url=community_publish_url,
            status=404,
            json=[
                {
                    "errorCode": "NOT_FOUND",
                    "message": "The requested resource does not exist",
                }
            ],
        )

        with self.assertRaises(SalesforceResourceNotFound):
            cc_task._init_task()
            cc_task._run_task()

    @responses.activate
    def test_throws_exception_for_unset_name_and_id(self):
        cc_task = create_task(PublishCommunity, {})

        cc_task._init_task()
        with self.assertRaises(TaskOptionsError):
            cc_task._run_task()
{'fraction_non_alphanumeric': 0.11205029689137269, 'fraction_numerical': 0.00852252881592735, 'mean_word_length': 1.7077737847550596, 'pattern_counts': {'":': 110, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '134262', 'n_tokens_mistral': 3888, 'n_tokens_neox': 3616, 'n_words': 571}