Пример #1
0
def test_cache_mixin_get_error(mocker):
    with mocker.patch.object(Team._cache_client, 'get',
                             mocker.MagicMock(side_effect=ConnectionError)):
        assert Team.get(0) is None

    with mocker.patch.object(Team._cache_client, 'get',
                             mocker.MagicMock(side_effect=TypeError)):
        assert Team.get(0) is None
Пример #2
0
def test_get_cache_miss(mocker):
    t = Team(id=0, team_name="hello")

    mocker.patch.object(_RedisWrapper, "get", mocker.Mock(return_value=None))
    mocker.patch.object(Team, "_db_session", MockSession(t))

    incr = mocker.MagicMock()
    mocker.patch.object(Team, "_statsd_incr", incr)
    Team.get(0)
    incr.assert_called_with("miss")
Пример #3
0
def test_get_cache_hit(mocker):
    mocker.patch.object(CacheMixin, "_db_session", DBSession)

    t = Team(id=0, team_name="hello")
    raw = t.__rawdata__

    mocker.patch.object(_RedisWrapper, "get", mocker.Mock(return_value=raw))

    # get
    incr = mocker.MagicMock()
    mocker.patch.object(Team, "_statsd_incr", incr)
    Team.get(0)
    incr.assert_called_with("hit")
Пример #4
0
def test_get_from_cache(mocker):
    mocker.patch.object(CacheMixin, "_db_session", DBSession)

    t = Team(id=0, team_name="test_get_from_cache")
    with mocker.patch.object(_RedisWrapper, "get", return_value=t.__rawdata__):
        m = Team.get(0)
        assert m._cached
Пример #5
0
def test_get_from_db(mocker):
    t = Team(id=0, team_name="hello")

    mocker.patch.object(_RedisWrapper, "get", mocker.Mock(return_value=None))
    mocker.patch.object(CacheMixin, "_db_session", MockSession(t))

    r = Team.get(0)
    assert r is t
Пример #6
0
def test_delete_team(client, db, admin_token, test_team, test_team_admin):
    assert db.query(Team.id).all() == [(test_team.id, )]
    assert [x.id for x in test_team.list_admin()] == [test_team_admin.id]

    r = client.delete('/api/team/%s' % test_team.team_name,
                      headers={'Authorization': admin_token})
    assert_response_ok(r)
    assert [x.id for x in test_team.list_admin()] == \
        [test_team_admin.id]
    assert Team.get(test_team.id).is_active is False
Пример #7
0
def test_new_cache_get(mocker):
    t = Team(id=0, team_name="hello")

    mocker.patch.object(_RedisWrapper, "get", mocker.Mock(return_value=None))
    mocker.patch.object(CacheMixin, "_db_session", MockSession(t))

    mock_set_raw = mocker.MagicMock()
    mocker.patch.object(Team, "set_raw", mock_set_raw)
    v = Team.get(0)
    mock_set_raw.assert_called_with(v.__rawdata__, nx=True)
    assert v is t
Пример #8
0
def test_get_from_session(mocker):
    session = DBSession

    mocker.patch.object(CacheMixin, "_db_session", DBSession)

    team = Team(id=0, team_name="test_get_from_session")
    session.add(team)
    session.commit()
    t = Team.get(0)
    assert t is team
    session.close()
Пример #9
0
def test_delete_team(team_foo, team_bar, user_foo):
    Application.create('biu', team_bar.id)
    TeamAdmin.ensure(team_foo.id, user_foo.id)

    # fill cache
    assert Team.get_by_name(team_foo.team_name) is team_foo
    assert Team.get_by_name(team_bar.team_name) is team_bar
    assert TeamAdmin.get_user_ids(team_foo.id) == [user_foo.id]

    Team.delete(team_foo.id)
    with raises(TeamNotEmptyError):
        Team.delete(team_bar.id)

    assert Team.get_by_name(team_foo.team_name) is None
    assert Team.get_by_name(team_bar.team_name) is not None
    assert Team.get(team_foo.id) is None
    assert Team.get(team_bar.id) is not None
    assert TeamAdmin.get_user_ids(team_foo.id) == []
    Team.delete(team_foo.id)
    assert Team.get(team_foo.id) is None
Пример #10
0
    def can_view_sensitive_data(cls, user_id, target_type, target_id):
        user = User.get(user_id)
        if user and user.is_admin:
            return True

        if target_type == cls.TYPE_TEAM:
            team = Team.get(target_id)
            return team and team.check_is_admin(user_id)

        if target_type in (cls.TYPE_APPLICATION, cls.TYPE_SERVICE,
                           cls.TYPE_CONFIG, cls.TYPE_SWITCH):
            application = Application.get(target_id)
            return application and application.check_auth(
                Authority.READ, user_id)
        return False