""" shrunk - Rutgers University URL Shortener

Unit tests for roles system.
"""

from pytest import raises
from shrunk.client import ShrunkClient
from shrunk.roles import *

client = ShrunkClient(host='unit_db')
mongo_client = client._mongo
init(None, mongo_client=mongo_client)

def test_invalid():
    with raises(InvalidEntity):
        grant('root', 'Justice League', 'peb60')

def test_get():
    def check(expected):
        assert sorted(get('peb60')) == sorted(expected)

    check([])
    grant('admin', 'Justice League', 'peb60')
    check(['admin'])
    grant('power_user', 'Justice League', 'peb60')
    check(['admin', 'power_user'])
    grant('facstaff', 'Justice League', 'peb60')
    check(['admin', 'power_user', 'facstaff'])

    revoke('power_user', 'peb60')
    check(['admin', 'facstaff'])
    revoke('admin', 'peb60')
    check(['facstaff'])
    revoke('facstaff', 'peb60')
    check([])

def test_granted_by():
    grant('admin', 'Justice League', 'peb60')
    assert granted_by('admin', 'peb60') == 'Justice League'
    assert granted_by('power_user', 'peb60') == None

def test_list_all():
    def check(role, expected):
        entities = map(lambda g: g['entity'], list_all(role))
        assert sorted(entities) == sorted(expected)

    check('facstaff', [])
    check('power_user', [])

    grant('facstaff', 'Justice League', 'peb60')
    check('facstaff', ['peb60'])
    check('power_user', [])

    grant('power_user', 'Justice League', 'jcc')
    check('facstaff', ['peb60'])
    check('power_user', ['jcc'])

    grant('facstaff', 'Justice League', 'mjw271')
    check('facstaff', ['peb60', 'mjw271'])
    check('power_user', ['jcc'])

    revoke('facstaff', 'peb60')
    check('facstaff', ['mjw271'])
    check('power_user', ['jcc'])

    revoke('facstaff', 'mjw271')
    check('facstaff', [])
    check('power_user', ['jcc'])

    revoke('power_user', 'jcc')
    check('facstaff', [])
    check('power_user', [])

def test_valid_roles():
    def check(expected):
        for r in expected:
            assert r in valid_roles()

    true = lambda _: True

    new('role0', true)
    check(['role0'])

    new('role1', true)
    check(['role0', 'role1'])

    new('role2', true)
    check(['role0', 'role1', 'role2'])

def test_template_data():
    grant('role0', 'shrunk_test', 'entity0')
    grant('role0', 'shrunk_test', 'entity1')
    grant('role0', 'not_shrunk_test', 'entity2')

    template = template_data('role0', 'shrunk_test')
    assert not template['admin']
    assert not template['power_user']
    assert not template['facstaff']
    assert len(template['grants']) == 2
    
    grant('admin', 'Justice League', 'shrunk_test')

    template = template_data('role0', 'shrunk_test')
    assert template['admin']
    assert not template['power_user']
    assert not template['facstaff']
    assert len(template['grants']) == 3

    template = template_data('role0', 'shrunk_test', invalid=True)
    assert template['msg'] == 'invalid entity for role role0'

@@ -273,16 +273,93 @@
 lient)%0A%0A
+def teardown_function():%0A        mongo_client.drop_database(%22shrunk_roles%22)%0A%0A
 def test
@@ -2694,13 +2694,9 @@
 = 2%0A
-    
 %0A
+
     
@@ -3004,24 +3004,24 @@
 valid=True)%0A
-
     assert t
@@ -3070,8 +3070,660 @@
  role0'%0A
+%0Adef test_has_one_of():%0A    new(%22prole0%22, True)%0A    new(%22prole1%22, True)%0A    grant('prole0', 'shrunk_test', 'entity0')%0A    grant('prole1', 'shrunk_test', 'entity0')%0A    grant('prole0', 'shrunk_test', 'entity1')%0A    grant('prole1', 'shrunk_test', 'entity2')%0A%0A    assert has_one_of(%5B%22prole0%22, %22prole1%22, %22bogus%22%5D, %22entity0%22)%0A    assert has_one_of(%5B%22prole0%22, %22prole1%22, %22bogus%22%5D, %22entity1%22)%0A    assert not has_one_of(%5B%22prole0%22, %22bogus%22%5D, %22entity2%22)%0A%0A    assert has_one_of(%5B%22prole0%22, %22prole1%22, %22bogus%22%5D, %22entity2%22)%0A    assert not has_one_of(%5B%22prole1%22, %22bogus%22%5D, %22entity1%22)%0A%0A    assert not has_one_of(%5B%22fweuihiwf%22, %22hash_slining_slasher%22, %22bogus%22%5D, %22entity0%22)%0A

add test for roles and add teardown