def test_sample_eq():
    sn = SampleNode('foo')
    sn2 = SampleNode('bar')

    assert Sample([sn], 'yay') == Sample([sn], 'yay')
    assert Sample([sn], 'yay') != Sample([sn2], 'yay')
    assert Sample([sn], 'yay') != Sample([sn], 'yooo')

    id1 = uuid.UUID('1234567890abcdef1234567890abcdef')
    id2 = uuid.UUID('1234567890abcdef1234567890abcdea')
    dt1 = dt(5)
    dt2 = dt(8)
    u = UserID('u')
    u2 = UserID('u2')

    assert SavedSample(id1, u, [sn], dt1) == SavedSample(id1, u, [sn], dt(5))
    assert SavedSample(id1, u, [sn], dt1) != SavedSample(id2, u, [sn], dt1)
    assert SavedSample(id1, u, [sn], dt1) != SavedSample(id1, u2, [sn], dt1)
    assert SavedSample(id1, u, [sn], dt1) != SavedSample(id1, u, [sn2], dt1)
    assert SavedSample(id1, u, [sn], dt1) != SavedSample(id1, u, [sn], dt2)

    assert SavedSample(id1, u, [sn], dt1, 'yay') == SavedSample(id1, u, [sn], dt1, 'yay')
    assert SavedSample(id1, u, [sn], dt1, 'yay') != SavedSample(id1, u, [sn], dt1, 'yooo')

    assert SavedSample(id1, u, [sn], dt2, 'yay', 6) == SavedSample(id1, u, [sn], dt2, 'yay', 6)
    assert SavedSample(id1, u, [sn], dt1, 'yay', 6) != SavedSample(id1, u, [sn], dt1, 'yay', 7)

    assert SavedSample(id1, u, [sn], dt1, 'yay') != Sample([sn], 'yay')
    assert Sample([sn], 'yay') != SavedSample(id1, u, [sn], dt1, 'yay')
Beispiel #2
0
def test_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__
    assert hash(UserID('u')) == hash(UserID('u'))

    assert hash(UserID('u')) != hash(UserID('v'))
def test_sample_build():
    sn = SampleNode('foo')
    sn2 = SampleNode('bat')
    sn3 = SampleNode('bar', type_=SubSampleType.TECHNICAL_REPLICATE, parent='foo')
    sn4 = SampleNode('baz', type_=SubSampleType.SUB_SAMPLE, parent='foo')
    sndup = SampleNode('foo')

    s = Sample([sn])
    assert s.nodes == (sndup,)
    assert s.name is None

    s = Sample([sn, sn2, sn4, sn3], '   \t   foo    ')
    assert s.nodes == (sndup, sn2, sn4, sn3)
    assert s.name == 'foo'

    s = Sample([sn], 'a' * 256)
    assert s.nodes == (sndup,)
    assert s.name == 'a' * 256

    id_ = uuid.UUID('1234567890abcdef1234567890abcdef')
    s = SavedSample(id_, UserID('user'), [sn], dt(6))
    assert s.id == uuid.UUID('1234567890abcdef1234567890abcdef')
    assert s.user == UserID('user')
    assert s.nodes == (sndup,)
    assert s.savetime == dt(6)
    assert s.name is None
    assert s.version is None

    s = SavedSample(id_, UserID('user2'), [sn], dt(6), 'foo')
    assert s.id == uuid.UUID('1234567890abcdef1234567890abcdef')
    assert s.user == UserID('user2')
    assert s.nodes == (sndup,)
    assert s.savetime == dt(6)
    assert s.name == 'foo'
    assert s.version is None

    s = SavedSample(id_, UserID('user'), [sn], dt(6), 'foo', 1)
    assert s.id == uuid.UUID('1234567890abcdef1234567890abcdef')
    assert s.user == UserID('user')
    assert s.nodes == (sndup,)
    assert s.savetime == dt(6)
    assert s.name == 'foo'
    assert s.version == 1

    s = SavedSample(id_, UserID('user'), [sn], dt(6), 'foo', 8)
    assert s.id == uuid.UUID('1234567890abcdef1234567890abcdef')
    assert s.user == UserID('user')
    assert s.nodes == (sndup,)
    assert s.savetime == dt(6)
    assert s.name == 'foo'
    assert s.version == 8

    s = SavedSample(id_, UserID('user'), [sn], dt(6), version=8)
    assert s.id == uuid.UUID('1234567890abcdef1234567890abcdef')
    assert s.user == UserID('user')
    assert s.nodes == (sndup,)
    assert s.savetime == dt(6)
    assert s.name is None
    assert s.version == 8
