Page MenuHomeSoftware Heritage
Paste P1311

trying to create a basic user with the proper add-forge-now permissions and it's just making excuses not to create it...
ActivePublic

Authored by ardumont on Mar 15 2022, 11:11 AM.
swh/web/tests/create_test_users.py:
```
from swh.web.auth.utils import SWH_AMBASSADOR_PERMISSION, SWH_MODERATOR_PERMISSION
from swh.web.tests.utils import create_django_permission
User = get_user_model()
users: Dict[str, Tuple[str, str, List[str]]] = {
"user": ("user", "user@swh-web.org", []),
"ambassador": (
"ambassador",
"ambassador@swh-web.org",
[SWH_AMBASSADOR_PERMISSION],
),
"moderator": ("moderator", "moderator@swh-web.org", [SWH_MODERATOR_PERMISSION],),
}
...
```
And then kaboom:
```
$ pytest -x -s swh/web/tests/test_create_users.py::test_create_users_test_users_exist
================================================================================================================== FAILURES ===================================================================================================================
_____________________________________________________________________________________________________ test_create_users_test_users_exist ______________________________________________________________________________________________________
self = <django.db.backends.utils.CursorWrapper object at 0x7f0092697a90>, sql = 'INSERT INTO "django_content_type" ("id", "app_label", "model") VALUES (%s, %s, %s)', params = (1000, 'swh.web.add_forge_now', 'dummy')
ignored_wrapper_args = (False, {'connection': <django.db.backends.postgresql.base.DatabaseWrapper object at 0x7f00af362cd0>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0x7f0092697a90>})
def _execute(self, sql, params, *ignored_wrapper_args):
self.db.validate_no_broken_transaction()
with self.db.wrap_database_errors:
if params is None:
return self.cursor.execute(sql)
else:
> return self.cursor.execute(sql, params)
E psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "django_content_type_pkey"
E DETAIL: Key (id)=(1000) already exists.
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/backends/utils.py:84: UniqueViolation
The above exception was the direct cause of the following exception:
db = None
def test_create_users_test_users_exist(db):
> from .create_test_users import User, users
swh/web/tests/test_create_users.py:8:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
<frozen importlib._bootstrap>:1007: in _find_and_load
???
<frozen importlib._bootstrap>:986: in _find_and_load_unlocked
???
<frozen importlib._bootstrap>:680: in _load_unlocked
???
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/_pytest/assertion/rewrite.py:170: in exec_module
exec(co, module.__dict__)
swh/web/tests/create_test_users.py:32: in <module>
permission = create_django_permission(perm_name)
swh/web/tests/utils.py:230: in create_django_permission
content_type = ContentType.objects.create(
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/models/manager.py:82: in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/models/query.py:422: in create
obj.save(force_insert=True, using=self.db)
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/models/base.py:743: in save
self.save_base(using=using, force_insert=force_insert,
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/models/base.py:780: in save_base
updated = self._save_table(
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/models/base.py:873: in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/models/base.py:910: in _do_insert
return manager._insert([self], fields=fields, return_id=update_pk,
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/models/manager.py:82: in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/models/query.py:1186: in _insert
return query.get_compiler(using=using).execute_sql(return_id)
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/models/sql/compiler.py:1377: in execute_sql
cursor.execute(sql, params)
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/backends/utils.py:67: in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/backends/utils.py:76: in _execute_with_wrappers
return executor(sql, params, many, context)
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/backends/utils.py:84: in _execute
return self.cursor.execute(sql, params)
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/utils.py:89: in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <django.db.backends.utils.CursorWrapper object at 0x7f0092697a90>, sql = 'INSERT INTO "django_content_type" ("id", "app_label", "model") VALUES (%s, %s, %s)', params = (1000, 'swh.web.add_forge_now', 'dummy')
ignored_wrapper_args = (False, {'connection': <django.db.backends.postgresql.base.DatabaseWrapper object at 0x7f00af362cd0>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0x7f0092697a90>})
def _execute(self, sql, params, *ignored_wrapper_args):
self.db.validate_no_broken_transaction()
with self.db.wrap_database_errors:
if params is None:
return self.cursor.execute(sql)
else:
> return self.cursor.execute(sql, params)
E django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_content_type_pkey"
E DETAIL: Key (id)=(1000) already exists.
/home/tony/.virtualenvs/swh/lib/python3.9/site-packages/django/db/backends/utils.py:84: IntegrityError

Event Timeline

ardumont edited the content of this paste. (Show Details)

found it create_django_permissions hard-codes stuff, dropping the hard-coded stuff and it's fine again.