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

realm / realm-core / thomas.goyne_442

02 Jul 2024 07:51PM UTC coverage: 90.995% (+0.02%) from 90.974%
thomas.goyne_442

push

Evergreen

web-flow
[RCORE-2146] CAPI Remove `is_fatal` flag flip (#7751)

102372 of 180620 branches covered (56.68%)

0 of 1 new or added line in 1 file covered. (0.0%)

625 existing lines in 26 files now uncovered.

215592 of 236928 relevant lines covered (90.99%)

5608163.57 hits per line

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

85.38
/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,777✔
42
    switch (type & ~PropertyType::Flags) {
5,777✔
43
        case PropertyType::UUID:
28✔
44
            return "uuid";
28✔
45
        case PropertyType::Mixed:
190✔
46
            return "mixed";
190✔
47
        case PropertyType::Bool:
✔
48
            return "bool";
×
49
        case PropertyType::Data:
✔
50
            return "binData";
×
51
        case PropertyType::Date:
15✔
52
            return "date";
15✔
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,121✔
60
            return "long";
1,121✔
61
        case PropertyType::Object:
✔
62
            return "object";
×
63
        case PropertyType::ObjectId:
2,016✔
64
            return "objectId";
2,016✔
65
        case PropertyType::String:
2,397✔
66
            return "string";
2,397✔
67
        case PropertyType::LinkingObjects:
✔
68
            return "linkingObjects";
×
69
        default:
✔
70
            REALM_COMPILER_HINT_UNREACHABLE();
×
71
    }
5,777✔
72
}
5,777✔
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)
201✔
80
        , m_partition_key(partition_key)
201✔
81
        , m_mongo_service_name(service_name)
201✔
82
        , m_mongo_db_name(db_name)
201✔
83
        , m_is_flx_sync(is_flx_sync)
201✔
84
    {
419✔
85
    }
419✔
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
{
2,016✔
105
    nlohmann::json required = nlohmann::json::array();
2,016✔
106
    nlohmann::json properties = nlohmann::json::object();
2,016✔
107
    for (const auto& prop : obj_schema.persisted_properties) {
7,104✔
108
        if (include_prop && !include_prop(prop)) {
7,104✔
109
            continue;
1,543✔
110
        }
1,543✔
111
        if (clear_path) {
5,561✔
112
            m_current_path.clear();
5,443✔
113
        }
5,443✔
114
        properties.emplace(prop.name, property_to_jsonschema(prop));
5,561✔
115
        if (!is_nullable(prop.type) && !is_collection(prop.type)) {
5,561✔
116
            required.push_back(prop.name);
2,469✔
117
        }
2,469✔
118
    }
5,561✔
119

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

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

131
    if ((prop.type & ~PropertyType::Flags) == PropertyType::Object) {
5,705✔
132
        auto target_obj = m_schema.find(prop.object_type);
562✔
133
        REALM_ASSERT(target_obj != m_schema.end());
562✔
134

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

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

171
    if (is_array(prop.type)) {
5,705✔
172
        return nlohmann::json{{"bsonType", "array"}, {"items", type_output}};
274✔
173
    }
274✔
174
    if (is_set(prop.type)) {
5,431✔
175
        return nlohmann::json{{"bsonType", "array"}, {"uniqueItems", true}, {"items", type_output}};
4✔
176
    }
4✔
177
    if (is_dictionary(prop.type)) {
5,427✔
178
        return nlohmann::json{
56✔
179
            {"bsonType", "object"}, {"properties", nlohmann::json::object()}, {"additionalProperties", type_output}};
56✔
180
    }
56✔
181

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

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

192
    auto schema_json = object_schema_to_jsonschema(obj_schema, include_prop, true);
1,904✔
193
    auto& prop_sub_obj = schema_json["properties"];
1,904✔
194
    if (!prop_sub_obj.contains(m_partition_key.name) && !m_is_flx_sync) {
1,904✔
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,904✔
201
        {"schema", schema_json},
1,904✔
202
        {"metadata", nlohmann::json::object({{"database", m_mongo_db_name},
1,904✔
203
                                             {"collection", obj_schema.name},
1,904✔
204
                                             {"data_source", m_mongo_service_name}})},
1,904✔
205
        {"relationships", m_relationships},
1,904✔
206
    };
1,904✔
207
}
1,904✔
208

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

219
    ~CurlGlobalGuard()
220
    {
16,641✔
221
        std::lock_guard<std::mutex> lk(m_mutex);
16,641✔
222
        if (--m_users == 0) {
16,641✔
223
            curl_global_cleanup();
16,641✔
224
        }
16,641✔
225
    }
16,641✔
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
{
13,566✔
242
    REALM_ASSERT(response);
13,566✔
243
    size_t realsize = size * nmemb;
13,566✔
244
    response->append(ptr, realsize);
13,566✔
245
    return realsize;
13,566✔
246
}
13,566✔
247

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

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

277
    return {};
1,550✔
278
}
1,556✔
279

280
const static std::string g_baas_coid_header_name("x-appservices-request-id");
281

282
} // namespace
283

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

292
    struct curl_slist* list = nullptr;
16,641✔
293
    auto curl_cleanup = util::ScopeExit([&]() noexcept {
16,641✔
294
        curl_easy_cleanup(curl);
16,641✔
295
        curl_slist_free_all(list);
16,641✔
296
    });
16,641✔
297

298
    std::string response;
16,641✔
299
    app::HttpHeaders response_headers;
16,641✔
300

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

306
    /* Now specify the POST data */
307
    if (request.method == app::HttpMethod::post) {
16,641✔
308
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
8,073✔
309
    }
8,073✔
310
    else if (request.method == app::HttpMethod::put) {
8,568✔
311
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
2,030✔
312
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
2,030✔
313
    }
2,030✔
314
    else if (request.method == app::HttpMethod::patch) {
6,538✔
315
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
770✔
316
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
770✔
317
    }
770✔
318
    else if (request.method == app::HttpMethod::del) {
5,768✔
319
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
455✔
320
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
455✔
321
    }
455✔
322
    else if (request.method == app::HttpMethod::patch) {
5,313✔
323
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
×
324
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
×
325
    }
×
326

327
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, request.timeout_ms);
16,641✔
328

329
    for (auto header : request.headers) {
45,777✔
330
        auto header_str = util::format("%1: %2", header.first, header.second);
45,777✔
331
        list = curl_slist_append(list, header_str.c_str());
45,777✔
332
    }
45,777✔
333

334
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
16,641✔
335
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_cb);
16,641✔
336
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
16,641✔
337
    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, curl_header_cb);
16,641✔
338
    curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response_headers);
16,641✔
339

340
#ifdef REALM_CURL_CACERTS
341
    auto ca_info = unquote_string(REALM_QUOTE(REALM_CURL_CACERTS));
342
    curl_easy_setopt(curl, CURLOPT_CAINFO, ca_info.c_str());
343
#endif
344

345
    auto start_time = std::chrono::steady_clock::now();
16,641✔
346
    auto response_code = curl_easy_perform(curl);
16,641✔
347
    auto total_time = std::chrono::steady_clock::now() - start_time;
16,641✔
348

349
    auto logger = util::Logger::get_default_logger();
16,641✔
350
    if (response_code != CURLE_OK) {
16,641✔
351
        std::string message = curl_easy_strerror(response_code);
1✔
352
        logger->error("curl_easy_perform() failed when sending request to '%1' with body '%2': %3", request.url,
1✔
353
                      request.body, message);
1✔
354
        // Return a failing response with the CURL error as the custom code
355
        return {0, response_code, {}, message};
1✔
356
    }
1✔
357
    if (logger->would_log(util::Logger::Level::trace)) {
16,640✔
358
        std::string coid = [&] {
×
359
            auto coid_header = response_headers.find(g_baas_coid_header_name);
×
360
            if (coid_header == response_headers.end()) {
×
361
                return std::string{};
×
362
            }
×
363
            return util::format("BaaS Coid: \"%1\"", coid_header->second);
×
364
        }();
×
365

366
        logger->trace("Baas API %1 request to %2 took %3 %4\n", request.method, request.url,
×
367
                      std::chrono::duration_cast<std::chrono::milliseconds>(total_time), coid);
×
368
    }
×
369

370
    int http_code = 0;
16,640✔
371
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
16,640✔
372
    return {
16,640✔
373
        http_code,
16,640✔
374
        0, // binding_response_code
16,640✔
375
        std::move(response_headers),
16,640✔
376
        std::move(response),
16,640✔
377
    };
16,640✔
378
}
16,641✔
379

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

406
        auto [resp, baas_coid] = do_request(std::move(url_path), app::HttpMethod::post);
2✔
407
        if (!resp["id"].is_string()) {
2✔
408
            throw RuntimeError(
×
409
                ErrorCodes::RuntimeError,
×
410
                util::format(
×
411
                    "Failed to start baas container, got response without container ID: \"%1\" (baas coid: %2)",
×
412
                    resp.dump(), baas_coid));
×
413
        }
×
414
        m_container_id = resp["id"].get<std::string>();
2✔
415
        if (m_container_id.empty()) {
2✔
416
            throw RuntimeError(
×
417
                ErrorCodes::InvalidArgument,
×
418
                util::format("Failed to start baas container, got response with empty container ID (baas coid: %1)",
×
419
                             baas_coid));
×
420
        }
