• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / 2123

12 Mar 2024 11:18PM UTC coverage: 91.833% (+0.05%) from 91.787%
2123

push

Evergreen

web-flow
RCORE-1928 Use baasaas for baas integration tests in CI (#7423)

94732 of 174812 branches covered (54.19%)

201 of 288 new or added lines in 4 files covered. (69.79%)

62 existing lines in 11 files now uncovered.

242847 of 264443 relevant lines covered (91.83%)

5989046.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.66
/test/object-store/util/sync/baas_admin_api.cpp
1
////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2021 Realm Inc.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
//
17
////////////////////////////////////////////////////////////////////////////
18

19
#include <util/sync/baas_admin_api.hpp>
20

21
#include <realm/object-store/sync/app_credentials.hpp>
22

23
#include <external/mpark/variant.hpp>
24

25
#if REALM_ENABLE_AUTH_TESTS
26

27
#include <realm/exceptions.hpp>
28
#include <realm/object_id.hpp>
29

30
#include <realm/util/scope_exit.hpp>
31

32
#include <catch2/catch_all.hpp>
33
#include <curl/curl.h>
34

35
#include <iostream>
36
#include <mutex>
37

38
namespace realm {
39
namespace {
40
std::string property_type_to_bson_type_str(PropertyType type)
41
{
5,089✔
42
    switch (type & ~PropertyType::Flags) {
5,089✔
43
        case PropertyType::UUID:
28✔
44
            return "uuid";
28✔
45
        case PropertyType::Mixed:
8✔
46
            return "mixed";
8✔
47
        case PropertyType::Bool:
✔
48
            return "bool";
×
49
        case PropertyType::Data:
✔
50
            return "binData";
×
51
        case PropertyType::Date:
12✔
52
            return "date";
12✔
53
        case PropertyType::Decimal:
2✔
54
            return "decimal";
2✔
55
        case PropertyType::Double:
8✔
56
            return "double";
8✔
57
        case PropertyType::Float:
✔
58
            return "float";
×
59
        case PropertyType::Int:
1,042✔
60
            return "long";
1,042✔
61
        case PropertyType::Object:
✔
62
            return "object";
×
63
        case PropertyType::ObjectId:
1,842✔
64
            return "objectId";
1,842✔
65
        case PropertyType::String:
2,147✔
66
            return "string";
2,147✔
67
        case PropertyType::LinkingObjects:
✔
68
            return "linkingObjects";
×
69
        default:
✔
70
            REALM_COMPILER_HINT_UNREACHABLE();
×
71
    }
5,089✔
72
}
5,089✔
73

74
class BaasRuleBuilder {
75
public:
76
    using IncludePropCond = util::UniqueFunction<bool(const Property&)>;
77
    BaasRuleBuilder(const Schema& schema, const Property& partition_key, const std::string& service_name,
78
                    const std::string& db_name, bool is_flx_sync)
79
        : m_schema(schema)
80
        , m_partition_key(partition_key)
81
        , m_mongo_service_name(service_name)
82
        , m_mongo_db_name(db_name)
83
        , m_is_flx_sync(is_flx_sync)
84
    {
389✔
85
    }
389✔
86

87
    nlohmann::json property_to_jsonschema(const Property& prop);
88
    nlohmann::json object_schema_to_jsonschema(const ObjectSchema& obj_schema, const IncludePropCond& include_prop,
89
                                               bool clear_path = false);
90
    nlohmann::json object_schema_to_baas_schema(const ObjectSchema& obj_schema, IncludePropCond include_prop);
91

92
private:
93
    const Schema& m_schema;
94
    const Property& m_partition_key;
95
    const std::string& m_mongo_service_name;
96
    const std::string& m_mongo_db_name;
97
    const bool m_is_flx_sync;
98
    nlohmann::json m_relationships;
99
    std::vector<std::string> m_current_path;
100
};
101

102
nlohmann::json BaasRuleBuilder::object_schema_to_jsonschema(const ObjectSchema& obj_schema,
103
                                                            const IncludePropCond& include_prop, bool clear_path)
104
{
1,718✔
105
    nlohmann::json required = nlohmann::json::array();
1,718✔
106
    nlohmann::json properties = nlohmann::json::object();
1,718✔
107
    for (const auto& prop : obj_schema.persisted_properties) {
5,984✔
108
        if (include_prop && !include_prop(prop)) {
5,984✔
109
            continue;
1,195✔
110
        }
1,195✔
111
        if (clear_path) {
4,789✔
112
            m_current_path.clear();
4,753✔
113
        }
4,753✔
114
        properties.emplace(prop.name, property_to_jsonschema(prop));
4,789✔
115
        if (!is_nullable(prop.type) && !is_collection(prop.type)) {
4,789✔
116
            required.push_back(prop.name);
2,144✔
117
        }
2,144✔
118
    }
4,789✔
119

830✔
120
    return {
1,718✔
121
        {"properties", properties},
1,718✔
122
        {"required", required},
1,718✔
123
        {"title", obj_schema.name},
1,718✔
124
    };
1,718✔
125
}
1,718✔
126

127
nlohmann::json BaasRuleBuilder::property_to_jsonschema(const Property& prop)
128
{
4,933✔
129
    nlohmann::json type_output;
4,933✔
130

2,361✔
131
    if ((prop.type & ~PropertyType::Flags) == PropertyType::Object) {
4,933✔
132
        auto target_obj = m_schema.find(prop.object_type);
394✔
133
        REALM_ASSERT(target_obj != m_schema.end());
394✔
134

197✔
135
        if (target_obj->table_type == ObjectSchema::ObjectType::Embedded) {
394✔
136
            m_current_path.push_back(prop.name);
30✔
137
            if (is_collection(prop.type)) {
30✔
138
                m_current_path.push_back("[]");
10✔
139
            }
10✔
140

15✔
141
            // embedded objects are normally not allowed to be queryable,
15✔
142
            // except if it is a GeoJSON type, and in that case the server
15✔
143
            // needs to know if it conforms to the expected schema shape.
15✔
144
            IncludePropCond always = [](const Property&) -> bool {
36✔
145
                return true;
36✔
146
            };
36✔
147
            type_output = object_schema_to_jsonschema(*target_obj, always);
30✔
148
            type_output.emplace("bsonType", "object");
30✔
149
        }
30✔
150
        else {
364✔
151
            REALM_ASSERT(target_obj->primary_key_property());
364✔
152
            std::string rel_name;
364✔
153
            for (const auto& path_elem : m_current_path) {
182✔
UNCOV
154
                rel_name.append(path_elem);
×
UNCOV
155
                rel_name.append(".");
×
UNCOV
156
            }
×
157
            rel_name.append(prop.name);
364✔
158
            m_relationships[rel_name] = {
364✔
159
                {"ref",
364✔
160
                 util::format("#/relationship/%1/%2/%3", m_mongo_service_name, m_mongo_db_name, target_obj->name)},
364✔
161
                {"foreign_key", target_obj->primary_key_property()->name},
364✔
162
                {"is_list", is_collection(prop.type)},
364✔
163
            };
364✔
164
            type_output.emplace("bsonType", property_type_to_bson_type_str(target_obj->primary_key_property()->type));
364✔
165
        }
364✔
166
    }
394✔
167
    else {
4,539✔
168
        type_output = {{"bsonType", property_type_to_bson_type_str(prop.type)}};
4,539✔
169
    }
4,539✔
170

2,361✔
171
    if (is_array(prop.type)) {
4,933✔
172
        return nlohmann::json{{"bsonType", "array"}, {"items", type_output}};
272✔
173
    }
272✔
174
    if (is_set(prop.type)) {
4,661✔
175
        return nlohmann::json{{"bsonType", "array"}, {"uniqueItems", true}, {"items", type_output}};
4✔
176
    }
4✔
177
    if (is_dictionary(prop.type)) {
4,657✔
178
        return nlohmann::json{
10✔
179
            {"bsonType", "object"}, {"properties", nlohmann::json::object()}, {"additionalProperties", type_output}};
10✔
180
    }
10✔
181

2,218✔
182
    // At this point we should have handled all the collection types and it's safe to return the prop_obj,
2,218✔
183
    REALM_ASSERT(!is_collection(prop.type));
4,647✔
184
    return type_output;
4,647✔
185
}
4,647✔
186

187
nlohmann::json BaasRuleBuilder::object_schema_to_baas_schema(const ObjectSchema& obj_schema,
188
                                                             IncludePropCond include_prop)
189
{
1,688✔
190
    m_relationships.clear();
1,688✔
191

815✔
192
    auto schema_json = object_schema_to_jsonschema(obj_schema, include_prop, true);
1,688✔
193
    auto& prop_sub_obj = schema_json["properties"];
1,688✔
194
    if (!prop_sub_obj.contains(m_partition_key.name) && !m_is_flx_sync) {
1,688✔
195
        prop_sub_obj.emplace(m_partition_key.name, property_to_jsonschema(m_partition_key));
144✔
196
        if (!is_nullable(m_partition_key.type)) {
144✔
197
            schema_json["required"].push_back(m_partition_key.name);
×
198
        }
×
199
    }
144✔
200
    return {
1,688✔
201
        {"schema", schema_json},
1,688✔
202
        {"metadata", nlohmann::json::object({{"database", m_mongo_db_name},
1,688✔
203
                                             {"collection", obj_schema.name},
1,688✔
204
                                             {"data_source", m_mongo_service_name}})},
1,688✔
205
        {"relationships", m_relationships},
1,688✔
206
    };
1,688✔
207
}
1,688✔
208

209
class CurlGlobalGuard {
210
public:
211
    CurlGlobalGuard()
212
    {
15,577✔
213
        std::lock_guard<std::mutex> lk(m_mutex);
15,577✔
214
        if (++m_users == 1) {
15,577✔
215
            curl_global_init(CURL_GLOBAL_ALL);
15,577✔
216
        }
15,577✔
217
    }
15,577✔
218

219
    ~CurlGlobalGuard()
220
    {
15,577✔
221
        std::lock_guard<std::mutex> lk(m_mutex);
15,577✔
222
        if (--m_users == 0) {
15,577✔
223
            curl_global_cleanup();
15,577✔
224
        }
15,577✔
225
    }
15,577✔
226

227
    CurlGlobalGuard(const CurlGlobalGuard&) = delete;
228
    CurlGlobalGuard(CurlGlobalGuard&&) = delete;
229
    CurlGlobalGuard& operator=(const CurlGlobalGuard&) = delete;
230
    CurlGlobalGuard& operator=(CurlGlobalGuard&&) = delete;
231

232
private:
233
    static std::mutex m_mutex;
234
    static int m_users;
235
};
236

237
std::mutex CurlGlobalGuard::m_mutex = {};
238
int CurlGlobalGuard::m_users = 0;
239

240
size_t curl_write_cb(char* ptr, size_t size, size_t nmemb, std::string* response)
241
{
12,785✔
242
    REALM_ASSERT(response);
12,785✔
243
    size_t realsize = size * nmemb;
12,785✔
244
    response->append(ptr, realsize);
12,785✔
245
    return realsize;
12,785✔
246
}
12,785✔
247

248
size_t curl_header_cb(char* buffer, size_t size, size_t nitems, std::map<std::string, std::string>* response_headers)
249
{
142,266✔
250
    REALM_ASSERT(response_headers);
142,266✔
251
    std::string_view combined(buffer, size * nitems);
142,266✔
252
    if (auto pos = combined.find(':'); pos != std::string::npos) {
142,266✔
253
        std::string_view key = combined.substr(0, pos);
110,944✔
254
        std::string_view value = combined.substr(pos + 1);
110,944✔
255
        if (auto first_not_space = value.find_first_not_of(' '); first_not_space != std::string::npos) {
110,944✔
256
            value = value.substr(first_not_space);
110,944✔
257
        }
110,944✔
258
        if (auto last_not_nl = value.find_last_not_of("\r\n"); last_not_nl != std::string::npos) {
110,944✔
259
            value = value.substr(0, last_not_nl + 1);
110,944✔
260
        }
110,944✔
261
        response_headers->insert({std::string{key}, std::string{value}});
110,944✔
262
    }
110,944✔
263
    else {
31,322✔
264
        if (combined.size() > 5 && combined.substr(0, 5) != "HTTP/") { // ignore for now HTTP/1.1 ...
31,322✔
265
            std::cerr << "test transport skipping header: " << combined << std::endl;
×
266
        }
×
267
    }
31,322✔
268
    return nitems * size;
142,266✔
269
}
142,266✔
270

271
std::string_view getenv_sv(const char* name) noexcept
272
{
1,470✔
273
    if (auto ptr = ::getenv(name); ptr != nullptr) {
1,470✔
274
        return std::string_view(ptr);
6✔
275
    }
6✔
276

702✔
277
    return {};
1,464✔
278
}
1,464✔
279

280
} // namespace
281

282
app::Response do_http_request(const app::Request& request)
283
{
15,577✔
284
    CurlGlobalGuard curl_global_guard;
15,577✔
285
    auto curl = curl_easy_init();
15,577✔
286
    if (!curl) {
15,577✔
287
        return app::Response{500, -1};
×
288
    }
×
289

7,422✔
290
    struct curl_slist* list = nullptr;
15,577✔
291
    auto curl_cleanup = util::ScopeExit([&]() noexcept {
15,577✔
292
        curl_easy_cleanup(curl);
15,577✔
293
        curl_slist_free_all(list);
15,577✔
294
    });
15,577✔
295

7,422✔
296
    std::string response;
15,577✔
297
    app::HttpHeaders response_headers;
15,577✔
298

7,422✔
299
    /* First set the URL that is about to receive our POST. This URL can
7,422✔
300
     just as well be a https:// URL if that is what should receive the
7,422✔
301
     data. */
7,422✔
302
    curl_easy_setopt(curl, CURLOPT_URL, request.url.c_str());
15,577✔
303

7,422✔
304
    /* Now specify the POST data */
7,422✔
305
    if (request.method == app::HttpMethod::post) {
15,577✔
306
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
7,639✔
307
    }
7,639✔
308
    else if (request.method == app::HttpMethod::put) {
7,938✔
309
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
1,829✔
310
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
1,829✔
311
    }
1,829✔
312
    else if (request.method == app::HttpMethod::patch) {
6,109✔
313
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
720✔
314
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
720✔
315
    }
720✔
316
    else if (request.method == app::HttpMethod::del) {
5,389✔
317
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
417✔
318
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
417✔
319
    }
417✔
320
    else if (request.method == app::HttpMethod::patch) {
4,972✔
321
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
×
322
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
×
323
    }
×
324

7,422✔
325
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, request.timeout_ms);
15,577✔
326

7,422✔
327
    for (auto header : request.headers) {
42,669✔
328
        auto header_str = util::format("%1: %2", header.first, header.second);
42,669✔
329
        list = curl_slist_append(list, header_str.c_str());
42,669✔
330
    }
42,669✔
331

7,422✔
332
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
15,577✔
333
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_cb);
15,577✔
334
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
15,577✔
335
    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, curl_header_cb);
