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

realm / realm-core / jorgen.edelbo_333

01 Jul 2024 07:21AM UTC coverage: 90.865% (-0.08%) from 90.948%
jorgen.edelbo_333

Pull #7826

Evergreen

jedelbo
Merge tag 'v14.10.2' into next-major
Pull Request #7826: Merge Next major

102912 of 181138 branches covered (56.81%)

3131 of 3738 new or added lines in 54 files covered. (83.76%)

80 existing lines in 14 files now uncovered.

217498 of 239364 relevant lines covered (90.86%)

6655796.15 hits per line

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

84.93
/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,745✔
42
    switch (type & ~PropertyType::Flags) {
5,745✔
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,008✔
64
            return "objectId";
2,008✔
65
        case PropertyType::String:
2,373✔
66
            return "string";
2,373✔
67
        case PropertyType::LinkingObjects:
✔
68
            return "linkingObjects";
×
69
        default:
✔
70
            REALM_COMPILER_HINT_UNREACHABLE();
×
71
    }
5,745✔
72
}
5,745✔
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)
199✔
80
        , m_partition_key(partition_key)
199✔
81
        , m_mongo_service_name(service_name)
199✔
82
        , m_mongo_db_name(db_name)
199✔
83
        , m_is_flx_sync(is_flx_sync)
199✔
84
    {
415✔
85
    }
415✔
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,008✔
105
    nlohmann::json required = nlohmann::json::array();
2,008✔
106
    nlohmann::json properties = nlohmann::json::object();
2,008✔
107
    for (const auto& prop : obj_schema.persisted_properties) {
7,072✔
108
        if (include_prop && !include_prop(prop)) {
7,072✔
109
            continue;
1,543✔
110
        }
1,543✔
111
        if (clear_path) {
5,529✔
112
            m_current_path.clear();
5,411✔
113
        }
5,411✔
114
        properties.emplace(prop.name, property_to_jsonschema(prop));
5,529✔
115
        if (!is_nullable(prop.type) && !is_collection(prop.type)) {
5,529✔
116
            required.push_back(prop.name);
2,437✔
117
        }
2,437✔
118
    }
5,529✔
119

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

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

131
    if ((prop.type & ~PropertyType::Flags) == PropertyType::Object) {
5,673✔
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,111✔
168
        type_output = {{"bsonType", property_type_to_bson_type_str(prop.type)}};
5,111✔
169
    }
5,111✔
170

171
    if (is_array(prop.type)) {
5,673✔
172
        return nlohmann::json{{"bsonType", "array"}, {"items", type_output}};
274✔
173
    }
274✔
174
    if (is_set(prop.type)) {
5,399✔
175
        return nlohmann::json{{"bsonType", "array"}, {"uniqueItems", true}, {"items", type_output}};
4✔
176
    }
4✔
177
    if (is_dictionary(prop.type)) {
5,395✔
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,339✔
184
    return type_output;
5,339✔
185
}
5,395✔
186

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

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

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

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

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

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

277
    return {};
1,534✔
278
}
1,540✔
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,464✔
286
    CurlGlobalGuard curl_global_guard;
16,464✔
287
    auto curl = curl_easy_init();
16,464✔
288
    if (!curl) {
16,464✔
289
        return app::Response{500, -1};
×
290
    }
×
291

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

298
    std::string response;
16,464✔
299
    app::HttpHeaders response_headers;
16,464✔
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,464✔
305

306
    /* Now specify the POST data */
307
    if (request.method == app::HttpMethod::post) {
16,464✔
308
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
8,014✔
309
    }
8,014✔
310
    else if (request.method == app::HttpMethod::put) {
8,450✔
311
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
2,004✔
312
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
2,004✔
313
    }
2,004✔
314
    else if (request.method == app::HttpMethod::patch) {
6,446✔
315
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
760✔
316
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
760✔
317
    }
760✔
318
    else if (request.method == app::HttpMethod::del) {
5,686✔
319
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
451✔
320
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.body.c_str());
451✔
321
    }
451✔
322
    else if (request.method == app::HttpMethod::patch) {
5,235✔
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,464✔
328

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

334
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
16,464✔
335
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_cb);
16,464✔
336
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
16,464✔
337
    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, curl_header_cb);
16,464✔
338
    curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response_headers);
16,464✔
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,464✔
346
    auto response_code = curl_easy_perform(curl);
16,464✔
347
    auto total_time = std::chrono::steady_clock::now() - start_time;
16,464✔
348

349
    auto logger = util::Logger::get_default_logger();
16,464✔
350
    if (response_code != CURLE_OK) {
16,464✔
UNCOV
351
        std::string message = curl_easy_strerror(response_code);
×
UNCOV
352
        logger->error("curl_easy_perform() failed when sending request to '%1' with body '%2': %3", request.url,
×
UNCOV
353
                      request.body, message);
×
354
        // Return a failing response with the CURL error as the custom code
UNCOV
355
        return {0, response_code, {}, message};
×
UNCOV
356
    }
×
357
    if (logger->would_log(util::Logger::Level::trace)) {
16,464✔
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,464✔
371
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
16,464✔
372
    return {
16,464✔
373
        http_code,
16,464✔
374
        0, // binding_response_code
16,464✔
375
        std::move(response_headers),
16,464✔
376
        std::move(response),
16,464✔
377
    };
16,464✔
378
}
16,464✔
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,151✔
448
        if (!m_http_endpoint.empty() || m_container_id.empty()) {
1,151✔
449
            return;
1,149✔
450
        }