×
421
        logger->info("Baasaas container started with id \"%1\"", m_container_id);
2✔
422
        util::File lock_file(s_baasaas_lock_file_name, util::File::mode_Write);
2✔
423
        lock_file.write(0, m_container_id);
2✔
424
    }
2✔
425

426
    explicit Baasaas(std::string api_key, std::string baasaas_instance_id)
427
        : m_api_key(std::move(api_key))
428
        , m_base_url(get_baasaas_base_url())
429
        , m_container_id(std::move(baasaas_instance_id))
430
        , m_externally_managed_instance(true)
431
    {
×
432
        auto logger = util::Logger::get_default_logger();
×
433
        logger->info("Using externally managed baasaas instance \"%1\"", m_container_id);
×
434
    }
×
435

436
    Baasaas(const Baasaas&) = delete;
437
    Baasaas(Baasaas&&) = delete;
438
    Baasaas& operator=(const Baasaas&) = delete;
439
    Baasaas& operator=(Baasaas&&) = delete;
440

441
    ~Baasaas()
442
    {
2✔
443
        stop();
2✔
444
    }
2✔
445

446
    void poll()
447
    {
1,163✔
448
        if (!m_http_endpoint.empty() || m_container_id.empty()) {
1,163✔
449
            return;
1,161✔
450
        }
1,161✔
451

452
        auto logger = util::Logger::get_default_logger();
2✔
453
        auto poll_start_at = std::chrono::system_clock::now();
2✔
454
        std::string http_endpoint;
2✔
455
        std::string mongo_endpoint;
2✔
456
        bool logged = false;
2✔
457
        while (std::chrono::system_clock::now() - poll_start_at < std::chrono::minutes(2) &&
7✔
458
               m_http_endpoint.empty()) {
7✔
459
            if (http_endpoint.empty()) {
7✔
460
                auto [status_obj, baas_coid] =
4✔
461
                    do_request(util::format("containerStatus?id=%1", m_container_id), app::HttpMethod::get);
4✔
462
                if (!status_obj["httpUrl"].is_null()) {
4✔
463
                    if (!status_obj["httpUrl"].is_string() || !status_obj["mongoUrl"].is_string()) {
2✔
464
                        throw RuntimeError(ErrorCodes::RuntimeError,
×
465
                                           util::format("Error polling for baasaas instance. httpUrl or mongoUrl is "
×
466
                                                        "the wrong format: \"%1\" (baas coid: %2)",
×
467
                                                        status_obj.dump(), baas_coid));
×
468
                    }
×
469
                    http_endpoint = status_obj["httpUrl"].get<std::string>();
2✔
470
                    mongo_endpoint = status_obj["mongoUrl"].get<std::string>();
2✔
471
                }
2✔
472
            }
4✔
473
            else {
3✔
474
                app::Request baas_req;
3✔
475
                baas_req.url = util::format("%1/api/private/v1.0/version", http_endpoint);
3✔
476
                baas_req.method = app::HttpMethod::get;
3✔
477
                baas_req.headers.insert_or_assign("Content-Type", "application/json");
3✔
478
                auto baas_resp = do_http_request(baas_req);
3✔
479
                if (baas_resp.http_status_code >= 200 && baas_resp.http_status_code < 300) {
3✔
480
                    m_http_endpoint = http_endpoint;
2✔
481
                    m_mongo_endpoint = mongo_endpoint;
2✔
482
                    break;
2✔
483
                }
2✔
484
            }
3✔
485

486
            if (!logged) {
5✔
487
                logger->info("Waiting for baasaas container \"%1\" to be ready", m_container_id);
2✔
488
                logged = true;
2✔
489
            }
2✔
490
            std::this_thread::sleep_for(std::chrono::seconds(3));
5✔
491
        }
5✔
492

493
        if (m_http_endpoint.empty()) {
2✔
494
            throw std::runtime_error(
×
495
                util::format("Failed to launch baasaas container %1 within 2 minutes", m_container_id));
×
496
        }
×
497
    }
2✔
498

499
    void stop()
500
    {
4✔
501
        if (m_externally_managed_instance) {
4✔
502
            return;
×
503
        }
×
504
        auto container_id = std::move(m_container_id);
4✔
505
        if (container_id.empty()) {
4✔
506
            return;
2✔
507
        }
2✔
508

509
        auto logger = util::Logger::get_default_logger();
2✔
510
        logger->info("Stopping baasaas container with id \"%1\"", container_id);
2✔
511
        do_request(util::format("stopContainer?id=%1", container_id), app::HttpMethod::post);
2✔
512
        auto lock_file = util::File(std::string{s_baasaas_lock_file_name}, util::File::mode_Write);
2✔
513
        lock_file.resize(0);
2✔
514
        lock_file.close();
2✔
515
        util::File::remove(lock_file.get_path());
2✔
516
    }
2✔
517

518
    const std::string& http_endpoint()
519
    {
784✔
520
        poll();
784✔
521
        return m_http_endpoint;
784✔
522
    }
784✔
523

524
    const std::string& mongo_endpoint()
525
    {
379✔
526
        poll();
379✔
527
        return m_mongo_endpoint;
379✔
528
    }
379✔
529

530
private:
531
    std::pair<nlohmann::json, std::string> do_request(std::string api_path, app::HttpMethod method)
532
    {
8✔
533
        app::Request request;
8✔
534

535
        request.url = util::format("%1/%2", m_base_url, api_path);
8✔
536
        request.method = method;
8✔
537
        request.headers.insert_or_assign("apiKey", m_api_key);
8✔
538
        request.headers.insert_or_assign("Content-Type", "application/json");
8✔
539
        auto response = do_http_request(request);
8✔
540
        if (response.http_status_code < 200 || response.http_status_code >= 300) {
8✔
541
            throw RuntimeError(ErrorCodes::HTTPError,
×
542
                               util::format("Baasaas api response code: %1 Response body: %2, Baas coid: %3",
×
543
                                            response.http_status_code, response.body,
×
544
                                            baas_coid_from_response(response)));
×
545
        }
×
546
        try {
8✔
547
            return {nlohmann::json::parse(response.body), baas_coid_from_response(response)};
8✔
548
        }
8✔
549
        catch (const nlohmann::json::exception& e) {
8✔
550
            throw RuntimeError(
×
551
                ErrorCodes::MalformedJson,
×
552
                util::format("Error making baasaas request to %1 (baas coid %2): Invalid json returned \"%3\" (%4)",
×
553
                             request.url, baas_coid_from_response(response), response.body, e.what()));
×
554
        }
×
555
    }
8✔
556

557
    std::string baas_coid_from_response(const app::Response& resp)
558
    {
8✔
559
        if (auto it = resp.headers.find(g_baas_coid_header_name); it != resp.headers.end()) {
8✔
560
            return it->second;
8✔
561
        }
8✔
562
        return "<not found>";
×
563
    }
8✔
564

565
    static std::string get_baasaas_base_url()
566
    {
2✔
567
        auto env_value = getenv_sv("BAASAAS_BASE_URL");
2✔
568
        if (env_value.empty()) {
2✔
569
            // This is the current default endpoint for baasaas maintained by the sync team.
570
            // You can reach out for help in #appx-device-sync-internal if there are problems.
571
            return "https://us-east-1.aws.data.mongodb-api.com/app/baas-container-service-autzb/endpoint";
2✔
572
        }
2✔
573

574
        return unquote_string(env_value);
×
575
    }
2✔
576

577
    constexpr static std::string_view s_baasaas_lock_file_name = "baasaas_instance.lock";
578

579
    std::string m_api_key;
580
    std::string m_base_url;
581
    std::string m_container_id;
582
    bool m_externally_managed_instance;
583
    std::string m_http_endpoint;
584
    std::string m_mongo_endpoint;
585
};
586

