test_slack_importer: Add end to end test.

TODO: Merge with previous commits.
This commit is contained in:
Aman Agrawal 2024-11-20 22:56:37 +05:30
parent 1253686287
commit 7ec9a8e851
2 changed files with 105 additions and 3 deletions

View File

@ -889,6 +889,7 @@ Output:
realm_type: int = Realm.ORG_TYPES["business"]["id"],
realm_default_language: str = "en",
realm_in_root_domain: str | None = None,
import_from: str = "none",
) -> "TestHttpResponse":
payload = {
"email": email,
@ -896,7 +897,7 @@ Output:
"realm_type": realm_type,
"realm_default_language": realm_default_language,
"realm_subdomain": realm_subdomain,
"import_from": "none",
"import_from": import_from,
}
if realm_in_root_domain is not None:
payload["realm_in_root_domain"] = realm_in_root_domain

View File

@ -47,9 +47,16 @@ from zerver.data_import.slack import (
)
from zerver.lib.import_realm import do_import_realm
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import read_test_image_file
from zerver.lib.test_helpers import find_key_by_email, read_test_image_file
from zerver.lib.topic import EXPORT_TOPIC_NAME
from zerver.models import Message, Realm, RealmAuditLog, Recipient, UserProfile
from zerver.models import (
Message,
PreregistrationRealm,
Realm,
RealmAuditLog,
Recipient,
UserProfile,
)
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.realms import get_realm
@ -1543,3 +1550,97 @@ class SlackImporter(ZulipTestCase):
# We need to mock EXTERNAL_HOST to be a valid domain because Slack's importer
# uses it to generate email addresses for users without an email specified.
do_convert_zipfile(test_slack_zip_file, output_dir, token)
@mock.patch("zerver.views.registration.queue_json_publish")
@responses.activate
def test_end_to_end_slack_import(
self,
mock_queue: mock.Mock,
) -> None:
# Choose import from slack
email = "ete-slack-import@zulip.com"
result = self.submit_realm_creation_form(
email,
realm_subdomain="ete-slack-import",
realm_name="Slack import end to end",
import_from="slack",
)
# Confirm email
self.assertEqual(result.status_code, 302)
self.assertTrue(
result["Location"].endswith(
"/accounts/new/send_confirm/?email=ete-slack-import%40zulip.com&realm_name=Slack+import+end+to+end&realm_type=10&realm_default_language=en&realm_subdomain=ete-slack-import"
)
)
result = self.client_get(result["Location"])
self.assert_in_response("check your email", result)
prereg_realm = PreregistrationRealm.objects.get(email=email)
self.assertEqual(prereg_realm.name, "Slack import end to end")
self.assertEqual(prereg_realm.data_import_metadata["import_from"], "slack")
# Redirect to slack data import form
confirmation_url = self.get_confirmation_url_from_outbox(email)
result = self.client_get(confirmation_url)
self.assert_in_success_response(["new/import/slack"], result)
# Mock slack API response and mark token as valid
access_token = "xoxb-valid-token"
slack_team_info_url = "https://slack.com/api/team.info"
responses.add_callback(
responses.GET,
slack_team_info_url,
callback=lambda _: (
200,
{"x-oauth-scopes": "emoji:read,users:read,users:read.email,team:read"},
orjson.dumps({"ok": True}),
),
)
confirmation_key = find_key_by_email(email)
result = self.client_post(
"/new/import/slack/",
{
"key": confirmation_key,
"slack_access_token": access_token,
},
)
self.assertEqual(result.status_code, 200)
prereg_realm.refresh_from_db()
self.assertEqual(prereg_realm.data_import_metadata["import_from"], "slack")
self.assertEqual(prereg_realm.data_import_metadata["slack_access_token"], access_token)
# Assume user uploaded a file.
prereg_realm.data_import_metadata["uploaded_import_file_name"] = "test_slack_importer.zip"
prereg_realm.save()
# Check that deferred_work for import is queued.
result = self.client_post(
"/new/import/slack/",
{
"key": confirmation_key,
"start_slack_import": "true",
},
)
self.assert_in_success_response(["Import progress"], result)
prereg_realm.refresh_from_db()
self.assertTrue(prereg_realm.data_import_metadata["is_import_work_queued"])
mock_queue.assert_called_once_with(
"deferred_work",
{
"type": "import_slack_data",
"preregistration_realm_id": prereg_realm.id,
"filename": f"import/{prereg_realm.id}/slack.zip",
"slack_access_token": access_token,
},
)
# Check cancel import cancels the import
result = self.client_post(
"/new/import/slack/",
{
"key": confirmation_key,
"cancel_import": "true",
},
)
prereg_realm.refresh_from_db()
self.assertEqual(prereg_realm.data_import_metadata.get("import_from"), None)