def test_get_by_token(self): TEST_MISSING_AUTH_TOKEN = hashlib.sha1('notAToken').hexdigest() # Token not a sha1, but exists in system cred = nova_models.CredentialsAuthorization.get_by_token( TEST_BAD_AUTH_TOKEN) self.assertTrue(cred is None) # Token doesn't exist cred = nova_models.CredentialsAuthorization.get_by_token( TEST_MISSING_AUTH_TOKEN) self.assertTrue(cred is None) # Good token cred = nova_models.CredentialsAuthorization.get_by_token( TEST_AUTH_TOKEN) self.assertTrue(cred is not None) # Expire the token cred.auth_date = utils.utcnow() - AUTH_EXPIRATION_LENGTH \ - HOUR cred.save() # Expired token cred = nova_models.CredentialsAuthorization.get_by_token( TEST_AUTH_TOKEN) self.assertTrue(cred is None)
def test_auth_token_expired(self): ''' Test expired in past, expires in future, expires _right now_ ''' cred = \ nova_models.CredentialsAuthorization.get_by_token(TEST_AUTH_TOKEN) cred.auth_date = utils.utcnow() - AUTH_EXPIRATION_LENGTH \ - HOUR self.assertTrue(cred.auth_token_expired()) cred.auth_date = utils.utcnow() self.assertFalse(cred.auth_token_expired()) # testing with time is tricky. Mock out "right now" test to avoid # timing issues time = utils.utcnow.override_time = utils.utcnow() cred.auth_date = time - AUTH_EXPIRATION_LENGTH self.assertTrue(cred.auth_token_expired()) utils.utcnow.override_time = None
def usage(request, tenant_id=None): today = utils.today() date_start = datetime.date(today.year, today.month, 1) datetime_start = datetime.datetime.combine(date_start, utils.time()) datetime_end = utils.utcnow() show_terminated = request.GET.get('show_terminated', False) usage = {} if not tenant_id: tenant_id = request.user.tenant try: usage = api.usage_get(request, tenant_id, datetime_start, datetime_end) except api_exceptions.ApiException, e: LOG.error('ApiException in instance usage', exc_info=True) messages.error(request, 'Unable to get usage info: %s' % e.message)
def usage(request, tenant_id=None): today = utils.today() date_start = datetime.date(today.year, today.month, 1) datetime_start = datetime.datetime.combine(date_start, utils.time()) datetime_end = utils.utcnow() show_terminated = request.GET.get('show_terminated', False) usage = {} if not tenant_id: tenant_id = request.user.tenant_id try: usage = api.usage_get(request, tenant_id, datetime_start, datetime_end) except api_exceptions.ApiException, e: LOG.exception(_('ApiException in instance usage')) messages.error(request, _('Unable to get usage info: %s') % e.message)
import mox import random from django import test from django.conf import settings from django.db.models.signals import post_save from django_openstack import models as nova_models from django_openstack import utils from django_openstack.core import connection from nova_adminclient import NovaAdminClient TEST_USER = '******' TEST_PROJECT = 'testProject' TEST_AUTH_TOKEN = hashlib.sha1('').hexdigest() TEST_AUTH_DATE = utils.utcnow() TEST_BAD_AUTH_TOKEN = 'badToken' HOUR = datetime.timedelta(seconds=3600) AUTH_EXPIRATION_LENGTH = \ datetime.timedelta(days=int(settings.CREDENTIAL_AUTHORIZATION_DAYS)) class CredentialsAuthorizationTests(test.TestCase): @classmethod def setUpClass(cls): # these post_save methods interact with external resources, shut them # down to test credentials post_save.disconnect(sender=nova_models.CredentialsAuthorization, dispatch_uid='django_openstack.CredentialsAuthorization.post_save') post_save.disconnect(sender=nova_models.CredentialsAuthorization,
import hashlib import mox import random from django import test from django.conf import settings from django.db.models.signals import post_save from django_openstack import models as nova_models from django_openstack import utils from django_openstack.core import connection from nova_adminclient import NovaAdminClient TEST_USER = '******' TEST_PROJECT = 'testProject' TEST_AUTH_TOKEN = hashlib.sha1('').hexdigest() TEST_AUTH_DATE = utils.utcnow() TEST_BAD_AUTH_TOKEN = 'badToken' HOUR = datetime.timedelta(seconds=3600) AUTH_EXPIRATION_LENGTH = \ datetime.timedelta(days=int(settings.CREDENTIAL_AUTHORIZATION_DAYS)) class CredentialsAuthorizationTests(test.TestCase): @classmethod def setUpClass(cls): # these post_save methods interact with external resources, shut them # down to test credentials post_save.disconnect( sender=nova_models.CredentialsAuthorization, dispatch_uid='django_openstack.CredentialsAuthorization.post_save')