def __init__ ( self, config ) : global controller #self.base_uri = 'http://172.24.42.141/api' self.base_uri = config.SFA_CLAB_URL controller = Api(self.base_uri) #(config.CLAP_API_URL) try: controller.retrieve() except requests.exceptions.MissingSchema as e: raise MalformedURI(self.base_uri, e.message) except requests.exceptions.ConnectionError as e: raise UnexistingURI(self.base_uri, e.message) except ValueError: raise InvalidURI(self.base_uri) # Use of a default user for the C-Lab SFAWrap #self.username='******' #self.password='******' #self.groupname='vct' self.username = config.SFA_CLAB_USER self.password = config.SFA_CLAB_PASSWORD self.groupname = config.SFA_CLAB_GROUP controller.login(self.username, self.password) self.default_template = config.SFA_CLAB_DEFAULT_TEMPLATE
class BaseLiveServerTestCase(AppDependencyMixin, LiveServerTestCase): @classmethod def setUpClass(cls): cls.vdisplay = Xvfb() cls.vdisplay.start() cls.selenium = WebDriver() super(BaseLiveServerTestCase, cls).setUpClass() @classmethod def tearDownClass(cls): cls.selenium.quit() cls.vdisplay.stop() super(BaseLiveServerTestCase, cls).tearDownClass() def setUp(self): super(BaseLiveServerTestCase, self).setUp() self.rest = Api(self.live_server_url + '/api/') self.account = Account.objects.create(name='orchestra') self.username = '******' self.password = '******' self.user = User.objects.create_superuser(username='******', password='******', email='*****@*****.**', account=self.account) def admin_login(self): session = SessionStore() session[SESSION_KEY] = self.user.pk session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session.save() ## to set a cookie we need to first visit the domain. self.selenium.get(self.live_server_url + '/admin/') self.selenium.add_cookie(dict( name=settings.SESSION_COOKIE_NAME, value=session.session_key, # path='/', )) def rest_login(self): self.rest.login(username=self.username, password=self.password)
class BaseLiveServerTestCase(AppDependencyMixin, LiveServerTestCase): @classmethod def setUpClass(cls): # Avoid problems with the overlaping menu when clicking settings.ADMIN_TOOLS_MENU = 'admin_tools.menu.Menu' cls.vdisplay = Xvfb() cls.vdisplay.start() cls.selenium = WebDriver() super(BaseLiveServerTestCase, cls).setUpClass() @classmethod def tearDownClass(cls): cls.selenium.quit() cls.vdisplay.stop() super(BaseLiveServerTestCase, cls).tearDownClass() def create_account(self, username='', superuser=False): if not username: username = '******' % random_ascii(5) password = '******' self.account_password = password if superuser: return Account.objects.create_superuser( username, password=password, email='*****@*****.**') return Account.objects.create_user(username, password=password, email='*****@*****.**') def setUp(self): from orm.api import Api super(BaseLiveServerTestCase, self).setUp() self.rest = Api(self.live_server_url + '/api/') self.rest.enable_logging() self.account = self.create_account(superuser=True) def admin_login(self): session = SessionStore() session[SESSION_KEY] = self.account.id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session.save() ## to set a cookie we need to first visit the domain. self.selenium.get(self.live_server_url + '/admin/') self.selenium.add_cookie( dict( name=settings.SESSION_COOKIE_NAME, value=session.session_key, # path='/', )) def rest_login(self): self.rest.login(username=self.account.username, password=self.account_password) def take_screenshot(self): timestamp = datetime.datetime.now().isoformat().replace(':', '') filename = 'screenshot_%s_%s.png' % (self.id(), timestamp) path = '/home/orchestra/snapshots' self.selenium.save_screenshot(os.path.join(path, filename)) def admin_delete(self, obj): opts = obj._meta app_label, model_name = opts.app_label, opts.model_name delete = reverse('admin:%s_%s_delete' % (app_label, model_name), args=(obj.pk, )) url = self.live_server_url + delete self.selenium.get(url) confirmation = self.selenium.find_element_by_name('post') confirmation.submit() self.assertNotEqual(url, self.selenium.current_url) def admin_disable(self, obj): opts = obj._meta app_label, model_name = opts.app_label, opts.model_name change = reverse('admin:%s_%s_change' % (app_label, model_name), args=(obj.pk, )) url = self.live_server_url + change self.selenium.get(url) is_active = self.selenium.find_element_by_id('id_is_active') is_active.click() save = self.selenium.find_element_by_name('_save') save.submit() self.assertNotEqual(url, self.selenium.current_url) def admin_change_password(self, obj, password): opts = obj._meta app_label, model_name = opts.app_label, opts.model_name change_password = reverse('admin:%s_%s_change_password' % (app_label, model_name), args=(obj.pk, )) url = self.live_server_url + change_password self.selenium.get(url) password_field = self.selenium.find_element_by_id('id_password1') password_field.send_keys(password) password_field = self.selenium.find_element_by_id('id_password2') password_field.send_keys(password) password_field.submit() self.assertNotEqual(url, self.selenium.current_url)
class ApiTests(unittest.TestCase): def setUp(self): self.api = Api(os.environ['CONFINE_SERVER_API']) def test_attributes(self): self.assertEqual(['uri'], self.api._data.keys()) def test_self_retrieve(self): self.api.retrieve() self.assertEqual(3, len(self.api._data)) def test_managers(self): self.api.nodes with self.assertRaises(AttributeError): self.api.rata def test_login(self): self.assertRaises(self.api.ResponseStatusError, self.api.login) self.api.username = os.environ['CONFINE_USER'] self.api.password = os.environ['CONFINE_PASSWORD'] self.api.login() auth_header = self.api.DEFAULT_HEADERS['authorization'] self.assertLess(20, auth_header) def test_login_providing_credentials(self): username = os.environ['CONFINE_USER'] password = os.environ['CONFINE_PASSWORD'] self.api.login(username=username, password=password) self.assertEqual(self.api.username, username) self.assertEqual(self.api.password, password) auth_header = self.api.DEFAULT_HEADERS['authorization'] self.assertLess(20, auth_header) def test_logout(self): self.test_login() self.api.logout() self.assertNotIn('authorization', self.api.DEFAULT_HEADERS) def test_retrieve_base(self): group = self.api.groups.retrieve()[0] name = 'RandomNode-%s' % random_ascii(10) self.test_login() node = self.api.nodes.create(name=name, group=group) controller = Api(node.uri) controller.retrieve() self.assertEqual(self.api, controller) self.assertEqual(self.api._data, controller._data) def test_caching(self): api = Api(os.environ['CONFINE_SERVER_API'], cache=True) api.nodes.retrieve() self.assertEqual(0, api.cache.hits) self.assertEqual(2, api.cache.misses) nodes = api.nodes.retrieve() self.assertEqual(1, api.cache.hits) self.assertEqual(2, api.cache.misses) for node in nodes: node.retrieve() self.assertEqual(len(nodes) + 2, api.cache.misses) self.assertEqual(1, api.cache.hits) for node in nodes: node.retrieve() self.assertEqual(len(nodes) + 1, api.cache.hits) self.assertEqual(len(nodes) + 2, api.cache.misses) api.cache.invalidate() self.assertEqual(0, api.stats['conditional']) for node in nodes: node.retrieve() self.assertEqual(len(nodes), api.stats['conditional']) api.cache.remove() for node in nodes: node.retrieve() self.assertEqual(len(nodes), api.stats['conditional']) self.assertEqual((2 * len(nodes)) + 1, api.cache.hits) self.assertEqual((2 * len(nodes)) + 2, api.cache.misses)
class ApiTests(unittest.TestCase): def setUp(self): self.api = Api(os.environ['CONFINE_SERVER_API']) def test_attributes(self): self.assertEqual(['uri'], self.api._data.keys()) def test_self_retrieve(self): self.api.retrieve() self.assertEqual(3, len(self.api._data)) def test_managers(self): self.api.nodes with self.assertRaises(AttributeError): self.api.rata def test_login(self): self.assertRaises(self.api.ResponseStatusError, self.api.login) self.api.username = os.environ['CONFINE_USER'] self.api.password = os.environ['CONFINE_PASSWORD'] self.api.login() auth_header = self.api.DEFAULT_HEADERS['authorization'] self.assertLess(20, auth_header) def test_login_providing_credentials(self): username = os.environ['CONFINE_USER'] password = os.environ['CONFINE_PASSWORD'] self.api.login(username=username, password=password) self.assertEqual(self.api.username, username) self.assertEqual(self.api.password, password) auth_header = self.api.DEFAULT_HEADERS['authorization'] self.assertLess(20, auth_header) def test_logout(self): self.test_login() self.api.logout() self.assertNotIn('authorization', self.api.DEFAULT_HEADERS) def test_retrieve_base(self): group = self.api.groups.retrieve()[0] name = 'RandomNode-%s' % random_ascii(10) self.test_login() node = self.api.nodes.create(name=name, group=group) controller = Api(node.uri) controller.retrieve() self.assertEqual(self.api, controller) self.assertEqual(self.api._data, controller._data) def test_caching(self): api = Api(os.environ['CONFINE_SERVER_API'], cache=True) api.nodes.retrieve() self.assertEqual(0, api.cache.hits) self.assertEqual(2, api.cache.misses) nodes = api.nodes.retrieve() self.assertEqual(1, api.cache.hits) self.assertEqual(2, api.cache.misses) for node in nodes: node.retrieve() self.assertEqual(len(nodes)+2, api.cache.misses) self.assertEqual(1, api.cache.hits) for node in nodes: node.retrieve() self.assertEqual(len(nodes)+1, api.cache.hits) self.assertEqual(len(nodes)+2, api.cache.misses) api.cache.invalidate() self.assertEqual(0, api.stats['conditional']) for node in nodes: node.retrieve() self.assertEqual(len(nodes), api.stats['conditional']) api.cache.remove() for node in nodes: node.retrieve() self.assertEqual(len(nodes), api.stats['conditional']) self.assertEqual((2*len(nodes))+1, api.cache.hits) self.assertEqual((2*len(nodes))+2, api.cache.misses)
class BaseLiveServerTestCase(AppDependencyMixin, LiveServerTestCase): @classmethod def setUpClass(cls): # Avoid problems with the overlaping menu when clicking settings.ADMIN_TOOLS_MENU = 'admin_tools.menu.Menu' cls.vdisplay = Xvfb() cls.vdisplay.start() cls.selenium = WebDriver() super(BaseLiveServerTestCase, cls).setUpClass() @classmethod def tearDownClass(cls): cls.selenium.quit() cls.vdisplay.stop() super(BaseLiveServerTestCase, cls).tearDownClass() def create_account(self, username='', superuser=False): if not username: username = '******' % random_ascii(5) password = '******' self.account_password = password if superuser: return Account.objects.create_superuser(username, password=password, email='*****@*****.**') return Account.objects.create_user(username, password=password, email='*****@*****.**') def setUp(self): from orm.api import Api super(BaseLiveServerTestCase, self).setUp() self.rest = Api(self.live_server_url + '/api/') self.rest.enable_logging() self.account = self.create_account(superuser=True) def admin_login(self): session = SessionStore() session[SESSION_KEY] = self.account_id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session.save() ## to set a cookie we need to first visit the domain. self.selenium.get(self.live_server_url + '/admin/') self.selenium.add_cookie(dict( name=settings.SESSION_COOKIE_NAME, value=session.session_key, # path='/', )) def rest_login(self): self.rest.login(username=self.account.username, password=self.account_password) def take_screenshot(self): timestamp = datetime.datetime.now().isoformat().replace(':', '') filename = 'screenshot_%s_%s.png' % (self.id(), timestamp) path = '/home/orchestra/snapshots' self.selenium.save_screenshot(os.path.join(path, filename)) def admin_delete(self, obj): opts = obj._meta app_label, model_name = opts.app_label, opts.model_name delete = reverse('admin:%s_%s_delete' % (app_label, model_name), args=(obj.pk,)) url = self.live_server_url + delete self.selenium.get(url) confirmation = self.selenium.find_element_by_name('post') confirmation.submit() self.assertNotEqual(url, self.selenium.current_url) def admin_disable(self, obj): opts = obj._meta app_label, model_name = opts.app_label, opts.model_name change = reverse('admin:%s_%s_change' % (app_label, model_name), args=(obj.pk,)) url = self.live_server_url + change self.selenium.get(url) is_active = self.selenium.find_element_by_id('id_is_active') is_active.click() save = self.selenium.find_element_by_name('_save') save.submit() self.assertNotEqual(url, self.selenium.current_url) def admin_change_password(self, obj, password): opts = obj._meta app_label, model_name = opts.app_label, opts.model_name change_password = reverse('admin:%s_%s_change_password' % (app_label, model_name), args=(obj.pk,)) url = self.live_server_url + change_password self.selenium.get(url) password_field = self.selenium.find_element_by_id('id_password1') password_field.send_keys(password) password_field = self.selenium.find_element_by_id('id_password2') password_field.send_keys(password) password_field.submit() self.assertNotEqual(url, self.selenium.current_url)