Repository: zulip/zulip

Issue:
`create_realm` management command generate an AssertionError
On zulip 5.2 creating a realm with the `create_realm` management command leads to an assert.

```
$ /home/zulip/deployments/current/manage.py create_realm Test email@domain.com "General Admin" --password "..."
2022-06-21 19:29:44.443 WARN [] Passing password on the command line is insecure; prefer --password-file.
2022-06-21 19:29:44.448 INFO [] Server not yet initialized. Creating the internal realm first.
Traceback (most recent call last):
  File "/home/zulip/deployments/current/manage.py", line 157, in <module>
    execute_from_command_line(sys.argv)
  File "/home/zulip/deployments/current/manage.py", line 122, in execute_from_command_line
    utility.execute()
  File "/home/zulip/deployments/2022-06-21-15-28-45/zulip-py3-venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/zulip/deployments/2022-06-21-15-28-45/zulip-py3-venv/lib/python3.7/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/zulip/deployments/2022-06-21-15-28-45/zulip-py3-venv/lib/python3.7/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/home/zulip/deployments/2022-06-21-15-28-45/zerver/management/commands/create_realm.py", line 63, in handle
    acting_user=None,
  File "/home/zulip/deployments/2022-06-21-15-28-45/zerver/actions/create_user.py", line 445, in do_create_user
    realm_creation=realm_creation,
  File "/home/zulip/deployments/2022-06-21-15-28-45/zerver/actions/create_user.py", line 231, in process_new_human_user
    revoke_preregistration_users(user_profile, prereg_user, realm_creation)
  File "/home/zulip/deployments/2022-06-21-15-28-45/zerver/actions/create_user.py", line 255, in revoke_preregistration_users
    assert not realm_creation, "realm_creation should only happen with a PreregistrationUser"
AssertionError: realm_creation should only happen with a PreregistrationUser
```

Related discussion about this issue: https://chat.zulip.org/#narrow/stream/9-issues/topic/create_realm.20does.20not.20work



Discussion:
Hello @zulip/server-production members, this issue was labeled with the "area: production" label, so you may want to check it out!

<!-- areaLabelAddition -->


Resolving patch:
diff --git a/zerver/actions/create_user.py b/zerver/actions/create_user.py
--- a/zerver/actions/create_user.py
+++ b/zerver/actions/create_user.py
@@ -235,11 +235,9 @@ def process_new_human_user(
                 ),
             )
 
-    # Revoke all preregistration users except prereg_user, and link prereg_user to
-    # the created user
-    if prereg_user is None:
-        assert not realm_creation, "realm_creation should only happen with a PreregistrationUser"
-
+    # For the sake of tracking the history of UserProfiles,
+    # we want to tie the newly created user to the PreregistrationUser
+    # it was created from.
     if prereg_user is not None:
         prereg_user.status = confirmation_settings.STATUS_ACTIVE
         prereg_user.created_user = user_profile


Test patch: