aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/ut/roles/parser_ut.cpp
blob: 87f8ade267d9ec757f30d1dfb3a68a9d04688756 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <library/cpp/tvmauth/client/misc/roles/parser.h>

#include <library/cpp/tvmauth/unittest.h>

#include <library/cpp/json/json_reader.h>
#include <library/cpp/testing/unittest/registar.h>

using namespace NTvmAuth;
using namespace NTvmAuth::NRoles;

Y_UNIT_TEST_SUITE(Parser) {
    static NJson::TJsonValue ToJsonValue(TStringBuf body) {
        NJson::TJsonValue doc;
        UNIT_ASSERT(NJson::ReadJsonTree(body, &doc));
        return doc;
    }

    Y_UNIT_TEST(GetEntity) {
        UNIT_ASSERT_EXCEPTION_CONTAINS(
            TParser::GetEntity(ToJsonValue(R"({"scope": false})"),
                               "cons",
                               "read"),
            yexception,
            "entity is map (str->str), got value Boolean. consumer 'cons' with role 'read'");

        TEntityPtr en;
        UNIT_ASSERT_NO_EXCEPTION(
            en = TParser::GetEntity(ToJsonValue(R"({})"),
                                    "cons",
                                    "read"));
        UNIT_ASSERT_VALUES_EQUAL(en->size(), 0);

        UNIT_ASSERT_NO_EXCEPTION(
            en = TParser::GetEntity(ToJsonValue(R"({"key1": "val1", "key2": "val2"})"),
                                    "cons",
                                    "read"));
        UNIT_ASSERT_VALUES_EQUAL(en->size(), 2);
    }

    Y_UNIT_TEST(GetEntities) {
        UNIT_ASSERT_EXCEPTION_CONTAINS(
            TParser::GetEntities(ToJsonValue(R"([{},[]])"),
                                 "cons",
                                 "read"),
            yexception,
            "role entity for role must be map: consumer 'cons' with role 'read' has Array");

        TEntitiesPtr en;
        UNIT_ASSERT_NO_EXCEPTION(
            en = TParser::GetEntities(ToJsonValue(R"([])"),
                                      "cons",
                                      "read"));
        UNIT_ASSERT(!en->Contains({}));

        UNIT_ASSERT_NO_EXCEPTION(
            en = TParser::GetEntities(ToJsonValue(R"([{}])"),
                                      "cons",
                                      "read"));
        UNIT_ASSERT(en->Contains({}));
    }

    Y_UNIT_TEST(GetConsumer) {
        UNIT_ASSERT_EXCEPTION_CONTAINS(
            TParser::GetConsumer(ToJsonValue(R"({"role1": [],"role2": {}})"),
                                 "cons"),
            yexception,
            "entities for roles must be array: 'role2' is Map");

        TConsumerRolesPtr c;
        UNIT_ASSERT_NO_EXCEPTION(
            c = TParser::GetConsumer(ToJsonValue(R"({"role1": [],"role2": []})"),
                                     "cons"));
        UNIT_ASSERT(c->HasRole("role1"));
        UNIT_ASSERT(c->HasRole("role2"));
        UNIT_ASSERT(!c->HasRole("role3"));
    }

    Y_UNIT_TEST(GetConsumers) {
        TRoles::TTvmConsumers cons;
        UNIT_ASSERT_NO_EXCEPTION(
            cons = TParser::GetConsumers<TTvmId>(ToJsonValue(R"({})"),
                                                 "tvm"));
        UNIT_ASSERT_VALUES_EQUAL(0, cons.size());

        UNIT_ASSERT_NO_EXCEPTION(
            cons = TParser::GetConsumers<TTvmId>(ToJsonValue(R"({"tvm": {}})"),
                                                 "tvm"));
        UNIT_ASSERT_VALUES_EQUAL(0, cons.size());

        UNIT_ASSERT_EXCEPTION_CONTAINS(
            TParser::GetConsumers<TTvmId>(ToJsonValue(R"({"tvm": []})"),
                                          "tvm"),
            yexception,
            "'tvm' must be object");

        UNIT_ASSERT_EXCEPTION_CONTAINS(
            TParser::GetConsumers<TTvmId>(ToJsonValue(R"({"tvm": {"asd": []}})"),
                                          "tvm"),
            yexception,
            "roles for consumer must be map: 'asd' is Array");

        UNIT_ASSERT_EXCEPTION_CONTAINS(
            TParser::GetConsumers<TTvmId>(ToJsonValue(R"({"tvm": {"asd": {}}})"),
                                          "tvm"),
            yexception,
            "id must be valid positive number of proper size for tvm. got 'asd'");
        UNIT_ASSERT_EXCEPTION_CONTAINS(
            TParser::GetConsumers<TTvmId>(ToJsonValue(R"({"tvm": {"1120000000001062": {}}})"),
                                          "tvm"),
            yexception,
            "id must be valid positive number of proper size for tvm. got '1120000000001062'");
        UNIT_ASSERT_NO_EXCEPTION(
            TParser::GetConsumers<TUid>(ToJsonValue(R"({"user": {"1120000000001062": {}}})"),
                                        "user"));

        UNIT_ASSERT_EXCEPTION_CONTAINS(
            TParser::GetConsumers<TTvmId>(ToJsonValue(R"({"tvm": {"42": {}, "042": {}}})"),
                                          "tvm"),
            yexception,
            "consumer duplicate detected: '42' for tvm");

        UNIT_ASSERT_NO_EXCEPTION(
            cons = TParser::GetConsumers<TTvmId>(ToJsonValue(R"({"tvm": {"42": {}}})"),
                                                 "tvm"));
        UNIT_ASSERT_VALUES_EQUAL(1, cons.size());
    }

    Y_UNIT_TEST(GetMeta) {
        UNIT_ASSERT_EXCEPTION_CONTAINS(
            TParser::GetMeta(ToJsonValue(R"({})")),
            yexception,
            "Missing 'revision'");

        UNIT_ASSERT_EXCEPTION_CONTAINS(
            TParser::GetMeta(ToJsonValue(R"({"revision": null})")),
            yexception,
            "'revision' has unexpected type: Null");

        UNIT_ASSERT_EXCEPTION_CONTAINS(
            TParser::GetMeta(ToJsonValue(R"({"revision": 100500})")),
            yexception,
            "Missing 'born_date'");

        UNIT_ASSERT_EXCEPTION_CONTAINS(
            TParser::GetMeta(ToJsonValue(R"({"revision": 100500, "born_date": false})")),
            yexception,
            "key 'born_date' must be uint");

        TRoles::TMeta meta;
        UNIT_ASSERT_NO_EXCEPTION(
            meta = TParser::GetMeta(ToJsonValue(R"({"revision": 100500, "born_date": 42})")));
        UNIT_ASSERT_VALUES_EQUAL("100500", meta.Revision);
        UNIT_ASSERT_VALUES_EQUAL(TInstant::Seconds(42), meta.BornTime);

        UNIT_ASSERT_NO_EXCEPTION(
            meta = TParser::GetMeta(ToJsonValue(R"({"revision": "100501", "born_date": 42})")));
        UNIT_ASSERT_VALUES_EQUAL("100501", meta.Revision);
        UNIT_ASSERT_VALUES_EQUAL(TInstant::Seconds(42), meta.BornTime);
    }
}