# Path: nappingcat/auth.py
# class AuthBackend(object):
#     def __init__(self, settings):
#         self.users = {}
#         self.settings = settings
#         self.require_update = False
# 
#     def has_permission(self, user, permission):
#         full_query = (user,) + tuple(permission)
#         result = self.users
#         for i in full_query:
#             result = result.get(i, {})
#         return bool(result)
# 
#     def add_permission(self, user, permission):
#         self.require_update = True
#         full_query = (user,) + tuple(permission)
#         result = self.users
#         for i in full_query[:-1]:
#             level = result.get(i, None)
#             if level is None:
#                 result[i] = {}
#             result = result[i]
#         result[full_query[-1]] = True
# 
#     def remove_permission(self, user, permission):
#         self.require_update = True
#         full_query = (user,) + tuple(permission)
#         result = self.users
#         for i in full_query[:-1]:
#             level = result.get(i, None)
#             if level is None:
#                 result[i] = {}
#             result = result[i]
#         del result[full_query[-1]]
# 
#     def add_user(self, username):
#         self.require_update = True
#         self.users[username] = {'keys':[]}
# 
#     def add_key_to_user(self, user, key):
#         self.require_update = True
#         self.users[user]['keys'].append(key)
# 
#     def get_keys(self, username):
#         return self.users[username]['keys']
# 
#     def finish(self, pubkey_handler):
#         if self.require_update:
#             pubkey_handler.flush_keys(self)
# 
#     def get_users(self):
#         return self.users.keys()
# 
# Path: nappingcat/contrib/auth/handlers.py
# PERMISSION_SEP = '::'
# def add_user(request, username):
# def add_key_to_user(request, username):
# def add_permission(request, username, permission):
# def remove_permission(request, username, permission):
# 
# Path: nappingcat/request.py
# class Request(object):
#     def __init__(self, user, command, settings, streams, root_patterns):
# 
# Path: nappingcat/exceptions.py
# class NappingCatRejected(NappingCatException):
#     pass


from unittest import TestCase
from nappingcat.auth import AuthBackend
from nappingcat.contrib.auth import handlers
from nappingcat import request
from nappingcat.exceptions import NappingCatRejected
import ConfigParser
import StringIO
import random
import os
        self.auth.add_user(self.user)
        self.auth.add_permission(self.user, ('auth', 'adduser'))
        random_user = 'new-user-%d' % random.randint(1,100)
        handlers.add_user(self.request, random_user)
        self.assertTrue(random_user in SimpleAuth.user_dict)

    def test_unauth_add_user(self):
        random_user = 'new-user-%d' % random.randint(1,100)
        self.assertRaises(NappingCatRejected, handlers.add_user, self.request, random_user)

    def test_unauth_add_key_to_user(self):
        self.auth.add_user(self.user)
        random_user = 'new-user-%d' % random.randint(1,100)
        self.auth.add_user(random_user)
        self.assertRaises(NappingCatRejected, handlers.add_key_to_user, self.request, random_user)

    def test_add_permission_actually_adds_permission(self):
        self.auth.add_user(self.user)
        self.auth.add_permission(self.user, ('auth', 'modifyuser'))
        random_user = 'new-user-%d' % random.randint(1,100)
        self.auth.add_user(random_user)
        random_perm = (str(random.randint(1,100)), str(random.randint(1,100))) * random.randint(1,10)
        handlers.add_permission(self.request, random_user, handlers.PERMISSION_SEP.join(random_perm))
        self.assertTrue(self.auth.has_permission(random_user, random_perm))

    def test_unauth_add_permission(self):
        self.auth.add_user(self.user)
        random_user = 'new-user-%d' % random.randint(1,100)
        self.auth.add_user(random_user)
        random_perm = (str(random.randint(1,100)), str(random.randint(1,100))) * random.randint(1,10)

