def test_upgrade(self): from BTrees.OOBTree import OOBTree from karl.models.users import pbkdf2 users = self._makeOne() users.add('id1', 'login1', 'password1', groups=['group.foo', 'group.bar']) users.add('id2', 'login2', 'password2', groups=['group.biz', 'group.baz']) bylogin = OOBTree() for userid, info in users.data.items(): bylogin[info['login']] = info users.byid = users.data users.bylogin = bylogin users.data = None users.groups = None users.logins = None users._upgrade() self.assertEqual(len(users.data), 2) self.assertEqual( users.data[u'id1'], { 'id': 'id1', 'login': '******', 'salt': users.data[u'id1']['salt'], 'password': pbkdf2('password1', users.data[u'id1']['salt']), 'groups': set([u'group.foo', u'group.bar']) }) self.assertEqual( users.data[u'id2'], { 'id': 'id2', 'login': '******', 'salt': users.data[u'id2']['salt'], 'password': pbkdf2('password2', users.data[u'id2']['salt']), 'groups': set([u'group.biz', u'group.baz']) }) self.assertEqual(len(users.logins), 2) self.assertEqual(users.logins[u'login1'], u'id1') self.assertEqual(users.logins[u'login2'], u'id2') self.assertEqual(len(users.groups), 4) self.assertEqual(users.groups[u'group.foo'], set([u'id1'])) self.assertEqual(users.groups[u'group.bar'], set([u'id1'])) self.assertEqual(users.groups[u'group.biz'], set([u'id2'])) self.assertEqual(users.groups[u'group.baz'], set([u'id2']))
def test_upgrade(self): from BTrees.OOBTree import OOBTree from karl.models.users import pbkdf2 users = self._makeOne() users.add('id1', 'login1', 'password1', groups=['group.foo', 'group.bar']) users.add('id2', 'login2', 'password2', groups=['group.biz', 'group.baz']) bylogin = OOBTree() for userid, info in users.data.items(): bylogin[info['login']] = info users.byid = users.data users.bylogin = bylogin users.data = None users.groups = None users.logins = None users._upgrade() self.assertEqual(len(users.data), 2) self.assertEqual( users.data[u'id1'], {'id': 'id1', 'login': '******', 'salt': users.data[u'id1']['salt'], 'password': pbkdf2('password1', users.data[u'id1']['salt']), 'groups': set([u'group.foo', u'group.bar'])} ) self.assertEqual( users.data[u'id2'], {'id': 'id2', 'login': '******', 'salt': users.data[u'id2']['salt'], 'password': pbkdf2('password2', users.data[u'id2']['salt']), 'groups': set([u'group.biz', u'group.baz'])} ) self.assertEqual(len(users.logins), 2) self.assertEqual(users.logins[u'login1'], u'id1') self.assertEqual(users.logins[u'login2'], u'id2') self.assertEqual(len(users.groups), 4) self.assertEqual(users.groups[u'group.foo'], set([u'id1'])) self.assertEqual(users.groups[u'group.bar'], set([u'id1'])) self.assertEqual(users.groups[u'group.biz'], set([u'id2'])) self.assertEqual(users.groups[u'group.baz'], set([u'id2']))
def test_handle_submit_utf8_password(self): password = u'password\xe1' reg = get_current_registry() config = Configurator(reg) renderer = config.testing_add_template('templates/reset_complete.pt') request = self.request request.params['key'] = '0' * 40 self._setupUsers() context = self.context context['profiles'] = testing.DummyModel() profile = context['profiles']['me'] = testing.DummyModel() profile.password_reset_key = '0' * 40 controller = self._makeOne(context, request) converted = {'login': '******', 'password': password} import datetime keytime = datetime.datetime.now() profile.password_reset_time = keytime controller.handle_submit(converted) self.failUnless(hasattr(renderer, 'api')) self.assertEqual(renderer.api.page_title, 'Password Reset Complete') renderer.assert_(login='******', password=password) self.failUnless(profile.password_reset_key is None) self.failUnless(profile.password_reset_time is None) user = self.context.users.get(login='******') from karl.models.users import pbkdf2 self.assertEqual(user['password'], pbkdf2(password, user['salt']))
def test_add_and_remove(self): from karl.models.users import pbkdf2 users = self._makeOne() users.add('id', 'login', 'password', groups=['group.foo']) user = users.get('id') expected = { 'id': 'id', 'login': '******', 'salt': user['salt'], 'password': pbkdf2('password', user['salt']), 'groups': set(['group.foo']), } self.assertEqual(users.logins[u'login'], u'id') self.assertEqual(users.data[u'id'], expected) users.remove('id') self.assertEqual(users.data.get('id'), None) self.assertEqual(users.logins.get(u'login'), None)
def change_password(self, userid, password): from karl.models.users import pbkdf2 self._by_id[userid]["password"] = pbkdf2(password, 'salt')
def _verifyPassword(self, users, userid, value): from karl.models.users import pbkdf2 user = users.get(userid) self.assertEqual(user['password'], pbkdf2(value, user['salt']))