15,577✔
336
    curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response_headers);
15,577✔
337

7,422✔
338
#ifdef REALM_CURL_CACERTS
339
    auto ca_info = unquote_string(REALM_QUOTE(REALM_CURL_CACERTS));
340
    curl_easy_setopt(curl, CURLOPT_CAINFO, ca_info.c_str());
341
#endif
342

7,422✔
343
    auto start_time = std::chrono::steady_clock::now();
15,577✔
344
    auto response_code = curl_easy_perform(curl);
15,577✔
345
    auto total_time = std::chrono::steady_clock::now() - start_time;
15,577✔
346

7,422✔
347
    auto logger = util::Logger::get_default_logger();
15,577✔
348
    if (response_code != CURLE_OK) {
15,577✔
349
        logger->error("curl_easy_perform() failed when sending request to '%1' with body '%2': %3", request.url,
2✔
350
                      request.body, curl_easy_strerror(response_code));
2✔
351
    }
2✔
352
    if (logger->would_log(util::Logger::Level::trace)) {
15,577✔
NEW
353
        std::string coid = [&] {
×
NEW
354
            auto coid_header = response_headers.find("X-Appservices-Request-Id");
×
NEW
355
            if (coid_header == response_headers.end()) {
×
NEW
356
                return std::string{};
×
NEW
357
            }
×
NEW
358
            return util::format("BaaS Coid: \"%1\"", coid_header->second);
×
NEW
359
        }();
×
360

NEW
361
        logger->trace("Baas API %1 request to %2 took %3 %4\n", app::httpmethod_to_string(request.method),
×
NEW
362
                      request.url, std::chrono::duration_cast<std::chrono::milliseconds>(total_time), coid);
×
NEW
363
    }
×
364

7,422✔
365
    int http_code = 0;
15,577✔
366
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
15,577✔
367
    return {
15,577✔
368
        http_code,
15,577✔
369
        0, // binding_response_code
15,577✔
370
        std::move(response_headers),
15,577✔
371
        std::move(response),
15,577✔
372
    };
15,577✔
373
}
15,577✔
374

375
class Baasaas {
376
public:
377
    enum class StartMode { Default, GitHash, Branch, PatchId };
378
    explicit Baasaas(std::string api_key, StartMode mode, std::string ref_spec)
379
        : m_api_key(std::move(api_key))
380
        , m_base_url(get_baasaas_base_url())
381
        , m_externally_managed_instance(false)
382
    {
2✔
383
        auto logger = util::Logger::get_default_logger();
2✔
384
        std::string url_path = "startContainer";
2✔
385
        if (mode == StartMode::GitHash) {
2✔
386
            url_path = util::format("startContainer?githash=%1", ref_spec);
2✔
387
            logger->info("Starting baasaas container with githash of %1", ref_spec);
2✔
388
        }
2✔
NEW
389
        else if (mode == StartMode::Branch) {
×
NEW
390
            url_path = util::format("startContainer?branch=%1", ref_spec);
×
NEW
391
            logger->info("Starting baasaas container on branch %1", ref_spec);
×
NEW
392
        }
×
NEW
393
        else if (mode == StartMode::PatchId) {
×
NEW
394
            url_path = util::format("startContainer?patchId=%1", ref_spec);
×
NEW
395
            logger->info("Starting baasaas container for patch id %1", ref_spec);
×
NEW
396
        }
×
NEW
397
        else {
×
NEW
398
            logger->info("Starting baasaas container");
×
NEW
399
        }
×
400

1✔
401
        auto resp = do_request(std::move(url_path), app::HttpMethod::post);
2✔
402
        m_container_id = resp["id"].get<std::string>();
2✔
403
        logger->info("Baasaas container started with id \"%1\"", m_container_id);
2✔
404
        auto lock_file = util::File(std::string{s_baasaas_lock_file_name}, util::File::mode_Write);
2✔
405
        lock_file.write(m_container_id);
2✔
406
    }
2✔
407

408
    explicit Baasaas(std::string api_key, std::string baasaas_instance_id)
409
        : m_api_key(std::move(api_key))
410
        , m_base_url(get_baasaas_base_url())
411
        , m_container_id(std::move(baasaas_instance_id))
412
        , m_externally_managed_instance(true)
NEW
413
    {
×
NEW
414
        auto logger = util::Logger::get_default_logger();
×
NEW
415
        logger->info("Using externally managed baasaas instance \"%1\"", m_container_id);
×
NEW
416
    }
×
417

418
    Baasaas(const Baasaas&) = delete;
419
    Baasaas(Baasaas&&) = delete;
420
    Baasaas& operator=(const Baasaas&) = delete;
421
    Baasaas& operator=(Baasaas&&) = delete;
422

423
    ~Baasaas()
424
    {
2✔
425
        stop();
2✔
426
    }
2✔
427

428
    void poll()
429
    {
1,101✔
430
        if (!m_http_endpoint.empty() || m_container_id.empty()) {
1,101✔
431
            return;
1,099✔
432
        }
1,099✔
433

1✔
434
        auto logger = util::Logger::get_default_logger();
2✔
435
        auto poll_start_at = std::chrono::system_clock::now();
2✔
436
        std::string http_endpoint;
2✔
437
        std::string mongo_endpoint;
2✔
438
        bool logged = false;
2✔
439
        while (std::chrono::system_clock::now() - poll_start_at < std::chrono::minutes(2) &&
8✔
440
               m_http_endpoint.empty()) {
8✔
441
            if (http_endpoint.empty()) {
8✔
442
                auto status_obj =
4✔
443
                    do_request(util::format("containerStatus?id=%1", m_container_id), app::HttpMethod::get);
4✔
444
                if (!status_obj["httpUrl"].is_null()) {
4✔
445
                    http_endpoint = status_obj["httpUrl"].get<std::string>();
2✔
446
                    mongo_endpoint = status_obj["mongoUrl"].get<std::string>();
2✔
447
                }
2✔
448
            }
4✔
449
            else {
4✔
450
                app::Request baas_req;
4✔
451
                baas_req.url = util::format("%1/api/private/v1.0/version", http_endpoint);
4✔
452
                baas_req.method = app::HttpMethod::get;
4✔
453
                baas_req.headers.insert_or_assign("Content-Type", "application/json");
4✔
454
                auto baas_resp = do_http_request(baas_req);
4✔
455
                if (baas_resp.http_status_code >= 200 && baas_resp.http_status_code < 300) {
4✔
456
                    m_http_endpoint = http_endpoint;
2✔
457
                    m_mongo_endpoint = mongo_endpoint;
2✔
458
                    break;
2✔
459
                }
2✔
460
            }
6✔
461

3✔
462
            if (!logged) {
6✔
463
                logger->info("Waiting for baasaas container \"%1\" to be ready", m_container_id);
2✔
464
                logged = true;
2✔
465
            }
2✔
466
            std::this_thread::sleep_for(std::chrono::seconds(3));
6✔
467
        }
6✔
468

1✔
469
        if (m_http_endpoint.empty()) {
2✔
NEW
470
            throw std::runtime_error(
×
NEW
471
                util::format("Failed to launch baasaas container %1 within 2 minutes", m_container_id));
×
NEW
472
        }
×
473
    }
2✔
474

475
    void stop()
476
    {
4✔
477
        if (m_externally_managed_instance) {
4✔
NEW
478
            return;
×
NEW
479
        }
×
480
        auto container_id = std::move(m_container_id);
4✔
481
        if (container_id.empty()) {
4✔
482
            return;
2✔
483
        }
2✔
484

1✔
485
        auto logger = util::Logger::get_default_logger();
2✔
486
        logger->info("Stopping baasaas container with id \"%1\"", container_id);
2✔
487
        do_request(util::format("stopContainer?id=%1", container_id), app::HttpMethod::post);
2✔
488
        auto lock_file = util::File(std::string{s_baasaas_lock_file_name}, util::File::mode_Write);
2✔
489
        lock_file.resize(0);
2✔
490
        lock_file.close();
2✔
491
        util::File::remove(lock_file.get_path());
2✔
492
    }
2✔
493

494
    const std::string& http_endpoint()
495
    {
746✔
496
        poll();
746✔
497
        return m_http_endpoint;
746✔
498
    }
746✔
499

500
    const std::string& mongo_endpoint()
501
    {
355✔
502
        poll();
355✔
503
        return m_mongo_endpoint;
355✔
504
    }
355✔
505

506
private:
507
    nlohmann::json do_request(std::string api_path, app::HttpMethod method)
508
    {
8✔
509
        app::Request request;
8✔
510

4✔
511
        request.url = util::format("%1/%2", m_base_url, api_path);
8✔
512
        request.method = method;
8✔
513
        request.headers.insert_or_assign("apiKey", m_api_key);
8✔
514
        request.headers.insert_or_assign("Content-Type", "application/json");
8✔
515
        auto response = do_http_request(request);
8✔
516
        REALM_ASSERT_EX(response.http_status_code >= 200 && response.http_status_code < 300,
8✔
517
                        util::format("Baasaas api response code: %1 Response body: %2", response.http_status_code,
8✔
518
                                     response.body));
8✔
519
        return nlohmann::json::parse(response.body);
8✔
520
    }
8✔
521

522
    static std::string get_baasaas_base_url()
523
    {
2✔
524
        auto env_value = getenv_sv("BAASAAS_BASE_URL");
2✔
525
        if (env_value.empty()) {
2✔
526
            // This is the current default endpoint for baasaas maintained by the sync team.
1✔
527
            // You can reach out for help in #appx-device-sync-internal if there are problems.
1✔
528
            return "https://us-east-1.aws.data.mongodb-api.com/app/baas-container-service-autzb/endpoint";
2✔
529
        }
2✔
530

NEW
531
        return unquote_string(env_value);
×
NEW
532
    }
×
533

534
    constexpr static std::string_view s_baasaas_lock_file_name = "baasaas_instance.lock";
535

536
    std::string m_api_key;
537
    std::string m_base_url;
538
    std::string m_container_id;
539
    bool m_externally_managed_instance;
540
    std::string m_http_endpoint;
541
    std::string m_mongo_endpoint;
542
};
543

544
class BaasaasLauncher : public Catch::EventListenerBase {
545
public:
546
    static std::optional<Baasaas>& get_baasaas_holder()
547
    {
1,107✔
548
        static std::optional<Baasaas> global_baasaas = std::nullopt;
1,107✔
549
        return global_baasaas;
1,107✔
550
    }
1,107✔
551

552
    using Catch::EventListenerBase::EventListenerBase;
553

554
    void testRunStarting(Catch::TestRunInfo const&) override
555
    {
4✔
556
        std::string_view api_key(getenv_sv("BAASAAS_API_KEY"));
4✔
557
        if (api_key.empty()) {
4✔
558
            return;
2✔
559
        }
2✔
560

1✔
561
        // Allow overriding the baas base url at runtime via an environment variable, even if BAASAAS_API_KEY
1✔
562
        // is also specified.
1✔
563
        if (!getenv_sv("BAAS_BASE_URL").empty()) {
2✔
NEW
564
            return;
×
NEW
565
        }
×
566

1✔
567
        // If we've started a baasaas container outside of running the tests, then use that instead of
1✔
568
        // figuring out how to start our own.
1✔
569
        if (auto baasaas_instance = getenv_sv("BAASAAS_INSTANCE_ID"); !baasaas_instance.empty()) {
2✔
NEW
570
            auto& baasaas_holder = get_baasaas_holder();
×
NEW
571
            REALM_ASSERT(!baasaas_holder);
×
NEW
572
            baasaas_holder.emplace(std::string{api_key}, std::string{baasaas_instance});
×
NEW
573
            return;
×
NEW
574
        }
×
575

1✔
576
        std::string_view ref_spec(getenv_sv("BAASAAS_REF_SPEC"));
2✔
577
        std::string_view mode_spec(getenv_sv("BAASAAS_START_MODE"));
2✔
578
        Baasaas::StartMode mode = Baasaas::StartMode::Default;
2✔
579
        if (mode_spec == "branch") {
2✔
NEW
580
            if (ref_spec.empty()) {
×
NEW
581
                throw std::runtime_error("Expected branch name in BAASAAS_REF_SPEC env variable, but it was empty");
×
NEW
582
            }
×
NEW
583
            mode = Baasaas::StartMode::Branch;
×
NEW
584
        }
×
585
        else if (mode_spec == "githash") {
2✔
586
            if (ref_spec.empty()) {
2✔
NEW
587
                throw std::runtime_error("Expected git hash in BAASAAS_REF_SPEC env variable, but it was empty");
×
NEW
588
            }
×
589
            mode = Baasaas::StartMode::GitHash;
2✔
590
        }
2✔
NEW
591
        else if (mode_spec == "patchid") {
×
NEW
592
            if (ref_spec.empty()) {
×
NEW
593
                throw std::runtime_error("Expected patch id in BAASAAS_REF_SPEC env variable, but it was empty");
×
NEW
594
            }
×
NEW
595
            mode = Baasaas::StartMode::PatchId;
×
NEW
596
        }
×
NEW
597
        else {
×
NEW
598
            if (!mode_spec.empty()) {
×
NEW
599
                throw std::runtime_error("Excepted BAASAAS_START_MODE to be \"githash\", \"patchid\", or \"branch\"");
×
NEW
600
            }
×
NEW
601
            ref_spec = {};
×
NEW
602
        }
×
603

1✔
604
        auto& baasaas_holder = get_baasaas_holder();
2✔
605
        REALM_ASSERT(!baasaas_holder);
2✔
606
        baasaas_holder.emplace(std::string{api_key}, mode, std::string{ref_spec});
2✔
607

1✔
608
        get_runtime_app_session();
2✔
609
    }
2✔
610

611
    void testRunEnded(Catch::TestRunStats const&) override
612
    {
4✔
613
        if (auto& baasaas_holder = get_baasaas_holder(); baasaas_holder.has_value()) {
4✔
614
            baasaas_holder->stop();
2✔
615
        }
2✔
616
    }
4✔
617
};
618

619
CATCH_REGISTER_LISTENER(BaasaasLauncher)
620

621
AdminAPIEndpoint AdminAPIEndpoint::operator[](StringData name) const
622
{
16,873✔
623
    return AdminAPIEndpoint(util::format("%1/%2", m_url, name), m_access_token);
16,873✔
624
}
16,873✔
625

626
app::Response AdminAPIEndpoint::do_request(app::Request request) const
627
{
10,842✔
628
    if (request.url.find('?') == std::string::npos) {
10,842✔
629
        request.url = util::format("%1?bypass_service_change=SyncSchemaVersionIncrease", request.url);
10,842✔
630
    }
10,842✔
631
    else {
×
632
        request.url = util::format("%1&bypass_service_change=SyncSchemaVersionIncrease", request.url);
×
633
    }
×
634
    request.headers["Content-Type"] = "application/json;charset=utf-8";
10,842✔
635
    request.headers["Accept"] = "application/json";
10,842✔
636
    request.headers["Authorization"] = util::format("Bearer %1", m_access_token);
10,842✔
637
    return do_http_request(std::move(request));
10,842✔
638
}
10,842✔
639

640
app::Response AdminAPIEndpoint::get(const std::vector<std::pair<std::string, std::string>>& params) const
641
{
3,357✔
642
    app::Request req;
3,357✔
643
    req.method = app::HttpMethod::get;
3,357✔
644
    std::stringstream ss;
3,357✔
645
    bool needs_and = false;
3,357✔
646
    ss << m_url;
3,357✔
647
    if (!params.empty() && m_url.find('?') != std::string::npos) {
3,357!
648
        needs_and = true;
×
649
    }
×
650
    for (const auto& param : params) {
1,657✔
651
        if (needs_and) {
×
652
            ss << "&";
×
653
        }
×
654
        else {
×
655
            ss << "?";
×
656
        }
×
657
        needs_and = true;
×
658
        ss << param.first << "=" << param.second;
×
659
    }
×
660
    req.url = ss.str();
3,357✔
661
    return do_request(std::move(req));
3,357✔
662
}
3,357✔
663