587
class BaasaasLauncher : public Catch::EventListenerBase {
588
public:
589
    static std::optional<Baasaas>& get_baasaas_holder()
590
    {
1,169✔
591
        static std::optional<Baasaas> global_baasaas = std::nullopt;
1,169✔
592
        return global_baasaas;
1,169✔
593
    }
1,169✔
594

595
    using Catch::EventListenerBase::EventListenerBase;
596

597
    void testRunStarting(Catch::TestRunInfo const&) override
598
    {
4✔
599
        std::string_view api_key(getenv_sv("BAASAAS_API_KEY"));
4✔
600
        if (api_key.empty()) {
4✔
601
            return;
2✔
602
        }
2✔
603

604
        // Allow overriding the baas base url at runtime via an environment variable, even if BAASAAS_API_KEY
605
        // is also specified.
606
        if (!getenv_sv("BAAS_BASE_URL").empty()) {
2✔
607
            return;
×
608
        }
×
609

610
        // If we've started a baasaas container outside of running the tests, then use that instead of
611
        // figuring out how to start our own.
612
        if (auto baasaas_instance = getenv_sv("BAASAAS_INSTANCE_ID"); !baasaas_instance.empty()) {
2✔
613
            auto& baasaas_holder = get_baasaas_holder();
×
614
            REALM_ASSERT(!baasaas_holder);
×
615
            baasaas_holder.emplace(std::string{api_key}, std::string{baasaas_instance});
×
616
            return;
×
617
        }
×
618

619
        std::string_view ref_spec(getenv_sv("BAASAAS_REF_SPEC"));
2✔
620
        std::string_view mode_spec(getenv_sv("BAASAAS_START_MODE"));
2✔
621
        Baasaas::StartMode mode = Baasaas::StartMode::Default;
2✔
622
        if (mode_spec == "branch") {
2✔
623
            if (ref_spec.empty()) {
×
624
                throw std::runtime_error("Expected branch name in BAASAAS_REF_SPEC env variable, but it was empty");
×
625
            }
×
626
            mode = Baasaas::StartMode::Branch;
×
627
        }
×
628
        else if (mode_spec == "githash") {
2✔
629
            if (ref_spec.empty()) {
2✔
630
                throw std::runtime_error("Expected git hash in BAASAAS_REF_SPEC env variable, but it was empty");
×
631
            }
×
632
            mode = Baasaas::StartMode::GitHash;
2✔
633
        }
2✔
634
        else if (mode_spec == "patchid") {
×
635
            if (ref_spec.empty()) {
×
636
                throw std::runtime_error("Expected patch id in BAASAAS_REF_SPEC env variable, but it was empty");
×
637
            }
×
638
            mode = Baasaas::StartMode::PatchId;
×
639
        }
×
640
        else {
×
641
            if (!mode_spec.empty()) {
×
642
                throw std::runtime_error("Excepted BAASAAS_START_MODE to be \"githash\", \"patchid\", or \"branch\"");
×
643
            }
×
644
            ref_spec = {};
×
645
        }
×
646

647
        auto& baasaas_holder = get_baasaas_holder();
2✔
648
        REALM_ASSERT(!baasaas_holder);
2✔
649
        baasaas_holder.emplace(std::string{api_key}, mode, std::string{ref_spec});
2✔
650

651
        get_runtime_app_session();
2✔
652
    }
2✔
653

654
    void testRunEnded(Catch::TestRunStats const&) override
655
    {
4✔
656
        if (auto& baasaas_holder = get_baasaas_holder()) {
4✔
657
            baasaas_holder->stop();
2✔
658
        }
2✔
659
    }
4✔
660
};
661

662
CATCH_REGISTER_LISTENER(BaasaasLauncher)
663

664
AdminAPIEndpoint AdminAPIEndpoint::operator[](StringData name) const
665
{
18,400✔
666
    return AdminAPIEndpoint(util::format("%1/%2", m_url, name), m_access_token);
18,400✔
667
}
18,400✔
668

669
app::Response AdminAPIEndpoint::do_request(app::Request request) const
670
{
11,774✔
671
    if (request.url.find('?') == std::string::npos) {
11,774✔
672
        request.url = util::format("%1?bypass_service_change=SyncSchemaVersionIncrease", request.url);
11,774✔
673
    }
11,774✔
674
    else {
×
675
        request.url = util::format("%1&bypass_service_change=SyncSchemaVersionIncrease", request.url);
×
676
    }
×
677
    request.headers["Content-Type"] = "application/json;charset=utf-8";
11,774✔
678
    request.headers["Accept"] = "application/json";
11,774✔
679
    request.headers["Authorization"] = util::format("Bearer %1", m_access_token);
11,774✔
680
    return do_http_request(std::move(request));
11,774✔
681
}
11,774✔
682

683
app::Response AdminAPIEndpoint::get(const std::vector<std::pair<std::string, std::string>>& params) const
684
{
3,669✔
685
    app::Request req;
3,669✔
686
    req.method = app::HttpMethod::get;
3,669✔
687
    std::stringstream ss;
3,669✔
688
    bool needs_and = false;
3,669✔
689
    ss << m_url;
3,669✔
690
    if (!params.empty() && m_url.find('?') != std::string::npos) {
3,669!
691
        needs_and = true;
×
692
    }
×
693
    for (const auto& param : params) {
3,669✔
694
        if (needs_and) {
×
695
            ss << "&";
×
696
        }
×
697
        else {
×
698
            ss << "?";
×
699
        }
×
700
        needs_and = true;
×
701
        ss << param.first << "=" << param.second;
×
702
    }
×
703
    req.url = ss.str();
3,669✔
704
    return do_request(std::move(req));
3,669✔
705
}
3,669✔
706

707
app::Response AdminAPIEndpoint::del() const
708
{
411✔
709
    app::Request req;
411✔
710
    req.method = app::HttpMethod::del;
411✔
711
    req.url = m_url;
411✔
712
    return do_request(std::move(req));
411✔
713
}
411✔
714

715
nlohmann::json AdminAPIEndpoint::get_json(const std::vector<std::pair<std::string, std::string>>& params) const
716
{
3,669✔
717
    auto resp = get(params);
3,669✔
718
    REALM_ASSERT_EX(resp.http_status_code >= 200 && resp.http_status_code < 300,
3,669✔
719
                    util::format("url: %1, reply: %2", m_url, resp.body));
3,669✔
720
    return nlohmann::json::parse(resp.body.empty() ? "{}" : resp.body);
3,669✔
721
}
3,669✔
722

723
app::Response AdminAPIEndpoint::post(std::string body) const
724
{
4,918✔
725
    app::Request req;
4,918✔
726
    req.method = app::HttpMethod::post;
4,918✔
727
    req.url = m_url;
4,918✔
728
    req.body = std::move(body);
4,918✔
729
    return do_request(std::move(req));
4,918✔
730
}
4,918✔
731

732
nlohmann::json AdminAPIEndpoint::post_json(nlohmann::json body) const
733
{
4,896✔
734
    auto resp = post(body.dump());
4,896✔
735
    REALM_ASSERT_EX(resp.http_status_code >= 200 && resp.http_status_code < 300, m_url, body.dump(), resp.body);
4,896✔
736
    return nlohmann::json::parse(resp.body.empty() ? "{}" : resp.body);
4,896✔
737
}
4,896✔
738

4,896✔
739
app::Response AdminAPIEndpoint::put(std::string body) const
740
{
741
    app::Request req;
2,006✔
742
    req.method = app::HttpMethod::put;
2,006✔
743
    req.url = m_url;
2,006✔
744
    req.body = std::move(body);
2,006✔
745
    return do_request(std::move(req));
2,006✔
746
}
2,006✔
747

2,006✔
748
nlohmann::json AdminAPIEndpoint::put_json(nlohmann::json body) const
749
{
750
    auto resp = put(body.dump());
1,617✔
751
    REALM_ASSERT_EX(resp.http_status_code >= 200 && resp.http_status_code < 300,
1,617✔
752
                    util::format("url: %1 request: %2, reply: %3", m_url, body.dump(), resp.body));
1,617✔
753
    return nlohmann::json::parse(resp.body.empty() ? "{}" : resp.body);
1,617✔
754
}
1,617✔
755

1,617✔
756
app::Response AdminAPIEndpoint::patch(std::string body) const
757
{
758
    app::Request req;
770✔
759
    req.method = app::HttpMethod::patch;
770✔
760
    req.url = m_url;
770✔
761
    req.body = std::move(body);
770✔
762
    return do_request(std::move(req));
770✔
763
}
770✔
764

770✔
765
nlohmann::json AdminAPIEndpoint::patch_json(nlohmann::json body) const
766
{
767
    auto resp = patch(body.dump());
770✔
768
    REALM_ASSERT_EX(resp.http_status_code >= 200 && resp.http_status_code < 300,
770✔
769
                    util::format("url: %1 request: %2, reply: %3", m_url, body.dump(), resp.body));
770✔
770
    return nlohmann::json::parse(resp.body.empty() ? "{}" : resp.body);
770✔
771
}
770✔
772

770✔
773
AdminAPISession AdminAPISession::login(const AppCreateConfig& config)
774
{
775
    std::string admin_url = config.admin_url;
381✔
776
    nlohmann::json login_req_body{
381✔
777
        {"provider", "userpass"},
381✔
778
        {"username", config.admin_username},
381✔
779
        {"password", config.admin_password},
381✔
780
    };
381✔
781
    if (config.logger) {
381✔
782
        config.logger->trace("Logging into baas admin api: %1", admin_url);
381✔
783
    }
381✔
784
    app::Request auth_req{
381✔
785
        app::HttpMethod::post,
381✔
786
        util::format("%1/api/admin/v3.0/auth/providers/local-userpass/login", admin_url),
381✔
787
        60000, // 1 minute timeout
381✔
788
        {
381✔
789
            {"Content-Type", "application/json;charset=utf-8"},
381✔
790
            {"Accept", "application/json"},
381✔
791
        },
381✔
792
        login_req_body.dump(),
381✔
793
    };
381✔
794
    auto login_resp = do_http_request(std::move(auth_req));
381✔
795
    REALM_ASSERT_EX(login_resp.http_status_code == 200, login_resp.http_status_code, login_resp.body);
381✔
796
    auto login_resp_body = nlohmann::json::parse(login_resp.body);
381✔
797

381✔
798
    std::string access_token = login_resp_body["access_token"];
799

381✔
800
    AdminAPIEndpoint user_profile(util::format("%1/api/admin/v3.0/auth/profile", admin_url), access_token);
801
    auto profile_resp = user_profile.get_json();
381✔
802

381✔
803
    std::string group_id = profile_resp["roles"][0]["group_id"];
804

381✔
805
    return AdminAPISession(std::move(admin_url), std::move(access_token), std::move(group_id));
806
}
381✔
807

