aboutsummaryrefslogtreecommitdiffstats
path: root/library/recipes/tvmtool/examples/ut_with_tvmapi_and_tirole/test.py
blob: 7700a45160255b9b09cfacb8f4f4881fead4e6bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
import os.path

import tvmauth


TVMAPI_PORT_FILE = "tvmapi.port"
TVMTOOL_PORT_FILE = "tvmtool.port"
TVMTOOL_AUTHTOKEN_FILE = "tvmtool.authtoken"


def _get_tvmapi_port():
    with open(TVMAPI_PORT_FILE) as f:
        return int(f.read())


def _get_tvmtool_params():
    tvmtool_port = int(open(TVMTOOL_PORT_FILE).read())
    authtoken = open(TVMTOOL_AUTHTOKEN_FILE).read()
    return tvmtool_port, authtoken


def test_tvmapi():
    assert os.path.isfile(TVMAPI_PORT_FILE)
    assert os.path.isfile(TVMTOOL_PORT_FILE)
    assert os.path.isfile(TVMTOOL_AUTHTOKEN_FILE)

    ca = tvmauth.TvmClient(
        tvmauth.TvmApiClientSettings(
            self_tvm_id=1000501,
            self_secret='bAicxJVa5uVY7MjDlapthw',
            disk_cache_dir="./",
            dsts={'my backend': 1000502},
            localhost_port=_get_tvmapi_port(),
        )
    )
    assert ca.status == tvmauth.TvmClientStatus.Ok

    tvmtool_port, authtoken = _get_tvmtool_params()
    ct = tvmauth.TvmClient(tvmauth.TvmToolClientSettings("me", auth_token=authtoken, port=tvmtool_port))
    assert ct.status == tvmauth.TvmClientStatus.Ok

    st = ct.check_service_ticket(ca.get_service_ticket_for('my backend'))
    assert st.src == 1000501

    expected_role = '/role/service/auth_type/without_user/access_type/read/handlers/routes/'
    assert expected_role in ct.get_roles().get_service_roles(st)

    ct.stop()
    ca.stop()