Set env variable to skip REST_API tests.

This commit is contained in:
Santiago L 2021-03-31 10:39:47 +02:00
parent e7aabf4799
commit dc722ec17a
5 changed files with 113 additions and 96 deletions

View File

@ -1,22 +1,24 @@
import MySQLdb
import os import os
import socket import socket
import time import time
import unittest
import MySQLdb
from django.conf import settings as djsettings from django.conf import settings as djsettings
from django.core.management.base import CommandError from django.core.management.base import CommandError
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from selenium.webdriver.support.select import Select
from orchestra.admin.utils import change_url from orchestra.admin.utils import change_url
from orchestra.contrib.orchestration.models import Server, Route from orchestra.contrib.orchestration.models import Route, Server
from orchestra.utils.sys import sshrun from orchestra.utils.sys import sshrun
from orchestra.utils.tests import (BaseLiveServerTestCase, random_ascii, save_response_on_error, from orchestra.utils.tests import (BaseLiveServerTestCase, random_ascii,
snapshot_on_error) save_response_on_error, snapshot_on_error)
from selenium.webdriver.support.select import Select
from ... import backends, settings from ... import backends, settings
from ...models import Database, DatabaseUser from ...models import Database, DatabaseUser
TEST_REST_API = int(os.getenv('TEST_REST_API', '0'))
class DatabaseTestMixin(object): class DatabaseTestMixin(object):
MASTER_SERVER = os.environ.get('ORCHESTRA_SECOND_SERVER', 'localhost') MASTER_SERVER = os.environ.get('ORCHESTRA_SECOND_SERVER', 'localhost')
@ -24,40 +26,40 @@ class DatabaseTestMixin(object):
'orchestra.contrib.orchestration', 'orchestra.contrib.orchestration',
'orcgestra.apps.databases', 'orcgestra.apps.databases',
) )
def setUp(self): def setUp(self):
super(DatabaseTestMixin, self).setUp() super(DatabaseTestMixin, self).setUp()
self.add_route() self.add_route()
djsettings.DEBUG = True djsettings.DEBUG = True
def add_route(self): def add_route(self):
raise NotImplementedError raise NotImplementedError
def save(self): def save(self):
raise NotImplementedError raise NotImplementedError
def add(self): def add(self):
raise NotImplementedError raise NotImplementedError
def delete(self): def delete(self):
raise NotImplementedError raise NotImplementedError
def update(self): def update(self):
raise NotImplementedError raise NotImplementedError
def disable(self): def disable(self):
raise NotImplementedError raise NotImplementedError
def add_group(self, username, groupname): def add_group(self, username, groupname):
raise NotImplementedError raise NotImplementedError
def test_add(self): def test_add(self):
dbname = '%s_database' % random_ascii(5) dbname = '%s_database' % random_ascii(5)
username = '%s_dbuser' % random_ascii(5) username = '%s_dbuser' % random_ascii(5)
password = '@!?%spppP001' % random_ascii(5) password = '@!?%spppP001' % random_ascii(5)
self.add(dbname, username, password) self.add(dbname, username, password)
self.validate_create_table(dbname, username, password) self.validate_create_table(dbname, username, password)
def test_delete(self): def test_delete(self):
dbname = '%s_database' % random_ascii(5) dbname = '%s_database' % random_ascii(5)
username = '%s_dbuser' % random_ascii(5) username = '%s_dbuser' % random_ascii(5)
@ -68,7 +70,7 @@ class DatabaseTestMixin(object):
self.delete_user(username) self.delete_user(username)
self.validate_delete(dbname, username, password) self.validate_delete(dbname, username, password)
self.validate_delete_user(dbname, username) self.validate_delete_user(dbname, username)
def test_change_password(self): def test_change_password(self):
dbname = '%s_database' % random_ascii(5) dbname = '%s_database' % random_ascii(5)
username = '%s_dbuser' % random_ascii(5) username = '%s_dbuser' % random_ascii(5)
@ -81,7 +83,7 @@ class DatabaseTestMixin(object):
self.change_password(username, new_password) self.change_password(username, new_password)
self.validate_login_error(dbname, username, password) self.validate_login_error(dbname, username, password)
self.validate_create_table(dbname, username, new_password) self.validate_create_table(dbname, username, new_password)
def test_add_user(self): def test_add_user(self):
dbname = '%s_database' % random_ascii(5) dbname = '%s_database' % random_ascii(5)
username = '%s_dbuser' % random_ascii(5) username = '%s_dbuser' % random_ascii(5)
@ -98,7 +100,7 @@ class DatabaseTestMixin(object):
self.add_user_to_db(username2, dbname) self.add_user_to_db(username2, dbname)
self.validate_create_table(dbname, username, password) self.validate_create_table(dbname, username, password)
self.validate_create_table(dbname, username2, password2) self.validate_create_table(dbname, username2, password2)
def test_delete_user(self): def test_delete_user(self):
dbname = '%s_database' % random_ascii(5) dbname = '%s_database' % random_ascii(5)
username = '%s_dbuser' % random_ascii(5) username = '%s_dbuser' % random_ascii(5)
@ -117,7 +119,7 @@ class DatabaseTestMixin(object):
self.delete_user(username2) self.delete_user(username2)
self.validate_login_error(dbname, username2, password2) self.validate_login_error(dbname, username2, password2)
self.validate_delete_user(username2, password2) self.validate_delete_user(username2, password2)
def test_swap_user(self): def test_swap_user(self):
dbname = '%s_database' % random_ascii(5) dbname = '%s_database' % random_ascii(5)
username = '%s_dbuser' % random_ascii(5) username = '%s_dbuser' % random_ascii(5)
@ -137,7 +139,7 @@ class DatabaseTestMixin(object):
class MySQLControllerMixin(object): class MySQLControllerMixin(object):
db_type = 'mysql' db_type = 'mysql'
def setUp(self): def setUp(self):
super(MySQLControllerMixin, self).setUp() super(MySQLControllerMixin, self).setUp()
# Get local ip address used to reach self.MASTER_SERVER # Get local ip address used to reach self.MASTER_SERVER
@ -145,7 +147,7 @@ class MySQLControllerMixin(object):
s.connect((self.MASTER_SERVER, 22)) s.connect((self.MASTER_SERVER, 22))
settings.DATABASES_DEFAULT_HOST = s.getsockname()[0] settings.DATABASES_DEFAULT_HOST = s.getsockname()[0]
s.close() s.close()
def add_route(self): def add_route(self):
server = Server.objects.create(name=self.MASTER_SERVER) server = Server.objects.create(name=self.MASTER_SERVER)
backend = backends.MySQLController.get_name() backend = backends.MySQLController.get_name()
@ -154,22 +156,22 @@ class MySQLControllerMixin(object):
match = "databaseuser.type == '%s'" % self.db_type match = "databaseuser.type == '%s'" % self.db_type
backend = backends.MySQLUserController.get_name() backend = backends.MySQLUserController.get_name()
Route.objects.create(backend=backend, match=match, host=server) Route.objects.create(backend=backend, match=match, host=server)
def validate_create_table(self, name, username, password): def validate_create_table(self, name, username, password):
db = MySQLdb.connect(host=self.MASTER_SERVER, port=3306, user=username, passwd=password, db=name) db = MySQLdb.connect(host=self.MASTER_SERVER, port=3306, user=username, passwd=password, db=name)
cur = db.cursor() cur = db.cursor()
cur.execute('CREATE TABLE table_%s ( id INT ) ;' % random_ascii(10)) cur.execute('CREATE TABLE table_%s ( id INT ) ;' % random_ascii(10))
def validate_login_error(self, dbname, username, password): def validate_login_error(self, dbname, username, password):
self.assertRaises(MySQLdb.OperationalError, self.assertRaises(MySQLdb.OperationalError,
self.validate_create_table, dbname, username, password self.validate_create_table, dbname, username, password
) )
def validate_delete(self, dbname, username, password): def validate_delete(self, dbname, username, password):
self.validate_login_error(dbname, username, password) self.validate_login_error(dbname, username, password)
self.assertRaises(CommandError, self.assertRaises(CommandError,
sshrun, self.MASTER_SERVER, 'mysql %s' % dbname, display=False) sshrun, self.MASTER_SERVER, 'mysql %s' % dbname, display=False)
def validate_delete_user(self, name, username): def validate_delete_user(self, name, username):
context = { context = {
'name': name, 'name': name,
@ -181,11 +183,12 @@ class MySQLControllerMixin(object):
"""mysql mysql -e 'SELECT * FROM user WHERE user="%(username)s";'""" % context, display=False).stdout) """mysql mysql -e 'SELECT * FROM user WHERE user="%(username)s";'""" % context, display=False).stdout)
@unittest.skipUnless(TEST_REST_API, "REST API tests")
class RESTDatabaseMixin(DatabaseTestMixin): class RESTDatabaseMixin(DatabaseTestMixin):
def setUp(self): def setUp(self):
super(RESTDatabaseMixin, self).setUp() super(RESTDatabaseMixin, self).setUp()
self.rest_login() self.rest_login()
@save_response_on_error @save_response_on_error
def add(self, dbname, username, password): def add(self, dbname, username, password):
user = self.rest.databaseusers.create(username=username, password=password, type=self.db_type) user = self.rest.databaseusers.create(username=username, password=password, type=self.db_type)
@ -193,31 +196,31 @@ class RESTDatabaseMixin(DatabaseTestMixin):
'username': user.username 'username': user.username
}] }]
self.rest.databases.create(name=dbname, users=users, type=self.db_type) self.rest.databases.create(name=dbname, users=users, type=self.db_type)
@save_response_on_error @save_response_on_error
def delete(self, dbname): def delete(self, dbname):
self.rest.databases.retrieve(name=dbname).delete() self.rest.databases.retrieve(name=dbname).delete()
@save_response_on_error @save_response_on_error
def change_password(self, username, password): def change_password(self, username, password):
user = self.rest.databaseusers.retrieve(username=username).get() user = self.rest.databaseusers.retrieve(username=username).get()
user.set_password(password) user.set_password(password)
@save_response_on_error @save_response_on_error
def add_user(self, username, password): def add_user(self, username, password):
self.rest.databaseusers.create(username=username, password=password, type=self.db_type) self.rest.databaseusers.create(username=username, password=password, type=self.db_type)
@save_response_on_error @save_response_on_error
def add_user_to_db(self, username, dbname): def add_user_to_db(self, username, dbname):
user = self.rest.databaseusers.retrieve(username=username).get() user = self.rest.databaseusers.retrieve(username=username).get()
db = self.rest.databases.retrieve(name=dbname).get() db = self.rest.databases.retrieve(name=dbname).get()
db.users.append(user) db.users.append(user)
db.save() db.save()
@save_response_on_error @save_response_on_error
def delete_user(self, username): def delete_user(self, username):
self.rest.databaseusers.retrieve(username=username).delete() self.rest.databaseusers.retrieve(username=username).delete()
@save_response_on_error @save_response_on_error
def swap_user(self, username, username2, dbname): def swap_user(self, username, username2, dbname):
user = self.rest.databaseusers.retrieve(username=username2).get() user = self.rest.databaseusers.retrieve(username=username2).get()
@ -231,84 +234,84 @@ class AdminDatabaseMixin(DatabaseTestMixin):
def setUp(self): def setUp(self):
super(AdminDatabaseMixin, self).setUp() super(AdminDatabaseMixin, self).setUp()
self.admin_login() self.admin_login()
@snapshot_on_error @snapshot_on_error
def add(self, dbname, username, password): def add(self, dbname, username, password):
url = self.live_server_url + reverse('admin:databases_database_add') url = self.live_server_url + reverse('admin:databases_database_add')
self.selenium.get(url) self.selenium.get(url)
type_input = self.selenium.find_element_by_id('id_type') type_input = self.selenium.find_element_by_id('id_type')
type_select = Select(type_input) type_select = Select(type_input)
type_select.select_by_value(self.db_type) type_select.select_by_value(self.db_type)
name_field = self.selenium.find_element_by_id('id_name') name_field = self.selenium.find_element_by_id('id_name')
name_field.send_keys(dbname) name_field.send_keys(dbname)
username_field = self.selenium.find_element_by_id('id_username') username_field = self.selenium.find_element_by_id('id_username')
username_field.send_keys(username) username_field.send_keys(username)
password_field = self.selenium.find_element_by_id('id_password1') password_field = self.selenium.find_element_by_id('id_password1')
password_field.send_keys(password) password_field.send_keys(password)
password_field = self.selenium.find_element_by_id('id_password2') password_field = self.selenium.find_element_by_id('id_password2')
password_field.send_keys(password) password_field.send_keys(password)
name_field.submit() name_field.submit()
self.assertNotEqual(url, self.selenium.current_url) self.assertNotEqual(url, self.selenium.current_url)
@snapshot_on_error @snapshot_on_error
def delete(self, dbname): def delete(self, dbname):
db = Database.objects.get(name=dbname) db = Database.objects.get(name=dbname)
self.admin_delete(db) self.admin_delete(db)
@snapshot_on_error @snapshot_on_error
def change_password(self, username, password): def change_password(self, username, password):
user = DatabaseUser.objects.get(username=username) user = DatabaseUser.objects.get(username=username)
self.admin_change_password(user, password) self.admin_change_password(user, password)
@snapshot_on_error @snapshot_on_error
def add_user(self, username, password): def add_user(self, username, password):
url = self.live_server_url + reverse('admin:databases_databaseuser_add') url = self.live_server_url + reverse('admin:databases_databaseuser_add')
self.selenium.get(url) self.selenium.get(url)
type_input = self.selenium.find_element_by_id('id_type') type_input = self.selenium.find_element_by_id('id_type')
type_select = Select(type_input) type_select = Select(type_input)
type_select.select_by_value(self.db_type) type_select.select_by_value(self.db_type)
username_field = self.selenium.find_element_by_id('id_username') username_field = self.selenium.find_element_by_id('id_username')
username_field.send_keys(username) username_field.send_keys(username)
password_field = self.selenium.find_element_by_id('id_password1') password_field = self.selenium.find_element_by_id('id_password1')
password_field.send_keys(password) password_field.send_keys(password)
password_field = self.selenium.find_element_by_id('id_password2') password_field = self.selenium.find_element_by_id('id_password2')
password_field.send_keys(password) password_field.send_keys(password)
username_field.submit() username_field.submit()
self.assertNotEqual(url, self.selenium.current_url) self.assertNotEqual(url, self.selenium.current_url)
@snapshot_on_error @snapshot_on_error
def add_user_to_db(self, username, dbname): def add_user_to_db(self, username, dbname):
database = Database.objects.get(name=dbname, type=self.db_type) database = Database.objects.get(name=dbname, type=self.db_type)
url = self.live_server_url + change_url(database) url = self.live_server_url + change_url(database)
self.selenium.get(url) self.selenium.get(url)
user = DatabaseUser.objects.get(username=username, type=self.db_type) user = DatabaseUser.objects.get(username=username, type=self.db_type)
users_from = self.selenium.find_element_by_id('id_users_from') users_from = self.selenium.find_element_by_id('id_users_from')
users_select = Select(users_from) users_select = Select(users_from)
users_select.select_by_value(str(user.pk)) users_select.select_by_value(str(user.pk))
add_user = self.selenium.find_element_by_id('id_users_add_link') add_user = self.selenium.find_element_by_id('id_users_add_link')
add_user.click() add_user.click()
save = self.selenium.find_element_by_name('_save') save = self.selenium.find_element_by_name('_save')
save.submit() save.submit()
self.assertNotEqual(url, self.selenium.current_url) self.assertNotEqual(url, self.selenium.current_url)
@snapshot_on_error @snapshot_on_error
def swap_user(self, username, username2, dbname): def swap_user(self, username, username2, dbname):
database = Database.objects.get(name=dbname, type=self.db_type) database = Database.objects.get(name=dbname, type=self.db_type)
url = self.live_server_url + change_url(database) url = self.live_server_url + change_url(database)
self.selenium.get(url) self.selenium.get(url)
# remove user "username" # remove user "username"
user = DatabaseUser.objects.get(username=username, type=self.db_type) user = DatabaseUser.objects.get(username=username, type=self.db_type)
users_to = self.selenium.find_element_by_id('id_users_to') users_to = self.selenium.find_element_by_id('id_users_to')
@ -317,7 +320,7 @@ class AdminDatabaseMixin(DatabaseTestMixin):
remove_user = self.selenium.find_element_by_id('id_users_remove_link') remove_user = self.selenium.find_element_by_id('id_users_remove_link')
remove_user.click() remove_user.click()
time.sleep(0.2) time.sleep(0.2)
# add user "username2" # add user "username2"
user = DatabaseUser.objects.get(username=username2, type=self.db_type) user = DatabaseUser.objects.get(username=username2, type=self.db_type)
users_from = self.selenium.find_element_by_id('id_users_from') users_from = self.selenium.find_element_by_id('id_users_from')
@ -326,11 +329,11 @@ class AdminDatabaseMixin(DatabaseTestMixin):
add_user = self.selenium.find_element_by_id('id_users_add_link') add_user = self.selenium.find_element_by_id('id_users_add_link')
add_user.click() add_user.click()
time.sleep(0.2) time.sleep(0.2)
save = self.selenium.find_element_by_name('_save') save = self.selenium.find_element_by_name('_save')
save.submit() save.submit()
self.assertNotEqual(url, self.selenium.current_url) self.assertNotEqual(url, self.selenium.current_url)
@snapshot_on_error @snapshot_on_error
def delete_user(self, username): def delete_user(self, username):
user = DatabaseUser.objects.get(username=username) user = DatabaseUser.objects.get(username=username)