664
app::Response AdminAPIEndpoint::del() const
665
{
381✔
666
    app::Request req;
381✔
667
    req.method = app::HttpMethod::del;
381✔
668
    req.url = m_url;
381✔
669
    return do_request(std::move(req));
381✔
670
}
381✔
671

672
nlohmann::json AdminAPIEndpoint::get_json(const std::vector<std::pair<std::string, std::string>>& params) const
673
{
3,357✔
674
    auto resp = get(params);
3,357✔
675
    REALM_ASSERT_EX(resp.http_status_code >= 200 && resp.http_status_code < 300,
3,357✔
676
                    util::format("url: %1, reply: %2", m_url, resp.body));
3,357✔
677
    return nlohmann::json::parse(resp.body.empty() ? "{}" : resp.body);
3,357✔
678
}
3,357✔
679

680
app::Response AdminAPIEndpoint::post(std::string body) const
681
{
4,579✔
682
    app::Request req;
4,579✔
683
    req.method = app::HttpMethod::post;
4,579✔
684
    req.url = m_url;
4,579✔
685
    req.body = std::move(body);
4,579✔
686
    return do_request(std::move(req));
4,579✔
687
}
4,579✔
688

689
nlohmann::json AdminAPIEndpoint::post_json(nlohmann::json body) const
690
{
4,557✔
691
    auto resp = post(body.dump());
4,557✔
692
    REALM_ASSERT_EX(resp.http_status_code >= 200 && resp.http_status_code < 300, m_url, body.dump(), resp.body);
4,557✔
693
    return nlohmann::json::parse(resp.body.empty() ? "{}" : resp.body);
4,557✔
694
}
4,557✔
695

696
app::Response AdminAPIEndpoint::put(std::string body) const
697
{
1,805✔
698
    app::Request req;
1,805✔
699
    req.method = app::HttpMethod::put;
1,805✔
700
    req.url = m_url;
1,805✔
701
    req.body = std::move(body);
1,805✔
702
    return do_request(std::move(req));
1,805✔
703
}
1,805✔
704

705
nlohmann::json AdminAPIEndpoint::put_json(nlohmann::json body) const
706
{
1,440✔
707
    auto resp = put(body.dump());
1,440✔
708
    REALM_ASSERT_EX(resp.http_status_code >= 200 && resp.http_status_code < 300,
1,440✔
709
                    util::format("url: %1 request: %2, reply: %3", m_url, body.dump(), resp.body));
1,440✔
710
    return nlohmann::json::parse(resp.body.empty() ? "{}" : resp.body);
1,364✔
711
}
1,440✔
712

713
app::Response AdminAPIEndpoint::patch(std::string body) const
714
{
720✔
715
    app::Request req;
720✔
716
    req.method = app::HttpMethod::patch;
720✔
717
    req.url = m_url;
720✔
718
    req.body = std::move(body);
720✔
719
    return do_request(std::move(req));
720✔
720
}
720✔
721

722
nlohmann::json AdminAPIEndpoint::patch_json(nlohmann::json body) const
723
{
720✔
724
    auto resp = patch(body.dump());
720✔
725
    REALM_ASSERT_EX(resp.http_status_code >= 200 && resp.http_status_code < 300,
720✔
726
                    util::format("url: %1 request: %2, reply: %3", m_url, body.dump(), resp.body));
720✔
727
    return nlohmann::json::parse(resp.body.empty() ? "{}" : resp.body);
720✔
728
}
720✔
729

730
AdminAPISession AdminAPISession::login(const AppCreateConfig& config)
731
{
357✔
732
    std::string admin_url = config.admin_url;
357✔
733
    nlohmann::json login_req_body{
357✔
734
        {"provider", "userpass"},
357✔
735
        {"username", config.admin_username},
357✔
736
        {"password", config.admin_password},
357✔
737
    };
357✔
738
    if (config.logger) {
357✔
739
        config.logger->trace("Logging into baas admin api: %1", admin_url);
357✔
740
    }
357✔
741
    app::Request auth_req{
357✔
742
        app::HttpMethod::post,
357✔
743
        util::format("%1/api/admin/v3.0/auth/providers/local-userpass/login", admin_url),
357✔
744
        60000, // 1 minute timeout
357✔
745
        {
357✔
746
            {"Content-Type", "application/json;charset=utf-8"},
357✔
747
            {"Accept", "application/json"},
357✔
748
        },
357✔
749
        login_req_body.dump(),
357✔
750
    };
357✔
751
    auto login_resp = do_http_request(std::move(auth_req));
357✔
752
    REALM_ASSERT_EX(login_resp.http_status_code == 200, login_resp.http_status_code, login_resp.body);
357✔
753
    auto login_resp_body = nlohmann::json::parse(login_resp.body);
357✔
754

170✔
755
    std::string access_token = login_resp_body["access_token"];
357✔
756

170✔
757
    AdminAPIEndpoint user_profile(util::format("%1/api/admin/v3.0/auth/profile", admin_url), access_token);
357✔
758
    auto profile_resp = user_profile.get_json();
357✔
759

170✔
760
    std::string group_id = profile_resp["roles"][0]["group_id"];
357✔
761

170✔
762
    return AdminAPISession(std::move(admin_url), std::move(access_token), std::move(group_id));
357✔
763
}
357✔
764

765
void AdminAPISession::revoke_user_sessions(const std::string& user_id, const std::string& app_id) const
766
{
4✔
767
    auto endpoint = apps()[app_id]["users"][user_id]["logout"];
4✔
768
    auto response = endpoint.put("");
4✔
769
    REALM_ASSERT_EX(response.http_status_code == 204, response.http_status_code, response.body);
4✔
770
}
4✔
771

772
void AdminAPISession::disable_user_sessions(const std::string& user_id, const std::string& app_id) const
773
{
2✔
774
    auto endpoint = apps()[app_id]["users"][user_id]["disable"];
2✔
775
    auto response = endpoint.put("");
2✔
776
    REALM_ASSERT_EX(response.http_status_code == 204, response.http_status_code, response.body);
2✔
777
}
2✔
778

779
void AdminAPISession::enable_user_sessions(const std::string& user_id, const std::string& app_id) const
780
{
2✔
781
    auto endpoint = apps()[app_id]["users"][user_id]["enable"];
2✔
782
    auto response = endpoint.put("");
2✔
783
    REALM_ASSERT_EX(response.http_status_code == 204, response.http_status_code, response.body);
2✔
784
}
2✔
785

786
// returns false for an invalid/expired access token
787
bool AdminAPISession::verify_access_token(const std::string& access_token, const std::string& app_id) const
788
{
22✔
789
    auto endpoint = apps()[app_id]["users"]["verify_token"];
22✔
790
    nlohmann::json request_body{
22✔
791
        {"token", access_token},
22✔
792
    };
22✔
793
    auto response = endpoint.post(request_body.dump());
22✔
794
    if (response.http_status_code == 200) {
22✔
795
        auto resp_json = nlohmann::json::parse(response.body.empty() ? "{}" : response.body);
16✔
796
        try {
16✔
797
            // if these fields are found, then the token is valid according to the server.
8✔
798
            // if it is invalid or expired then an error response is sent.
8✔
799
            int64_t issued_at = resp_json["iat"];
16✔
800
            int64_t expires_at = resp_json["exp"];
16✔
801
            return issued_at != 0 && expires_at != 0;
16✔
802
        }
16✔
803
        catch (...) {
×
804
            return false;
×
805
        }
×
806
    }
6✔
807
    return false;
6✔
808
}
6✔
809

810
void AdminAPISession::set_development_mode_to(const std::string& app_id, bool enable) const
811
{
24✔
812
    auto endpoint = apps()[app_id]["sync"]["config"];
24✔
813
    endpoint.put_json({{"development_mode_enabled", enable}});
24✔
814
}
24✔
815

816
void AdminAPISession::delete_app(const std::string& app_id) const
817
{
355✔
818
    auto app_endpoint = apps()[app_id];
355✔
819
    auto resp = app_endpoint.del();
355✔
820
    REALM_ASSERT_EX(resp.http_status_code == 204, resp.http_status_code, resp.body);
355✔
821
}
355✔
822

823
std::vector<AdminAPISession::Service> AdminAPISession::get_services(const std::string& app_id) const
824
{
68✔
825
    auto endpoint = apps()[app_id]["services"];
68✔
826
    auto response = endpoint.get_json();
68✔
827
    std::vector<AdminAPISession::Service> services;
68✔
828
    for (auto service : response) {
136✔
829
        services.push_back(
136✔
830
            {service["_id"], service["name"], service["type"], service["version"], service["last_modified"]});
136✔
831
    }
136✔
832
    return services;
68✔
833
}
68✔
834

835

836
std::vector<std::string> AdminAPISession::get_errors(const std::string& app_id) const
837
{
×
838
    auto endpoint = apps()[app_id]["logs"];
×
839
    auto response = endpoint.get_json({{"errors_only", "true"}});
×
840
    std::vector<std::string> errors;
×
841
    const auto& logs = response["logs"];
×
842
    std::transform(logs.begin(), logs.end(), std::back_inserter(errors), [](const auto& err) {
×
843
        return err["error"];
×
844
    });
×
845
    return errors;
×
846
}
×
847

848

849
AdminAPISession::Service AdminAPISession::get_sync_service(const std::string& app_id) const
850
{
68✔
851
    auto services = get_services(app_id);
68✔
852
    auto sync_service = std::find_if(services.begin(), services.end(), [&](auto s) {
68✔
853
        return s.type == "mongodb";
68✔
854
    });
68✔
855
    REALM_ASSERT(sync_service != services.end());
68✔
856
    return *sync_service;
68✔
857
}
68✔
858

859
void AdminAPISession::trigger_client_reset(const std::string& app_id, int64_t file_ident) const
860
{
152✔
861
    auto endpoint = apps(APIFamily::Admin)[app_id]["sync"]["force_reset"];
152✔
862
    endpoint.put_json(nlohmann::json{{"file_ident", file_ident}});
152✔
863
}
152✔
864