381✔
808
void AdminAPISession::revoke_user_sessions(const std::string& user_id, const std::string& app_id) const
809
{
810
    auto endpoint = apps()[app_id]["users"][user_id]["logout"];
4✔
811
    auto response = endpoint.put("");
4✔
812
    REALM_ASSERT_EX(response.http_status_code == 204, response.http_status_code, response.body);
4✔
813
}
4✔
814

4✔
815
void AdminAPISession::disable_user_sessions(const std::string& user_id, const std::string& app_id) const
816
{
817
    auto endpoint = apps()[app_id]["users"][user_id]["disable"];
2✔
818
    auto response = endpoint.put("");
2✔
819
    REALM_ASSERT_EX(response.http_status_code == 204, response.http_status_code, response.body);
2✔
820
}
2✔
821

2✔
822
void AdminAPISession::enable_user_sessions(const std::string& user_id, const std::string& app_id) const
823
{
824
    auto endpoint = apps()[app_id]["users"][user_id]["enable"];
2✔
825
    auto response = endpoint.put("");
2✔
826
    REALM_ASSERT_EX(response.http_status_code == 204, response.http_status_code, response.body);
2✔
827
}
2✔
828

2✔
829
// returns false for an invalid/expired access token
830
bool AdminAPISession::verify_access_token(const std::string& access_token, const std::string& app_id) const
831
{
832
    auto endpoint = apps()[app_id]["users"]["verify_token"];
22✔
833
    nlohmann::json request_body{
22✔
834
        {"token", access_token},
22✔
835
    };
22✔
836
    auto response = endpoint.post(request_body.dump());
22✔
837
    if (response.http_status_code == 200) {
22✔
838
        auto resp_json = nlohmann::json::parse(response.body.empty() ? "{}" : response.body);
22✔
839
        try {
16✔
840
            // if these fields are found, then the token is valid according to the server.
16✔
841
            // if it is invalid or expired then an error response is sent.
842
            int64_t issued_at = resp_json["iat"];
843
            int64_t expires_at = resp_json["exp"];
16✔
844
            return issued_at != 0 && expires_at != 0;
16✔
845
        }
16✔
846
        catch (...) {
16✔
847
            return false;
16✔
848
        }
×
UNCOV
849
    }
×
850
    return false;
16✔
851
}
6✔
852

22✔
853
void AdminAPISession::set_development_mode_to(const std::string& app_id, bool enable) const
854
{
855
    auto endpoint = apps()[app_id]["sync"]["config"];
24✔
856
    endpoint.put_json({{"development_mode_enabled", enable}});
24✔
857
}
24✔
858

24✔
859
void AdminAPISession::delete_app(const std::string& app_id) const
860
{
861
    auto app_endpoint = apps()[app_id];
379✔
862
    auto resp = app_endpoint.del();
379✔
863
    REALM_ASSERT_EX(resp.http_status_code == 204, resp.http_status_code, resp.body);
379✔
864
}
379✔
865

379✔
866
std::vector<AdminAPISession::Service> AdminAPISession::get_services(const std::string& app_id) const
867
{
868
    auto endpoint = apps()[app_id]["services"];
88✔
869
    auto response = endpoint.get_json();
88✔
870
    std::vector<AdminAPISession::Service> services;
88✔
871
    for (auto service : response) {
88✔
872
        services.push_back(
176✔
873
            {service["_id"], service["name"], service["type"], service["version"], service["last_modified"]});
176✔
874
    }
176✔
875
    return services;
176✔
876
}
88✔
877

88✔
878

879
std::vector<std::string> AdminAPISession::get_errors(const std::string& app_id) const
880
{
881
    auto endpoint = apps()[app_id]["logs"];
×
882
    auto response = endpoint.get_json({{"errors_only", "true"}});
×
883
    std::vector<std::string> errors;
×
884
    const auto& logs = response["logs"];
×
885
    std::transform(logs.begin(), logs.end(), std::back_inserter(errors), [](const auto& err) {
×
886
        return err["error"];
×
887
    });
×
888
    return errors;
×
889
}
×
UNCOV
890

×
891

892
AdminAPISession::Service AdminAPISession::get_sync_service(const std::string& app_id) const
893
{
894
    auto services = get_services(app_id);
88✔
895
    auto sync_service = std::find_if(services.begin(), services.end(), [&](auto s) {
88✔
896
        return s.type == "mongodb";
88✔
897
    });
88✔
898
    REALM_ASSERT(sync_service != services.end());
88✔
899
    return *sync_service;
88✔
900
}
88✔
901

88✔
902
void AdminAPISession::trigger_client_reset(const std::string& app_id, int64_t file_ident) const
903
{
904
    auto endpoint = apps(APIFamily::Admin)[app_id]["sync"]["force_reset"];
158✔
905
    endpoint.put_json(nlohmann::json{{"file_ident", file_ident}});
158✔
906
}
158✔
907

158✔
908
void AdminAPISession::migrate_to_flx(const std::string& app_id, const std::string& service_id,
909
                                     bool migrate_to_flx) const
910
{
911
    auto endpoint = apps()[app_id]["sync"]["migration"];
30✔
912
    endpoint.put_json(nlohmann::json{{"serviceId", service_id}, {"action", migrate_to_flx ? "start" : "rollback"}});
30✔
913
}
30✔
914

30✔
915
// Each breaking change bumps the schema version, so you can create a new version for each breaking change if
916
// 'use_draft' is false. Set 'use_draft' to true if you want all changes to the schema to be deployed at once
917
// resulting in only one schema version.
918
void AdminAPISession::create_schema(const std::string& app_id, const AppCreateConfig& config, bool use_draft) const
919
{
920
    static const std::string mongo_service_name = "BackingDB";
419✔
921

419✔
922
    auto drafts = apps()[app_id]["drafts"];
923
    std::string draft_id;
419✔
924
    if (use_draft) {
419✔
925
        auto draft_create_resp = drafts.post_json({});
419✔
926
        draft_id = draft_create_resp["_id"];
38✔
927
    }
38✔
928

38✔
929
    auto schemas = apps()[app_id]["schemas"];
930
    auto current_schema = schemas.get_json();
419✔
931
    auto target_schema = config.schema;
419✔
932

419✔
933
    std::unordered_map<std::string, std::string> current_schema_tables;
934
    for (const auto& schema : current_schema) {
419✔
935
        current_schema_tables[schema["metadata"]["collection"]] = schema["_id"];
419✔
936
    }
148✔
937

148✔
938
    // Add new tables
939

940
    auto pk_and_queryable_only = [&](const Property& prop) {
941
        if (config.flx_sync_config) {
3,333✔
942
            const auto& queryable_fields = config.flx_sync_config->queryable_fields;
3,333✔
943

1,509✔
944
            if (std::find(queryable_fields.begin(), queryable_fields.end(), prop.name) != queryable_fields.end()) {
945
                return true;
1,509✔
946
            }
534✔
947
        }
534✔
948
        return prop.name == "_id" || prop.name == config.partition_key.name;
1,509✔
949
    };
2,799✔
950

3,333✔
951
    // Create the schemas in two passes: first populate just the primary key and
952
    // partition key, then add the rest of the properties. This ensures that the
953
    // targets of links exist before adding the links.
954
    std::vector<std::pair<std::string, const ObjectSchema*>> object_schema_to_create;
955
    BaasRuleBuilder rule_builder(target_schema, config.partition_key, mongo_service_name, config.mongo_dbname,
419✔
956
                                 static_cast<bool>(config.flx_sync_config));
419✔
957
    for (const auto& obj_schema : target_schema) {
419✔
958
        auto it = current_schema_tables.find(obj_schema.name);
1,010✔
959
        if (it != current_schema_tables.end()) {
1,010✔
960
            object_schema_to_create.push_back({it->second, &obj_schema});
1,010✔
961
            continue;
116✔
962
        }
116✔
963

116✔
964
        auto schema_to_create = rule_builder.object_schema_to_baas_schema(obj_schema, pk_and_queryable_only);
965
        auto schema_create_resp = schemas.post_json(schema_to_create);
894✔
966
        object_schema_to_create.push_back({schema_create_resp["_id"], &obj_schema});
894✔
967
    }
894✔
968

894✔
969
    // Update existing tables (including the ones just created)
970
    for (const auto& [id, obj_schema] : object_schema_to_create) {
971
        auto schema_to_create = rule_builder.object_schema_to_baas_schema(*obj_schema, nullptr);
1,010✔
972
        schema_to_create["_id"] = id;
1,010✔
973
        schemas[id].put_json(schema_to_create);
1,010✔
974
    }
1,010✔
975

1,010✔
976
    // Delete removed tables
977
    for (const auto& table : current_schema_tables) {
978
        if (target_schema.find(table.first) == target_schema.end()) {
419✔
979
            schemas[table.second].del();
148✔
980
        }
32✔
981
    }
32✔
982

148✔
983
    if (use_draft) {
984
        drafts[draft_id]["deployment"].post_json({});
419✔
985
    }
38✔
986
}
38✔
987

