尽管单元测试正常运行,但 Authtoken 迁移不起作用
Posted
技术标签:
【中文标题】尽管单元测试正常运行,但 Authtoken 迁移不起作用【英文标题】:Authtoken migration not working despite unit test functioning 【发布时间】:2020-03-14 02:39:02 【问题描述】:我正在将TokenAuthentication 添加到我们的 django 项目中。一切进展顺利,我为令牌身份验证添加了迁移和单元测试:
# Migration
from django.db import migrations
def create_missing_tokens(apps, schema_editor):
"""
Tokens were added in 0002_auto_20160226_1747, we thus need to populate
the tokens table for existing users
"""
Token = apps.get_model('authtoken', 'Token')
User = apps.get_model('accounts', 'CustomUser')
for user in User.objects.all():
Token.objects.get_or_create(user=user)
class Migration(migrations.Migration):
dependencies = [
# depends on authtoken migration
('accounts', '0003_subscription_max_updates_per_day'),
('authtoken', '0002_auto_20160226_1747'), # latest migration in the authtoken package
]
operations = [
migrations.RunPython(create_missing_tokens, reverse_code=migrations.RunPython.noop),
]
# unit test
class MigrationTestCase(TransactionTestCase):
'''A Test case for testing migrations'''
# These must be defined by subclasses.
migrate_from = None
migrate_to = None
def setUp(self):
super(MigrationTestCase, self).setUp()
self.executor = MigrationExecutor(connection)
self.executor.migrate(self.migrate_from)
def migrate_to_dest(self):
self.executor.loader.build_graph() # reload.
self.executor.migrate(self.migrate_to)
@property
def old_apps(self):
return self.executor.loader.project_state(self.migrate_from).apps
@property
def new_apps(self):
return self.executor.loader.project_state(self.migrate_to).apps
from accounts.models import CustomUserManager
class SummaryTestCase(MigrationTestCase):
"""
We need to test that data is populated in the summary field on running the migration
"""
migrate_from = [('accounts', '0003_subscription_max_updates_per_day')]
migrate_to = [('accounts', '0004_create_tokens')]
def setup_before_migration(self):
manager = CustomUserManager()
User = self.old_apps.get_model('accounts', 'CustomUser')
manager.model = User
manager.create_user(email='contact@a.fr', # nosec
password='kjnfrkj',
)
def test_token_populated(self):
# runs setup
self.setup_before_migration()
# now migrate
self.migrate_to_dest()
# grab new models
Token = self.new_apps.get_model('authtoken', 'Token')
User = self.new_apps.get_model('accounts', 'CustomUser')
for user in User.objects.all():
self.assertTrue(Token.objects.filter(user_id=user.pk).exists())
这很好用,但是当我实际运行迁移时,我收到消息:
django.db.utils.IntegrityError: 重复的键值违反了唯一性 约束“authtoken_token_pkey”详细信息:密钥(密钥)=()已经 存在。
这里是我所说的“实际运行迁移”的一些伪代码:
$ git checkout <old commit> # grab old commit
$ ./run.sh go # spin up docker with server and db
$ git checkout master # which includes migrations
$ ./run.sh again # log into docker image with django
$ (docker) python manage.py migrate # run the migrations
因此我看到的错误是(问题末尾的完整堆栈跟踪):
django.db.utils.IntegrityError: duplicate key value violates unique constraint "authtoken_token_pkey"
DETAIL: Key (key)=() already exists.
我无法理解使用 Token.objects.get_or_create(user=user)
的迁移如何获得重复的密钥?任何帮助将不胜感激
Applying accounts.0004_create_tokens...Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 538, in get_or_create
return self.get(**kwargs), False
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 408, in get
self.model._meta.object_name
__fake__.DoesNotExist: Token matching query does not exist.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "authtoken_token_pkey"
DETAIL: Key (key)=() already exists.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
self.code(from_state.apps, schema_editor)
File "/code/accounts/migrations/0004_create_tokens.py", line 12, in create_missing_tokens
Token.objects.get_or_create(user=user)
File "/usr/local/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 541, in get_or_create
return self._create_object_from_params(kwargs, params)
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 583, in _create_object_from_params
raise e
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 575, in _create_object_from_params
obj = self.create(**params)
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 422, in create
obj.save(force_insert=True, using=self.db)
File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 741, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 779, in save_base
force_update, using, update_fields,
File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 870, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 908, in _do_insert
using=using, raw=raw)
File "/usr/local/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1335, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "authtoken_token_pkey"
DETAIL: Key (key)=() already exists.
编辑:自定义用户类没有什么特别之处,如下所示:
class CustomUser(AbstractUser):
"""
Replace username by email as required and unique.
"""
is_alphanumeric_or_dash = RegexValidator(r'^[0-9a-zA-Z\-]*$', 'Only alphanumeric and "-" characters are allowed.')
# Hide username
username = None
# Overidde other fields
email = models.EmailField(_('email address'), unique=True)
first_name = models.CharField(_('first name'),
max_length=100,
blank=True,
validators=[is_alphanumeric_or_dash])
last_name = models.CharField(_('last name'),
max_length=100,
blank=True,
validators=[is_alphanumeric_or_dash])
# /!\ At some point, user should have a default subcription /!\
subscription = models.ForeignKey(Subscription, on_delete=models.PROTECT, blank=True, null=True)
# some other fields, but nothing special...
USERNAME_FIELD = 'email'
# Override the UserManager with our custom one (for objects creation)
objects = CustomUserManager()
【问题讨论】:
你的CustomUser
班级是什么样子的?
我猜你的数据库在你设置的时候不知何故被吓坏了。您是否尝试过打开 shell 并检查现有的 Token
对象?
@Ben 更新了问题,感谢您的关注
我怀疑username = None
是个问题。但我没有任何确凿的答案。我想我还会查看以下内容:1. AUTH_USER_MODEL = 'accounts.CustomUser',2. INSTALLED_APPS 设置正确,3.确定python manage.py drf_create_token <username>
有效
【参考方案1】:
Token
的密钥通常由其save()
方法生成。当您手动生成令牌时,这很好,但在迁移中,您通过 apps.get_model()
、none of the custom model methods are available 引用模型。
所以发生的事情是使用空键生成令牌。第一个会起作用,但之后会产生这个错误,因为键不是唯一的。
一个简单的解决方法是将 DRF 用于生成密钥的代码复制到您的迁移中。像这样的东西应该可以工作:
for user in User.objects.using(db_alias).all():
key = binascii.hexlify(os.urandom(20)).decode()
Token.objects.using(db_alias).get_or_create(user=user, key=key)
【讨论】:
以上是关于尽管单元测试正常运行,但 Authtoken 迁移不起作用的主要内容,如果未能解决你的问题,请参考以下文章