changeset 6142:437b41b18420

tests: admin_permissions: split test_delete_ips from test_add_ips While it is not necessary to be pedantic and split each assert in a separate test, it makes sense to create separate tests for separate logical actions. This makes it easy to understand from a test summary what works and what doesn't. Add and delete are deemed two such separate logical actions. The original test_add_ips test did both add and delete, but was not even named to cover this. Note that the 'add' in the delete test is not the same as the 'add' in the add test, i.e. for the delete test direct method calls are made instead of passing through self.app.get/post (which is a higher layer of abstraction). Background of this commit: during the Turbogears2 port, delete actions were not yet functional, and 'test_add_ips' failed as a result even though 'add' was perfectly fine.
author Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
date Fri, 19 Aug 2016 21:32:23 +0200
parents 1ba7e0c0d3b3
children 4d7dcd25c149
files kallithea/tests/functional/test_admin_permissions.py
diffstat 1 files changed, 22 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/tests/functional/test_admin_permissions.py	Fri Jul 29 22:04:53 2016 +0200
+++ b/kallithea/tests/functional/test_admin_permissions.py	Fri Aug 19 21:32:23 2016 +0200
@@ -1,6 +1,8 @@
 import time
 
 from kallithea.model.db import User, UserIpMap
+from kallithea.model.user import UserModel
+from kallithea.model.meta import Session
 from kallithea.tests import *
 
 class TestAdminPermissionsController(TestController):
@@ -34,13 +36,29 @@
         response.mustcontain('127.0.0.0/24')
         response.mustcontain('127.0.0.0 - 127.0.0.255')
 
-        ## delete
+    def test_delete_ips(self, auto_clear_ip_permissions):
+        self.log_user()
         default_user_id = User.get_default_user().user_id
-        del_ip_id = UserIpMap.query().filter(UserIpMap.user_id ==
-                                             default_user_id).first().ip_id
+
+        ## first add
+        new_ip = '127.0.0.0/24'
+        user_model = UserModel()
+        ip_obj = user_model.add_extra_ip(default_user_id, new_ip)
+        Session().commit()
 
+        ## double check that add worked
+        # 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')
+
+        ## now delete
         response = self.app.post(url('edit_user_ips_delete', id=default_user_id),
-                                 params=dict(del_ip_id=del_ip_id,
+                                 params=dict(del_ip_id=ip_obj.ip_id,
                                              _authentication_token=self.authentication_token()),
                                  extra_environ={'REMOTE_ADDR': '127.0.0.1'})