# trial --help # https://twistedmatrix.com/trac/wiki/TwistedTrial
# trial --temp-directory=/tmp/_trial_temp -e test/libs/test_server_license.py
# http://twistedmatrix.com/documents/current/core/howto/trial.html
import json
import os
import tempfile
import unittest
from datetime import datetime, timedelta
from twisted.internet import defer
from libs.server_license import ServerLicense
class ServerLicenseTest(unittest.TestCase):
VALID_LICENSE = """{
"license": "Y1MRXuDsD8/djbUPi1+q4VHOx9LVIHaYn4w2OnFcpWlm/whypdZOujXL2MxvbtJgpWcJ8E8Sp6C+4Vc+",
"license_parameters": {
"ContactEmail": "jpfeiffer@example.com",
"LicenseExpiryDate": "2014-12-29",
}
}"""
EXPIRED_LICENSE = """{
"license": "+hrsQxm9gVYWnNppsVBm1tbYpquxKTrw+Y1MRXuDsD8/djbUPi1+",
"license_parameters": {
"ContactEmail": "jpfeiffer@example.com",
"LicenseExpiryDate": "2014-12-29",
}
}"""
location = None
def setUp(self):
self.location = self.write_license_file(ServerLicenseTest.VALID_LICENSE)
self.license = ServerLicense(license_location=self.location)
def tearDown(self):
os.remove(self.location)
def test_constructor(self):
self.assertIsNotNone(self.license.get_license())
def test_is_valid_success_true(self):
self.assertTrue(self.license.is_valid())
def test_is_valid_expired_false(self):
location = self.write_license_file(ServerLicenseTest.EXPIRED_LICENSE, 'test_license_expired.json')
try:
self.assertFalse(self.license.is_valid())
finally:
if os.path.isfile(location):
os.remove(location)
# Helpers
def write_license_file(self, text, filename='test_license.json'):
location = os.path.join(tempfile.gettempdir(), filename)
with open(location, 'w') as f:
f.write(text)
return location
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# instead of assertRaises real async integrated systems need assertFailure to handle the timing/deferreds
# coding=UTF-8
from mock import Mock
from twisted.internet import defer
from myservice.error import ValidationError
from myservice.principal import Principal
from myservice.user.user import User
from myservice.user.user_manager import UserManager
from myservice.error import PermissionError
...
@defer.inlineCallbacks
def test_get_active_user_with_mention(self):
user = User(1, self.core)
self.patch("myservice.user.user_manager.User.fetch_by_mention_name", return_value=user)
result = yield self.mgr.get_active_user(self.principal, "@foo")
self.assertEquals(user, result)
self.assertFailure(self.mgr.get_active_user(self.principal, None), ValidationError)
self.assertFailure(self.mgr.get_active_user(self.principal, ""), ValidationError)