From 77c0384f79bc646f3333e1c1a163324cdc89e497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20V=C3=A9zina?= Date: Wed, 5 Oct 2016 12:41:47 -0400 Subject: [PATCH] Make pass test_login_bare() and little other improvements --- gluon/tests/test_tools.py | 87 ++++++++++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 25 deletions(-) diff --git a/gluon/tests/test_tools.py b/gluon/tests/test_tools.py index 1fdf80f1..083d83af 100644 --- a/gluon/tests/test_tools.py +++ b/gluon/tests/test_tools.py @@ -525,28 +525,47 @@ class TestAuth(unittest.TestCase): self.auth.define_tables(username=True, signature=False) self.db.define_table('t0', Field('tt'), self.auth.signature) self.auth.enable_record_versioning(self.db) + self.auth.settings.registration_requires_verification = False + self.auth.settings.registration_requires_approval = False # Create a user - self.auth.get_or_create_user(dict(first_name='Bart', - last_name='Simpson', - username='bart', - email='bart@simpson.com', - password='bart_password', - registration_key='bart', - registration_id='' - ), - login=False) - self.db.commit() - self.assertFalse(self.auth.is_logged_in()) - # self.auth.settings.registration_requires_verification = False - # self.auth.settings.registration_requires_approval = False + # Note: get_or_create_user() doesn't seems to create user properly it better to use register_bare() and + # prevent login_bare() test from succeed. db insert the user manually not properly work either. + # Not working + # self.auth.get_or_create_user(dict(first_name='Bart', + # last_name='Simpson', + # username='bart', + # email='bart@simpson.com', + # password='bart_password', + # # registration_key=None, + # #registration_id='bart@simpson.com' + # ), + # login=False) + # Not working + # self.db.auth_user.insert(first_name='Bart', + # last_name='Simpson', + # username='bart', + # email='bart@simpson.com', + # password='bart_password') + # self.db.commit() + self.auth.register_bare(first_name='Bart', + last_name='Simpson', + username='bart', + email='bart@simpson.com', + password='bart_password') def test_assert_setup(self): - self.assertEqual(self.db(self.db.auth_user.username == 'bart').select().first()['username'], 'bart') self.assertTrue('auth_user' in self.db) self.assertTrue('auth_group' in self.db) self.assertTrue('auth_membership' in self.db) self.assertTrue('auth_permission' in self.db) self.assertTrue('auth_event' in self.db) + bart_record = self.db(self.db.auth_user.username == 'bart').select().first() + self.assertEqual(bart_record['username'], 'bart') + self.assertEqual(bart_record['registration_key'], '') + bart_id = self.db(self.db.auth_user.username == 'bart').select().first().id + bart_group_id = self.db(self.db.auth_group.role == 'user_{0}'.format(bart_id)).select().first().id + self.assertTrue(self.db((self.db.auth_membership.group_id == bart_group_id) & + (self.db.auth_membership.user_id == bart_id)).select().first()) # Just calling many form functions def test_basic_blank_forms(self): @@ -652,16 +671,31 @@ class TestAuth(unittest.TestCase): # TODO: def test_login_user(self): # TODO: def test__get_login_settings(self): - # login_bare() seems broken see my post on web2py-developpers - # commented for now - # def test_login_bare(self): - # # The following test case should succeed but failed as I never received the user record but False - # self.auth.login_bare(username='bart', password='bart_password') - # self.assertTrue(self.auth.is_logged_in()) - # # Failing login because bad_password - # self.assertEqual(self.auth.login_bare(username='bart', password='wrong_password'), False) - # self.auth.logout_bare() - # self.db.auth_user.truncate() + def test_login_bare(self): + self.auth.login_bare(username='bart', password='bart_password') + self.assertTrue(self.auth.is_logged_in()) + self.auth.logout_bare() + # Failing login because wrong_password + self.assertFalse(self.auth.login_bare(username='bart', password='wrong_password')) + # NOTE : The following failed for some reason, but I can't find out why + # self.auth = Auth(self.db) + # self.auth.define_tables(username=False, signature=False) + # self.auth.settings.registration_requires_verification = False + # self.auth.settings.registration_requires_approval = False + # self.auth.register_bare(first_name='Omer', + # last_name='Simpson', + # # no username field passed, failed with : + # # ValueError('register_bare: userfield not provided or invalid') + # # Or + # # username='omer', + # # Or + # # username='omer@simpson.com', + # # In either previous cases, it failed with : + # # self.assertTrue(self.auth.is_logged_in()) AssertionError: False is not true + # email='omer@simpson.com', + # password='omer_password') + # self.auth.login_bare(username='omer@sympson.com', password='omer_password') + # self.assertTrue(self.auth.is_logged_in()) def test_register_bare(self): # corner case empty register call register_bare without args @@ -817,7 +851,10 @@ class TestAuth(unittest.TestCase): self.myassertRaisesRegex(HTTP, "400*", self.auth.allows_jwt) # TODO: def test_requires(self): - # TODO: def test_requires_login(self): + + # def test_login(self): + # Basic testing above in "test_basic_blank_forms()" could be refined here + # TODO: def test_requires_login_or_token(self): # TODO: def test_requires_membership(self): # TODO: def test_requires_permission(self):