419✔
988
static nlohmann::json convert_config(AdminAPISession::ServiceConfig config)
989
{
990
    if (config.mode == AdminAPISession::ServiceConfig::SyncMode::Flexible) {
4✔
991
        auto payload = nlohmann::json{{"database_name", config.database_name},
4✔
992
                                      {"state", config.state},
4✔
993
                                      {"is_recovery_mode_disabled", config.recovery_is_disabled}};
4✔
994
        if (config.queryable_field_names) {
4✔
995
            payload["queryable_fields_names"] = *config.queryable_field_names;
4✔
996
        }
997
        if (config.permissions) {
998
            payload["permissions"] = *config.permissions;
4✔
999
        }
4✔
1000
        if (config.asymmetric_tables) {
4✔
1001
            payload["asymmetric_tables"] = *config.asymmetric_tables;
4✔
1002
        }
39✔
1003
        return payload;
39✔
1004
    }
39✔
1005
    return nlohmann::json{{"database_name", config.database_name},
4✔
UNCOV
1006
                          {"partition", *config.partition},
×
1007
                          {"state", config.state},
4✔
1008
                          {"is_recovery_mode_disabled", config.recovery_is_disabled}};
1009
}
1010

6✔
1011
AdminAPIEndpoint AdminAPISession::service_config_endpoint(const std::string& app_id,
6✔
1012
                                                          const std::string& service_id) const
6✔
1013
{
6✔
1014
    return apps()[app_id]["services"][service_id]["config"];
6✔
1015
}
6✔
1016

1017
AdminAPISession::ServiceConfig AdminAPISession::disable_sync(const std::string& app_id, const std::string& service_id,
1018
                                                             AdminAPISession::ServiceConfig sync_config) const
14✔
1019
{
14✔
1020
    auto endpoint = service_config_endpoint(app_id, service_id);
14✔
1021
    if (sync_config.state != "") {
×
1022
        sync_config.state = "";
×
1023
        endpoint.patch_json({{sync_config.sync_service_name(), convert_config(sync_config)}});
1024
    }
14✔
1025
    return sync_config;
14✔
1026
}
14✔
1027

14✔
1028
AdminAPISession::ServiceConfig AdminAPISession::pause_sync(const std::string& app_id, const std::string& service_id,
14✔
1029
                                                           AdminAPISession::ServiceConfig sync_config) const
1030
{
1031
    auto endpoint = service_config_endpoint(app_id, service_id);
×
1032
    if (sync_config.state != "disabled") {
×
1033
        sync_config.state = "disabled";
×
1034
        endpoint.patch_json({{sync_config.sync_service_name(), convert_config(sync_config)}});
×
1035
    }
1036
    return sync_config;
1037
}
2✔
1038

2✔
1039
AdminAPISession::ServiceConfig AdminAPISession::enable_sync(const std::string& app_id, const std::string& service_id,
2✔
1040
                                                            AdminAPISession::ServiceConfig sync_config) const
2✔
1041
{
2✔
1042
    auto endpoint = service_config_endpoint(app_id, service_id);
1043
    sync_config.state = "enabled";
1044
    endpoint.patch_json({{sync_config.sync_service_name(), convert_config(sync_config)}});
387✔
1045
    return sync_config;
387✔
1046
}
199✔
1047

199✔
1048
AdminAPISession::ServiceConfig AdminAPISession::set_disable_recovery_to(const std::string& app_id,
199✔
1049
                                                                        const std::string& service_id,
199✔
1050
                                                                        ServiceConfig sync_config, bool disable) const
199✔
1051
{
199✔
1052
    auto endpoint = service_config_endpoint(app_id, service_id);
199✔
1053
    sync_config.recovery_is_disabled = disable;
2✔
1054
    endpoint.patch_json({{sync_config.sync_service_name(), convert_config(sync_config)}});
2✔
1055
    return sync_config;
199✔
1056
}
197✔
1057

197✔
1058
std::vector<AdminAPISession::SchemaVersionInfo> AdminAPISession::get_schema_versions(const std::string& app_id) const
199✔
1059
{
199✔
1060
    std::vector<AdminAPISession::SchemaVersionInfo> ret;
188✔
1061
    auto endpoint = apps()[app_id]["sync"]["schemas"]["versions"];
188✔
1062
    auto res = endpoint.get_json();
188✔
1063
    for (auto&& version : res["versions"].get<std::vector<nlohmann::json>>()) {
188✔
1064
        SchemaVersionInfo info;
387✔
1065
        info.version_major = version["version_major"];
1066
        ret.push_back(std::move(info));
1067
    }
1068

429✔
1069
    return ret;
429✔
1070
}
429✔
1071

1072
AdminAPISession::ServiceConfig AdminAPISession::get_config(const std::string& app_id,
1073
                                                           const AdminAPISession::Service& service) const
UNCOV
1074
{
×
UNCOV
1075
    auto endpoint = service_config_endpoint(app_id, service.id);
×
UNCOV
1076
    auto response = endpoint.get_json();
×
UNCOV
1077
    AdminAPISession::ServiceConfig config;
×
UNCOV
1078
    if (response.contains("flexible_sync")) {
×
UNCOV
1079
        auto sync = response["flexible_sync"];
×
UNCOV
1080
        config.mode = AdminAPISession::ServiceConfig::SyncMode::Flexible;
×
UNCOV
1081
        config.state = sync["state"];
×
1082
        config.database_name = sync["database_name"];
1083
        config.permissions = sync["permissions"];
1084
        config.queryable_field_names = sync["queryable_fields_names"];
UNCOV
1085
        auto recovery_disabled = sync["is_recovery_mode_disabled"];
×
UNCOV
1086
        config.recovery_is_disabled = recovery_disabled.is_boolean() ? recovery_disabled.get<bool>() : false;
×
UNCOV
1087
    }
×
UNCOV
1088
    else if (response.contains("sync")) {
×
UNCOV
1089
        auto sync = response["sync"];
×
UNCOV
1090
        config.mode = AdminAPISession::ServiceConfig::SyncMode::Partitioned;
×
UNCOV
1091
        config.state = sync["state"];
×
UNCOV
1092
        config.database_name = sync["database_name"];
×
1093
        config.partition = sync["partition"];
1094
        auto recovery_disabled = sync["is_recovery_mode_disabled"];
1095
        config.recovery_is_disabled = recovery_disabled.is_boolean() ? recovery_disabled.get<bool>() : false;
1096
    }
383✔
1097
    else {
383✔
1098
        throw std::runtime_error(util::format("Unsupported config format from server: %1", response));
383✔
1099
    }
383✔
1100
    return config;
383✔
1101
}
383✔
1102

1103
bool AdminAPISession::is_sync_enabled(const std::string& app_id) const
1104
{
1105
    auto sync_service = get_sync_service(app_id);
1106
    auto config = get_config(app_id, sync_service);
4✔
1107
    return config.state == "enabled";
4✔
1108
}
4✔
1109

4✔
1110
bool AdminAPISession::is_sync_terminated(const std::string& app_id) const
4✔
1111
{
4✔
1112
    auto sync_service = get_sync_service(app_id);
1113
    auto config = get_config(app_id, sync_service);
1114
    if (config.state == "enabled") {
123✔
1115
        return false;
123✔
1116
    }
123✔
1117
    auto state_endpoint = apps()[app_id]["sync"]["state"];
123✔
1118
    auto state_result = state_endpoint.get_json(
175✔
1119
        {{"sync_type", config.mode == ServiceConfig::SyncMode::Flexible ? "flexible" : "partition"}});
175✔
1120
    return state_result["state"].get<std::string>().empty();
175✔
1121
}
175✔
1122

175✔
1123
bool AdminAPISession::is_initial_sync_complete(const std::string& app_id) const
1124
{
123✔
1125
    auto progress_endpoint = apps()[app_id]["sync"]["progress"];
123✔
1126
    auto progress_result = progress_endpoint.get_json();
1127
    if (auto it = progress_result.find("progress"); it != progress_result.end() && it->is_object() && !it->empty()) {
1128
        for (auto& elem : *it) {
1129
            auto is_complete = elem["complete"];
42✔
1130
            if (!is_complete.is_boolean() || !is_complete.get<bool>()) {
42✔
1131
                return false;
42✔
1132
            }
42✔
1133
        }
42✔
1134
        return true;
16✔
1135
    }
16✔
1136
    return false;
16✔
1137
}
16✔
1138

16✔
1139
AdminAPISession::MigrationStatus AdminAPISession::get_migration_status(const std::string& app_id) const
16✔
1140
{
16✔
1141
    MigrationStatus status;
16✔
1142
    auto progress_endpoint = apps()[app_id]["sync"]["migration"];
16✔
1143
    auto progress_result = progress_endpoint.get_json();
26✔
1144
    auto errorMessage = progress_result["errorMessage"];
26✔
1145
    if (errorMessage.is_string() && !errorMessage.get<std::string>().empty()) {
26✔
1146
        throw Exception(Status{ErrorCodes::RuntimeError, errorMessage.get<std::string>()});
26✔
1147
    }
26✔
1148
    if (!progress_result["statusMessage"].is_string() || !progress_result["isMigrated"].is_boolean()) {
26✔
1149
        throw Exception(
26✔
1150
            Status{ErrorCodes::RuntimeError, util::format("Invalid result returned from migration status request: %1",
26✔
1151
                                                          progress_result.dump(4, 32, true))});
26✔
1152
    }
×
UNCOV
1153

×
UNCOV
1154
    status.statusMessage = progress_result["statusMessage"].get<std::string>();
×
1155
    status.isMigrated = progress_result["isMigrated"].get<bool>();
42✔
1156
    status.isCancelable = progress_result["isCancelable"].get<bool>();
42✔
1157
    status.isRevertible = progress_result["isRevertible"].get<bool>();
1158
    status.complete = status.statusMessage.empty();
1159
    return status;
30✔
1160
}
30✔
1161