View File

@ -1,24 +1,26 @@
import os import os
import smtplib import smtplib
import time import time
import requests import unittest
from email.mime.text import MIMEText from email.mime.text import MIMEText
import requests
from django.conf import settings as djsettings from django.conf import settings as djsettings
from django.core.management.base import CommandError from django.core.management.base import CommandError
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from selenium.webdriver.support.select import Select
from orchestra.admin.utils import change_url from orchestra.admin.utils import change_url
from orchestra.contrib.domains.models import Domain from orchestra.contrib.domains.models import Domain
from orchestra.contrib.orchestration.models import Server, Route from orchestra.contrib.orchestration.models import Route, Server
from orchestra.utils.sys import sshrun from orchestra.utils.sys import sshrun
from orchestra.utils.tests import (BaseLiveServerTestCase, random_ascii, snapshot_on_error, from orchestra.utils.tests import (BaseLiveServerTestCase, random_ascii,
save_response_on_error) save_response_on_error, snapshot_on_error)
from selenium.webdriver.support.select import Select
from ... import backends, settings from ... import backends, settings
from ...models import List from ...models import List
TEST_REST_API = int(os.getenv('TEST_REST_API', '0'))
class ListMixin(object): class ListMixin(object):
MASTER_SERVER = os.environ.get('ORCHESTRA_SLAVE_SERVER', 'localhost') MASTER_SERVER = os.environ.get('ORCHESTRA_SLAVE_SERVER', 'localhost')
@ -27,12 +29,12 @@ class ListMixin(object):
'orchestra.contrib.domains', 'orchestra.contrib.domains',
'orchestra.contrib.lists', 'orchestra.contrib.lists',
) )
def setUp(self): def setUp(self):
super(ListMixin, self).setUp() super(ListMixin, self).setUp()
self.add_route() self.add_route()
djsettings.DEBUG = True djsettings.DEBUG = True
def validate_add(self, name, address=None): def validate_add(self, name, address=None):
sshrun(self.MASTER_SERVER, 'list_members %s' % name, display=False) sshrun(self.MASTER_SERVER, 'list_members %s' % name, display=False)
if not address: if not address:
@ -44,11 +46,11 @@ class ListMixin(object):
sshrun(self.MASTER_SERVER, sshrun(self.MASTER_SERVER,
'grep -v ":\|^\s\|^$\|-\|\.\|\s" /var/spool/mail/nobody | base64 -d | grep "%s"' 'grep -v ":\|^\s\|^$\|-\|\.\|\s" /var/spool/mail/nobody | base64 -d | grep "%s"'
% request_address, display=False) % request_address, display=False)
def validate_login(self, name, password): def validate_login(self, name, password):
url = 'http://%s/cgi-bin/mailman/admin/%s' % (settings.LISTS_DEFAULT_DOMAIN, name) url = 'http://%s/cgi-bin/mailman/admin/%s' % (settings.LISTS_DEFAULT_DOMAIN, name)
self.assertEqual(200, requests.post(url, data={'adminpw': password}).status_code) self.assertEqual(200, requests.post(url, data={'adminpw': password}).status_code)
def validate_delete(self, name): def validate_delete(self, name):
context = { context = {
'name': name, 'name': name,
@ -62,7 +64,7 @@ class ListMixin(object):
'grep "^\s*$(domain)s\s*$" %(virtual_domain)s' % context, display=False) 'grep "^\s*$(domain)s\s*$" %(virtual_domain)s' % context, display=False)
self.assertRaises(CommandError, sshrun, self.MASTER_SERVER, self.assertRaises(CommandError, sshrun, self.MASTER_SERVER,
'list_lists | grep -i "^\s*%(name)s\s"' % context, display=False) 'list_lists | grep -i "^\s*%(name)s\s"' % context, display=False)
def subscribe(self, subscribe_address): def subscribe(self, subscribe_address):
msg = MIMEText('') msg = MIMEText('')
msg['To'] = subscribe_address msg['To'] = subscribe_address
@ -76,12 +78,12 @@ class ListMixin(object):
server.sendmail(msg['From'], msg['To'], msg.as_string()) server.sendmail(msg['From'], msg['To'], msg.as_string())
finally: finally:
server.quit() server.quit()
def add_route(self): def add_route(self):
server = Server.objects.create(name=self.MASTER_SERVER) server = Server.objects.create(name=self.MASTER_SERVER)
backend = backends.MailmanController.get_name() backend = backends.MailmanController.get_name()
Route.objects.create(backend=backend, match=True, host=server) Route.objects.create(backend=backend, match=True, host=server)
def test_add(self): def test_add(self):
name = '%s_list' % random_ascii(10) name = '%s_list' % random_ascii(10)
password = '@!?%spppP001' % random_ascii(5) password = '@!?%spppP001' % random_ascii(5)
@ -90,7 +92,7 @@ class ListMixin(object):
self.validate_add(name) self.validate_add(name)
self.validate_login(name, password) self.validate_login(name, password)
self.addCleanup(self.delete, name) self.addCleanup(self.delete, name)
def test_add_with_address(self): def test_add_with_address(self):
name = '%s_list' % random_ascii(10) name = '%s_list' % random_ascii(10)
password = '@!?%spppP001' % random_ascii(5) password = '@!?%spppP001' % random_ascii(5)
@ -102,7 +104,7 @@ class ListMixin(object):
self.addCleanup(self.delete, name) self.addCleanup(self.delete, name)
# Mailman doesn't support changing the address, only the domain # Mailman doesn't support changing the address, only the domain
self.validate_add(name, address="%s@%s" % (address_name, address_domain)) self.validate_add(name, address="%s@%s" % (address_name, address_domain))
def test_change_password(self): def test_change_password(self):
name = '%s_list' % random_ascii(10) name = '%s_list' % random_ascii(10)
password = '@!?%spppP001' % random_ascii(5) password = '@!?%spppP001' % random_ascii(5)
@ -113,7 +115,7 @@ class ListMixin(object):
new_password = '@!?%spppP001' % random_ascii(5) new_password = '@!?%spppP001' % random_ascii(5)
self.change_password(name, new_password) self.change_password(name, new_password)
self.validate_login(name, new_password) self.validate_login(name, new_password)
def test_change_domain(self): def test_change_domain(self):
name = '%s_list' % random_ascii(10) name = '%s_list' % random_ascii(10)
password = '@!?%spppP001' % random_ascii(5) password = '@!?%spppP001' % random_ascii(5)
@ -128,7 +130,7 @@ class ListMixin(object):
address_domain = Domain.objects.create(name=domain_name, account=self.account) address_domain = Domain.objects.create(name=domain_name, account=self.account)
self.update_domain(name, domain_name) self.update_domain(name, domain_name)
self.validate_add(name, address="%s@%s" % (address_name, address_domain)) self.validate_add(name, address="%s@%s" % (address_name, address_domain))
def test_change_address_name(self): def test_change_address_name(self):
name = '%s_list' % random_ascii(10) name = '%s_list' % random_ascii(10)
password = '@!?%spppP001' % random_ascii(5) password = '@!?%spppP001' % random_ascii(5)
@ -142,7 +144,7 @@ class ListMixin(object):
address_name = '%s_name' % random_ascii(10) address_name = '%s_name' % random_ascii(10)
self.update_address_name(name, address_name) self.update_address_name(name, address_name)
self.validate_add(name, address="%s@%s" % (address_name, address_domain)) self.validate_add(name, address="%s@%s" % (address_name, address_domain))
def test_delete(self): def test_delete(self):
name = '%s_list' % random_ascii(10) name = '%s_list' % random_ascii(10)
password = '@!?%spppP001' % random_ascii(5) password = '@!?%spppP001' % random_ascii(5)
@ -158,11 +160,12 @@ class ListMixin(object):
self.validate_delete(name) self.validate_delete(name)
@unittest.skipUnless(TEST_REST_API, "REST API tests")
class RESTListMixin(ListMixin): class RESTListMixin(ListMixin):
def setUp(self): def setUp(self):
super(RESTListMixin, self).setUp() super(RESTListMixin, self).setUp()
self.rest_login() self.rest_login()
@save_response_on_error @save_response_on_error
def add(self, name, password, admin_email, address_name=None, address_domain=None): def add(self, name, password, admin_email, address_name=None, address_domain=None):
extra = {} extra = {}
@ -172,22 +175,22 @@ class RESTListMixin(ListMixin):
'address_domain': self.rest.domains.retrieve(name=address_domain.name).get(), 'address_domain': self.rest.domains.retrieve(name=address_domain.name).get(),
}) })
self.rest.lists.create(name=name, password=password, admin_email=admin_email, **extra) self.rest.lists.create(name=name, password=password, admin_email=admin_email, **extra)
@save_response_on_error @save_response_on_error
def delete(self, name): def delete(self, name):
self.rest.lists.retrieve(name=name).delete() self.rest.lists.retrieve(name=name).delete()
@save_response_on_error @save_response_on_error
def change_password(self, name, password): def change_password(self, name, password):
mail_list = self.rest.lists.retrieve(name=name).get() mail_list = self.rest.lists.retrieve(name=name).get()
mail_list.set_password(password) mail_list.set_password(password)
@save_response_on_error @save_response_on_error
def update_domain(self, name, domain_name): def update_domain(self, name, domain_name):
mail_list = self.rest.lists.retrieve(name=name).get() mail_list = self.rest.lists.retrieve(name=name).get()
domain = self.rest.domains.retrieve(name=domain_name).get() domain = self.rest.domains.retrieve(name=domain_name).get()
mail_list.update(address_domain=domain) mail_list.update(address_domain=domain)
@save_response_on_error @save_response_on_error
def update_address_name(self, name, address_name): def update_address_name(self, name, address_name):
mail_list = self.rest.lists.retrieve(name=name).get() mail_list = self.rest.lists.retrieve(name=name).get()
@ -198,70 +201,70 @@ class AdminListMixin(ListMixin):
def setUp(self): def setUp(self):
super(AdminListMixin, self).setUp() super(AdminListMixin, self).setUp()
self.admin_login() self.admin_login()
@snapshot_on_error @snapshot_on_error
def add(self, name, password, admin_email, address_name=None, address_domain=None): def add(self, name, password, admin_email, address_name=None, address_domain=None):
url = self.live_server_url + reverse('admin:lists_list_add') url = self.live_server_url + reverse('admin:lists_list_add')
self.selenium.get(url) self.selenium.get(url)
name_field = self.selenium.find_element_by_id('id_name') name_field = self.selenium.find_element_by_id('id_name')
name_field.send_keys(name) name_field.send_keys(name)
password_field = self.selenium.find_element_by_id('id_password1') password_field = self.selenium.find_element_by_id('id_password1')
password_field.send_keys(password) password_field.send_keys(password)
password_field = self.selenium.find_element_by_id('id_password2') password_field = self.selenium.find_element_by_id('id_password2')
password_field.send_keys(password) password_field.send_keys(password)
admin_email_field = self.selenium.find_element_by_id('id_admin_email') admin_email_field = self.selenium.find_element_by_id('id_admin_email')
admin_email_field.send_keys(admin_email) admin_email_field.send_keys(admin_email)
if address_name: if address_name:
address_name_field = self.selenium.find_element_by_id('id_address_name') address_name_field = self.selenium.find_element_by_id('id_address_name')
address_name_field.send_keys(address_name) address_name_field.send_keys(address_name)
domain = Domain.objects.get(name=address_domain) domain = Domain.objects.get(name=address_domain)
domain_input = self.selenium.find_element_by_id('id_address_domain') domain_input = self.selenium.find_element_by_id('id_address_domain')
domain_select = Select(domain_input) domain_select = Select(domain_input)
domain_select.select_by_value(str(domain.pk)) domain_select.select_by_value(str(domain.pk))
name_field.submit() name_field.submit()
self.assertNotEqual(url, self.selenium.current_url) self.assertNotEqual(url, self.selenium.current_url)
@snapshot_on_error @snapshot_on_error
def delete(self, name): def delete(self, name):
mail_list = List.objects.get(name=name) mail_list = List.objects.get(name=name)
self.admin_delete(mail_list) self.admin_delete(mail_list)
@snapshot_on_error @snapshot_on_error
def change_password(self, name, password): def change_password(self, name, password):
mail_list = List.objects.get(name=name) mail_list = List.objects.get(name=name)
self.admin_change_password(mail_list, password) self.admin_change_password(mail_list, password)
@snapshot_on_error @snapshot_on_error
def update_domain(self, name, domain_name): def update_domain(self, name, domain_name):
mail_list = List.objects.get(name=name) mail_list = List.objects.get(name=name)
url = self.live_server_url + change_url(mail_list) url = self.live_server_url + change_url(mail_list)
self.selenium.get(url) self.selenium.get(url)
domain = Domain.objects.get(name=domain_name) domain = Domain.objects.get(name=domain_name)
domain_input = self.selenium.find_element_by_id('id_address_domain') domain_input = self.selenium.find_element_by_id('id_address_domain')
domain_select = Select(domain_input) domain_select = Select(domain_input)
domain_select.select_by_value(str(domain.pk)) domain_select.select_by_value(str(domain.pk))
save = self.selenium.find_element_by_name('_save') save = self.selenium.find_element_by_name('_save')
save.submit() save.submit()
self.assertNotEqual(url, self.selenium.current_url) self.assertNotEqual(url, self.selenium.current_url)
@snapshot_on_error @snapshot_on_error
def update_address_name(self, name, address_name): def update_address_name(self, name, address_name):
mail_list = List.objects.get(name=name) mail_list = List.objects.get(name=name)
url = self.live_server_url + change_url(mail_list) url = self.live_server_url + change_url(mail_list)
self.selenium.get(url) self.selenium.get(url)
address_name_field = self.selenium.find_element_by_id('id_address_name') address_name_field = self.selenium.find_element_by_id('id_address_name')
address_name_field.clear() address_name_field.clear()
address_name_field.send_keys(address_name) address_name_field.send_keys(address_name)
save = self.selenium.find_element_by_name('_save') save = self.selenium.find_element_by_name('_save')
save.submit() save.submit()
self.assertNotEqual(url, self.selenium.current_url) self.assertNotEqual(url, self.selenium.current_url)