def _is_equivalent(duid1, sna1, duid2, sna2, expected):
    dl1 = DataLink(uuid.UUID('1234567890abcdef1234567890abcdee'), duid1, sna1,
                   dt(400), UserID('myuserᚥnameisHank'), dt(400),
                   UserID('yay'))

    dl2 = DataLink(uuid.UUID('1234567890abcdef1234567890abcdef'), duid2, sna2,
                   dt(500), UserID('waaaahrrgg'), dt(8900),
                   UserID('go faster stripes'))

    assert dl1.is_equivalent(dl2) is expected
    assert dl2.is_equivalent(dl1) is expected
def test_is_equivalent_fail():
    sid = uuid.UUID('1234567890abcdef1234567890abcdef')
    dl1 = DataLink(uuid.UUID('1234567890abcdef1234567890abcdee'),
                   DataUnitID(UPA('2/6/4'), 'whee'),
                   SampleNodeAddress(SampleAddress(sid, 8), 'bar'), dt(400),
                   UserID('myuserᚥnameisHank'), dt(400), UserID('yay'))
    with raises(Exception) as got:
        dl1.is_equivalent(None)
    assert_exception_correct(
        got.value,
        ValueError('link cannot be a value that evaluates to false'))
def test_hash():
    # 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__
    lid1 = uuid.UUID('1234567890abcdef1234567890abcdee')
    lid1a = uuid.UUID('1234567890abcdef1234567890abcdee')
    lid2 = uuid.UUID('1234567890abcdef1234567890abcdec')
    lid2a = uuid.UUID('1234567890abcdef1234567890abcdec')
    d1 = DataUnitID(UPA('1/1/1'))
    d1a = DataUnitID(UPA('1/1/1'))
    d2 = DataUnitID(UPA('1/1/2'))
    d2a = DataUnitID(UPA('1/1/2'))
    id_ = uuid.UUID('1234567890abcdef1234567890abcdef')
    s1 = SampleNodeAddress(SampleAddress(id_, 1), 'foo')
    s1a = SampleNodeAddress(SampleAddress(id_, 1), 'foo')
    s2 = SampleNodeAddress(SampleAddress(id_, 2), 'foo')
    s2a = SampleNodeAddress(SampleAddress(id_, 2), 'foo')
    t1 = dt(500)
    t1a = dt(500)
    t2 = dt(600)
    t2a = dt(600)
    u1 = UserID('u')
    u1a = UserID('u')
    u2 = UserID('y')
    u2a = UserID('y')

    assert hash(DataLink(lid1, d1, s1, t1,
                         u1)) == hash(DataLink(lid1a, d1a, s1a, t1a, u1a))
    assert hash(DataLink(lid1, d1, s1, t1, u1, None)) == hash(
        DataLink(lid1a, d1a, s1a, t1a, u1a, None))
    assert hash(DataLink(lid2, d2, s2, t1, u2, t2, u1)) == hash(
        DataLink(lid2a, d2a, s2a, t1a, u2a, t2a, u1a))

    assert hash(DataLink(lid1, d1, s1, t1, u1)) != hash(
        DataLink(lid2, d1a, s1a, t1a, u1a))
    assert hash(DataLink(lid1, d1, s1, t1, u1)) != hash(
        DataLink(lid1a, d2, s1a, t1a, u1a))
    assert hash(DataLink(lid1, d1, s1, t1, u1)) != hash(
        DataLink(lid1a, d1a, s2, t1a, u1a))
    assert hash(DataLink(lid1, d1, s1, t1, u1)) != hash(
        DataLink(lid1a, d1a, s1a, t2, u1a))
    assert hash(DataLink(lid1, d1, s1, t1, u1)) != hash(
        DataLink(lid1a, d1a, s1a, t1a, u2))
    assert hash(DataLink(lid1, d1, s1, t1, u1, t2, u2)) != hash(
        DataLink(lid1a, d1a, s1a, t1a, u1a, t1, u2a))
    assert hash(DataLink(lid1, d1, s1, t1, u1, t2, u2)) != hash(
        DataLink(lid1a, d1a, s1a, t1a, u1a, t2a, u1a))
    assert hash(DataLink(lid1, d1, s1, t1, u1, t1, u2)) != hash(
        DataLink(lid1a, d1a, s1a, t1a, u1a))
    assert hash(DataLink(lid1, d1, s1, t1, u1)) != hash(
        DataLink(lid1a, d1a, s1a, t1a, u1a, t1a, u2a))