30✔
1162
AdminAPIEndpoint AdminAPISession::apps(APIFamily family) const
30✔
1163
{
30✔
1164
    switch (family) {
1165
        case APIFamily::Admin:
UNCOV
1166
            return AdminAPIEndpoint(util::format("%1/api/admin/v3.0/groups/%2/apps", m_base_url, m_group_id),
×
UNCOV
1167
                                    m_access_token);
×
1168
        case APIFamily::Private:
×
1169
            return AdminAPIEndpoint(util::format("%1/api/private/v1.0/groups/%2/apps", m_base_url, m_group_id),
×
1170
                                    m_access_token);
×
UNCOV
1171
    }
×
UNCOV
1172
    REALM_UNREACHABLE();
×
1173
}
×
UNCOV
1174

×
UNCOV
1175
realm::Schema get_default_schema()
×
UNCOV
1176
{
×
1177
    const auto dog_schema =
1178
        ObjectSchema("Dog", {realm::Property("_id", PropertyType::ObjectId | PropertyType::Nullable, true),
1179
                             realm::Property("breed", PropertyType::String | PropertyType::Nullable),
1,488✔
1180
                             realm::Property("name", PropertyType::String),
1,488✔
1181
                             realm::Property("realm_id", PropertyType::String | PropertyType::Nullable)});
1,488✔
1182
    const auto cat_schema =
1,488✔
1183
        ObjectSchema("Cat", {realm::Property("_id", PropertyType::String | PropertyType::Nullable, true),
1,677✔
1184
                             realm::Property("breed", PropertyType::String | PropertyType::Nullable),
1,677✔
1185
                             realm::Property("name", PropertyType::String),
1,677✔
1186
                             realm::Property("realm_id", PropertyType::String | PropertyType::Nullable)});
499✔
1187
    const auto person_schema =
499✔
1188
        ObjectSchema("Person", {realm::Property("_id", PropertyType::ObjectId | PropertyType::Nullable, true),
1,677✔
1189
                                realm::Property("age", PropertyType::Int),
471✔
1190
                                realm::Property("dogs", PropertyType::Object | PropertyType::Array, "Dog"),
970✔
1191
                                realm::Property("firstName", PropertyType::String),
518✔
1192
                                realm::Property("lastName", PropertyType::String),
1,488✔
1193
                                realm::Property("realm_id", PropertyType::String | PropertyType::Nullable)});
1194
    return realm::Schema({dog_schema, cat_schema, person_schema});
1195
}
737✔
1196

737✔
1197
std::string get_base_url()
737✔
1198
{
737✔
1199
    if (auto baas_url = getenv_sv("BAAS_BASE_URL"); !baas_url.empty()) {
737✔
1200
        return std::string{baas_url};
737✔
1201
    }
×
UNCOV
1202
    if (auto& baasaas_holder = BaasaasLauncher::get_baasaas_holder(); baasaas_holder.has_value()) {
×
1203
        return baasaas_holder->http_endpoint();
737✔
UNCOV
1204
    }
×
UNCOV
1205

×
1206
    return get_compile_time_base_url();
×
UNCOV
1207
}
×
1208

1209
std::string get_admin_url()
737✔
1210
{
737✔
1211
    if (auto baas_admin_url = getenv_sv("BAAS_ADMIN_URL"); !baas_admin_url.empty()) {
737✔
1212
        return std::string{baas_admin_url};
737✔
1213
    }
737✔
1214
    if (auto compile_url = get_compile_time_admin_url(); !compile_url.empty()) {
737✔
1215
        return compile_url;
737✔
1216
    }
1217

1218
    return get_base_url();
5,116✔
1219
}
5,116✔
1220

5,106✔
1221
std::string get_mongodb_server()
5,106✔
1222
{
5,106✔
1223
    if (auto baas_url = getenv_sv("BAAS_MONGO_URL"); !baas_url.empty()) {
10✔
1224
        return std::string{baas_url};
10✔
1225
    }
10✔
1226

5,116✔
1227
    if (auto& baasaas_holder = BaasaasLauncher::get_baasaas_holder(); baasaas_holder.has_value()) {
UNCOV
1228
        return baasaas_holder->mongo_endpoint();
×
1229
    }
1230
    return "mongodb://localhost:26000";
1231
}
82✔
1232

82✔
1233

82✔
1234
AppCreateConfig default_app_config()
82✔
1235
{
82✔
1236
    ObjectId id = ObjectId::gen();
82✔
1237
    std::string db_name = util::format("test_data_%1", id.to_string());
82✔
1238
    std::string app_url = get_base_url();
82✔
1239
    std::string admin_url = get_admin_url();
82✔
1240
    REALM_ASSERT(!app_url.empty());
82✔
1241
    REALM_ASSERT(!admin_url.empty());
82✔
1242

82✔
1243
    std::string update_user_data_func = util::format(R"(
82✔
1244
        exports = async function(data) {
82✔
1245
            const user = context.user;
82✔
1246
            const mongodb = context.services.get("BackingDB");
82✔
1247
            const userDataCollection = mongodb.db("%1").collection("UserData");
82✔
1248
            await userDataCollection.updateOne(
82✔
1249
                                               { "user_id": user.id },
82✔
1250
                                               { "$set": data },
82✔
1251
                                               { "upsert": true }
1252
                                               );
1253
            return true;
784✔
1254
        };
784✔
UNCOV
1255
    )",
×
UNCOV
1256
                                                     db_name);
×
1257

784✔
1258
    constexpr const char* sum_func = R"(
784✔
1259
        exports = function(...args) {
784✔
1260
            return args.reduce((a,b) => a + b, 0);
UNCOV
1261
        };
×
1262
    )";
784✔
1263

1264
    constexpr const char* confirm_func = R"(
1265
        exports = ({ token, tokenId, username }) => {
379✔
1266
            // process the confirm token, tokenId and username
379✔
UNCOV
1267
            if (username.includes("realm_tests_do_autoverify")) {
×
UNCOV
1268
              return { status: 'success' }
×
1269
            }
379✔
UNCOV
1270
            // do not confirm the user
×
UNCOV
1271
            return { status: 'fail' };
×
1272
        };
1273
    )";
379✔
1274

379✔
1275
    constexpr const char* auth_func = R"(
1276
        exports = (loginPayload) => {
1277
            return loginPayload["realmCustomAuthFuncUserId"];
379✔
1278
        };
379✔
UNCOV
1279
    )";
×
UNCOV
1280

×
1281
    constexpr const char* reset_func = R"(
1282
        exports = ({ token, tokenId, username, password }) => {
379✔
1283
            // process the reset token, tokenId, username and password
379✔
1284
            if (password.includes("realm_tests_do_reset")) {
379✔
UNCOV
1285
              return { status: 'success' };
×
1286
            }
379✔
1287
            // will not reset the password
1288
            return { status: 'fail' };
1289
        };
1290
    )";
14✔
1291

14✔
1292
    std::vector<AppCreateConfig::FunctionDef> funcs = {
14✔
1293
        {"updateUserData", update_user_data_func, false},
14✔
1294
        {"sumFunc", sum_func, false},
14✔
1295
        {"confirmFunc", confirm_func, false},
14✔
1296
        {"authFunc", auth_func, false},
14✔
1297
        {"resetFunc", reset_func, false},
1298
    };
14✔
1299

14✔
1300
    Property partition_key("realm_id", PropertyType::String | PropertyType::Nullable);
14✔
1301

14✔
1302
    AppCreateConfig::UserPassAuthConfig user_pass_config{
14✔
1303
        false,
14✔
1304
        "",
14✔
1305
        "confirmFunc",
14✔
1306
        "http://localhost/confirmEmail",
14✔
1307
        "resetFunc",
14✔
1308
        "",
14✔
1309
        "http://localhost/resetPassword",
14✔
1310
        true,
14✔
1311
        true,
14✔
1312
    };
1313

14✔
1314
    return AppCreateConfig{
14✔
1315
        "test",
14✔
1316
        std::move(app_url),
14✔
1317
        std::move(admin_url), // BAAS Admin API URL may be different
14✔
1318
        "unique_user@domain.com",
1319
        "password",
14✔
1320
        get_mongodb_server(),
14✔
1321
        db_name,
14✔
1322
        get_default_schema(),
14✔
1323
        std::move(partition_key),
14✔
1324
        false,                              // Dev mode disabled
14✔
1325
        util::none,                         // Default to no FLX sync config
14✔
1326
        std::move(funcs),                   // Add default functions
14✔
1327
        std::move(user_pass_config),        // enable basic user/pass auth
14✔
1328
        std::string{"authFunc"},            // custom auth function
14✔
1329
        true,                               // enable_api_key_auth
1330
        true,                               // enable_anonymous_auth
14✔
1331
        true,                               // enable_custom_token_auth
14✔
1332
        {},                                 // no service roles on the default rule
14✔
1333
        util::Logger::get_default_logger(), // provide the logger to the admin api
14✔
1334
    };
