prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Test management commands
  Because in three years, run_tasks will silently fail on the production system and nobody will notice.
  - [x] **run_tasks** - shouldn't be too hard and is rather important
  - [x] **anonymize** - might be a bit of work to cover it properly, but should be straightforward.
  - [x] **refresh_results_cache** - should be easy
  - [x] **dump_testdata** - don't know how not to overwrite the file during testing, but should be possible
  
  the other commands are already tested or rather unsuitable for testing
  - [x] **merge_users** - already has a test (#703) and is shown to be pretty broken.
  - [x] **run** - don't know how to test this and there isn't really anything that could break. still, somehow running it to check that it doesn't crash right away on e.g. imports would be cool
  - [x] **reload_testdata** - don't know whether it's possible at all to test that, i mean it drops the whole database...
  - [ ] **import_ad** - we never used it and i don't know whether it's feasible to mock ldap
  
  use `self.stdout.write` instead of `print` and `call_command("command_name", stdout=StringIO())` to avoid console output during tests. don't know what to do about calls to `input`.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `evap/evaluation/management/commands/import_ad.py`
  Content:
  ```
  1 import getpass
  2 import ldap
  3 import sys
  4 
  5 from django.core.management.base import BaseCommand
  6 
  7 from evap.evaluation.models import UserProfile
  8 
  9 
  10 class Command(BaseCommand):
  11     args = '<ldap server> <username>'
  12     help = 'Imports user data from Active Directory. The username should be specified with realm.'
  13 
  14     def handle(self, *args, **options):
  15         try:
  16             # connect
  17             l = ldap.initialize(args[0])
  18 
  19             # bind
  20             l.bind_s(args[1], getpass.getpass("AD Password: "))
  21 
  22             # find all users
  23             result = l.search_s("OU=INSTITUT,DC=hpi,DC=uni-potsdam,DC=de", ldap.SCOPE_SUBTREE, filterstr="(&(&(objectClass=user)(!(objectClass=computer)))(givenName=*)(sn=*)(mail=*))")
  24             for _, attrs in result:
  25                 try:
  26                     user = UserProfile.objects.get(username__iexact=attrs['sAMAccountName'][0])
  27                     user.first_name = attrs['givenName'][0]
  28                     user.last_name = attrs['sn'][0]
  29                     user.email = attrs['mail'][0]
  30                     user.save()
  31 
  32                     print("Successfully updated: '{0}'".format(user.username))
  33                 except UserProfile.DoesNotExist:
  34                     pass
  35                 except Exception as e:
  36                     print(e)
  37 
  38             l.unbind_s()
  39 
  40         except KeyboardInterrupt:
  41             sys.stderr.write("\nOperation cancelled.\n")
  42             sys.exit(1)
  43
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/evap/evaluation/management/commands/import_ad.py b/evap/evaluation/management/commands/import_ad.py
  deleted file mode 100644
  --- a/evap/evaluation/management/commands/import_ad.py
  +++ /dev/null
  @@ -1,42 +0,0 @@
  -import getpass
  -import ldap
  -import sys
  -
  -from django.core.management.base import BaseCommand
  -
  -from evap.evaluation.models import UserProfile
  -
  -
  -class Command(BaseCommand):
  -    args = '<ldap server> <username>'
  -    help = 'Imports user data from Active Directory. The username should be specified with realm.'
  -
  -    def handle(self, *args, **options):
  -        try:
  -            # connect
  -            l = ldap.initialize(args[0])
  -
  -            # bind
  -            l.bind_s(args[1], getpass.getpass("AD Password: "))
  -
  -            # find all users
  -            result = l.search_s("OU=INSTITUT,DC=hpi,DC=uni-potsdam,DC=de", ldap.SCOPE_SUBTREE, filterstr="(&(&(objectClass=user)(!(objectClass=computer)))(givenName=*)(sn=*)(mail=*))")
  -            for _, attrs in result:
  -                try:
  -                    user = UserProfile.objects.get(username__iexact=attrs['sAMAccountName'][0])
  -                    user.first_name = attrs['givenName'][0]
  -                    user.last_name = attrs['sn'][0]
  -                    user.email = attrs['mail'][0]
  -                    user.save()
  -
  -                    print("Successfully updated: '{0}'".format(user.username))
  -                except UserProfile.DoesNotExist:
  -                    pass
  -                except Exception as e:
  -                    print(e)
  -
  -            l.unbind_s()
  -
  -        except KeyboardInterrupt:
  -            sys.stderr.write("\nOperation cancelled.\n")
  -            sys.exit(1)
  
verification_info: {"golden_diff": "diff --git a/evap/evaluation/management/commands/import_ad.py b/evap/evaluation/management/commands/import_ad.py\ndeleted file mode 100644\n--- a/evap/evaluation/management/commands/import_ad.py\n+++ /dev/null\n@@ -1,42 +0,0 @@\n-import getpass\n-import ldap\n-import sys\n-\n-from django.core.management.base import BaseCommand\n-\n-from evap.evaluation.models import UserProfile\n-\n-\n-class Command(BaseCommand):\n-    args = '<ldap server> <username>'\n-    help = 'Imports user data from Active Directory. The username should be specified with realm.'\n-\n-    def handle(self, *args, **options):\n-        try:\n-            # connect\n-            l = ldap.initialize(args[0])\n-\n-            # bind\n-            l.bind_s(args[1], getpass.getpass(\"AD Password: \"))\n-\n-            # find all users\n-            result = l.search_s(\"OU=INSTITUT,DC=hpi,DC=uni-potsdam,DC=de\", ldap.SCOPE_SUBTREE, filterstr=\"(&(&(objectClass=user)(!(objectClass=computer)))(givenName=*)(sn=*)(mail=*))\")\n-            for _, attrs in result:\n-                try:\n-                    user = UserProfile.objects.get(username__iexact=attrs['sAMAccountName'][0])\n-                    user.first_name = attrs['givenName'][0]\n-                    user.last_name = attrs['sn'][0]\n-                    user.email = attrs['mail'][0]\n-                    user.save()\n-\n-                    print(\"Successfully updated: '{0}'\".format(user.username))\n-                except UserProfile.DoesNotExist:\n-                    pass\n-                except Exception as e:\n-                    print(e)\n-\n-            l.unbind_s()\n-\n-        except KeyboardInterrupt:\n-            sys.stderr.write(\"\\nOperation cancelled.\\n\")\n-            sys.exit(1)\n", "issue": "Test management commands\nBecause in three years, run_tasks will silently fail on the production system and nobody will notice.\n- [x] **run_tasks** - shouldn't be too hard and is rather important\n- [x] **anonymize** - might be a bit of work to cover it properly, but should be straightforward.\n- [x] **refresh_results_cache** - should be easy\n- [x] **dump_testdata** - don't know how not to overwrite the file during testing, but should be possible\n\nthe other commands are already tested or rather unsuitable for testing\n- [x] **merge_users** - already has a test (#703) and is shown to be pretty broken.\n- [x] **run** - don't know how to test this and there isn't really anything that could break. still, somehow running it to check that it doesn't crash right away on e.g. imports would be cool\n- [x] **reload_testdata** - don't know whether it's possible at all to test that, i mean it drops the whole database...\n- [ ] **import_ad** - we never used it and i don't know whether it's feasible to mock ldap\n\nuse `self.stdout.write` instead of `print` and `call_command(\"command_name\", stdout=StringIO())` to avoid console output during tests. don't know what to do about calls to `input`.\n\n", "before_files": [{"content": "import getpass\nimport ldap\nimport sys\n\nfrom django.core.management.base import BaseCommand\n\nfrom evap.evaluation.models import UserProfile\n\n\nclass Command(BaseCommand):\n    args = '<ldap server> <username>'\n    help = 'Imports user data from Active Directory. The username should be specified with realm.'\n\n    def handle(self, *args, **options):\n        try:\n            # connect\n            l = ldap.initialize(args[0])\n\n            # bind\n            l.bind_s(args[1], getpass.getpass(\"AD Password: \"))\n\n            # find all users\n            result = l.search_s(\"OU=INSTITUT,DC=hpi,DC=uni-potsdam,DC=de\", ldap.SCOPE_SUBTREE, filterstr=\"(&(&(objectClass=user)(!(objectClass=computer)))(givenName=*)(sn=*)(mail=*))\")\n            for _, attrs in result:\n                try:\n                    user = UserProfile.objects.get(username__iexact=attrs['sAMAccountName'][0])\n                    user.first_name = attrs['givenName'][0]\n                    user.last_name = attrs['sn'][0]\n                    user.email = attrs['mail'][0]\n                    user.save()\n\n                    print(\"Successfully updated: '{0}'\".format(user.username))\n                except UserProfile.DoesNotExist:\n                    pass\n                except Exception as e:\n                    print(e)\n\n            l.unbind_s()\n\n        except KeyboardInterrupt:\n            sys.stderr.write(\"\\nOperation cancelled.\\n\")\n            sys.exit(1)\n", "path": "evap/evaluation/management/commands/import_ad.py"}], "after_files": [{"content": null, "path": "evap/evaluation/management/commands/import_ad.py"}]}