def test_check_admin_fail_no_such_user():
    ul = create_autospec(KBaseUserLookup, spec_set=True, instance=True)
    log = []

    ul.is_admin.return_value = (AdminPermission.FULL, 'user1')
    ul.invalid_users.return_value = [UserID('bruh')]

    _check_admin_fail(
        ul, 'token', AdminPermission.FULL, 'a method', lambda x: log.append(x), UserID('bruh'),
        NoSuchUserError('bruh'))

    ul.is_admin.assert_called_once_with('token')
    ul.invalid_users.assert_called_once_with([UserID('bruh')])
    assert log == []
def test_links_to_dicts_fail_bad_args():
    dl = DataLink(
            UUID('f5bd78c3-823e-40b2-9f93-20e78680e41e'),
            DataUnitID(UPA('1/2/3'), 'foo'),
            SampleNodeAddress(
                SampleAddress(UUID('f5bd78c3-823e-40b2-9f93-20e78680e41f'), 6), 'foo'),
            dt(0.067),
            UserID('usera'),
            dt(89),
            UserID('userb')
        )

    _links_to_dicts_fail(None, ValueError('links cannot be None'))
    _links_to_dicts_fail([dl, None], ValueError(
        'Index 1 of iterable links cannot be a value that evaluates to false'))
def test_sample_build_fail():
    # not testing every permutation of failing check_string here, just one test to make sure
    # it's there

    id_ = uuid.UUID('1234567890abcdef1234567890abcdef')
    u = UserID('user')
    sn = SampleNode('foo')
    tn = SampleNode('bar', SubSampleType.TECHNICAL_REPLICATE, 'foo')
    sn2 = SampleNode('baz')
    dup = SampleNode('foo')
    d = dt(8)

    _sample_build_fail(
        [sn], 'a' * 257, IllegalParameterError('name exceeds maximum length of 256'))
    _sample_build_fail([], None, MissingParameterError('At least one node per sample is required'))
    _sample_build_fail(
        [tn, sn], 'a', IllegalParameterError('The first node in a sample must be a BioReplicate'))
    _sample_build_fail([sn, tn, sn2], 'a', IllegalParameterError(
                       'BioReplicates must be the first nodes in the list of sample nodes.'))
    _sample_build_fail([sn, sn2, dup], 'a', IllegalParameterError(
                       'Duplicate sample node name: foo'))
    _sample_build_fail([sn2, tn], 'a', IllegalParameterError(
                        'Parent foo of node bar does not appear in node list prior to node.'))

    _sample_with_id_build_fail(None, u, [sn], d, None, None,
                               ValueError('id_ cannot be a value that evaluates to false'))
    _sample_with_id_build_fail(id_, None, [sn], d, None, None,
                               ValueError('user cannot be a value that evaluates to false'))
    _sample_with_id_build_fail(id_, u, [sn], None, None, None, ValueError(
                               'savetime cannot be a value that evaluates to false'))
    _sample_with_id_build_fail(id_, u, [sn], datetime.datetime.now(), None, None, ValueError(
                               'savetime cannot be a naive datetime'))
    _sample_with_id_build_fail(id_, u, [sn], d, None, 0, ValueError('version must be > 0'))
