Commit 9442403e authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Add some unit tests for models

parent 0df7203c
from django.contrib import auth
from django.test import TestCase
#from django.test import Client
from django_backend.backend import models
from django_backend.backend.auth.v1 import models as auth_models
TEST_NAME = 'test_name'
TEST_EMAIL = 'test@hdf.org'
TEST_SUB = 'fb0fa558-cfa2-49f9-b847-5c651d1f6135'
TEST_KEY = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC/7NWc1pxCxi1f/+DtgCenIC9DDiT6l1cQSz4vJc1ecJA/gEUkd6CF/uR1Zl+yalWZej9UII5902Dm8WkIsIrHdRXevyt0QSzMbzZvaUAdJWcmWpqEKxJT6KGkd50O+qAJBXd5WZ2jUstmbHjoXio9eBY4hDcOofLD8vJT73DrZAEurd3F2S4s04oSt+onUmQ0inqeN38oqYOZGJhe/zZsDj65ctiTpj85rjglndNq+K7IHETGWXg9ht13FH9trjtEpLmAmR9UZGFkof+P+LiIUSYa05sVh6/MPFCgz7gzMCXw99lxoMz4lX2/wV/jnJ0+itn1WByWFWuqbDXUnH4pjk7K+YalFi2lA7F4ckWSn51IQRnh8Qp3CMuvid/Y3Q0qYa4Mtq5zhDnj+uGAUltoENqQCIKLp2KaCRQSbiDudYHXq9dufGRuAsls3rXnRhRJ26wOW+sggjfhB6LhGD20vSdJbuQQwUxZtRLB+y6uP6HXq/eqq+N+f8lfsw64b16xTOYFSpDniV/OE+PuDSNUFlO1V91FWhhiFWCyi+q6dTW5PTWBnnksXhR/LRaivA+UuNxPPbEfefBWmu7/y3jcqAn+CXSzbnvOrVXCU3E7DKBCR3d8TjBQj2No1qTNz6ZOD+dXEQvvjhl2lXkdGazG18hpaWN2hApzBpUulukg9Q== foo@bar'
TEST_PASSWORD = 'asnoteuhsn'
TEST_USERINFO = {
'email': TEST_EMAIL,
'name': TEST_NAME,
'sub': TEST_SUB,
}
def setup_fixture():
idp = auth_models.OIDCConfig(
name=TEST_NAME,
client_id='',
client_secret='',
redirect_uri='',
issuer_uri='',
)
idp.save()
user = models.User(
username=TEST_NAME,
sub=TEST_NAME,
user_type='oidcuser',
userinfo={
'email': TEST_NAME,
'sub': TEST_NAME,
},
idp=idp,
)
user.set_password(TEST_PASSWORD)
user.save()
site = models.Site(
name=TEST_NAME,
description=TEST_NAME,
)
site.save()
service = models.Service(
name=TEST_NAME,
description=TEST_NAME,
)
service.save()
service.site.add(site)
service.save()
models.SSHPublicKey(
name=TEST_NAME,
key=TEST_KEY,
user=user,
).save()
def teardown_fixture():
models.SSHPublicKey.objects.get(name=TEST_NAME).delete()
models.User.objects.get(username=TEST_NAME).delete()
auth_models.OIDCConfig.objects.get(name=TEST_NAME).delete()
class UserTest(TestCase):
def setUp(self):
setup_fixture()
def tearDown(self):
teardown_fixture()
def test_user(self):
user = auth.authenticate(
username=TEST_NAME,
password=TEST_PASSWORD,
)
self.assertIsNotNone(user)
self.assertTrue(user.is_active)
user.deactivate()
self.assertFalse(user.is_active)
user.activate()
self.assertTrue(user.is_active)
def test_user_construction(self):
idp = auth_models.OIDCConfig.objects.get(name=TEST_NAME)
user = models.User.get_user(
TEST_USERINFO,
idp,
)
self.assertIsNotNone(user)
class DeploymentTest(TestCase):
def setUp(self):
setup_fixture()
def tearDown(self):
teardown_fixture()
def test_deployment(self):
user = models.User.objects.get(username=TEST_NAME)
key = models.SSHPublicKey.objects.get(name=TEST_NAME)
service = models.Service.objects.get(name=TEST_NAME)
site = models.Site.objects.get(name=TEST_NAME)
deployment = models.Deployment.get_deployment(user, service)
self.assertIsNotNone(deployment)
self.assertFalse(deployment.deploys.exists())
self.assertFalse(deployment.withdrawals.exists())
models.DeploymentTask.construct_deployment_task(
deployment=deployment,
key=key,
)
self.assertTrue(deployment.deploys.exists())
self.assertFalse(deployment.withdrawals.exists())
withdrawal_task = models.DeploymentTask.construct_withdrawal_task(
deployment=deployment,
key=key,
)
self.assertFalse(deployment.deploys.exists())
self.assertTrue(deployment.withdrawals.exists())
withdrawal_task.item_finished(site)
self.assertFalse(deployment.deploys.exists())
self.assertFalse(deployment.withdrawals.exists())
#class ViewTest(TestCase):
# def test_view(self):
# client = Client()
# client.get('/backend/api/state')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment