Changeset - 1ba7e0c0d3b3
[Not reviewed]
default
0 2 0
Thomas De Schampheleire - 9 years ago 2016-07-29 22:04:53
thomas.de.schampheleire@gmail.com
tests: admin_permissions: make sure all custom IP permissions are cleared

test_add_ips changes IP permissions and at the end tries to clean up by
deleting it again. When the delete fails, there is still a restricted IP
permission configuration, causing other tests to fail.

Attempt a direct delete to the database without relying on the application
post/get interface.
2 files changed with 19 insertions and 2 deletions:
0 comments (0 inline, 0 general)
kallithea/tests/conftest.py
Show inline comments
 
import os
 
import sys
 
import logging
 

	
 
import pkg_resources
 
from paste.deploy import loadapp
 
import pylons.test
 
from pylons.i18n.translation import _get_translator
 
import pytest
 
from kallithea.model.user import UserModel
 
from kallithea.model.meta import Session
 
from kallithea.model.db import Setting, User
 
from kallithea.model.db import Setting, User, UserIpMap
 
from kallithea.tests import invalidate_all_caches
 

	
 

	
 
def pytest_configure():
 
    path = os.getcwd()
 
    sys.path.insert(0, path)
 
    pkg_resources.working_set.add_entry(path)
 

	
 
    # Disable INFO logging of test database creation, restore with NOTSET
 
    logging.disable(logging.INFO)
 
    pylons.test.pylonsapp = loadapp('config:kallithea/tests/test.ini', relative_to=path)
 
    logging.disable(logging.NOTSET)
 

	
 
    # Setup the config and app_globals, only works if we can get
 
    # to the config object
 
    conf = getattr(pylons.test.pylonsapp, 'config')
 
    if conf:
 
        pylons.config._push_object(conf)
 

	
 
        if 'pylons.app_globals' in conf:
 
            pylons.app_globals._push_object(conf['pylons.app_globals'])
 

	
 
    # Initialize a translator for tests that utilize i18n
 
    translator = _get_translator(pylons.config.get('lang'))
 
    pylons.translator._push_object(translator)
 
@@ -63,24 +64,40 @@ def _set_settings(*kvtseq):
 
        session.add(setting)
 
    session.commit()
 

	
 

	
 
@pytest.yield_fixture
 
def set_test_settings():
 
    """Restore settings after test is over."""
 
    # Save settings.
 
    settings_snapshot = [
 
        (s.app_settings_name, s.app_settings_value, s.app_settings_type)
 
        for s in Setting.query().all()]
 
    yield _set_settings
 
    # Restore settings.
 
    session = Session()
 
    keys = frozenset(k for (k, v, t) in settings_snapshot)
 
    for s in Setting.query().all():
 
        if s.app_settings_name not in keys:
 
            session.delete(s)
 
    for k, v, t in settings_snapshot:
 
        if t == 'list' and hasattr(v, '__iter__'):
 
            v = ','.join(v) # Quirk: must format list value manually.
 
        s = Setting.create_or_update(k, v, t)
 
        session.add(s)
 
    session.commit()
 

	
 
@pytest.yield_fixture
 
def auto_clear_ip_permissions():
 
    """Fixture that provides nothing but clearing IP permissions upon test
 
    exit. This clearing is needed to avoid other test failing to make fake http
 
    accesses."""
 
    yield
 
    # cleanup
 
    user_model = UserModel()
 
    default_user_id = User.get_default_user().user_id
 
    for ip in UserIpMap.query().filter(UserIpMap.user_id ==
 
                                       default_user_id):
 
        user_model.delete_extra_ip(default_user_id, ip.ip_id)
 

	
 
    # IP permissions are cached, need to invalidate this cache explicitly
 
    invalidate_all_caches()
kallithea/tests/functional/test_admin_permissions.py
Show inline comments
 
import time
 

	
 
from kallithea.model.db import User, UserIpMap
 
from kallithea.tests import *
 

	
 
class TestAdminPermissionsController(TestController):
 

	
 
    def test_index(self):
 
        self.log_user()
 
        response = self.app.get(url('admin_permissions'))
 
        # Test response...
 

	
 
    def test_index_ips(self):
 
        self.log_user()
 
        response = self.app.get(url('admin_permissions_ips'))
 
        # Test response...
 
        response.mustcontain('All IP addresses are allowed')
 

	
 
    def test_add_ips(self):
 
    def test_add_ips(self, auto_clear_ip_permissions):
 
        self.log_user()
 
        default_user_id = User.get_default_user().user_id
 
        response = self.app.post(url('edit_user_ips_update', id=default_user_id),
 
                                 params=dict(new_ip='127.0.0.0/24',
 
                                 _authentication_token=self.authentication_token()))
 

	
 
        # IP permissions are cached, need to invalidate this cache explicitly
 
        invalidate_all_caches()
 

	
 
        self.app.get(url('admin_permissions_ips'), status=302)
 

	
 
        # REMOTE_ADDR must match 127.0.0.0/24
 
        response = self.app.get(url('admin_permissions_ips'),
 
                                extra_environ={'REMOTE_ADDR': '127.0.0.1'})
 
        response.mustcontain('127.0.0.0/24')
 
        response.mustcontain('127.0.0.0 - 127.0.0.255')
 

	
 
        ## delete
 
        default_user_id = User.get_default_user().user_id
 
        del_ip_id = UserIpMap.query().filter(UserIpMap.user_id ==
 
                                             default_user_id).first().ip_id
 

	
 
        response = self.app.post(url('edit_user_ips_delete', id=default_user_id),
 
                                 params=dict(del_ip_id=del_ip_id,
0 comments (0 inline, 0 general)