Index: ow/tests/models/test_root.py
===================================================================
--- ow/tests/models/test_root.py	(revision 39b14977ea6c9734e86296a94d7560ff9591a1c9)
+++ ow/tests/models/test_root.py	(revision 1d92bf2b47c1a3bf1482a589cdd6cc7ec38d552f)
@@ -1,6 +1,8 @@
 import json
+from unittest.mock import Mock
 from datetime import datetime, timedelta, timezone
 
 import pytest
+from repoze.catalog.catalog import Catalog
 
 from ow.models.workout import Workout
@@ -12,9 +14,14 @@
 
     @pytest.fixture
-    def root(self):
+    def john(self):
+        user = User(firstname='John', lastname='Doe',
+                    email='john.doe@example.net')
+        user.password = 's3cr3t'
+        return user
+
+    @pytest.fixture
+    def root(self, john):
         root = OpenWorkouts()
-        root['john'] = User(firstname='John', lastname='Doe',
-                            email='john.doe@example.net')
-        root['john'].password = 's3cr3t'
+        root.add_user(john)
         workout = Workout(
             start=datetime(2015, 6, 28, 12, 55, tzinfo=timezone.utc),
@@ -22,43 +29,104 @@
             distance=30, sport='cycling'
         )
-        root['john'].add_workout(workout)
+        john.add_workout(workout)
         return root
 
-    def test_all_usernames(self, root):
-        # the method returns an OOBtree object
-        assert [u for u in root.all_usernames()] == ['john']
+    def test__init__(self, root):
+        # a new OpenWorkouts instance has a catalog created automatically
+        assert isinstance(root.catalog, Catalog)
+        assert len(root.catalog) == 2
+        assert 'email' in root.catalog
+        assert 'sport' in root.catalog
 
-    def test_lowercase_usernames(self, root):
-        root.add_user(uid='Jack', firstname='Jack', lastname='Dumb',
-                      email='jack.dumb@example.net')
-        assert root.lowercase_usernames() == ['jack', 'john']
+    def test_add_user_ok(self, root):
+        assert len(root.users) == 1
+        user = User(firstname='New', lastname='For Testing',
+                    email='new.for.testing@example.net')
+        root.add_user(user)
+        assert len(root.users) == 2
+        assert user in root.users
+
+    def test_add_user_invalid(self, root):
+        assert len(root.users) == 1
+        with pytest.raises(AttributeError):
+            root.add_user('faked-user-object')
+
+    def test_del_user_ok(self, root, john):
+        assert len(root.users) == 1
+        root.del_user(john)
+        assert len(root.users) == 0
+
+    def test_del_user_failure(self, root):
+        assert len(root.users) == 1
+        with pytest.raises(AttributeError):
+            root.add_user('faked-user-object')
+
+    def test_get_user_by_uid(self, root, john):
+        # first, get an user that does exist
+        user = root.get_user_by_uid(str(john.uid))
+        assert user == john
+        # now, without converting first to str, works too
+        user = root.get_user_by_uid(john.uid)
+        assert user == john
+        # now, something that is not there
+        new_user = User(firstname='someone', lastname='else',
+                        email='someone.else@example.net')
+        user = root.get_user_by_uid(new_user.uid)
+        assert user is None
+        # now, something that is not an uid
+        user = root.get_user_by_uid('faked-user-uid')
+        assert user is None
+
+    def test_get_user_by_email(self, root, john):
+        # first, get an user that does exist
+        user = root.get_user_by_email(str(john.email))
+        assert user == john
+        # now, something that is not there
+        new_user = User(firstname='someone', lastname='else',
+                        email='someone.else@example.net')
+        user = root.get_user_by_email(new_user.email)
+        assert user is None
+        # now, something that is not an email
+        user = root.get_user_by_email('faked-user-email')
+        assert user is None
+        # passing in None
+        user = root.get_user_by_email(None)
+        assert user is None
+        # passing in something that is not None or a string will break
+        # the query code
+        with pytest.raises(TypeError):
+            user = root.get_user_by_email(False)
+        with pytest.raises(TypeError):
+            user = root.get_user_by_email(Mock())
+
+    def test_users(self, root, john):
+        assert root.users == [john]
+
+    def test_all_nicknames(self, root, john):
+        # the existing user has not a nickname, and empty nicknames are not
+        # added to the list of nicknames
+        assert root.all_nicknames == []
+        # now set one
+        john.nickname = 'MrJohn'
+        assert root.all_nicknames == ['MrJohn']
+
+    def test_lowercase_nicknames(self, root, john):
+        # the existing user has not a nickname
+        assert root.lowercase_nicknames == []
+        # now set one
+        john.nickname = 'MrJohn'
+        assert root.lowercase_nicknames == ['mrjohn']
 
     def test_emails(self, root):
-        assert root.emails() == ['john.doe@example.net']
+        assert root.emails == ['john.doe@example.net']
 
     def test_lowercase_emails(self, root):
-        root.add_user(uid='Jack', firstname='Jack', lastname='Dumb',
-                      email='Jack.Dumb@example.net')
-        assert root.lowercase_emails() == ['jack.dumb@example.net',
-                                           'john.doe@example.net']
+        user = User(firstname='Jack', lastname='Dumb',
+                    email='Jack.Dumb@example.net')
+        root.add_user(user)
+        assert root.lowercase_emails == ['john.doe@example.net',
+                                         'jack.dumb@example.net']
 
-    def test_users(self, root):
-        # the method returns an OOBtree object
-        assert [u for u in root.users()] == [root['john']]
-
-    def test_get_user(self, root):
-        assert root.get_user('john') == root['john']
-        assert root.get_user('jack') is None
-        with pytest.raises(TypeError):
-            root.get_user()
-
-    def test_add_user(self, root):
-        assert len(root.users()) == 1
-        root.add_user(uid='jack', firstname='Jack', lastname='Dumb',
-                      email='jack.dumb@example.net')
-        assert len(root.users()) == 2
-        assert 'jack' in root.all_usernames()
-
-    def test_sports(self, root):
+    def test_sports(self, root, john):
         assert root.sports == ['cycling']
         workout = Workout(
@@ -66,8 +134,8 @@
             duration=timedelta(minutes=60),
             distance=10, sport='running')
-        root['john'].add_workout(workout)
+        john.add_workout(workout)
         assert root.sports == ['cycling', 'running']
 
-    def test_sports_json(self, root):
+    def test_sports_json(self, root, john):
         assert root.sports_json == json.dumps(["cycling"])
         workout = Workout(
@@ -75,4 +143,4 @@
             duration=timedelta(minutes=60),
             distance=10, sport='running')
-        root['john'].add_workout(workout)
+        john.add_workout(workout)
         assert root.sports_json == json.dumps(["cycling", "running"])