865
void AdminAPISession::migrate_to_flx(const std::string& app_id, const std::string& service_id,
866
                                     bool migrate_to_flx) const
867
{
30✔
868
    auto endpoint = apps()[app_id]["sync"]["migration"];
30✔
869
    endpoint.put_json(nlohmann::json{{"serviceId", service_id}, {"action", migrate_to_flx ? "start" : "rollback"}});
24✔
870
}
30✔
871

872
// Each breaking change bumps the schema version, so you can create a new version for each breaking change if
873
// 'use_draft' is false. Set 'use_draft' to true if you want all changes to the schema to be deployed at once
874
// resulting in only one schema version.
875
void AdminAPISession::create_schema(const std::string& app_id, const AppCreateConfig& config, bool use_draft) const
876
{
389✔
877
    static const std::string mongo_service_name = "BackingDB";
389✔
878

186✔
879
    auto drafts = apps()[app_id]["drafts"];
389✔
880
    std::string draft_id;
389✔
881
    if (use_draft) {
389✔
882
        auto draft_create_resp = drafts.post_json({});
32✔
883
        draft_id = draft_create_resp["_id"];
32✔
884
    }
32✔
885

186✔
886
    auto schemas = apps()[app_id]["schemas"];
389✔
887
    auto current_schema = schemas.get_json();
389✔
888
    auto target_schema = config.schema;
389✔
889

186✔
890
    std::unordered_map<std::string, std::string> current_schema_tables;
389✔
891
    for (const auto& schema : current_schema) {
232✔
892
        current_schema_tables[schema["metadata"]["collection"]] = schema["_id"];
92✔
893
    }
92✔
894

186✔
895
    // Add new tables
186✔
896

186✔
897
    auto pk_and_queryable_only = [&](const Property& prop) {
2,884✔
898
        if (config.flx_sync_config) {
2,884✔
899
            const auto& queryable_fields = config.flx_sync_config->queryable_fields;
1,120✔
900

554✔
901
            if (std::find(queryable_fields.begin(), queryable_fields.end(), prop.name) != queryable_fields.end()) {
1,120✔
902
                return true;
468✔
903
            }
468✔
904
        }
2,416✔
905
        return prop.name == "_id" || prop.name == config.partition_key.name;
2,416✔
906
    };
2,416✔
907

186✔
908
    // Create the schemas in two passes: first populate just the primary key and
186✔
909
    // partition key, then add the rest of the properties. This ensures that the
186✔
910
    // targets of links exist before adding the links.
186✔
911
    std::vector<std::pair<std::string, const ObjectSchema*>> object_schema_to_create;
389✔
912
    BaasRuleBuilder rule_builder(target_schema, config.partition_key, mongo_service_name, config.mongo_dbname,
389✔
913
                                 static_cast<bool>(config.flx_sync_config));
389✔
914
    for (const auto& obj_schema : target_schema) {
877✔
915
        auto it = current_schema_tables.find(obj_schema.name);
877✔
916
        if (it != current_schema_tables.end()) {
877✔
917
            object_schema_to_create.push_back({it->second, &obj_schema});
66✔
918
            continue;
66✔
919
        }
66✔
920

391✔
921
        auto schema_to_create = rule_builder.object_schema_to_baas_schema(obj_schema, pk_and_queryable_only);
811✔
922
        auto schema_create_resp = schemas.post_json(schema_to_create);
811✔
923
        object_schema_to_create.push_back({schema_create_resp["_id"], &obj_schema});
811✔
924
    }
811✔
925

186✔
926
    // Update existing tables (including the ones just created)
186✔
927
    for (const auto& [id, obj_schema] : object_schema_to_create) {
877✔
928
        auto schema_to_create = rule_builder.object_schema_to_baas_schema(*obj_schema, nullptr);
877✔
929
        schema_to_create["_id"] = id;
877✔
930
        schemas[id].put_json(schema_to_create);
877✔
931
    }
877✔
932

186✔
933
    // Delete removed tables
186✔
934
    for (const auto& table : current_schema_tables) {
232✔
935
        if (target_schema.find(table.first) == target_schema.end()) {
92✔
936
            schemas[table.second].del();
26✔
937
        }
26✔
938
    }
92✔
939

186✔
940
    if (use_draft) {
389✔
941
        drafts[draft_id]["deployment"].post_json({});
32✔
942
    }
32✔
943
}
389✔
944

945
static nlohmann::json convert_config(AdminAPISession::ServiceConfig config)
946
{
363✔
947
    if (config.mode == AdminAPISession::ServiceConfig::SyncMode::Flexible) {
363✔
948
        auto payload = nlohmann::json{{"database_name", config.database_name},
173✔
949
                                      {"state", config.state},
173✔
950
                                      {"is_recovery_mode_disabled", config.recovery_is_disabled}};
173✔
951
        if (config.queryable_field_names) {
173✔
952
            payload["queryable_fields_names"] = *config.queryable_field_names;
173✔
953
        }
173✔
954
        if (config.permissions) {
173✔
955
            payload["permissions"] = *config.permissions;
2✔
956
        }
2✔
957
        if (config.asymmetric_tables) {
173✔
958
            payload["asymmetric_tables"] = *config.asymmetric_tables;
171✔
959
        }
171✔
960
        return payload;
173✔
961
    }
173✔
962
    return nlohmann::json{{"database_name", config.database_name},
190✔
963
                          {"partition", *config.partition},
190✔
964
                          {"state", config.state},
190✔
965
                          {"is_recovery_mode_disabled", config.recovery_is_disabled}};
190✔
966
}
190✔
967

968
AdminAPIEndpoint AdminAPISession::service_config_endpoint(const std::string& app_id,
969
                                                          const std::string& service_id) const
970
{
405✔
971
    return apps()[app_id]["services"][service_id]["config"];
405✔
972
}
405✔
973

974
AdminAPISession::ServiceConfig AdminAPISession::disable_sync(const std::string& app_id, const std::string& service_id,
975
                                                             AdminAPISession::ServiceConfig sync_config) const
976
{
×
977
    auto endpoint = service_config_endpoint(app_id, service_id);
×
978
    if (sync_config.state != "") {
×
979
        sync_config.state = "";
×
980
        endpoint.patch_json({{sync_config.sync_service_name(), convert_config(sync_config)}});
×
981
    }
×
982
    return sync_config;
×
983
}
×
984

985
AdminAPISession::ServiceConfig AdminAPISession::pause_sync(const std::string& app_id, const std::string& service_id,
986
                                                           AdminAPISession::ServiceConfig sync_config) const
987
{
×
988
    auto endpoint = service_config_endpoint(app_id, service_id);
×
989
    if (sync_config.state != "disabled") {
×
990
        sync_config.state = "disabled";
×
991
        endpoint.patch_json({{sync_config.sync_service_name(), convert_config(sync_config)}});
×
992
    }
×
993
    return sync_config;
×
994
}
×
995

996
AdminAPISession::ServiceConfig AdminAPISession::enable_sync(const std::string& app_id, const std::string& service_id,
997
                                                            AdminAPISession::ServiceConfig sync_config) const
998
{
359✔
999
    auto endpoint = service_config_endpoint(app_id, service_id);
359✔
1000
    sync_config.state = "enabled";
359✔
1001
    endpoint.patch_json({{sync_config.sync_service_name(), convert_config(sync_config)}});
359✔
1002
    return sync_config;
359✔
1003
}
359✔
1004

1005
AdminAPISession::ServiceConfig AdminAPISession::set_disable_recovery_to(const std::string& app_id,
1006
                                                                        const std::string& service_id,
1007
                                                                        ServiceConfig sync_config, bool disable) const
1008
{
4✔
1009
    auto endpoint = service_config_endpoint(app_id, service_id);
4✔
1010
    sync_config.recovery_is_disabled = disable;
4✔
1011
    endpoint.patch_json({{sync_config.sync_service_name(), convert_config(sync_config)}});
4✔
1012
    return sync_config;
4✔
1013
}
4✔
1014

1015
std::vector<AdminAPISession::SchemaVersionInfo> AdminAPISession::get_schema_versions(const std::string& app_id) const
1016
{
93✔
1017
    std::vector<AdminAPISession::SchemaVersionInfo> ret;
93✔
1018
    auto endpoint = apps()[app_id]["sync"]["schemas"]["versions"];
93✔
1019
    auto res = endpoint.get_json();
93✔
1020
    for (auto&& version : res["versions"].get<std::vector<nlohmann::json>>()) {
140✔
1021
        SchemaVersionInfo info;
140✔
1022
        info.version_major = version["version_major"];
140✔
1023
        ret.push_back(std::move(info));
140✔
1024
    }
140✔
1025

45✔
1026
    return ret;
93✔
1027
}
93✔
1028

1029
AdminAPISession::ServiceConfig AdminAPISession::get_config(const std::string& app_id,
1030
                                                           const AdminAPISession::Service& service) const
1031
{
42✔
1032
    auto endpoint = service_config_endpoint(app_id, service.id);
42✔
1033
    auto response = endpoint.get_json();
42✔
1034
    AdminAPISession::ServiceConfig config;
42✔
1035
    if (response.contains("flexible_sync")) {
42✔
1036
        auto sync = response["flexible_sync"];
16✔
1037
        config.mode = AdminAPISession::ServiceConfig::SyncMode::Flexible;
16✔
1038
        config.state = sync["state"];
16✔
1039
        config.database_name = sync["database_name"];
16✔
1040
        config.permissions = sync["permissions"];
16✔
1041
        config.queryable_field_names = sync["queryable_fields_names"];
16✔
1042
        auto recovery_disabled = sync["is_recovery_mode_disabled"];
16✔
1043
        config.recovery_is_disabled = recovery_disabled.is_boolean() ? recovery_disabled.get<bool>() : false;
16✔
1044
    }
16✔
1045
    else if (response.contains("sync")) {
26✔
1046
        auto sync = response["sync"];
26✔
1047
        config.mode = AdminAPISession::ServiceConfig::SyncMode::Partitioned;
26✔
1048
        config.state = sync["state"];
26✔
1049
        config.database_name = sync["database_name"];
26✔
1050
        config.partition = sync["partition"];
26✔
1051
        auto recovery_disabled = sync["is_recovery_mode_disabled"];
26✔
1052
        config.recovery_is_disabled = recovery_disabled.is_boolean() ? recovery_disabled.get<bool>() : false;
26✔
1053
    }
26✔
1054
    else {
×
1055
        throw std::runtime_error(util::format("Unsupported config format from server: %1", response));
×
1056
    }
×
1057
    return config;
42✔
1058
}
42✔
1059

