toolshed/deploy/dev/instance_b/testdata.py
2024-08-25 19:39:33 +02:00

108 lines
5.1 KiB
Python

import base64
def create_test_data():
print('Creating test data for instance B')
from authentication.models import ToolshedUser
admin = None
try:
if ToolshedUser.objects.filter(username='admin').exists():
admin = ToolshedUser.objects.get(username='admin')
else:
admin = ToolshedUser.objects.create_superuser('admin', 'admin@localhost', '')
admin.set_password('j7Th5TfCGUZmQ7U')
admin.save()
print('Created {}@{} with private key {} and public key {}'.format(admin.username, admin.domain,
admin.private_key,
admin.public_identity.public_key))
except Exception as e:
print('Admin user already exists, skipping')
from hostadmin.models import Domain
try:
domain1 = Domain.objects.create(name='localhost', owner=admin, open_registration=False)
domain1.save()
print('Created domain {} with closed registration'.format(domain1.name))
except Exception as e:
print('Domain localhost already exists, skipping')
domain2 = None
try:
if Domain.objects.filter(name='b.localhost').exists():
domain2 = Domain.objects.get(name='b.localhost')
domain2 = Domain.objects.create(name='b.localhost', owner=admin, open_registration=True)
domain2.save()
print('Created domain {} with open registration'.format(domain2.name))
except Exception as e:
print('Domain b.localhost already exists, skipping')
user = None
try:
if ToolshedUser.objects.filter(username='test_b').exists():
user = ToolshedUser.objects.get(username='test_b')
user = ToolshedUser.objects.create_user('test_b', 'testb@example.com', '', domain=domain2.name,
private_key='2ec1e7d5f5b8d5f87233944970d57f942095fe9e6c4fc49edde61fcd3fb1bf40')
user.set_password('test_b')
user.save()
print('Created user {}@{} with private key {} and public key {}'.format(user.username, user.domain,
user.private_key,
user.public_identity.public_key))
except Exception as e:
print('User foobar already exists, skipping')
from authentication.models import KnownIdentity
identity = None
try:
if KnownIdentity.objects.filter(username='test_a').exists():
identity = KnownIdentity.objects.get(username='test_a')
identity = KnownIdentity.objects.create(username='test_a', domain='a.localhost',
public_key='cdcf6f1897b0ab3a123047d9b707d4123f01daf41921dbe787d1d0697f0ee42b')
# pk '4ab79601edc400fd0263c7d5fd045ea7f5de28f1dd8905e2479f96893f3257d8'
identity.save()
print('Created identity {}@{} with public key {}'.format(identity.username, identity.domain,
identity.public_key))
except Exception as e:
print('Identity already exists, skipping')
from toolshed.models import Category
category1 = None
try:
if Category.objects.filter(name='Test Category').exists():
category1 = Category.objects.get(name='Test Category')
category1 = Category.objects.get_or_create(name='Test Category', origin='test')[0]
except Exception as e:
print('Category already exists, skipping')
from toolshed.models import InventoryItem
item1 = None
try:
if InventoryItem.objects.filter(name='Test Item 3').exists():
item1 = InventoryItem.objects.get(name='Test Item 3')
item1 = InventoryItem.objects.create(name='Test Item 3', description='This is a test item', category=category1,
availability_policy='sell', owned_quantity=1, owner=user)
item1.save()
except Exception as e:
print('Item already exists, skipping')
try:
item2 = InventoryItem.objects.create(name='Test Item 4', description='This is a test item', category=category1,
availability_policy='lend', owned_quantity=1, owner=user)
item2.save()
print('Created test items with IDs "{}" and "{}"'.format(item1.id, item2.id))
except Exception as e:
print('Item already exists, skipping')
try:
user.friends.add(identity)
print('Added identity {} to friends list of user {}'.format(identity.username, user.username))
except Exception as e:
print('Identity already in friends list, skipping')
from files.models import File
try:
file1 = File.objects.create(mime_type='text/plain', data=base64.b64encode(b'testcontent1').decode('utf-8'))
file1.save()
print(f'Created file at /{file1.hash[:2]}/{file1.hash[2:4]}/{file1.hash[4:6]}/{file1.hash[6:]}')
except Exception as e:
print('File already exists, skipping')