Skip to content

Commit

Permalink
Simplify session tests by consitently using TestSyncManager::fake_user()
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed Feb 16, 2024
1 parent 33dfd8a commit 60d6c33
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 199 deletions.
6 changes: 3 additions & 3 deletions test/object-store/audit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ util::Optional<std::string> to_optional_string(StringData sd)
std::vector<AuditEvent> get_audit_events(TestSyncManager& manager, bool parse_events = true)
{
// Wait for all sessions to be fully uploaded and then tear them down
auto sync_manager = manager.app()->sync_manager();
auto sync_manager = manager.sync_manager();
REALM_ASSERT(sync_manager);
auto sessions = sync_manager->get_all_sessions();
for (auto& session : sessions) {
Expand Down Expand Up @@ -1572,7 +1572,7 @@ TEST_CASE("audit realm sharding", "[sync][pbs][audit]") {
auto close_all_sessions = [&] {
realm->close();
realm = nullptr;
auto sync_manager = test_session.app()->sync_manager();
auto sync_manager = test_session.sync_manager();
for (auto& session : sync_manager->get_all_sessions()) {
session->shutdown_and_wait();
}
Expand Down Expand Up @@ -1785,7 +1785,7 @@ TEST_CASE("audit integration tests", "[sync][pbs][audit][baas]") {
auto audit_user = session.app()->current_user();
config.audit_config->audit_user = audit_user;
auto realm = Realm::get_shared_realm(config);
session.app()->sync_manager()->remove_user(audit_user->identity());
session.sync_manager()->remove_user(audit_user->identity());

auto audit = realm->audit_context();
auto scope = audit->begin_scope("scope");
Expand Down
28 changes: 9 additions & 19 deletions test/object-store/sync/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3083,7 +3083,7 @@ TEST_CASE("app: sync integration", "[sync][pbs][app][baas]") {
REQUIRE(!wait_for_download(*r));

SECTION("Valid websocket redirect") {
auto sync_manager = test_session.app()->sync_manager();
auto sync_manager = test_session.sync_manager();
auto sync_session = sync_manager->get_existing_session(r->config().path);
sync_session->pause();

Expand Down Expand Up @@ -3150,7 +3150,7 @@ TEST_CASE("app: sync integration", "[sync][pbs][app][baas]") {
REQUIRE((server_url && server_url->find(redirect_host) != std::string::npos));
}
SECTION("Websocket redirect logs out user") {
auto sync_manager = test_session.app()->sync_manager();
auto sync_manager = test_session.sync_manager();
auto sync_session = sync_manager->get_existing_session(r->config().path);
sync_session->pause();

Expand Down Expand Up @@ -3204,7 +3204,7 @@ TEST_CASE("app: sync integration", "[sync][pbs][app][baas]") {
REQUIRE(!user1->is_logged_in());
}
SECTION("Too many websocket redirects logs out user") {
auto sync_manager = test_session.app()->sync_manager();
auto sync_manager = test_session.sync_manager();
auto sync_session = sync_manager->get_existing_session(r->config().path);
sync_session->pause();

Expand Down Expand Up @@ -4846,8 +4846,7 @@ TEST_CASE("app: UserAPIKeyProviderClient unit_tests", "[sync][app][user][api key
auto app = sync_manager.app();
auto client = app->provider_client<App::UserAPIKeyProviderClient>();

std::shared_ptr<SyncUser> logged_in_user =
app->sync_manager()->get_user("userid", good_access_token, good_access_token, dummy_device_id);
auto logged_in_user = sync_manager.fake_user();
bool processed = false;
ObjectId obj_id(UnitTestTransport::api_key_id.c_str());

Expand Down Expand Up @@ -5379,13 +5378,6 @@ TEST_CASE("app: auth providers", "[sync][app][user]") {
}

TEST_CASE("app: refresh access token unit tests", "[sync][app][user][token]") {
auto setup_user = [](std::shared_ptr<App> app) {
if (app->sync_manager()->get_current_user()) {
return;
}
app->sync_manager()->get_user("a_user_id", good_access_token, good_access_token, dummy_device_id);
};

SECTION("refresh custom data happy path") {
static bool session_route_hit = false;

Expand All @@ -5406,10 +5398,10 @@ TEST_CASE("app: refresh access token unit tests", "[sync][app][user][token]") {

TestSyncManager sync_manager(get_config(instance_of<transport>));
auto app = sync_manager.app();
setup_user(app);
auto user = sync_manager.fake_user();

bool processed = false;
app->refresh_custom_data(app->sync_manager()->get_current_user(), [&](const Optional<AppError>& error) {
app->refresh_custom_data(user, [&](const Optional<AppError>& error) {
REQUIRE_FALSE(error);
CHECK(session_route_hit);
processed = true;
Expand Down Expand Up @@ -5437,10 +5429,10 @@ TEST_CASE("app: refresh access token unit tests", "[sync][app][user][token]") {

TestSyncManager sync_manager(get_config(instance_of<transport>));
auto app = sync_manager.app();
setup_user(app);
auto user = sync_manager.fake_user();

bool processed = false;
app->refresh_custom_data(app->sync_manager()->get_current_user(), [&](const Optional<AppError>& error) {
app->refresh_custom_data(user, [&](const Optional<AppError>& error) {
CHECK(error->reason() == "malformed JWT");
CHECK(error->code() == ErrorCodes::BadToken);
CHECK(session_route_hit);
Expand Down Expand Up @@ -5515,9 +5507,7 @@ TEST_CASE("app: refresh access token unit tests", "[sync][app][user][token]") {
};

TestSyncManager sync_manager(get_config(instance_of<transport>));
auto app = sync_manager.app();
setup_user(app);
REQUIRE(log_in(app));
REQUIRE(log_in(sync_manager.app()));
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/object-store/sync/client_reset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ TEST_CASE("sync: client reset", "[sync][pbs][client reset][baas]") {
recovery_path = recovery_path_it->second;
REQUIRE(util::File::exists(orig_path));
REQUIRE(!util::File::exists(recovery_path));
bool did_reset_files = test_app_session.app()->sync_manager()->immediately_run_file_actions(orig_path);
bool did_reset_files = test_app_session.sync_manager()->immediately_run_file_actions(orig_path);
REQUIRE(did_reset_files);
REQUIRE(!util::File::exists(orig_path));
REQUIRE(util::File::exists(recovery_path));
Expand Down Expand Up @@ -953,7 +953,7 @@ TEST_CASE("sync: client reset", "[sync][pbs][client reset][baas]") {
auto realm = Realm::get_shared_realm(temp_config);
wait_for_upload(*realm);

session = test_app_session.app()->sync_manager()->get_existing_session(temp_config.path);
session = test_app_session.sync_manager()->get_existing_session(temp_config.path);
REQUIRE(session);
}
sync::SessionErrorInfo synthetic(Status{ErrorCodes::SyncClientResetRequired, "A fake client reset error"},
Expand Down Expand Up @@ -1005,7 +1005,7 @@ TEST_CASE("sync: client reset", "[sync][pbs][client reset][baas]") {
},
std::chrono::seconds(20));
}
auto session = test_app_session.app()->sync_manager()->get_existing_session(local_config.path);
auto session = test_app_session.sync_manager()->get_existing_session(local_config.path);
if (session) {
session->shutdown_and_wait();
}
Expand Down
30 changes: 2 additions & 28 deletions test/object-store/sync/session/connection_change_notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,15 @@
#include <util/test_utils.hpp>
#include <util/sync/session_util.hpp>

#include <realm/object-store/feature_checks.hpp>
#include <realm/object-store/object_schema.hpp>
#include <realm/object-store/object_store.hpp>
#include <realm/object-store/property.hpp>
#include <realm/object-store/schema.hpp>

#include <realm/util/scope_exit.hpp>
#include <realm/util/time.hpp>

#include <catch2/catch_all.hpp>

#include <atomic>
#include <chrono>
#include <fstream>
#ifndef _WIN32
#include <unistd.h>
#endif

using namespace realm;
using namespace realm::util;

static const std::string dummy_device_id = "123400000000000000000000";

static const std::string base_path = util::make_temp_dir() + "realm_objectstore_sync_connection_state_changes";

TEST_CASE("sync: Connection state changes", "[sync][session][connection change]") {
if (!EventLoop::has_implementation())
return;

TestSyncManager::Config config;
config.base_path = base_path;
TestSyncManager init_sync_manager(config);
auto app = init_sync_manager.app();
auto user = app->sync_manager()->get_user("user", ENCODE_FAKE_JWT("not_a_real_token"),
ENCODE_FAKE_JWT("also_not_a_real_token"), dummy_device_id);
TestSyncManager tsm;
auto user = tsm.fake_user();

SECTION("register connection change listener") {
auto session = sync_session(
Expand Down
Loading

0 comments on commit 60d6c33

Please sign in to comment.