109 lines
5.1 KiB
Python
109 lines
5.1 KiB
Python
|
import base64
|
||
|
|
||
|
|
||
|
def create_test_data():
|
||
|
print('Creating test data for instance A')
|
||
|
|
||
|
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='a.localhost').exists():
|
||
|
domain2 = Domain.objects.get(name='a.localhost')
|
||
|
domain2 = Domain.objects.create(name='a.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_a').exists():
|
||
|
user = ToolshedUser.objects.get(username='test_a')
|
||
|
user = ToolshedUser.objects.create_user('test_a', 'testa@example.com', '', domain=domain2.name,
|
||
|
private_key='4ab79601edc400fd0263c7d5fd045ea7f5de28f1dd8905e2479f96893f3257d8')
|
||
|
user.set_password('test_a')
|
||
|
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_b').exists():
|
||
|
identity = KnownIdentity.objects.get(username='test_b')
|
||
|
identity = KnownIdentity.objects.create(username='test_b', domain='b.localhost',
|
||
|
public_key='4b0dffe21764c591762615ef84cfea7fd4055fab3e9f58ae077fd8c79c34af91')
|
||
|
# pk '2ec1e7d5f5b8d5f87233944970d57f942095fe9e6c4fc49edde61fcd3fb1bf40'
|
||
|
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').exists():
|
||
|
item1 = InventoryItem.objects.get(name='Test Item')
|
||
|
item1 = InventoryItem.objects.create(name='Test Item', description='This is a test item', category=category1,
|
||
|
availability_policy='rent', owned_quantity=1, owner=user)
|
||
|
item1.save()
|
||
|
except Exception as e:
|
||
|
print('Item already exists, skipping')
|
||
|
|
||
|
try:
|
||
|
item2 = InventoryItem.objects.create(name='Test Item 2', description='This is a test item', category=category1,
|
||
|
availability_policy='share', 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')
|