View File

@ -4,6 +4,7 @@ import poplib
import smtplib import smtplib
import time import time
import textwrap import textwrap
import unittest
from email.mime.text import MIMEText from email.mime.text import MIMEText
from django.apps import apps from django.apps import apps
@ -21,6 +22,8 @@ from orchestra.utils.tests import BaseLiveServerTestCase, random_ascii, snapshot
from ... import backends, settings from ... import backends, settings
from ...models import Mailbox from ...models import Mailbox
TEST_REST_API = int(os.getenv('TEST_REST_API', '0'))
class MailboxMixin(object): class MailboxMixin(object):
MASTER_SERVER = os.environ.get('ORCHESTRA_SLAVE_SERVER', 'localhost') MASTER_SERVER = os.environ.get('ORCHESTRA_SLAVE_SERVER', 'localhost')
@ -235,6 +238,7 @@ class MailboxMixin(object):
# TODO test autoreply # TODO test autoreply
@unittest.skipUnless(TEST_REST_API, "REST API tests")
class RESTMailboxMixin(MailboxMixin): class RESTMailboxMixin(MailboxMixin):
def setUp(self): def setUp(self):
super(RESTMailboxMixin, self).setUp() super(RESTMailboxMixin, self).setUp()

View File

@ -2,6 +2,7 @@ import ftplib
import os import os
import re import re
import time import time
import unittest
from functools import partial from functools import partial
import paramiko import paramiko
@ -21,6 +22,7 @@ from ... import backends
from ...models import SystemUser from ...models import SystemUser
TEST_REST_API = int(os.getenv('TEST_REST_API', '0'))
r = partial(run, silent=True, display=False) r = partial(run, silent=True, display=False)
sshr = partial(sshrun, silent=True, display=False) sshr = partial(sshrun, silent=True, display=False)
@ -185,6 +187,7 @@ class SystemUserMixin(object):
# TODO test resources # TODO test resources
@unittest.skipUnless(TEST_REST_API, "REST API tests")
class RESTSystemUserMixin(SystemUserMixin): class RESTSystemUserMixin(SystemUserMixin):
def setUp(self): def setUp(self):
super(RESTSystemUserMixin, self).setUp() super(RESTSystemUserMixin, self).setUp()

