def test_add_and_remove_namespace_users(idstorage): nsid = NamespaceID('foo') idstorage.create_namespace(nsid) assert idstorage.get_namespace(NamespaceID('foo')) == Namespace( NamespaceID('foo'), False) idstorage.add_user_to_namespace( nsid, User(AuthsourceID('asone'), Username('u1'))) users = set([User(AuthsourceID('asone'), Username('u1'))]) assert idstorage.get_namespace(nsid) == Namespace(NamespaceID('foo'), False, users) idstorage.add_user_to_namespace( nsid, User(AuthsourceID('astwo'), Username('u2'))) users.add(User(AuthsourceID('astwo'), Username('u2'))) assert idstorage.get_namespace(nsid) == Namespace(NamespaceID('foo'), False, users) idstorage.remove_user_from_namespace( NamespaceID('foo'), User(AuthsourceID('asone'), Username('u1'))) users = set([User(AuthsourceID('astwo'), Username('u2'))]) assert idstorage.get_namespace(nsid) == Namespace(NamespaceID('foo'), False, users) idstorage.remove_user_from_namespace( NamespaceID('foo'), User(AuthsourceID('astwo'), Username('u2'))) assert idstorage.get_namespace(nsid) == Namespace(NamespaceID('foo'), False)
def set_up_data_for_get_namespaces(idstorage): idstorage.create_namespace(NamespaceID('ns1')) idstorage.set_namespace_publicly_mappable(NamespaceID('ns1'), True) idstorage.add_user_to_namespace(NamespaceID('ns1'), User(AuthsourceID('as'), Username('u'))) idstorage.create_namespace(NamespaceID('ns2')) idstorage.create_namespace(NamespaceID('ns3')) idstorage.add_user_to_namespace(NamespaceID('ns3'), User(AuthsourceID('as'), Username('u'))) idstorage.add_user_to_namespace( NamespaceID('ns3'), User(AuthsourceID('astwo'), Username('u3'))) expected = [ Namespace(NamespaceID('ns1'), True, set([User(AuthsourceID('as'), Username('u'))])), Namespace(NamespaceID('ns2'), False), Namespace( NamespaceID('ns3'), False, set([ User(AuthsourceID('as'), Username('u')), User(AuthsourceID('astwo'), Username('u3')) ])) ] return expected
def test_get_mappings_with_filter(): storage = create_autospec(IDMappingStorage, spec_set=True, instance=True) handlers = create_autospec(UserLookupSet, spec_set=True, instance=True) idm = IDMapper(handlers, set(), storage) storage.get_namespaces.return_value = set([Namespace(NamespaceID('n'), False), Namespace(NamespaceID('n1'), False), Namespace(NamespaceID('n2'), False), Namespace(NamespaceID('n3'), False), Namespace(NamespaceID('n4'), False)]) storage.find_mappings.return_value = ( set([ObjectID(NamespaceID('n1'), 'o1'), ObjectID(NamespaceID('n2'), 'o2')]), set([ObjectID(NamespaceID('n3'), 'o3'), ObjectID(NamespaceID('n4'), 'o4')])) assert idm.get_mappings( ObjectID(NamespaceID('n'), 'o'), [NamespaceID('n1'), NamespaceID('n2'), NamespaceID('n4'), NamespaceID('n4')]) == ( set([ObjectID(NamespaceID('n1'), 'o1'), ObjectID(NamespaceID('n2'), 'o2')]), set([ObjectID(NamespaceID('n3'), 'o3'), ObjectID(NamespaceID('n4'), 'o4')])) assert storage.get_namespaces.call_args_list == [(([NamespaceID('n'), NamespaceID('n1'), NamespaceID('n2'), NamespaceID('n4'), NamespaceID('n4')],), {})] assert storage.find_mappings.call_args_list == [((ObjectID(NamespaceID('n'), 'o'),), {'ns_filter': [ NamespaceID('n1'), NamespaceID('n2'), NamespaceID('n4'), NamespaceID('n4')]})]
def test_namespace_without_users(): ns = Namespace( NamespaceID('n'), True, set([ User(AuthsourceID('a'), Username('u')), User(AuthsourceID('b'), Username('b')) ])) assert ns.without_users() == Namespace(NamespaceID('n'), True, set()) assert ns.without_users() == Namespace(NamespaceID('n'), True, None)
def test_create_and_get_namespace(idstorage): idstorage.create_namespace(NamespaceID('foo')) expected = Namespace(NamespaceID('foo'), False, None) assert idstorage.get_namespace(NamespaceID('foo')) == expected idstorage.create_namespace(NamespaceID('bar')) expected = Namespace(NamespaceID('bar'), False, None) assert idstorage.get_namespace(NamespaceID('bar')) == expected
def test_get_namespaces_only_private(): storage = create_autospec(IDMappingStorage, spec_set=True, instance=True) handlers = create_autospec(UserLookupSet, spec_set=True, instance=True) idm = IDMapper(handlers, set(), storage) storage.get_namespaces.return_value = set([Namespace(NamespaceID('n3'), False), Namespace(NamespaceID('n4'), False)]) assert idm.get_namespaces() == (set(), set([NamespaceID('n3'), NamespaceID('n4')])) assert storage.get_namespaces.call_args_list == [((), {})]
def test_get_namespace_no_auth(): storage = create_autospec(IDMappingStorage, spec_set=True, instance=True) handlers = create_autospec(UserLookupSet, spec_set=True, instance=True) idm = IDMapper(handlers, set(), storage) storage.get_namespace.return_value = Namespace(NamespaceID('n'), True, set([ User(AuthsourceID('a'), Username('u')), User(AuthsourceID('a'), Username('u1'))])) assert idm.get_namespace(NamespaceID('n')) == Namespace(NamespaceID('n'), True) assert storage.get_namespace.call_args_list == [((NamespaceID('n'), ), {})]
def check_set_namespace_publicly_mappable(pub_value, log_collector): storage = create_autospec(IDMappingStorage, spec_set=True, instance=True) handlers = create_autospec(UserLookupSet, spec_set=True, instance=True) idm = IDMapper(handlers, set([AuthsourceID('asone')]), storage) handlers.get_user.return_value = (User(AuthsourceID('asone'), Username('u')), False) storage.get_namespace.return_value = Namespace(NamespaceID('n'), False, set([ User(AuthsourceID('astwo'), Username('u2')), User(AuthsourceID('asone'), Username('u')), User(AuthsourceID('asthree'), Username('u'))])) idm.set_namespace_publicly_mappable( AuthsourceID('asone'), Token('t'), NamespaceID('n'), pub_value) assert handlers.get_user.call_args_list == [((AuthsourceID('asone'), Token('t'),), {})] assert storage.get_namespace.call_args_list == [((NamespaceID('n'),), {})] assert storage.set_namespace_publicly_mappable.call_args_list == \ [((NamespaceID('n'), pub_value), {})] print(log_collector) assert_logs_correct(log_collector, 'User asone/u set namespace n public map property to ' + str(pub_value))
def test_create_mapping_fail_unauthed_for_other_namespace(): storage = create_autospec(IDMappingStorage, spec_set=True, instance=True) handlers = create_autospec(UserLookupSet, spec_set=True, instance=True) idm = IDMapper(handlers, set(), storage) handlers.get_user.return_value = (User(AuthsourceID('a'), Username('n')), False) storage.get_namespace.side_effect = [ Namespace(NamespaceID('n1'), True, set([ User(AuthsourceID('a'), Username('n')), User(AuthsourceID('a'), Username('n2'))])), Namespace(NamespaceID('n2'), False, set([ User(AuthsourceID('a'), Username('n2')), User(AuthsourceID('b'), Username('n2'))]))] fail_create_mapping(idm, AuthsourceID('a'), Token('t'), ObjectID(NamespaceID('n1'), 'o1'), ObjectID(NamespaceID('n2'), 'o2'), UnauthorizedError('User a/n may not administrate namespace n2'))
def test_namespace_hash(): # string hashes will change from instance to instance of the python interpreter, and therefore # tests can't be written that directly test the hash value. See # https://docs.python.org/3/reference/datamodel.html#object.__hash__ asid = AuthsourceID('as') assert hash(Namespace(NamespaceID('foo'), True, None)) == \ hash(Namespace(NamespaceID('foo'), True, set())) assert hash(Namespace(NamespaceID('foo'), False, set([User(asid, 'foo'), User(asid, 'baz')]))) == \ hash(Namespace(NamespaceID('foo'), False, set([User(asid, 'baz'), User(asid, 'foo')]))) assert hash(Namespace(NamespaceID('bar'), True, set())) != \ hash(Namespace(NamespaceID('foo'), True, set())) assert hash(Namespace(NamespaceID('foo'), False, set())) != \ hash(Namespace(NamespaceID('foo'), True, set())) assert hash(Namespace(NamespaceID('foo'), False, set([User(asid, 'foo'), User(asid, 'baz')]))) != \ hash(Namespace(NamespaceID('foo'), False, set([User(asid, 'baz'), User(asid, 'fob')])))
def test_set_namespace_publicly_mappable(idstorage): idstorage.create_namespace(NamespaceID('foo')) assert idstorage.get_namespace(NamespaceID('foo')) == Namespace( NamespaceID('foo'), False) idstorage.set_namespace_publicly_mappable(NamespaceID('foo'), True) assert idstorage.get_namespace(NamespaceID('foo')) == Namespace( NamespaceID('foo'), True) idstorage.set_namespace_publicly_mappable(NamespaceID('foo'), False) assert idstorage.get_namespace(NamespaceID('foo')) == Namespace( NamespaceID('foo'), False) idstorage.set_namespace_publicly_mappable(NamespaceID('foo'), True) assert idstorage.get_namespace(NamespaceID('foo')) == Namespace( NamespaceID('foo'), True) idstorage.set_namespace_publicly_mappable(NamespaceID('foo'), None) assert idstorage.get_namespace(NamespaceID('foo')) == Namespace( NamespaceID('foo'), False)
def test_namespace_init_pass(): ns = Namespace(NamespaceID('foo'), True) assert ns.namespace_id == NamespaceID('foo') assert ns.is_publicly_mappable is True assert ns.authed_users == set() ns = Namespace(NamespaceID('whee'), False, None) assert ns.namespace_id == NamespaceID('whee') assert ns.is_publicly_mappable is False assert ns.authed_users == set() ns = Namespace(NamespaceID('baz'), True, set()) assert ns.namespace_id == NamespaceID('baz') assert ns.is_publicly_mappable is True assert ns.authed_users == set() ns = Namespace(NamespaceID('foo'), False, set([User(AuthsourceID('bar'), Username('baz'))])) assert ns.namespace_id == NamespaceID('foo') assert ns.is_publicly_mappable is False assert ns.authed_users == set([User(AuthsourceID('bar'), Username('baz'))])
def test_remove_mapping(log_collector): storage = create_autospec(IDMappingStorage, spec_set=True, instance=True) handlers = create_autospec(UserLookupSet, spec_set=True, instance=True) idm = IDMapper(handlers, set(), storage) handlers.get_user.return_value = (User(AuthsourceID('a'), Username('n')), False) storage.get_namespace.side_effect = [ Namespace(NamespaceID('n1'), False, set([ User(AuthsourceID('a'), Username('n')), User(AuthsourceID('a'), Username('n2'))])), Namespace(NamespaceID('n2'), False)] idm.remove_mapping(AuthsourceID('a'), Token('t'), ObjectID(NamespaceID('n1'), 'o1'), ObjectID(NamespaceID('n2'), 'o2')) assert handlers.get_user.call_args_list == [((AuthsourceID('a'), Token('t'),), {})] assert storage.get_namespace.call_args_list == [((NamespaceID('n1'),), {}), ((NamespaceID('n2'),), {})] assert storage.remove_mapping.call_args_list == [((ObjectID(NamespaceID('n1'), 'o1'), ObjectID(NamespaceID('n2'), 'o2')), {})] assert_logs_correct(log_collector, 'User a/n removed mapping n1/o1 <---> n2/o2')
def test_set_namespace_publicly_mappable_fail_unauthed(): storage = create_autospec(IDMappingStorage, spec_set=True, instance=True) handlers = create_autospec(UserLookupSet, spec_set=True, instance=True) idm = IDMapper(handlers, set([AuthsourceID('asone')]), storage) handlers.get_user.return_value = (User(AuthsourceID('asone'), Username('u')), False) storage.get_namespace.return_value = Namespace(NamespaceID('n'), False, set([ User(AuthsourceID('asone'), Username('u2')), User(AuthsourceID('asthree'), Username('u'))])) fail_set_namespace_publicly_mappable( idm, AuthsourceID('asone'), Token('t'), NamespaceID('n'), UnauthorizedError('User asone/u may not administrate namespace n'))
def test_remove_mapping_fail_no_such_other_namespace(): # since the return value of the 2nd get namespace call isn't used, and the reason for the # call is to check the namespace exists, we explicitly test the call is made by throwing # an exception. storage = create_autospec(IDMappingStorage, spec_set=True, instance=True) handlers = create_autospec(UserLookupSet, spec_set=True, instance=True) idm = IDMapper(handlers, set(), storage) handlers.get_user.return_value = (User(AuthsourceID('a'), Username('n')), False) storage.get_namespace.side_effect = [ Namespace(NamespaceID('n1'), False, set([ User(AuthsourceID('a'), Username('n')), User(AuthsourceID('a'), Username('n2'))])), NoSuchNamespaceError('n2')] fail_remove_mapping(idm, AuthsourceID('a'), Token('t'), ObjectID(NamespaceID('n1'), 'o1'), ObjectID(NamespaceID('n2'), 'o2'), NoSuchNamespaceError('n2'))
def test_create_mapping_privately_mappable(log_collector): targetns = Namespace(NamespaceID('n2'), False, set([ User(AuthsourceID('a'), Username('n')), User(AuthsourceID('b'), Username('n2'))])) check_create_mapping(targetns, log_collector)
def fail_namespace_init(id_, authed_users, expected): with raises(Exception) as got: Namespace(id_, True, authed_users) assert_exception_correct(got.value, expected)
def test_namespace_equals(): asid = AuthsourceID('as') assert Namespace(NamespaceID('foo'), True, None) == Namespace(NamespaceID('foo'), True, set()) assert Namespace(NamespaceID('foo'), False, set([User(asid, 'foo'), User(asid, 'baz')])) == \ Namespace(NamespaceID('foo'), False, set([User(asid, 'baz'), User(asid, 'foo')])) assert Namespace(NamespaceID('bar'), True, set()) != Namespace( NamespaceID('foo'), True, set()) assert Namespace(NamespaceID('foo'), False, set()) != \ Namespace(NamespaceID('foo'), True, set()) assert Namespace(NamespaceID('foo'), False, set([User(asid, 'foo'), User(asid, 'baz')])) != \ Namespace(NamespaceID('foo'), False, set([User(asid, 'baz'), User(asid, 'fob')])) assert Namespace(NamespaceID('foo'), False, set()) != NamespaceID('foo')
def test_create_mapping_publicly_mappable(log_collector): check_create_mapping(Namespace(NamespaceID('n2'), True), log_collector)
def _to_ns(self, nsdoc): return Namespace(NamespaceID(nsdoc[_FLD_NS_ID]), nsdoc[_FLD_PUB_MAP], self._to_user_set(nsdoc[_FLD_USERS]))