From 7ec9a8e8516dd4cc89ff33ef495d440fa7fb01a4 Mon Sep 17 00:00:00 2001 From: Aman Agrawal Date: Wed, 20 Nov 2024 22:56:37 +0530 Subject: [PATCH] test_slack_importer: Add end to end test. TODO: Merge with previous commits. --- zerver/lib/test_classes.py | 3 +- zerver/tests/test_slack_importer.py | 105 +++++++++++++++++++++++++++- 2 files changed, 105 insertions(+), 3 deletions(-) diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index 89b6bc11d8..97c3734f65 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -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 diff --git a/zerver/tests/test_slack_importer.py b/zerver/tests/test_slack_importer.py index 21c5c454f1..559d36b3e1 100644 --- a/zerver/tests/test_slack_importer.py +++ b/zerver/tests/test_slack_importer.py @@ -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)