# Path: wikked/auth.py
# class UserManager:
#     """ A class that keeps track of users and their permissions.
#     """
#     def __init__(self, config):
#         self._roles = {}
#         self._groups = {}
#         self._users = {}
#         parse_config(config, self._roles, self._groups, self._users)
# 
#     def start(self, wiki):
#         pass
# 
#     def init(self, wiki):
#         pass
# 
#     def postInit(self):
#         pass
# 
#     def getUsers(self):
#         for name, info in self._users.items():
#             yield self._createUser(name, info)
# 
#     def getUserNames(self):
#         return self._users.keys()
# 
#     def getUser(self, username):
#         info = self._users.get(username)
#         if info is not None:
#             return self._createUser(username, info)
#         return None
# 
#     def getGroupNames(self):
#         return self._groups.keys()
# 
#     def hasPagePermission(self, page, username, perm):
#         extra_acl = None
#         page_perms = page.getLocalMeta('acl')
#         if page_perms is not None:
#             extra_acl = []
#             for pp in page_perms:
#                 m = re_page_acl.match(pp)
#                 if m:
#                     name = m.group('name')
#                     perms = [p.strip()
#                              for p in re_sep.split(m.group('perms'))]
#                     extra_acl.append((name, perms))
# 
#         return self.hasPermission(username, perm, extra_acl)
# 
#     def hasPermission(self, username, perm, extra_acl=None):
#         username = username or ANONYMOUS_USERNAME
#         user_info = self._users.get(username)
#         if user_info is None:
#             raise NoSuchGroupOrUserError(username)
# 
#         # Start with the user permissions, and patch them with whatever
#         # extra permissions specify.
#         effective_perms = user_info.flattened_perms
#         if extra_acl is not None:
#             for name, perms in extra_acl:
#                 if (name == username or
#                         name in user_info.flattened_lineage):
#                     for p in perms:
#                         if p[0] == '+':
#                             # Add permission.
#                             p_bit = self._getPermissions(p[1:])
#                             effective_perms |= p_bit
#                         elif p[0] == '-':
#                             # Remove permission.
#                             p_bit = self._getPermissions(p[1:])
#                             effective_perms &= ~p_bit
#                         else:
#                             # Replace permissions.
#                             p_bit = self._getPermissions(p)
#                             effective_perms = p_bit
# 
#         # Test the effective permissions now!
#         return (effective_perms & perm) != 0
# 
#     def _getPermissions(self, perm_or_role):
#         role_info = self._roles.get(perm_or_role)
#         if role_info is not None:
#             return (role_info.allows & ~role_info.denies)
#         p_bit = PERM_NAMES.get(perm_or_role)
#         if p_bit is not None:
#             return p_bit
#         raise InvalidPermissionError(
#             "'%s' is not a valid permission or role." % perm_or_role)
# 
#     def _createUser(self, name, info):
#         u = User(name, info.password)
#         u.permissions = info.flattened_perms
#         u.groups += info.flattened_lineage
#         return u
# 
# PERM_NAMES = {
#         # Page permissions.
#         'none': PERM_NONE,
#         'read': PERM_READ,
#         'edit': PERM_EDIT,
#         'create': PERM_CREATE,
#         'delete': PERM_DELETE,
#         'history': PERM_HISTORY,
#         'revert': PERM_REVERT,
#         'search': PERM_SEARCH,
#         'upload': PERM_UPLOAD,
#         # Site-wide permissions.
#         'index': PERM_INDEX,
#         'list': PERM_LIST,
#         'listrefresh': PERM_LISTREFRESH,
#         'wikihistory': PERM_WIKIHISTORY,
#         'wikiupload': PERM_WIKIUPLOAD,
#         'users': PERM_USERS,
#         # Aliases
#         'write': PERM_EDIT,
#         'all': 0xffff
#         }
# 
# class NoSuchGroupOrUserError(Exception):
#     pass
# 
# class MultipleGroupMembershipError(Exception):
#     pass
# 
# class CyclicUserGroupError(Exception):
#     pass
# 
# class InvalidPermissionError(Exception):
#     pass


import pytest
from configparser import SafeConfigParser
from wikked.auth import (
        UserManager, PERM_NAMES,
        NoSuchGroupOrUserError, MultipleGroupMembershipError,
        CyclicUserGroupError, InvalidPermissionError)


def _user_manager_from_str(txt):
    config = SafeConfigParser()
    config.read_string(txt)
    return UserManager(config)


def _p(name):
    return PERM_NAMES[name]


def test_empty_auth():
    m = _user_manager_from_str("")
    assert list(m.getUserNames()) == ['anonymous']
    assert list(m.getGroupNames()) == ['*']


def test_missing_user1():
    with pytest.raises(NoSuchGroupOrUserError):
        m = _user_manager_from_str("""
[permissions]
dorothy = read
""")


def test_missing_user2():