1060
bool AdminAPISession::is_sync_enabled(const std::string& app_id) const
1061
{
30✔
1062
    auto sync_service = get_sync_service(app_id);
30✔
1063
    auto config = get_config(app_id, sync_service);
30✔
1064
    return config.state == "enabled";
30✔
1065
}
30✔
1066

1067
bool AdminAPISession::is_sync_terminated(const std::string& app_id) const
1068
{
×
1069
    auto sync_service = get_sync_service(app_id);
×
1070
    auto config = get_config(app_id, sync_service);
×
1071
    if (config.state == "enabled") {
×
1072
        return false;
×
1073
    }
×
1074
    auto state_endpoint = apps()[app_id]["sync"]["state"];
×
1075
    auto state_result = state_endpoint.get_json(
×
1076
        {{"sync_type", config.mode == ServiceConfig::SyncMode::Flexible ? "flexible" : "partition"}});
×
1077
    return state_result["state"].get<std::string>().empty();
×
1078
}
×
1079

1080
bool AdminAPISession::is_initial_sync_complete(const std::string& app_id) const
1081
{
1,272✔
1082
    auto progress_endpoint = apps()[app_id]["sync"]["progress"];
1,272✔
1083
    auto progress_result = progress_endpoint.get_json();
1,272✔
1084
    if (auto it = progress_result.find("progress"); it != progress_result.end() && it->is_object() && !it->empty()) {
1,272✔
1085
        for (auto& elem : *it) {
1,496✔
1086
            auto is_complete = elem["complete"];
1,496✔
1087
            if (!is_complete.is_boolean() || !is_complete.get<bool>()) {
1,496✔
1088
                return false;
364✔
1089
            }
364✔
1090
        }
1,496✔
1091
        return true;
623✔
1092
    }
463✔
1093
    return false;
463✔
1094
}
463✔
1095

1096
AdminAPISession::MigrationStatus AdminAPISession::get_migration_status(const std::string& app_id) const
1097
{
779✔
1098
    MigrationStatus status;
779✔
1099
    auto progress_endpoint = apps()[app_id]["sync"]["migration"];
779✔
1100
    auto progress_result = progress_endpoint.get_json();
779✔
1101
    auto errorMessage = progress_result["errorMessage"];
779✔
1102
    if (errorMessage.is_string() && !errorMessage.get<std::string>().empty()) {
779✔
1103
        throw Exception(Status{ErrorCodes::RuntimeError, errorMessage.get<std::string>()});
×
1104
    }
×
1105
    if (!progress_result["statusMessage"].is_string() || !progress_result["isMigrated"].is_boolean()) {
779✔
1106
        throw Exception(
×
1107
            Status{ErrorCodes::RuntimeError, util::format("Invalid result returned from migration status request: %1",
×
1108
                                                          progress_result.dump(4, 32, true))});
×
1109
    }
×
1110

406✔
1111
    status.statusMessage = progress_result["statusMessage"].get<std::string>();
779✔
1112
    status.isMigrated = progress_result["isMigrated"].get<bool>();
779✔
1113
    status.isCancelable = progress_result["isCancelable"].get<bool>();
779✔
1114
    status.isRevertible = progress_result["isRevertible"].get<bool>();
779✔
1115
    status.complete = status.statusMessage.empty();
779✔
1116
    return status;
779✔
1117
}
779✔
1118

1119
AdminAPIEndpoint AdminAPISession::apps(APIFamily family) const
1120
{
4,700✔
1121
    switch (family) {
4,700✔
1122
        case APIFamily::Admin:
4,700✔
1123
            return AdminAPIEndpoint(util::format("%1/api/admin/v3.0/groups/%2/apps", m_base_url, m_group_id),
4,700✔
1124
                                    m_access_token);
4,700✔
UNCOV
1125
        case APIFamily::Private:
✔
UNCOV
1126
            return AdminAPIEndpoint(util::format("%1/api/private/v1.0/groups/%2/apps", m_base_url, m_group_id),
×
UNCOV
1127
                                    m_access_token);
×
1128
    }
×
1129
    REALM_UNREACHABLE();
1130
}
×
1131

1132
realm::Schema get_default_schema()
1133
{
72✔
1134
    const auto dog_schema =
72✔
1135
        ObjectSchema("Dog", {realm::Property("_id", PropertyType::ObjectId | PropertyType::Nullable, true),
72✔
1136
                             realm::Property("breed", PropertyType::String | PropertyType::Nullable),
72✔
1137
                             realm::Property("name", PropertyType::String),
72✔
1138
                             realm::Property("realm_id", PropertyType::String | PropertyType::Nullable)});
72✔
1139
    const auto cat_schema =
72✔
1140
        ObjectSchema("Cat", {realm::Property("_id", PropertyType::String | PropertyType::Nullable, true),
72✔
1141
                             realm::Property("breed", PropertyType::String | PropertyType::Nullable),
72✔
1142
                             realm::Property("name", PropertyType::String),
72✔
1143
                             realm::Property("realm_id", PropertyType::String | PropertyType::Nullable)});
72✔
1144
    const auto person_schema =
72✔
1145
        ObjectSchema("Person", {realm::Property("_id", PropertyType::ObjectId | PropertyType::Nullable, true),
72✔
1146
                                realm::Property("age", PropertyType::Int),
72✔
1147
                                realm::Property("dogs", PropertyType::Object | PropertyType::Array, "Dog"),
72✔
1148
                                realm::Property("firstName", PropertyType::String),
72✔
1149
                                realm::Property("lastName", PropertyType::String),
72✔
1150
                                realm::Property("realm_id", PropertyType::String | PropertyType::Nullable)});
72✔
1151
    return realm::Schema({dog_schema, cat_schema, person_schema});
72✔
1152
}
72✔
1153

1154
std::string get_base_url()
1155
{
746✔
1156
    if (auto baas_url = getenv_sv("BAAS_BASE_URL"); !baas_url.empty()) {
746✔
NEW
1157
        return std::string{baas_url};
×
NEW
1158
    }
×
1159
    if (auto& baasaas_holder = BaasaasLauncher::get_baasaas_holder(); baasaas_holder.has_value()) {
746✔
1160
        return baasaas_holder->http_endpoint();
746✔
1161
    }
746✔
1162

NEW
1163
    return get_compile_time_base_url();
×
NEW
1164
}
×
1165

1166
std::string get_admin_url()
1167
{
355✔
1168
    if (auto baas_admin_url = getenv_sv("BAAS_ADMIN_URL"); !baas_admin_url.empty()) {
355✔
NEW
1169
        return std::string{baas_admin_url};
×
NEW
1170
    }
×
1171
    if (auto compile_url = get_compile_time_admin_url(); !compile_url.empty()) {
355✔
NEW
1172
        return compile_url;
×
NEW
1173
    }
×
1174

170✔
1175
    return get_base_url();
355✔
1176
}
355✔
1177

1178
std::string get_mongodb_server()
1179
{
355✔
1180
    if (auto baas_url = getenv_sv("BAAS_MONGO_URL"); !baas_url.empty()) {
355✔
NEW
1181
        return std::string{baas_url};
×
NEW
1182
    }
×
1183

170✔
1184
    if (auto& baasaas_holder = BaasaasLauncher::get_baasaas_holder(); baasaas_holder.has_value()) {
355✔
1185
        return baasaas_holder->mongo_endpoint();
355✔
1186
    }
355✔
NEW
1187
    return "mongodb://localhost:26000";
×
NEW
1188
}
×
1189

1190

1191
AppCreateConfig default_app_config()
1192
{
14✔
1193
    ObjectId id = ObjectId::gen();
14✔
1194
    std::string db_name = util::format("test_data_%1", id.to_string());
14✔
1195
    std::string app_url = get_base_url();
14✔
1196
    std::string admin_url = get_admin_url();
14✔
1197
    REALM_ASSERT(!app_url.empty());
14✔
1198
    REALM_ASSERT(!admin_url.empty());
14✔
1199

1✔
1200
    std::string update_user_data_func = util::format(R"(
14✔
1201
        exports = async function(data) {
14✔
1202
            const user = context.user;
14✔
1203
            const mongodb = context.services.get("BackingDB");
14✔
1204
            const userDataCollection = mongodb.db("%1").collection("UserData");
14✔
1205
            await userDataCollection.updateOne(
14✔
1206
                                               { "user_id": user.id },
14✔
1207
                                               { "$set": data },
14✔
1208
                                               { "upsert": true }
14✔
1209
                                               );
14✔
1210
            return true;
14✔
1211
        };
14✔
1212
    )",
14✔
1213
                                                     db_name);
14✔
1214

1✔
1215
    constexpr const char* sum_func = R"(
14✔
1216
        exports = function(...args) {
14✔
1217
            return args.reduce((a,b) => a + b, 0);
14✔
1218
        };
14✔
1219
    )";
14✔
1220

1✔
1221
    constexpr const char* confirm_func = R"(
14✔
1222
        exports = ({ token, tokenId, username }) => {
14✔
1223
            // process the confirm token, tokenId and username
14✔
1224
            if (username.includes("realm_tests_do_autoverify")) {
14✔
1225
              return { status: 'success' }
14✔
1226
            }
14✔
1227
            // do not confirm the user
14✔
1228
            return { status: 'fail' };
14✔
1229
        };
14✔
1230
    )";
14✔
1231

1✔
1232
    constexpr const char* auth_func = R"(
14✔
1233
        exports = (loginPayload) => {
14✔
1234
            return loginPayload["realmCustomAuthFuncUserId"];
14✔
1235
        };
14✔
1236
    )";
14✔
1237

1✔
1238
    constexpr const char* reset_func = R"(
14✔
1239
        exports = ({ token, tokenId, username, password }) => {
14✔
1240
            // process the reset token, tokenId, username and password
14✔
1241
            if (password.includes("realm_tests_do_reset")) {
14✔
1242
              return { status: 'success' };
14✔
1243
            }
14✔
1244
            // will not reset the password
14✔
1245
            return { status: 'fail' };
14✔
1246
        };