1,149✔
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) &&
6✔
458
               m_http_endpoint.empty()) {
6✔
459
            if (http_endpoint.empty()) {
6✔
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 {
2✔
474
                app::Request baas_req;
2✔
475
                baas_req.url = util::format("%1/api/private/v1.0/version", http_endpoint);
2✔
476
                baas_req.method = app::HttpMethod::get;
2✔
477
                baas_req.headers.insert_or_assign("Content-Type", "application/json");
2✔
478
                auto baas_resp = do_http_request(baas_req);
2✔
479
                if (baas_resp.http_status_code >= 200 && baas_resp.http_status_code < 300) {
2✔
480
                    m_http_endpoint = http_endpoint;
2✔
481
                    m_mongo_endpoint = mongo_endpoint;
2✔
482
                    break;
2✔
483
                }
2✔
484
            }
2✔
485

486
            if (!logged) {
4✔
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));
4✔
491
        }
4✔
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
    {
776✔
520
        poll();
776✔
521
        return m_http_endpoint;
776✔
522
    }
776✔
523

524
    const std::string& mongo_endpoint()
525
    {
375✔
526
        poll();
375✔
527
        return m_mongo_endpoint;
375✔
528
    }
375✔
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,157✔
591
        static std::optional<Baasaas> global_baasaas = std::nullopt;
1,157✔
592
        return global_baasaas;
1,157✔
593
    }
1,157✔
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,093✔
666
    return AdminAPIEndpoint(util::format("%1/%2", m_url, name), m_access_token);
18,093✔
667
}
18,093✔
668

669
app::Response AdminAPIEndpoint::do_request(app::Request request) const
670
{
11,619✔
671
    if (request.url.find('?') == std::string::npos) {
11,619✔
672
        request.url = util::format("%1?bypass_service_change=SyncSchemaVersionIncrease", request.url);
11,619✔
673
    }
11,619✔
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,619✔
678
    request.headers["Accept"] = "application/json";
11,619✔
679
    request.headers["Authorization"] = util::format("Bearer %1", m_access_token);
11,619✔
680
    return do_http_request(std::move(request));
11,619✔
681
}
11,619✔
682

683
app::Response AdminAPIEndpoint::get(const std::vector<std::pair<std::string, std::string>>& params) const
684
{
3,602✔
685
    app::Request req;
3,602✔
686
    req.method = app::HttpMethod::get;
3,602✔
687
    std::stringstream ss;
3,602✔
688
    bool needs_and = false;
3,602✔
689
    ss << m_url;
3,602✔
690
    if (!params.empty() && m_url.find('?') != std::string::npos) {
3,602!
691
        needs_and = true;
×
692
    }
×
693
    for (const auto& param : params) {
3,602✔
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,602✔
704
    return do_request(std::move(req));
3,602✔
705
}
3,602✔
706

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

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

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

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

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

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

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

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

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

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

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

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

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

808
void AdminAPISession::revoke_user_sessions(const std::string& user_id, const std::string& app_id) const
809
{
4✔
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

815
void AdminAPISession::disable_user_sessions(const std::string& user_id, const std::string& app_id) const
816
{
2✔
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

822
void AdminAPISession::enable_user_sessions(const std::string& user_id, const std::string& app_id) const
823
{
2✔
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

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
{
22✔
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);
16✔
839
        try {
16✔
840
            // if these fields are found, then the token is valid according to the server.
841
            // if it is invalid or expired then an error response is sent.
842
            int64_t issued_at = resp_json["iat"];
16✔
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;
×
848
        }
×
849
    }
16✔
850
    return false;
6✔
851
}
22✔
852

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

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

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

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
}
×
890

891

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

902
void AdminAPISession::trigger_client_reset(const std::string& app_id, int64_t file_ident) const
903
{
158✔
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

908
void AdminAPISession::migrate_to_flx(const std::string& app_id, const std::string& service_id,
909
                                     bool migrate_to_flx) const
910
{
30✔
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

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
{
415✔
920
    static const std::string mongo_service_name = "BackingDB";
415✔
921

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

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

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

938
    // Add new tables
939

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

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

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;
415✔
955
    BaasRuleBuilder rule_builder(target_schema, config.partition_key, mongo_service_name, config.mongo_dbname,
415✔
956
                                 static_cast<bool>(config.flx_sync_config));
415✔
957
    for (const auto& obj_schema : target_schema) {
1,006✔
958
        auto it = current_schema_tables.find(obj_schema.name);
1,006✔
959
        if (it != current_schema_tables.end()) {
1,006✔
960
            object_schema_to_create.push_back({it->second, &obj_schema});
116✔
961
            continue;
116✔
962
        }
116✔
963

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

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

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

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

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

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

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

1028
AdminAPISession::ServiceConfig AdminAPISession::pause_sync(const std::string& app_id, const std::string& service_id,
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
}
×
1038

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

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

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

1069
    return ret;
127✔
1070
}
127✔
1071

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

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

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

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

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

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

1162
AdminAPIEndpoint AdminAPISession::apps(APIFamily family) const
1163
{
5,017✔
1164
    switch (family) {
5,017✔
1165
        case APIFamily::Admin:
5,017✔
1166
            return AdminAPIEndpoint(util::format("%1/api/admin/v3.0/groups/%2/apps", m_base_url, m_group_id),
5,017✔
1167
                                    m_access_token);
5,017✔
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);
×
1171
    }
5,017✔
1172
    REALM_UNREACHABLE();
1173
}
×
1174

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

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

1206
    return get_compile_time_base_url();
×
1207
}
776✔
1208

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

1218
    return get_base_url();
375✔
1219
}
375✔
1220

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

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

1233

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

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

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

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

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

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

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},
14✔
1298
    };
14✔
1299

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

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
    };
14✔
1313

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",
14✔
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
14✔
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
}
14✔
1336

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1631

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

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

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

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

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

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

1674
        auto test_app_config = minimal_app_config("test", schema);
1675
        create_app(test_app_config);
1676
    }
1677
}
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