@@ -167,51 +167,28 @@ void sort_events(std::vector<AuditEvent>& events)
167167static std::vector<AuditEvent> get_audit_events_from_baas (TestAppSession& session, SyncUser& user,
168168 size_t expected_count)
169169{
170- auto & app_session = session.app_session ();
171- app::MongoClient remote_client = user.mongo_client (" BackingDB" );
172- app::MongoDatabase db = remote_client.db (app_session.config .mongo_dbname );
173- app::MongoCollection collection = db[" AuditEvent" ];
174- std::vector<AuditEvent> events;
175170 static const std::set<std::string> nonmetadata_fields = {" activity" , " event" , " data" , " realm_id" };
176171
177- timed_wait_for (
178- [&] {
179- uint64_t count = 0 ;
180- collection.count ({}, [&](uint64_t c, util::Optional<app::AppError> error) {
181- REQUIRE (!error);
182- count = c;
183- });
184- if (count < expected_count) {
185- millisleep (500 ); // slow down the number of retries
186- return false ;
187- }
188- return true ;
189- },
190- std::chrono::minutes (5 ));
191-
192- collection.find ({}, {},
193- [&](util::Optional<std::vector<bson::Bson>>&& result, util::Optional<app::AppError> error) {
194- REQUIRE (!error);
195- REQUIRE (result->size () >= expected_count);
196- events.reserve (result->size ());
197- for (auto bson : *result) {
198- auto doc = static_cast <const bson::BsonDocument&>(bson).entries ();
199- AuditEvent event;
200- event.activity = static_cast <std::string>(doc[" activity" ]);
201- event.timestamp = static_cast <Timestamp>(doc[" timestamp" ]);
202- if (auto it = doc.find (" event" ); it != doc.end () && it->second != bson::Bson ()) {
203- event.event = static_cast <std::string>(it->second );
204- }
205- if (auto it = doc.find (" data" ); it != doc.end () && it->second != bson::Bson ()) {
206- event.data = json::parse (static_cast <std::string>(it->second ));
207- }
208- for (auto & [key, value] : doc) {
209- if (value.type () == bson::Bson::Type::String && !nonmetadata_fields.count (key))
210- event.metadata .insert ({key, static_cast <std::string>(value)});
211- }
212- events.push_back (event);
213- }
214- });
172+ auto documents = session.get_documents (user, " AuditEvent" , expected_count);
173+ std::vector<AuditEvent> events;
174+ events.reserve (documents.size ());
175+ for (auto document : documents) {
176+ auto doc = document.entries ();
177+ AuditEvent event;
178+ event.activity = static_cast <std::string>(doc[" activity" ]);
179+ event.timestamp = static_cast <Timestamp>(doc[" timestamp" ]);
180+ if (auto it = doc.find (" event" ); it != doc.end () && it->second != bson::Bson ()) {
181+ event.event = static_cast <std::string>(it->second );
182+ }
183+ if (auto it = doc.find (" data" ); it != doc.end () && it->second != bson::Bson ()) {
184+ event.data = json::parse (static_cast <std::string>(it->second ));
185+ }
186+ for (auto & [key, value] : doc) {
187+ if (value.type () == bson::Bson::Type::String && !nonmetadata_fields.count (key))
188+ event.metadata .insert ({key, static_cast <std::string>(value)});
189+ }
190+ events.push_back (event);
191+ }
215192 sort_events (events);
216193 return events;
217194}
0 commit comments