14✔
1335
}
1336

14✔
1337
AppCreateConfig minimal_app_config(const std::string& name, const Schema& schema)
14✔
1338
{
14✔
1339
    Property partition_key("realm_id", PropertyType::String | PropertyType::Nullable);
14✔
1340
    std::string app_url = get_base_url();
14✔
1341
    std::string admin_url = get_admin_url();
14✔
1342
    REALM_ASSERT(!app_url.empty());
14✔
1343
    REALM_ASSERT(!admin_url.empty());
14✔
1344

14✔
1345
    AppCreateConfig::UserPassAuthConfig user_pass_config{
14✔
1346
        true,  "Confirm", "", "http://example.com/confirmEmail", "", "Reset", "http://exmaple.com/resetPassword",
1347
        false, false,
14✔
1348
    };
14✔
1349

14✔
1350
    ObjectId id = ObjectId::gen();
14✔
1351
    return AppCreateConfig{
14✔
1352
        name,
14✔
1353
        std::move(app_url),
14✔
1354
        std::move(admin_url), // BAAS Admin API URL may be different
1355
        "unique_user@domain.com",
14✔
1356
        "password",
1357
        get_mongodb_server(),
14✔
1358
        util::format("test_data_%1_%2", name, id.to_string()),
14✔
1359
        schema,
14✔
1360
        std::move(partition_key),
14✔
1361
        false,                              // Dev mode disabled
14✔
1362
        util::none,                         // no FLX sync config
14✔
1363
        {},                                 // no functions
14✔
1364
        std::move(user_pass_config),        // enable basic user/pass auth
14✔
1365
        util::none,                         // disable custom auth
14✔
1366
        true,                               // enable api key auth
14✔
1367
        true,                               // enable anonymous auth
14✔
1368
        false,                              // enable_custom_token_auth
1369
        {},                                 // no service roles on the default rule
14✔
1370
        util::Logger::get_default_logger(), // provide the logger to the admin api
14✔
1371
    };
14✔
1372
}
14✔
1373

14✔
1374
AppSession create_app(const AppCreateConfig& config)
14✔
1375
{
14✔
1376
    auto session = AdminAPISession::login(config);
14✔
1377
    auto create_app_resp = session.apps().post_json(nlohmann::json{{"name", config.app_name}});
14✔
1378
    std::string app_id = create_app_resp["_id"];
14✔
1379
    std::string client_app_id = create_app_resp["client_app_id"];
14✔
1380

14✔
1381
    auto app = session.apps()[app_id];
14✔
1382

14✔
1383
    auto functions = app["functions"];
14✔
1384
    std::unordered_map<std::string, std::string> function_name_to_id;
14✔
1385
    for (const auto& func : config.functions) {
14✔
1386
        auto create_func_resp = functions.post_json({
14✔
1387
            {"name", func.name},
14✔
1388
            {"private", func.is_private},
14✔
1389
            {"can_evaluate", nlohmann::json::object()},
14✔
1390
            {"source", func.source},
14✔
1391
        });
1392
        function_name_to_id.insert({func.name, create_func_resp["_id"]});
1393
    }
365✔
1394

365✔
1395
    auto auth_providers = app["auth_providers"];
365✔
1396
    if (config.enable_anonymous_auth) {
365✔
1397
        auth_providers.post_json({{"type", "anon-user"}});
365✔
1398
    }
365✔
1399
    if (config.user_pass_auth) {
1400
        auto user_pass_config_obj = nlohmann::json{
365✔
1401
            {"autoConfirm", config.user_pass_auth->auto_confirm},
365✔
1402
            {"confirmEmailSubject", config.user_pass_auth->confirm_email_subject},
365✔
1403
            {"emailConfirmationUrl", config.user_pass_auth->email_confirmation_url},
365✔
1404
            {"resetPasswordSubject", config.user_pass_auth->reset_password_subject},
1405
            {"resetPasswordUrl", config.user_pass_auth->reset_password_url},
365✔
1406
        };
365✔
1407
        if (!config.user_pass_auth->confirmation_function_name.empty()) {
365✔
1408
            const auto& confirm_func_name = config.user_pass_auth->confirmation_function_name;
365✔
1409
            user_pass_config_obj.emplace("confirmationFunctionName", confirm_func_name);
365✔
1410
            user_pass_config_obj.emplace("confirmationFunctionId", function_name_to_id[confirm_func_name]);
365✔
1411
            user_pass_config_obj.emplace("runConfirmationFunction", config.user_pass_auth->run_confirmation_function);
365✔
1412
        }
365✔
1413
        if (!config.user_pass_auth->reset_function_name.empty()) {
365✔
1414
            const auto& reset_func_name = config.user_pass_auth->reset_function_name;
365✔
1415
            user_pass_config_obj.emplace("resetFunctionName", reset_func_name);
365✔
1416
            user_pass_config_obj.emplace("resetFunctionId", function_name_to_id[reset_func_name]);
365✔
1417
            user_pass_config_obj.emplace("runResetFunction", config.user_pass_auth->run_reset_function);
365✔
1418
        }
365✔
1419
        auth_providers.post_json({{"type", "local-userpass"}, {"config", std::move(user_pass_config_obj)}});
365✔
1420
    }
365✔
1421
    if (config.custom_function_auth) {
365✔
1422
        auth_providers.post_json({{"type", "custom-function"},
365✔
1423
                                  {"config",
365✔
1424
                                   {
365✔
1425
                                       {"authFunctionName", *config.custom_function_auth},
365✔
1426
                                       {"authFunctionId", function_name_to_id[*config.custom_function_auth]},
365✔
1427
                                   }}});
365✔
1428
    }
1429

1430
    if (config.enable_api_key_auth) {
389✔
1431
        auto all_auth_providers = auth_providers.get_json();
389✔
1432
        auto api_key_provider =
389✔
1433
            std::find_if(all_auth_providers.begin(), all_auth_providers.end(), [](const nlohmann::json& provider) {
389✔
1434
                return provider["type"] == "api-key";
389✔
1435
            });
389✔
1436
        REALM_ASSERT(api_key_provider != all_auth_providers.end());
389✔
1437
        std::string api_key_provider_id = (*api_key_provider)["_id"];
389✔
1438
        auto api_key_enable_resp = auth_providers[api_key_provider_id]["enable"].put("");
389✔
1439
        REALM_ASSERT(api_key_enable_resp.http_status_code >= 200 && api_key_enable_resp.http_status_code < 300);
389✔
1440
    }
389✔
1441

389✔
1442
    auto secrets = app["secrets"];
389✔
1443
    secrets.post_json({{"name", "BackingDB_uri"}, {"value", config.mongo_uri}});
389✔
1444
    secrets.post_json({{"name", "gcm"}, {"value", "gcm"}});
389✔
1445
    secrets.post_json({{"name", "customTokenKey"}, {"value", "My_very_confidential_secretttttt"}});
1446

1447
    if (config.enable_custom_token_auth) {
381✔
1448
        auth_providers.post_json(
381✔
1449
            {{"type", "custom-token"},
381✔
1450
             {"config",
381✔
1451
              {
381✔
1452
                  {"audience", nlohmann::json::array()},
1453
                  {"signingAlgorithm", "HS256"},
381✔
1454
                  {"useJWKURI", false},
1455
              }},
381✔
1456
             {"secret_config", {{"signingKeys", nlohmann::json::array({"customTokenKey"})}}},
381✔
1457
             {"disabled", false},
381✔
1458
             {"metadata_fields",
80✔
1459
              {{{"required", false}, {"name", "user_data.name"}, {"field_name", "name"}},
80✔
1460
               {{"required", true}, {"name", "user_data.occupation"}, {"field_name", "occupation"}},
80✔
1461
               {{"required", true}, {"name", "my_metadata.name"}, {"field_name", "anotherName"}}}}});
80✔
1462
    }
80✔
1463

80✔
1464
    auto services = app["services"];
80✔
1465
    static const std::string mongo_service_name = "BackingDB";
80✔
1466

1467
    nlohmann::json mongo_service_def = {
381✔
1468
        {"name", mongo_service_name},
381✔
1469
        {"type", "mongodb"},
381✔
1470
        {"config", {{"uri", config.mongo_uri}}},
381✔
1471
    };
381✔
1472
    nlohmann::json sync_config;
381✔
1473
    if (config.flx_sync_config) {
381✔
1474
        auto queryable_fields = nlohmann::json::array();
381✔
1475
        const auto& queryable_fields_src = config.flx_sync_config->queryable_fields;
381✔
1476
        std::copy(queryable_fields_src.begin(), queryable_fields_src.end(), std::back_inserter(queryable_fields));
381✔
1477
        auto asymmetric_tables = nlohmann::json::array();
381✔
1478
        for (const auto& obj_schema : config.schema) {
381✔
1479
            if (obj_schema.table_type == ObjectSchema::ObjectType::TopLevelAsymmetric) {
381✔
1480
                asymmetric_tables.emplace_back(obj_schema.name);
16✔
1481
            }
16✔
1482
        }
16✔
1483
        sync_config = nlohmann::json{{"database_name", config.mongo_dbname},
16✔
1484
                                     {"queryable_fields_names", queryable_fields},
16✔
1485
                                     {"asymmetric_tables", asymmetric_tables}};
381✔
1486
        mongo_service_def["config"]["flexible_sync"] = sync_config;
16✔
1487
    }
16✔
1488
    else {
16✔
1489
        sync_config = nlohmann::json{
16✔
1490
            {"database_name", config.mongo_dbname},
16✔
1491
            {"partition",
381✔
1492
             {
381✔
1493
                 {"key", config.partition_key.name},
381✔
1494
                 {"type", property_type_to_bson_type_str(config.partition_key.type)},
16✔
1495
                 {"required", !is_nullable(config.partition_key.type)},
16✔
1496
                 {"permissions",
16✔
1497
                  {
16✔
1498
                      {"read", true},
16✔
1499
                      {"write", true},
16✔
1500
                  }},
16✔
1501
             }},
1502
        };
381✔
1503
        mongo_service_def["config"]["sync"] = sync_config;
381✔
1504
    }
381✔
1505

381✔
1506
    auto create_mongo_service_resp = services.post_json(std::move(mongo_service_def));
381✔
1507
    std::string mongo_service_id = create_mongo_service_resp["_id"];
381✔
1508

381✔
1509
    auto default_rule = services[mongo_service_id]["default_rule"];
381✔
1510
    auto service_roles = nlohmann::json::array();
381✔
1511
    if (config.service_roles.empty()) {
381✔
1512
        service_roles = nlohmann::json::array({{{"name", "default"},
381✔
1513
                                                {"apply_when", nlohmann::json::object()},
1514
                                                {"document_filters",
381✔
1515
                                                 {
381✔
1516
                                                     {"read", true},
381✔
1517
                                                     {"write", true},
381✔
1518
                                                 }},
1519
                                                {"read", true},
381✔
1520
                                                {"write", true},
16✔
1521
                                                {"insert", true},
16✔
1522
                                                {"delete", true}}});
16✔
1523
    }
16✔
1524
    else {
16✔
1525
        std::transform(config.service_roles.begin(), config.service_roles.end(), std::back_inserter(service_roles),
16✔
1526
                       [](const AppCreateConfig::ServiceRole& role_def) {
16✔
1527
                           nlohmann::json ret{
16✔
1528
                               {"name", role_def.name},
16✔
1529
                               {"apply_when", role_def.apply_when},
16✔
1530
                               {"document_filters",
16✔
1531
                                {
16✔
1532
                                    {"read", role_def.document_filters.read},
16✔
1533
                                    {"write", role_def.document_filters.write},
16✔
1534
                                }},
16✔
1535
                               {"insert", role_def.insert_filter},
1536
                               {"delete", role_def.delete_filter},
381✔
1537
                               {"read", role_def.read},
381✔
1538
                               {"write", role_def.write},
1539
                           };
381✔
1540
                           return ret;
381✔
1541
                       });
381✔
1542
    }
381✔
1543

381✔
1544
    default_rule.post_json({{"roles", service_roles}});
381✔
1545

381✔
1546
    // No need for a draft because there are no breaking changes in the initial schema when the app is created.
197✔
1547
    bool use_draft = false;
197✔
1548
    session.create_schema(app_id, config, use_draft);
197✔
1549

197✔
1550
    // Enable sync after schema is created.
394✔
1551
    AdminAPISession::ServiceConfig service_config;
394✔
1552
    service_config.database_name = sync_config["database_name"];
4✔
1553
    if (config.flx_sync_config) {
4✔
1554
        service_config.mode = AdminAPISession::ServiceConfig::SyncMode::Flexible;
394✔
1555
        service_config.queryable_field_names = sync_config["queryable_fields_names"];
197✔
1556
        service_config.asymmetric_tables = sync_config["asymmetric_tables"];
197✔
1557
    }
197✔
1558
    else {
197✔
1559
        service_config.mode = AdminAPISession::ServiceConfig::SyncMode::Partitioned;
197✔
1560
        service_config.partition = sync_config["partition"];
184✔
1561
    }
184✔
1562
    session.enable_sync(app_id, mongo_service_id, service_config);
184✔
1563

184✔
1564
    app["sync"]["config"].put_json({{"development_mode_enabled", config.dev_mode_enabled}});
184✔
1565

184✔
1566
    auto rules = services[mongo_service_id]["rules"];
184✔
1567
    rules.post_json({
184✔
1568
        {"database", config.mongo_dbname},
184✔
1569
        {"collection", "UserData"},
184✔
1570
        {"roles",
184✔
1571
         {{{"name", "default"},
184✔
1572
           {"apply_when", nlohmann::json::object()},
184✔
1573
           {"document_filters",
184✔
1574
            {
184✔
1575
                {"read", true},
184✔
1576
                {"write", true},
184✔
1577
            }},
1578
           {"read", true},
381✔
1579
           {"write", true},
381✔
1580
           {"insert", true},
1581
           {"delete", true}}}},
381✔
1582
    });
381✔
1583

381✔
1584
    app["custom_user_data"].patch_json({
375✔
1585
        {"mongo_service_id", mongo_service_id},
375✔
1586
        {"enabled", true},
6✔
1587
        {"database_name", config.mongo_dbname},
6✔
1588
        {"collection_name", "UserData"},
6✔
1589
        {"user_id_field", "user_id"},
6✔
1590
    });
1591

381✔
1592
    services.post_json({
1593
        {"name", "gcm"},
1594
        {"type", "gcm"},
381✔
1595
        {"config",
381✔
1596
         {
1597
             {"senderId", "gcm"},
1598
         }},
381✔
1599
        {"secret_config",
381✔
1600
         {
381✔
1601
             {"apiKey", "gcm"},
197✔
1602
         }},
197✔
1603
        {"version", 1},
197✔
1604
    });
197✔
1605

184✔
1606
    // Wait for initial sync to complete, as connecting while this is happening
184✔
1607
    // causes various problems
184✔
1608
    bool any_sync_types = std::any_of(config.schema.begin(), config.schema.end(), [](auto& object_schema) {
184✔
1609
        return object_schema.table_type == ObjectSchema::ObjectType::TopLevel;
381✔
1610
    });
1611
    if (any_sync_types) {
381✔
1612
        timed_sleeping_wait_for(
1613
            [&] {
381✔
1614
                return session.is_initial_sync_complete(app_id);
381✔
1615
            },
381✔
1616
            std::chrono::seconds(30), std::chrono::seconds(1));
381✔
1617
    }
381✔
1618

381✔
1619
    return {client_app_id, app_id, session, config};
381✔
1620
}
381✔
1621