View File

@ -1,16 +1,19 @@
import ftplib import ftplib
import os import os
import unittest
from io import StringIO from io import StringIO
from django.conf import settings as djsettings from django.conf import settings as djsettings
from orchestra.contrib.orchestration.models import Route, Server
from orchestra.contrib.orchestration.models import Server, Route
from orchestra.contrib.systemusers.backends import UNIXUserController from orchestra.contrib.systemusers.backends import UNIXUserController
from orchestra.utils.tests import BaseLiveServerTestCase, random_ascii, snapshot_on_error, save_response_on_error from orchestra.utils.tests import BaseLiveServerTestCase, random_ascii, save_response_on_error, snapshot_on_error
from ... import backends from ... import backends
TEST_REST_API = int(os.getenv('TEST_REST_API', '0'))
class WebAppMixin(object): class WebAppMixin(object):
MASTER_SERVER = os.environ.get('ORCHESTRA_MASTER_SERVER', 'localhost') MASTER_SERVER = os.environ.get('ORCHESTRA_MASTER_SERVER', 'localhost')
DEPENDENCIES = ( DEPENDENCIES = (
@ -74,6 +77,7 @@ class PHPFPMWebAppMixin(StaticWebAppMixin):
) )
@unittest.skipUnless(TEST_REST_API, "REST API tests")
class RESTWebAppMixin(object): class RESTWebAppMixin(object):
def setUp(self): def setUp(self):
super(RESTWebAppMixin, self).setUp() super(RESTWebAppMixin, self).setUp()