def test_acls_to_dict_minimal():
    assert acls_to_dict(SampleACL(UserID('user'))) == {
        'owner': 'user',
        'admin': (),
        'write': (),
        'read': ()
    }
def test_init_fail():
    lid = uuid.UUID('1234567890abcdef1234567890abcdee')
    d = DataUnitID(UPA('1/1/1'))
    sid = uuid.UUID('1234567890abcdef1234567890abcdef')
    s = SampleNodeAddress(SampleAddress(sid, 1), 'a')
    t = dt(1)
    u = UserID('u')
    bt = datetime.datetime.now()
    n = None

    _init_fail(None, d, s, t, u, n, n,
               ValueError('id_ cannot be a value that evaluates to false'))
    _init_fail(lid, None, s, t, u, n, n,
               ValueError('duid cannot be a value that evaluates to false'))
    _init_fail(
        lid, d, None, t, u, n, n,
        ValueError(
            'sample_node_address cannot be a value that evaluates to false'))
    _init_fail(lid, d, s, None, u, n, n,
               ValueError('created cannot be a value that evaluates to false'))
    _init_fail(lid, d, s, bt, u, n, n,
               ValueError('created cannot be a naive datetime'))
    _init_fail(
        lid, d, s, t, None, n, n,
        ValueError('created_by cannot be a value that evaluates to false'))
    _init_fail(lid, d, s, t, u, bt, u,
               ValueError('expired cannot be a naive datetime'))
    _init_fail(
        lid, d, s, t, u, t, None,
        ValueError('expired_by cannot be a value that evaluates to false'))
    _init_fail(lid, d, s, dt(100), u, dt(99), u,
               ValueError('link cannot expire before it is created'))
def test_equals():
    lid1 = uuid.UUID('1234567890abcdef1234567890abcdee')
    lid1a = uuid.UUID('1234567890abcdef1234567890abcdee')
    lid2 = uuid.UUID('1234567890abcdef1234567890abcdec')
    lid2a = uuid.UUID('1234567890abcdef1234567890abcdec')
    d1 = DataUnitID(UPA('1/1/1'))
    d1a = DataUnitID(UPA('1/1/1'))
    d2 = DataUnitID(UPA('1/1/2'))
    d2a = DataUnitID(UPA('1/1/2'))
    sid = uuid.UUID('1234567890abcdef1234567890abcdef')
    s1 = SampleNodeAddress(SampleAddress(sid, 1), 'foo')
    s1a = SampleNodeAddress(SampleAddress(sid, 1), 'foo')
    s2 = SampleNodeAddress(SampleAddress(sid, 2), 'foo')
    s2a = SampleNodeAddress(SampleAddress(sid, 2), 'foo')
    t1 = dt(500)
    t1a = dt(500)
    t2 = dt(600)
    t2a = dt(600)
    u1 = UserID('u')
    u1a = UserID('u')
    u2 = UserID('y')
    u2a = UserID('y')

    assert DataLink(lid1, d1, s1, t1, u1) == DataLink(lid1a, d1a, s1a, t1a,
                                                      u1a)
    assert DataLink(lid1, d1, s1, t1, u1,
                    None) == DataLink(lid1a, d1a, s1a, t1a, u1a, None)
    assert DataLink(lid2, d2, s2, t1, u2, t2,
                    u1) == DataLink(lid2a, d2a, s2a, t1a, u2a, t2a, u1a)

    assert DataLink(lid1, d1, s1, t1, u1) != (lid1, d1, s1, t1, u1)
    assert DataLink(lid1, d1, s1, t1, u1, t2,
                    u2) != (lid1, d1, s1, t1, u1, t2, u2)

    assert DataLink(lid1, d1, s1, t1, u1) != DataLink(lid2, d1a, s1a, t1a, u1a)
    assert DataLink(lid1, d1, s1, t1, u1) != DataLink(lid1a, d2, s1a, t1a, u1a)
    assert DataLink(lid1, d1, s1, t1, u1) != DataLink(lid1a, d1a, s2, t1a, u1a)
    assert DataLink(lid1, d1, s1, t1, u1) != DataLink(lid1a, d1a, s1a, t2, u1a)
    assert DataLink(lid1, d1, s1, t1, u1) != DataLink(lid1a, d1a, s1a, t1, u2)
    assert DataLink(lid1, d1, s1, t1, u1, t2, u2) != DataLink(
        lid1a, d1a, s1a, t1a, u1a, t1, u2a)
    assert DataLink(lid1, d1, s1, t1, u1, t2, u2) != DataLink(
        lid1a, d1a, s1a, t1a, u1a, t2a, u1)
    assert DataLink(lid1, d1, s1, t1, u1, t1, u1) != DataLink(
        lid1a, d1a, s1a, t1a, u1a)
    assert DataLink(lid1, d1, s1, t1, u1) != DataLink(lid1a, d1a, s1a, t1a,
                                                      u1a, t1a, u1a)
def test_has_permission_fail_bad_input():
    r = WorkspaceAccessType.READ
    u = UserID('b')
    _has_permission_fail(u, None, None, r, ValueError(
        'Either an UPA or a workpace ID must be supplied'))
    _has_permission_fail(u, 0, None, r, IllegalParameterError('0 is not a valid workspace ID'))
    _has_permission_fail(u, 1, None, None, ValueError(
        'perm cannot be a value that evaluates to false'))
def test_has_permission():
    _has_permission(UserID('a'), None, UPA('42/65/3'), WorkspaceAccessType.READ, 42)
    _has_permission(UserID('b'), 24, UPA('67/2/92'), WorkspaceAccessType.READ, 24)
    _has_permission(UserID('c'), 1, None, WorkspaceAccessType.READ, 1)
    _has_permission(UserID('a'), None, UPA('7/45/789'), WorkspaceAccessType.WRITE, 7)
    _has_permission(UserID('c'), None, UPA('1/1/1'), WorkspaceAccessType.WRITE, 1)
    _has_permission(UserID('c'), 301, None, WorkspaceAccessType.ADMIN, 301)
    _has_permission(UserID('none'), 301, None, WorkspaceAccessType.NONE, 301)
    _has_permission(UserID('none'), 301, None, WorkspaceAccessType.READ, 301, public=True)
    _has_permission(None, 301, None, WorkspaceAccessType.READ, 301, public=True)
def test_links_to_dicts():
    links = [
        DataLink(
            UUID('f5bd78c3-823e-40b2-9f93-20e78680e41e'),
            DataUnitID(UPA('1/2/3'), 'foo'),
            SampleNodeAddress(
                SampleAddress(UUID('f5bd78c3-823e-40b2-9f93-20e78680e41f'), 6), 'foo'),
            dt(0.067),
            UserID('usera'),
            dt(89),
            UserID('userb')
        ),
        DataLink(
            UUID('f5bd78c3-823e-40b2-9f93-20e78680e41a'),
            DataUnitID(UPA('4/9/10')),
            SampleNodeAddress(
                SampleAddress(UUID('f5bd78c3-823e-40b2-9f93-20e78680e41b'), 4), 'bar'),
            dt(1),
            UserID('userc'),
        ),
    ]
    assert links_to_dicts(links) == [
        {
            'upa': '1/2/3',
            'dataid': 'foo',
            'id': 'f5bd78c3-823e-40b2-9f93-20e78680e41f',
            'version': 6,
            'node': 'foo',
            'created': 67,
            'createdby': 'usera',
            'expired': 89000,
            'expiredby': 'userb'
            },
        {
            'upa': '4/9/10',
            'dataid': None,
            'id': 'f5bd78c3-823e-40b2-9f93-20e78680e41b',
            'version': 4,
            'node': 'bar',
            'created': 1000,
            'createdby': 'userc',
            'expired': None,
            'expiredby': None
            }
    ]