14✔
1247
    )";
14✔
1248

1✔
1249
    std::vector<AppCreateConfig::FunctionDef> funcs = {
14✔
1250
        {"updateUserData", update_user_data_func, false},
14✔
1251
        {"sumFunc", sum_func, false},
14✔
1252
        {"confirmFunc", confirm_func, false},
14✔
1253
        {"authFunc", auth_func, false},
14✔
1254
        {"resetFunc", reset_func, false},
14✔
1255
    };
14✔
1256

1✔
1257
    Property partition_key("realm_id", PropertyType::String | PropertyType::Nullable);
14✔
1258

1✔
1259
    AppCreateConfig::UserPassAuthConfig user_pass_config{
14✔
1260
        false,
14✔
1261
        "",
14✔
1262
        "confirmFunc",
14✔
1263
        "http://localhost/confirmEmail",
14✔
1264
        "resetFunc",
14✔
1265
        "",
14✔
1266
        "http://localhost/resetPassword",
14✔
1267
        true,
14✔
1268
        true,
14✔
1269
    };
14✔
1270

1✔
1271
    return AppCreateConfig{
14✔
1272
        "test",
14✔
1273
        std::move(app_url),
14✔
1274
        std::move(admin_url), // BAAS Admin API URL may be different
14✔
1275
        "unique_user@domain.com",
14✔
1276
        "password",
14✔
1277
        get_mongodb_server(),
14✔
1278
        db_name,
14✔
1279
        get_default_schema(),
14✔
1280
        std::move(partition_key),
14✔
1281
        false,                              // Dev mode disabled
14✔
1282
        util::none,                         // Default to no FLX sync config
14✔
1283
        std::move(funcs),                   // Add default functions
14✔
1284
        std::move(user_pass_config),        // enable basic user/pass auth
14✔
1285
        std::string{"authFunc"},            // custom auth function
14✔
1286
        true,                               // enable_api_key_auth
14✔
1287
        true,                               // enable_anonymous_auth
14✔
1288
        true,                               // enable_custom_token_auth
14✔
1289
        {},                                 // no service roles on the default rule
14✔
1290
        util::Logger::get_default_logger(), // provide the logger to the admin api
14✔
1291
    };
14✔
1292
}
14✔
1293

1294
AppCreateConfig minimal_app_config(const std::string& name, const Schema& schema)
1295
{
341✔
1296
    Property partition_key("realm_id", PropertyType::String | PropertyType::Nullable);
341✔
1297
    std::string app_url = get_base_url();
341✔
1298
    std::string admin_url = get_admin_url();
341✔
1299
    REALM_ASSERT(!app_url.empty());
341✔
1300
    REALM_ASSERT(!admin_url.empty());
341✔
1301

169✔
1302
    AppCreateConfig::UserPassAuthConfig user_pass_config{
341✔
1303
        true,  "Confirm", "", "http://example.com/confirmEmail", "", "Reset", "http://exmaple.com/resetPassword",
341✔
1304
        false, false,
341✔
1305
    };
341✔
1306

169✔
1307
    ObjectId id = ObjectId::gen();
341✔
1308
    return AppCreateConfig{
341✔
1309
        name,
341✔
1310
        std::move(app_url),
341✔
1311
        std::move(admin_url), // BAAS Admin API URL may be different
341✔
1312
        "unique_user@domain.com",
341✔
1313
        "password",
341✔
1314
        get_mongodb_server(),
341✔
1315
        util::format("test_data_%1_%2", name, id.to_string()),
341✔
1316
        schema,
341✔
1317
        std::move(partition_key),
341✔
1318
        false,                              // Dev mode disabled
341✔
1319
        util::none,                         // no FLX sync config
341✔
1320
        {},                                 // no functions
341✔
1321
        std::move(user_pass_config),        // enable basic user/pass auth
341✔
1322
        util::none,                         // disable custom auth
341✔
1323
        true,                               // enable api key auth
341✔
1324
        true,                               // enable anonymous auth
341✔
1325
        false,                              // enable_custom_token_auth
341✔
1326
        {},                                 // no service roles on the default rule
341✔
1327
        util::Logger::get_default_logger(), // provide the logger to the admin api
341✔
1328
    };
341✔
1329
}
341✔
1330