# Path: nappingcat/auth.py
# class AuthBackend(object):
#     def __init__(self, settings):
#         self.users = {}
#         self.settings = settings
#         self.require_update = False
# 
#     def has_permission(self, user, permission):
#         full_query = (user,) + tuple(permission)
#         result = self.users
#         for i in full_query:
#             result = result.get(i, {})
#         return bool(result)
# 
#     def add_permission(self, user, permission):
#         self.require_update = True
#         full_query = (user,) + tuple(permission)
#         result = self.users
#         for i in full_query[:-1]:
#             level = result.get(i, None)
#             if level is None:
#                 result[i] = {}
#             result = result[i]
#         result[full_query[-1]] = True
# 
#     def remove_permission(self, user, permission):
#         self.require_update = True
#         full_query = (user,) + tuple(permission)
#         result = self.users
#         for i in full_query[:-1]:
#             level = result.get(i, None)
#             if level is None:
#                 result[i] = {}
#             result = result[i]
#         del result[full_query[-1]]
# 
#     def add_user(self, username):
#         self.require_update = True
#         self.users[username] = {'keys':[]}
# 
#     def add_key_to_user(self, user, key):
#         self.require_update = True
#         self.users[user]['keys'].append(key)
# 
#     def get_keys(self, username):
#         return self.users[username]['keys']
# 
#     def finish(self, pubkey_handler):
#         if self.require_update:
#             pubkey_handler.flush_keys(self)
# 
#     def get_users(self):
#         return self.users.keys()
# 
# Path: nappingcat/contrib/auth/handlers.py
# PERMISSION_SEP = '::'
# def add_user(request, username):
# def add_key_to_user(request, username):
# def add_permission(request, username, permission):
# def remove_permission(request, username, permission):
# 
# Path: nappingcat/request.py
# class Request(object):
#     def __init__(self, user, command, settings, streams, root_patterns):
# 
# Path: nappingcat/exceptions.py
# class NappingCatRejected(NappingCatException):
#     pass


# Path: tests/contribauth.py
from unittest import TestCase
from nappingcat.auth import AuthBackend
from nappingcat.contrib.auth import handlers
from nappingcat import request
from nappingcat.exceptions import NappingCatRejected
import ConfigParser
import StringIO
import random
import os

        self.auth.add_user(self.user)
        self.auth.add_permission(self.user, ('auth', 'adduser'))
        random_user = 'new-user-%d' % random.randint(1,100)
        handlers.add_user(self.request, random_user)
        self.assertTrue(random_user in SimpleAuth.user_dict)

    def test_unauth_add_user(self):
        random_user = 'new-user-%d' % random.randint(1,100)
        self.assertRaises(NappingCatRejected, handlers.add_user, self.request, random_user)

    def test_unauth_add_key_to_user(self):
        self.auth.add_user(self.user)
        random_user = 'new-user-%d' % random.randint(1,100)
        self.auth.add_user(random_user)
        self.assertRaises(NappingCatRejected, handlers.add_key_to_user, self.request, random_user)

    def test_add_permission_actually_adds_permission(self):
        self.auth.add_user(self.user)
        self.auth.add_permission(self.user, ('auth', 'modifyuser'))
        random_user = 'new-user-%d' % random.randint(1,100)
        self.auth.add_user(random_user)
        random_perm = (str(random.randint(1,100)), str(random.randint(1,100))) * random.randint(1,10)
        handlers.add_permission(self.request, random_user, handlers.PERMISSION_SEP.join(random_perm))
        self.assertTrue(self.auth.has_permission(random_user, random_perm))

    def test_unauth_add_permission(self):
        self.auth.add_user(self.user)
        random_user = 'new-user-%d' % random.randint(1,100)
        self.auth.add_user(random_user)
        random_perm = (str(random.randint(1,100)), str(random.randint(1,100))) * random.randint(1,10)

        self.assertRaises(NappingCatRejected, handlers.add_permission, self.request, random_user, handlers.PERMISSION_SEP.join(random_perm))