def _sample_build_fail(nodes, name, expected):
    with raises(Exception) as got:
        Sample(nodes, name)
    assert_exception_correct(got.value, expected)

    id_ = uuid.UUID('1234567890abcdef1234567890abcdef')
    with raises(Exception) as got:
        SavedSample(id_, UserID('u'), nodes, dt(8), name)
    assert_exception_correct(got.value, expected)
Beispiel #17
0
def test_init():
    u = UserID('foo')

    assert u.id == 'foo'
    assert str(u) == 'foo'
    assert repr(u) == 'UserID("foo")'

    u = UserID('u' * 256)

    assert u.id == 'u' * 256
    assert str(u) == 'u' * 256
    assert repr(u) == f'UserID("{"u" * 256}")'

    u = UserID('u⎇a')

    assert u.id == 'u⎇a'
    assert str(u) == 'u⎇a'
    assert repr(u) == 'UserID("u⎇a")'
def test_sample_build_fail_sample_count():
    nodes = [SampleNode('s' + str(i)) for i in range(10000)]

    s = Sample(nodes)
    assert s.nodes == tuple(nodes)
    assert s.name is None

    id_ = uuid.UUID('1234567890abcdef1234567890abcdef')
    s = SavedSample(id_, UserID('u'), nodes, dt(8))
    assert s.id == uuid.UUID('1234567890abcdef1234567890abcdef')
    assert s.user == UserID('u')
    assert s.nodes == tuple(nodes)
    assert s.savetime == dt(8)
    assert s.name is None
    assert s.version is None

    nodes.append(SampleNode('s10000'))
    _sample_build_fail(nodes, None, IllegalParameterError(
                       'At most 10000 nodes are allowed per sample'))
def test_acls_to_dict_maximal():
    assert acls_to_dict(
        SampleACL(
            UserID('user'),
            [UserID('foo'), UserID('bar')],
            [UserID('baz')],
            [UserID('hello'), UserID("I'm"), UserID('a'), UserID('robot')])) == {
        'owner': 'user',
        'admin': ('foo', 'bar'),
        'write': ('baz',),
        'read': ('hello', "I'm", 'a', 'robot')
    }
Beispiel #20
0
def _get_acl(acls, type_):
    ret = []
    if acls.get(type_) is not None:
        acl = acls[type_]
        if type(acl) != list:
            raise _IllegalParameterError(f'{type_} ACL must be a list')
        for i, item, in enumerate(acl):
            if type(item) != str:
                raise _IllegalParameterError(f'Index {i} of {type_} ACL does not contain a string')
            ret.append(UserID(item))
    return ret
def test_init_with_expire1():
    sid = uuid.UUID('1234567890abcdef1234567890abcdef')

    dl = DataLink(uuid.UUID('1234567890abcdef1234567890abcdee'),
                  DataUnitID(UPA('2/6/4'), 'whee'),
                  SampleNodeAddress(SampleAddress(sid, 7), 'bar'), dt(400),
                  UserID('u'), dt(800), UserID('gotdam'))

    assert dl.id == uuid.UUID('1234567890abcdef1234567890abcdee')
    assert dl.duid == DataUnitID(UPA('2/6/4'), 'whee')
    assert dl.sample_node_address == SampleNodeAddress(SampleAddress(sid, 7),
                                                       'bar')
    assert dl.created == dt(400)
    assert dl.created_by == UserID('u')
    assert dl.expired == dt(800)
    assert dl.expired_by == UserID('gotdam')
    assert str(dl) == (
        'id=12345678-90ab-cdef-1234-567890abcdee ' + 'duid=[2/6/4:whee] ' +
        'sample_node_address=[12345678-90ab-cdef-1234-567890abcdef:7:bar] ' +
        'created=400.0 created_by=u expired=800.0 expired_by=gotdam')
def test_get_admin_request_from_object():
    assert get_admin_request_from_object({'user': '******'}, 'as_ad', 'user') == (False, None)
    assert get_admin_request_from_object(
        {'as_ad': False, 'user': '******'}, 'as_ad', 'user') == (False, None)
    assert get_admin_request_from_object(
        {'as_ad': [], 'user': '******'}, 'as_ad', 'user') == (False, None)
    assert get_admin_request_from_object({'as_ad': True}, 'as_ad', 'user') == (True, None)
    assert get_admin_request_from_object(
        {'as_ad': True, 'user': None}, 'as_ad', 'user') == (True, None)
    assert get_admin_request_from_object(
        {'as_ad': 3, 'user': '******'}, 'as_ad', 'user') == (True, UserID('a'))
def test_acls_from_dict():
    assert acls_from_dict({'acls': {}}) == SampleACLOwnerless()
    assert acls_from_dict({'acls': {
        'read': [],
        'admin': ['whee', 'whoo']}}) == SampleACLOwnerless([UserID('whee'), UserID('whoo')])
    assert acls_from_dict({'acls': {
        'read': ['a', 'b'],
        'write': ['x'],
        'admin': ['whee', 'whoo']}}) == SampleACLOwnerless(
            [UserID('whee'), UserID('whoo')], [UserID('x')], [UserID('a'), UserID('b')])
def test_check_admin():
    f = AdminPermission.FULL
    r = AdminPermission.READ
    _check_admin(f, f, 'user1', 'somemethod', None,
                 f'User user1 is running method somemethod with administration permission FULL')
    _check_admin(f, f, 'user1', 'somemethod', UserID('otheruser'),
                 f'User user1 is running method somemethod with administration permission FULL ' +
                 'as user otheruser')
    _check_admin(f, r, 'someuser', 'a_method', None,
                 f'User someuser is running method a_method with administration permission FULL')
    _check_admin(r, r, 'user2', 'm', None,
                 f'User user2 is running method m with administration permission READ')
def _get_user_workspaces_fail_ws_exception(ws_exception, expected):
    wsc = create_autospec(Workspace, spec_set=True, instance=True)

    ws = WS(wsc)

    wsc.administer.assert_called_once_with({'command': 'listModRequests'})

    wsc.administer.side_effect = ws_exception

    with raises(Exception) as got:
        ws.get_user_workspaces(UserID('foo'))
    assert_exception_correct(got.value, expected)
def test_has_permission_fail_on_get_info_server_error():
    wsc = create_autospec(Workspace, spec_set=True, instance=True)

    ws = WS(wsc)
    wsc.administer.assert_called_once_with({'command': 'listModRequests'})

    wsc.administer.side_effect = [
        {'perms': [{'a': 'w', 'b': 'r', 'c': 'a'}]},
        ServerError('JSONRPCError', -32500, 'Thanks Obama')]

    with raises(Exception) as got:
        ws.has_permission(UserID('b'), WorkspaceAccessType.READ, upa=UPA('67/8/90'))
    assert_exception_correct(got.value, ServerError('JSONRPCError', -32500, 'Thanks Obama'))
def test_has_permission_fail_no_object():
    wsc = create_autospec(Workspace, spec_set=True, instance=True)

    ws = WS(wsc)
    wsc.administer.assert_called_once_with({'command': 'listModRequests'})

    wsc.administer.side_effect = [
        {'perms': [{'a': 'w', 'b': 'r', 'c': 'a'}]},
        {'infos': [None]}]

    with raises(Exception) as got:
        ws.has_permission(UserID('b'), WorkspaceAccessType.READ, upa=UPA('67/8/90'))
    assert_exception_correct(got.value, NoSuchWorkspaceDataError('Object 67/8/90 does not exist'))