1331
AppSession create_app(const AppCreateConfig& config)
1332
{
357✔
1333
    auto session = AdminAPISession::login(config);
357✔
1334
    auto create_app_resp = session.apps().post_json(nlohmann::json{{"name", config.app_name}});
357✔
1335
    std::string app_id = create_app_resp["_id"];
357✔
1336
    std::string client_app_id = create_app_resp["client_app_id"];
357✔
1337

170✔
1338
    auto app = session.apps()[app_id];
357✔
1339

170✔
1340
    auto functions = app["functions"];
357✔
1341
    std::unordered_map<std::string, std::string> function_name_to_id;
357✔
1342
    for (const auto& func : config.functions) {
245✔
1343
        auto create_func_resp = functions.post_json({
80✔
1344
            {"name", func.name},
80✔
1345
            {"private", func.is_private},
80✔
1346
            {"can_evaluate", nlohmann::json::object()},
80✔
1347
            {"source", func.source},
80✔
1348
        });
80✔
1349
        function_name_to_id.insert({func.name, create_func_resp["_id"]});
80✔
1350
    }
80✔
1351

170✔
1352
    auto auth_providers = app["auth_providers"];
357✔
1353
    if (config.enable_anonymous_auth) {
357✔
1354
        auth_providers.post_json({{"type", "anon-user"}});
357✔
1355
    }
357✔
1356
    if (config.user_pass_auth) {
357✔
1357
        auto user_pass_config_obj = nlohmann::json{
357✔
1358
            {"autoConfirm", config.user_pass_auth->auto_confirm},
357✔
1359
            {"confirmEmailSubject", config.user_pass_auth->confirm_email_subject},
357✔
1360
            {"emailConfirmationUrl", config.user_pass_auth->email_confirmation_url},
357✔
1361
            {"resetPasswordSubject", config.user_pass_auth->reset_password_subject},
357✔
1362
            {"resetPasswordUrl", config.user_pass_auth->reset_password_url},
357✔
1363
        };
357✔
1364
        if (!config.user_pass_auth->confirmation_function_name.empty()) {
357✔
1365
            const auto& confirm_func_name = config.user_pass_auth->confirmation_function_name;
16✔
1366
            user_pass_config_obj.emplace("confirmationFunctionName", confirm_func_name);
16✔
1367
            user_pass_config_obj.emplace("confirmationFunctionId", function_name_to_id[confirm_func_name]);
16✔
1368
            user_pass_config_obj.emplace("runConfirmationFunction", config.user_pass_auth->run_confirmation_function);
16✔
1369
        }
16✔
1370
        if (!config.user_pass_auth->reset_function_name.empty()) {
357✔
1371
            const auto& reset_func_name = config.user_pass_auth->reset_function_name;
16✔
1372
            user_pass_config_obj.emplace("resetFunctionName", reset_func_name);
16✔
1373
            user_pass_config_obj.emplace("resetFunctionId", function_name_to_id[reset_func_name]);
16✔
1374
            user_pass_config_obj.emplace("runResetFunction", config.user_pass_auth->run_reset_function);
16✔
1375
        }
16✔
1376
        auth_providers.post_json({{"type", "local-userpass"}, {"config", std::move(user_pass_config_obj)}});
357✔
1377
    }
357✔
1378
    if (config.custom_function_auth) {
357✔
1379
        auth_providers.post_json({{"type", "custom-function"},
16✔
1380
                                  {"config",
16✔
1381
                                   {
16✔
1382
                                       {"authFunctionName", *config.custom_function_auth},
16✔
1383
                                       {"authFunctionId", function_name_to_id[*config.custom_function_auth]},
16✔
1384
                                   }}});
16✔
1385
    }
16✔
1386

170✔
1387
    if (config.enable_api_key_auth) {
357✔
1388
        auto all_auth_providers = auth_providers.get_json();
357✔
1389
        auto api_key_provider =
357✔
1390
            std::find_if(all_auth_providers.begin(), all_auth_providers.end(), [](const nlohmann::json& provider) {
357✔
1391
                return provider["type"] == "api-key";
357✔
1392
            });
357✔
1393
        REALM_ASSERT(api_key_provider != all_auth_providers.end());
357✔
1394
        std::string api_key_provider_id = (*api_key_provider)["_id"];
357✔
1395
        auto api_key_enable_resp = auth_providers[api_key_provider_id]["enable"].put("");
357✔
1396
        REALM_ASSERT(api_key_enable_resp.http_status_code >= 200 && api_key_enable_resp.http_status_code < 300);
357✔
1397
    }
357✔
1398

170✔
1399
    auto secrets = app["secrets"];
357✔
1400
    secrets.post_json({{"name", "BackingDB_uri"}, {"value", config.mongo_uri}});
357✔
1401
    secrets.post_json({{"name", "gcm"}, {"value", "gcm"}});
357✔
1402
    secrets.post_json({{"name", "customTokenKey"}, {"value", "My_very_confidential_secretttttt"}});
357✔
1403

170✔
1404
    if (config.enable_custom_token_auth) {
357✔
1405
        auth_providers.post_json(
16✔
1406
            {{"type", "custom-token"},
16✔
1407
             {"config",
16✔
1408
              {
16✔
1409
                  {"audience", nlohmann::json::array()},
16✔
1410
                  {"signingAlgorithm", "HS256"},
16✔
1411
                  {"useJWKURI", false},
16✔
1412
              }},
16✔
1413
             {"secret_config", {{"signingKeys", nlohmann::json::array({"customTokenKey"})}}},
16✔
1414
             {"disabled", false},
16✔
1415
             {"metadata_fields",
16✔
1416
              {{{"required", false}, {"name", "user_data.name"}, {"field_name", "name"}},
16✔
1417
               {{"required", true}, {"name", "user_data.occupation"}, {"field_name", "occupation"}},
16✔
1418
               {{"required", true}, {"name", "my_metadata.name"}, {"field_name", "anotherName"}}}}});
16✔
1419
    }
16✔
1420

170✔
1421
    auto services = app["services"];
357✔
1422
    static const std::string mongo_service_name = "BackingDB";
357✔
1423

170✔
1424
    nlohmann::json mongo_service_def = {
357✔
1425
        {"name", mongo_service_name},
357✔
1426
        {"type", "mongodb"},
357✔
1427
        {"config", {{"uri", config.mongo_uri}}},
357✔
1428
    };
357✔
1429
    nlohmann::json sync_config;
357✔
1430
    if (config.flx_sync_config) {
357✔
1431
        auto queryable_fields = nlohmann::json::array();
171✔
1432
        const auto& queryable_fields_src = config.flx_sync_config->queryable_fields;
171✔
1433
        std::copy(queryable_fields_src.begin(), queryable_fields_src.end(), std::back_inserter(queryable_fields));
171✔
1434
        auto asymmetric_tables = nlohmann::json::array();
171✔
1435
        for (const auto& obj_schema : config.schema) {
307✔
1436
            if (obj_schema.table_type == ObjectSchema::ObjectType::TopLevelAsymmetric) {
307✔
1437
                asymmetric_tables.emplace_back(obj_schema.name);
4✔
1438
            }
4✔
1439
        }
307✔
1440
        sync_config = nlohmann::json{{"database_name", config.mongo_dbname},
171✔
1441
                                     {"queryable_fields_names", queryable_fields},
171✔
1442
                                     {"asymmetric_tables", asymmetric_tables}};
171✔
1443
        mongo_service_def["config"]["flexible_sync"] = sync_config;
171✔
1444
    }
171✔
1445
    else {
186✔
1446
        sync_config = nlohmann::json{
186✔
1447
            {"database_name", config.mongo_dbname},
186✔
1448
            {"partition",
186✔
1449
             {
186✔
1450
                 {"key", config.partition_key.name},
186✔
1451
                 {"type", property_type_to_bson_type_str(config.partition_key.type)},
186✔
1452
                 {"required", !is_nullable(config.partition_key.type)},
186✔
1453
                 {"permissions",
186✔
1454
                  {
186✔
1455
                      {"read", true},
186✔
1456
                      {"write", true},
186✔
1457
                  }},
186✔
1458
             }},
186✔
1459
        };
186✔
1460
        mongo_service_def["config"]["sync"] = sync_config;
186✔
1461
    }
186✔
1462

170✔
1463
    auto create_mongo_service_resp = services.post_json(std::move(mongo_service_def));
357✔
1464
    std::string mongo_service_id = create_mongo_service_resp["_id"];
357✔
1465

170✔
1466
    auto default_rule = services[mongo_service_id]["default_rule"];
357✔
1467
    auto service_roles = nlohmann::json::array();
357✔
1468
    if (config.service_roles.empty()) {
357✔
1469
        service_roles = nlohmann::json::array({{{"name", "default"},
351✔
1470
                                                {"apply_when", nlohmann::json::object()},
351✔
1471
                                                {"document_filters",
351✔
1472
                                                 {
351✔
1473
                                                     {"read", true},
351✔
1474
                                                     {"write", true},
351✔
1475
                                                 }},
351✔
1476
                                                {"read", true},
351✔
1477
                                                {"write", true},
351✔
1478
                                                {"insert", true},
351✔
1479
                                                {"delete", true}}});
351✔
1480
    }
351✔
1481
    else {
6✔
1482
        std::transform(config.service_roles.begin(), config.service_roles.end(), std::back_inserter(service_roles),
6✔
1483
                       [](const AppCreateConfig::ServiceRole& role_def) {
6✔
1484
                           nlohmann::json ret{
6✔
1485
                               {"name", role_def.name},
6✔
1486
                               {"apply_when", role_def.apply_when},
6✔
1487
                               {"document_filters",
6✔
1488
                                {
6✔
1489
                                    {"read", role_def.document_filters.read},
6✔
1490
                                    {"write", role_def.document_filters.write},
6✔
1491
                                }},
6✔
1492
                               {"insert", role_def.insert_filter},
6✔
1493
                               {"delete", role_def.delete_filter},
6✔
1494
                               {"read", role_def.read},
6✔
1495
                               {"write", role_def.write},
6✔
1496
                           };
6✔
1497
                           return ret;
6✔
1498
                       });
6✔
1499
    }
6✔
1500

170✔
1501
    default_rule.post_json({{"roles", service_roles}});
357✔
1502

170✔
1503
    // No need for a draft because there are no breaking changes in the initial schema when the app is created.
170✔
1504
    bool use_draft = false;
357✔
1505
    session.create_schema(app_id, config, use_draft);
357✔
1506

170✔
1507
    // Enable sync after schema is created.
170✔
1508
    AdminAPISession::ServiceConfig service_config;
357✔
1509
    service_config.database_name = sync_config["database_name"];
357✔
1510
    if (config.flx_sync_config) {
357✔
1511
        service_config.mode = AdminAPISession::ServiceConfig::SyncMode::Flexible;
171✔
1512
        service_config.queryable_field_names = sync_config["queryable_fields_names"];
171✔
1513
        service_config.asymmetric_tables = sync_config["asymmetric_tables"];
171✔
1514
    }
171✔
1515
    else {
186✔
1516
        service_config.mode = AdminAPISession::ServiceConfig::SyncMode::Partitioned;
186✔
1517
        service_config.partition = sync_config["partition"];
186✔
1518
    }
186✔
1519
    session.enable_sync(app_id, mongo_service_id, service_config);
357✔
1520

170✔
1521
    app["sync"]["config"].put_json({{"development_mode_enabled", config.dev_mode_enabled}});
357✔
1522

170✔
1523
    auto rules = services[mongo_service_id]["rules"];
357✔
1524
    rules.post_json({
357✔
1525
        {"database", config.mongo_dbname},
357✔
1526
        {"collection", "UserData"},
357✔
1527
        {"roles",
357✔
1528
         {{{"name", "default"},
357✔
1529
           {"apply_when", nlohmann::json::object()},
357✔
1530
           {"document_filters",
357✔
1531
            {
357✔
1532
                {"read", true},
357✔
1533
                {"write", true},
357✔
1534
            }},
357✔
1535
           {"read", true},
357✔
1536
           {"write", true},
357✔
1537
           {"insert", true},
357✔
1538
           {"delete", true}}}},
357✔
1539
    });
357✔
1540

170✔
1541
    app["custom_user_data"].patch_json({
357✔
1542
        {"mongo_service_id", mongo_service_id},
357✔
1543
        {"enabled", true},
357✔
1544
        {"database_name", config.mongo_dbname},
357✔
1545
        {"collection_name", "UserData"},
357✔
1546
        {"user_id_field", "user_id"},
357✔
1547
    });
357✔
1548

170✔
1549
    services.post_json({
357✔
1550
        {"name", "gcm"},
357✔
1551
        {"type", "gcm"},
357✔
1552
        {"config",
357✔
1553
         {
357✔
1554
             {"senderId", "gcm"},
357✔
1555
         }},
357✔
1556
        {"secret_config",
357✔
1557
         {
357✔
1558
             {"apiKey", "gcm"},
357✔
1559
         }},
357✔
1560
        {"version", 1},
357✔
1561
    });
357✔
1562

170✔
1563
    // Wait for initial sync to complete, as connecting while this is happening
170✔
1564
    // causes various problems
170✔
1565
    bool any_sync_types = std::any_of(config.schema.begin(), config.schema.end(), [](auto& object_schema) {
365✔
1566
        return object_schema.table_type == ObjectSchema::ObjectType::TopLevel;
365✔
1567
    });
365✔
1568
    if (any_sync_types) {
357✔
1569
        timed_sleeping_wait_for(
351✔
1570
            [&] {
1,178✔
1571
                return session.is_initial_sync_complete(app_id);
1,178✔
1572
            },
1,178✔
1573
            std::chrono::seconds(30), std::chrono::seconds(1));
351✔
1574
    }
351✔
1575

170✔
1576
    return {client_app_id, app_id, session, config};
357✔
1577
}
357✔
1578

1579
AppSession get_runtime_app_session()
1580
{
172✔
1581
    static const AppSession cached_app_session = [&] {
87✔
1582
        auto cached_app_session = create_app(default_app_config());
2✔
1583
        return cached_app_session;
2✔
1584
    }();
2✔
1585
    return cached_app_session;
172✔
1586
}
172✔
1587

1588

1589
#ifdef REALM_MONGODB_ENDPOINT
1590
TEST_CASE("app: baas admin api", "[sync][app][admin api][baas]") {
1591
    SECTION("embedded objects") {
1592
        Schema schema{{"top",
1593
                       {{"_id", PropertyType::String, true},
1594
                        {"location", PropertyType::Object | PropertyType::Nullable, "location"}}},
1595
                      {"location",
1596
                       ObjectSchema::ObjectType::Embedded,
1597
                       {{"coordinates", PropertyType::Double | PropertyType::Array}}}};
1598

1599
        auto test_app_config = minimal_app_config("test", schema);
1600
        create_app(test_app_config);
1601
    }
1602

1603
    SECTION("embedded object with array") {
1604
        Schema schema{
1605
            {"a",
1606
             {{"_id", PropertyType::String, true},
1607
              {"b_link", PropertyType::Object | PropertyType::Array | PropertyType::Nullable, "b"}}},
1608
            {"b",
1609
             ObjectSchema::ObjectType::Embedded,
1610
             {{"c_link", PropertyType::Object | PropertyType::Nullable, "c"}}},
1611
            {"c", {{"_id", PropertyType::String, true}, {"d_str", PropertyType::String}}},
1612
        };
1613
        auto test_app_config = minimal_app_config("test", schema);
1614
        create_app(test_app_config);
1615
    }
1616

1617
    SECTION("dictionaries") {
1618
        Schema schema{
1619
            {"a", {{"_id", PropertyType::String, true}, {"b_dict", PropertyType::Dictionary | PropertyType::String}}},
1620
        };
1621

1622
        auto test_app_config = minimal_app_config("test", schema);
1623
        create_app(test_app_config);
1624
    }
1625

1626
    SECTION("set") {
1627
        Schema schema{
1628
            {"a", {{"_id", PropertyType::String, true}, {"b_dict", PropertyType::Set | PropertyType::String}}},
1629
        };
1630

1631
        auto test_app_config = minimal_app_config("test", schema);
1632
        create_app(test_app_config);
1633
    }
1634
}
1635
#endif
1636

1637
} // namespace realm
1638

1639
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc