Skip to content

Commit b899432

Browse files
authored
Merge pull request #2642 from dandi/create-test-user-command
Add create_test_user management command
2 parents a7d8913 + a377ff9 commit b899432

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from __future__ import annotations
2+
3+
from allauth.socialaccount.models import SocialAccount
4+
from django.contrib.auth.models import User
5+
from django.db import transaction
6+
import djclick as click
7+
import faker
8+
9+
from dandiapi.api.models.user import UserMetadata
10+
11+
12+
@click.command()
13+
@click.option(
14+
'--auto-approve',
15+
is_flag=True,
16+
help='Auto approve this user, skipping the questionnaire.',
17+
)
18+
@click.option(
19+
'--password',
20+
default='password',
21+
show_default=True,
22+
help='The password for this user.',
23+
)
24+
def create_test_user(*, auto_approve: bool, password: str):
25+
fake = faker.Faker()
26+
27+
with transaction.atomic():
28+
email = fake.email()
29+
user = User.objects.create(
30+
first_name=fake.first_name(),
31+
last_name=fake.last_name(),
32+
username=email,
33+
email=email,
34+
)
35+
36+
user.set_password(password)
37+
user.save()
38+
39+
UserMetadata.objects.create(
40+
user=user,
41+
status=UserMetadata.Status.APPROVED if auto_approve else UserMetadata.Status.INCOMPLETE,
42+
)
43+
44+
uid = fake.random_number(digits=8, fix_len=True)
45+
SocialAccount.objects.create(
46+
user=user,
47+
provider='github',
48+
uid=uid,
49+
extra_data={
50+
'id': uid,
51+
'name': user.get_full_name(),
52+
'email': user.email,
53+
'login': fake.user_name(),
54+
},
55+
)
56+
57+
click.echo(
58+
click.style(
59+
f'Created user "{user.email}" with password "{password}"',
60+
fg='green',
61+
bold=True,
62+
)
63+
)

0 commit comments

Comments
 (0)