def test_sample_hash():
    # 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__

    sn = SampleNode('foo')
    sn2 = SampleNode('bar')
    id1 = uuid.UUID('1234567890abcdef1234567890abcdef')
    id2 = uuid.UUID('1234567890abcdef1234567890abcdea')
    dt1 = dt(5)
    dt2 = dt(8)
    u = UserID('u')
    u2 = UserID('u2')

    assert hash(Sample([sn], 'yay')) == hash(Sample([sn], 'yay'))
    assert hash(Sample([sn], 'foo')) == hash(Sample([sn], 'foo'))
    assert hash(Sample([sn], 'yay')) != hash(Sample([sn2], 'yay'))
    assert hash(Sample([sn], 'yay')) != hash(Sample([sn], 'yo'))

    assert hash(SavedSample(id1, u, [sn], dt1, 'yay')) == hash(SavedSample(
                                                                 id1, u, [sn], dt(5), 'yay'))
    assert hash(SavedSample(id2, u, [sn], dt1, 'foo')) == hash(SavedSample(
                                                                 id2, u, [sn], dt1, 'foo'))
    assert hash(SavedSample(id1, u, [sn], dt1, 'foo')) != hash(SavedSample(
                                                                 id2, u, [sn], dt1, 'foo'))
    assert hash(SavedSample(id1, u, [sn], dt1, 'foo')) != hash(SavedSample(
                                                                 id1, u2, [sn], dt1, 'foo'))
    assert hash(SavedSample(id2, u, [sn], dt1, 'foo')) != hash(SavedSample(
                                                                 id2, u, [sn2], dt1, 'foo'))
    assert hash(SavedSample(id2, u, [sn], dt1, 'foo')) != hash(SavedSample(
                                                                 id2, u, [sn], dt2, 'foo'))
    assert hash(SavedSample(id2, u, [sn], dt1, 'foo')) != hash(SavedSample(
                                                                 id2, u, [sn], dt1, 'bar'))
    assert hash(SavedSample(id1, u, [sn], dt1, 'foo', 6)) == hash(SavedSample(
                                                                    id1, u, [sn], dt1, 'foo', 6))
    assert hash(SavedSample(id1, u, [sn], dt1, 'foo', 6)) != hash(SavedSample(
                                                                    id1, u, [sn], dt1, 'foo', 7))
def test_init_no_expire():
    sid = uuid.UUID('1234567890abcdef1234567890abcdef')

    dl = DataLink(
        uuid.UUID('1234567890abcdef1234567890abcdee'),
        DataUnitID(UPA('2/3/4')),
        SampleNodeAddress(SampleAddress(sid, 5), 'foo'),
        dt(500),
        UserID('usera'),
        expired_by=UserID('u')  # should be ignored
    )

    assert dl.id == uuid.UUID('1234567890abcdef1234567890abcdee')
    assert dl.duid == DataUnitID(UPA('2/3/4'))
    assert dl.sample_node_address == SampleNodeAddress(SampleAddress(sid, 5),
                                                       'foo')
    assert dl.created == dt(500)
    assert dl.created_by == UserID('usera')
    assert dl.expired is None
    assert dl.expired_by is None
    assert str(dl) == (
        'id=12345678-90ab-cdef-1234-567890abcdee ' + 'duid=[2/3/4] ' +
        'sample_node_address=[12345678-90ab-cdef-1234-567890abcdef:5:foo] ' +
        'created=500.0 created_by=usera expired=None expired_by=None')
def _get_user_workspaces(workspaces, pub, expected):
    wsc = create_autospec(Workspace, spec_set=True, instance=True)

    ws = WS(wsc)

    wsc.administer.assert_called_once_with({'command': 'listModRequests'})

    wsc.administer.return_value = {'workspaces': workspaces, 'pub': pub}

    assert ws.get_user_workspaces(UserID('usera')) == expected

    wsc.administer.assert_called_with({'command': 'listWorkspaceIDs',
                                       'user': '******',
                                       'params': {'perm': 'r', 'excludeGlobal': 0}})

    assert wsc.administer.call_count == 2