# Path: wikked/auth.py
# class UserManager:
#     """ A class that keeps track of users and their permissions.
#     """
#     def __init__(self, config):
#         self._roles = {}
#         self._groups = {}
#         self._users = {}
#         parse_config(config, self._roles, self._groups, self._users)
# 
#     def start(self, wiki):
#         pass
# 
#     def init(self, wiki):
#         pass
# 
#     def postInit(self):
#         pass
# 
#     def getUsers(self):
#         for name, info in self._users.items():
#             yield self._createUser(name, info)
# 
#     def getUserNames(self):
#         return self._users.keys()
# 
#     def getUser(self, username):
#         info = self._users.get(username)
#         if info is not None:
#             return self._createUser(username, info)
#         return None
# 
#     def getGroupNames(self):
#         return self._groups.keys()
# 
#     def hasPagePermission(self, page, username, perm):
#         extra_acl = None
#         page_perms = page.getLocalMeta('acl')
#         if page_perms is not None:
#             extra_acl = []
#             for pp in page_perms:
#                 m = re_page_acl.match(pp)
#                 if m:
#                     name = m.group('name')
#                     perms = [p.strip()
#                              for p in re_sep.split(m.group('perms'))]
#                     extra_acl.append((name, perms))
# 
#         return self.hasPermission(username, perm, extra_acl)
# 
#     def hasPermission(self, username, perm, extra_acl=None):
#         username = username or ANONYMOUS_USERNAME
#         user_info = self._users.get(username)
#         if user_info is None:
#             raise NoSuchGroupOrUserError(username)
# 
#         # Start with the user permissions, and patch them with whatever
#         # extra permissions specify.
#         effective_perms = user_info.flattened_perms
#         if extra_acl is not None:
#             for name, perms in extra_acl:
#                 if (name == username or
#                         name in user_info.flattened_lineage):
#                     for p in perms:
#                         if p[0] == '+':
#                             # Add permission.
#                             p_bit = self._getPermissions(p[1:])
#                             effective_perms |= p_bit
#                         elif p[0] == '-':
#                             # Remove permission.
#                             p_bit = self._getPermissions(p[1:])
#                             effective_perms &= ~p_bit
#                         else:
#                             # Replace permissions.
#                             p_bit = self._getPermissions(p)
#                             effective_perms = p_bit
# 
#         # Test the effective permissions now!
#         return (effective_perms & perm) != 0
# 
#     def _getPermissions(self, perm_or_role):
#         role_info = self._roles.get(perm_or_role)
#         if role_info is not None:
#             return (role_info.allows & ~role_info.denies)
#         p_bit = PERM_NAMES.get(perm_or_role)
#         if p_bit is not None:
#             return p_bit
#         raise InvalidPermissionError(
#             "'%s' is not a valid permission or role." % perm_or_role)
# 
#     def _createUser(self, name, info):
#         u = User(name, info.password)
#         u.permissions = info.flattened_perms
#         u.groups += info.flattened_lineage
#         return u
# 
# PERM_NAMES = {
#         # Page permissions.
#         'none': PERM_NONE,
#         'read': PERM_READ,
#         'edit': PERM_EDIT,
#         'create': PERM_CREATE,
#         'delete': PERM_DELETE,
#         'history': PERM_HISTORY,
#         'revert': PERM_REVERT,
#         'search': PERM_SEARCH,
#         'upload': PERM_UPLOAD,
#         # Site-wide permissions.
#         'index': PERM_INDEX,
#         'list': PERM_LIST,
#         'listrefresh': PERM_LISTREFRESH,
#         'wikihistory': PERM_WIKIHISTORY,
#         'wikiupload': PERM_WIKIUPLOAD,
#         'users': PERM_USERS,
#         # Aliases
#         'write': PERM_EDIT,
#         'all': 0xffff
#         }
# 
# class NoSuchGroupOrUserError(Exception):
#     pass
# 
# class MultipleGroupMembershipError(Exception):
#     pass
# 
# class CyclicUserGroupError(Exception):
#     pass
# 
# class InvalidPermissionError(Exception):
#     pass


# Path: tests/test_auth.py
import pytest
from configparser import SafeConfigParser
from wikked.auth import (
        UserManager, PERM_NAMES,
        NoSuchGroupOrUserError, MultipleGroupMembershipError,
        CyclicUserGroupError, InvalidPermissionError)



def _user_manager_from_str(txt):
    config = SafeConfigParser()
    config.read_string(txt)
    return UserManager(config)


def _p(name):
    return PERM_NAMES[name]


def test_empty_auth():
    m = _user_manager_from_str("")
    assert list(m.getUserNames()) == ['anonymous']
    assert list(m.getGroupNames()) == ['*']


def test_missing_user1():
    with pytest.raises(NoSuchGroupOrUserError):
        m = _user_manager_from_str("""
[permissions]
dorothy = read
""")


def test_missing_user2():

    with pytest.raises(NoSuchGroupOrUserError):