381✔
1622
AppSession get_runtime_app_session()
381✔
1623
{
381✔
1624
    static const AppSession cached_app_session = [&] {
381✔
1625
        auto cached_app_session = create_app(default_app_config());
381✔
1626
        return cached_app_session;
381✔
1627
    }();
381✔
1628
    return cached_app_session;
381✔
1629
}
381✔
1630

1631

381✔
1632
#ifdef REALM_MONGODB_ENDPOINT
381✔
1633
TEST_CASE("app: baas admin api", "[sync][app][admin api][baas]") {
381✔
1634
    SECTION("embedded objects") {
381✔
1635
        Schema schema{{"top",
381✔
1636
                       {{"_id", PropertyType::String, true},
381✔
1637
                        {"location", PropertyType::Object | PropertyType::Nullable, "location"}}},
381✔
1638
                      {"location",
1639
                       ObjectSchema::ObjectType::Embedded,
381✔
1640
                       {{"coordinates", PropertyType::Double | PropertyType::Array}}}};
381✔
1641

381✔
1642
        auto test_app_config = minimal_app_config("test", schema);
381✔
1643
        create_app(test_app_config);
381✔
1644
    }
381✔
1645

381✔
1646
    SECTION("embedded object with array") {
381✔
1647
        Schema schema{
381✔
1648
            {"a",
381✔
1649
             {{"_id", PropertyType::String, true},
381✔
1650
              {"b_link", PropertyType::Object | PropertyType::Array | PropertyType::Nullable, "b"}}},
381✔
1651
            {"b",
381✔
1652
             ObjectSchema::ObjectType::Embedded,
1653
             {{"c_link", PropertyType::Object | PropertyType::Nullable, "c"}}},
1654
            {"c", {{"_id", PropertyType::String, true}, {"d_str", PropertyType::String}}},
1655
        };
433✔
1656
        auto test_app_config = minimal_app_config("test", schema);
433✔
1657
        create_app(test_app_config);
433✔
1658
    }
381✔
1659

1660
    SECTION("dictionaries") {
375✔
1661
        Schema schema{
1,392✔
1662
            {"a", {{"_id", PropertyType::String, true}, {"b_dict", PropertyType::Dictionary | PropertyType::String}}},
1,392✔
1663
        };
1,392✔
1664

375✔
1665
        auto test_app_config = minimal_app_config("test", schema);
375✔
1666
        create_app(test_app_config);
1667
    }
381✔
1668

381✔
1669
    SECTION("set") {
1670
        Schema schema{
1671
            {"a", {{"_id", PropertyType::String, true}, {"b_dict", PropertyType::Set | PropertyType::String}}},
162✔
1672
        };
162✔
1673

2✔
1674
        auto test_app_config = minimal_app_config("test", schema);
2✔
1675
        create_app(test_app_config);
2✔
1676
    }
162✔
1677
}
162✔
1678
#endif
1679

1680
} // namespace realm
1681

1682
#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