Skip to content

replacing Auth/FSTUser by C++ auth implementation #804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Firestore/Example/Tests/Local/FSTLevelDBLocalStoreTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#import <XCTest/XCTest.h>

#import "Firestore/Source/Auth/FSTUser.h"
#import "Firestore/Source/Local/FSTLevelDB.h"

#import "Firestore/Example/Tests/Local/FSTLocalStoreTests.h"
Expand Down
5 changes: 3 additions & 2 deletions Firestore/Example/Tests/Local/FSTLevelDBMutationQueueTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
#include <leveldb/db.h>

#import "Firestore/Protos/objc/firestore/local/Mutation.pbobjc.h"
#import "Firestore/Source/Auth/FSTUser.h"
#import "Firestore/Source/Local/FSTLevelDB.h"
#import "Firestore/Source/Local/FSTLevelDBKey.h"
#import "Firestore/Source/Local/FSTWriteGroup.h"

#import "Firestore/Example/Tests/Local/FSTMutationQueueTests.h"
#import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"

#include "Firestore/core/src/firebase/firestore/auth/user.h"
#include "Firestore/core/src/firebase/firestore/util/ordered_code.h"

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -37,6 +37,7 @@
using leveldb::Status;
using leveldb::WriteOptions;
using Firestore::StringView;
using firebase::firestore::auth::User;
using firebase::firestore::util::OrderedCode;

// A dummy mutation value, useful for testing code that's known to examine only mutation keys.
Expand Down Expand Up @@ -69,7 +70,7 @@ @implementation FSTLevelDBMutationQueueTests {
- (void)setUp {
[super setUp];
_db = [FSTPersistenceTestHelpers levelDBPersistence];
self.mutationQueue = [_db mutationQueueForUser:[[FSTUser alloc] initWithUID:@"user"]];
self.mutationQueue = [_db mutationQueueForUser:User("user")];
self.persistence = _db;

FSTWriteGroup *group = [self.persistence startGroupWithAction:@"Start MutationQueue"];
Expand Down
9 changes: 6 additions & 3 deletions Firestore/Example/Tests/Local/FSTLocalStoreTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#import <XCTest/XCTest.h>

#import "Firestore/Source/Auth/FSTUser.h"
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTTimestamp.h"
#import "Firestore/Source/Local/FSTEagerGarbageCollector.h"
Expand All @@ -42,6 +41,10 @@
#import "Firestore/third_party/Immutable/Tests/FSTImmutableSortedDictionary+Testing.h"
#import "Firestore/third_party/Immutable/Tests/FSTImmutableSortedSet+Testing.h"

#include "Firestore/core/src/firebase/firestore/auth/user.h"

using firebase::firestore::auth::User;

NS_ASSUME_NONNULL_BEGIN

/** Creates a document version dictionary mapping the document in @a mutation to @a version. */
Expand Down Expand Up @@ -77,7 +80,7 @@ - (void)setUp {
id<FSTGarbageCollector> garbageCollector = [[FSTEagerGarbageCollector alloc] init];
self.localStore = [[FSTLocalStore alloc] initWithPersistence:persistence
garbageCollector:garbageCollector
initialUser:[FSTUser unauthenticatedUser]];
initialUser:User::Unauthenticated()];
[self.localStore start];

_batches = [NSMutableArray array];
Expand Down Expand Up @@ -112,7 +115,7 @@ - (void)restartWithNoopGarbageCollector {
id<FSTGarbageCollector> garbageCollector = [[FSTNoOpGarbageCollector alloc] init];
self.localStore = [[FSTLocalStore alloc] initWithPersistence:self.localStorePersistence
garbageCollector:garbageCollector
initialUser:[FSTUser unauthenticatedUser]];
initialUser:User::Unauthenticated()];
[self.localStore start];
}

Expand Down
8 changes: 5 additions & 3 deletions Firestore/Example/Tests/Local/FSTMemoryMutationQueueTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

#import "Firestore/Source/Local/FSTMemoryMutationQueue.h"

#import "Firestore/Source/Auth/FSTUser.h"
#import "Firestore/Source/Local/FSTMemoryPersistence.h"

#import "Firestore/Example/Tests/Local/FSTMutationQueueTests.h"
#import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"

#include "Firestore/core/src/firebase/firestore/auth/user.h"

using firebase::firestore::auth::User;

@interface FSTMemoryMutationQueueTests : FSTMutationQueueTests
@end

Expand All @@ -35,8 +38,7 @@ - (void)setUp {
[super setUp];

self.persistence = [FSTPersistenceTestHelpers memoryPersistence];
self.mutationQueue =
[self.persistence mutationQueueForUser:[[FSTUser alloc] initWithUID:@"user"]];
self.mutationQueue = [self.persistence mutationQueueForUser:User("user")];
}

@end
8 changes: 5 additions & 3 deletions Firestore/Example/Tests/Local/FSTMutationQueueTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#import "Firestore/Example/Tests/Local/FSTMutationQueueTests.h"

#import "Firestore/Source/Auth/FSTUser.h"
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTTimestamp.h"
#import "Firestore/Source/Local/FSTEagerGarbageCollector.h"
Expand All @@ -28,6 +27,10 @@

#import "Firestore/Example/Tests/Util/FSTHelpers.h"

#include "Firestore/core/src/firebase/firestore/auth/user.h"

using firebase::firestore::auth::User;

NS_ASSUME_NONNULL_BEGIN

@implementation FSTMutationQueueTests
Expand Down Expand Up @@ -127,8 +130,7 @@ - (void)testHighestAcknowledgedBatchIDNeverExceedsNextBatchID {

// Restart the queue so that nextBatchID will be reset.
[self.mutationQueue shutdown];
self.mutationQueue =
[self.persistence mutationQueueForUser:[[FSTUser alloc] initWithUID:@"user"]];
self.mutationQueue = [self.persistence mutationQueueForUser:User("user")];

FSTWriteGroup *group = [self.persistence startGroupWithAction:@"Start MutationQueue"];
[self.mutationQueue startWithGroup:group];
Expand Down
21 changes: 15 additions & 6 deletions Firestore/Example/Tests/SpecTests/FSTSpecTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#import <FirebaseFirestore/FIRFirestoreErrors.h>
#import <GRPCClient/GRPCCall.h>

#import "Firestore/Source/Auth/FSTUser.h"
#import "Firestore/Source/Core/FSTEventManager.h"
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTSnapshotVersion.h"
Expand All @@ -43,6 +42,12 @@
#import "Firestore/Example/Tests/SpecTests/FSTSyncEngineTestDriver.h"
#import "Firestore/Example/Tests/Util/FSTHelpers.h"

#include "Firestore/core/src/firebase/firestore/auth/user.h"
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"

namespace util = firebase::firestore::util;
using firebase::firestore::auth::User;

NS_ASSUME_NONNULL_BEGIN

// Disables all other tests; useful for debugging. Multiple tests can have this tag and they'll all
Expand Down Expand Up @@ -327,15 +332,19 @@ - (void)doEnableNetwork {
}

- (void)doChangeUser:(id)UID {
FSTUser *user = [UID isEqual:[NSNull null]] ? [FSTUser unauthenticatedUser]
: [[FSTUser alloc] initWithUID:UID];
[self.driver changeUser:user];
if (UID == nil || [UID isEqual:[NSNull null]]) {
[self.driver changeUser:User::Unauthenticated()];
} else {
XCTAssert([UID isKindOfClass:[NSString class]]);
[self.driver changeUser:User(UID)];
}
}

- (void)doRestart {
// Any outstanding user writes should be automatically re-sent, so we want to preserve them
// when re-creating the driver.
FSTOutstandingWriteQueues *outstandingWrites = self.driver.outstandingWrites;
FSTOutstandingWriteQueues outstandingWrites = self.driver.outstandingWrites;
User currentUser = self.driver.currentUser;

[self.driver shutdown];

Expand All @@ -347,7 +356,7 @@ - (void)doRestart {

self.driver = [[FSTSyncEngineTestDriver alloc] initWithPersistence:self.driverPersistence
garbageCollector:self.garbageCollector
initialUser:self.driver.currentUser
initialUser:currentUser
outstandingWrites:outstandingWrites];
[self.driver start];
}
Expand Down
20 changes: 13 additions & 7 deletions Firestore/Example/Tests/SpecTests/FSTSyncEngineTestDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@

#import <Foundation/Foundation.h>

#include <unordered_map>

#import "Firestore/Source/Core/FSTTypes.h"
#import "Firestore/Source/Remote/FSTRemoteStore.h"

#include "Firestore/core/src/firebase/firestore/auth/user.h"

@class FSTDocumentKey;
@class FSTMutation;
@class FSTMutationResult;
@class FSTQuery;
@class FSTQueryData;
@class FSTSnapshotVersion;
@class FSTUser;
@class FSTViewSnapshot;
@class FSTWatchChange;
@protocol FSTGarbageCollector;
Expand Down Expand Up @@ -54,7 +57,10 @@ NS_ASSUME_NONNULL_BEGIN
@end

/** Mapping of user => array of FSTMutations for that user. */
typedef NSDictionary<FSTUser *, NSArray<FSTOutstandingWrite *> *> FSTOutstandingWriteQueues;
typedef std::unordered_map<firebase::firestore::auth::User,
NSMutableArray<FSTOutstandingWrite *> *,
firebase::firestore::auth::HashUser>
FSTOutstandingWriteQueues;

/**
* A test driver for FSTSyncEngine that allows simulated event delivery and capture. As much as
Expand Down Expand Up @@ -93,8 +99,8 @@ typedef NSDictionary<FSTUser *, NSArray<FSTOutstandingWrite *> *> FSTOutstanding
*/
- (instancetype)initWithPersistence:(id<FSTPersistence>)persistence
garbageCollector:(id<FSTGarbageCollector>)garbageCollector
initialUser:(FSTUser *)initialUser
outstandingWrites:(FSTOutstandingWriteQueues *)outstandingWrites
initialUser:(const firebase::firestore::auth::User &)initialUser
outstandingWrites:(const FSTOutstandingWriteQueues &)outstandingWrites
NS_DESIGNATED_INITIALIZER;

- (instancetype)init NS_UNAVAILABLE;
Expand Down Expand Up @@ -222,7 +228,7 @@ typedef NSDictionary<FSTUser *, NSArray<FSTOutstandingWrite *> *> FSTOutstanding
* each user, so future receiveWriteAck/Error operations will validate the write sent to the mock
* datastore matches the next outstanding write for that user.
*/
- (void)changeUser:(FSTUser *)user;
- (void)changeUser:(const firebase::firestore::auth::User &)user;

/**
* Returns all query events generated by the FSTSyncEngine in response to the event injection
Expand All @@ -246,10 +252,10 @@ typedef NSDictionary<FSTUser *, NSArray<FSTOutstandingWrite *> *> FSTOutstanding
* sentWritesCount, but not necessarily, since the FSTRemoteStore limits the number of
* outstanding writes to the backend at a given time.
*/
@property(nonatomic, strong, readonly) FSTOutstandingWriteQueues *outstandingWrites;
@property(nonatomic, assign, readonly) const FSTOutstandingWriteQueues &outstandingWrites;

/** The current user for the FSTSyncEngine; determines which mutation queue is active. */
@property(nonatomic, strong, readonly) FSTUser *currentUser;
@property(nonatomic, assign, readonly) const firebase::firestore::auth::User &currentUser;

/** The current set of documents in limbo. */
@property(nonatomic, strong, readonly)
Expand Down
50 changes: 28 additions & 22 deletions Firestore/Example/Tests/SpecTests/FSTSyncEngineTestDriver.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

#import "Firestore/Example/Tests/SpecTests/FSTSyncEngineTestDriver.h"

#include <unordered_map>

#import <FirebaseFirestore/FIRFirestoreErrors.h>
#import <GRPCClient/GRPCCall.h>

#import "Firestore/Source/Auth/FSTUser.h"
#import "Firestore/Source/Core/FSTEventManager.h"
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTSnapshotVersion.h"
Expand All @@ -36,6 +37,11 @@
#import "Firestore/Example/Tests/Core/FSTSyncEngine+Testing.h"
#import "Firestore/Example/Tests/SpecTests/FSTMockDatastore.h"

#include "Firestore/core/src/firebase/firestore/auth/user.h"

using firebase::firestore::auth::HashUser;
using firebase::firestore::auth::User;

NS_ASSUME_NONNULL_BEGIN

@implementation FSTQueryEvent
Expand Down Expand Up @@ -71,35 +77,32 @@ @interface FSTSyncEngineTestDriver ()
@property(nonatomic, strong, readonly)
NSMutableDictionary<FSTQuery *, FSTQueryListener *> *queryListeners;

#pragma mark - Other data structures.
@property(nonatomic, strong, readwrite) FSTUser *currentUser;

@end

@implementation FSTSyncEngineTestDriver {
// ivar is declared as mutable.
NSMutableDictionary<FSTUser *, NSMutableArray<FSTOutstandingWrite *> *> *_outstandingWrites;
std::unordered_map<User, NSMutableArray<FSTOutstandingWrite *> *, HashUser> _outstandingWrites;

User _currentUser;
}

- (instancetype)initWithPersistence:(id<FSTPersistence>)persistence
garbageCollector:(id<FSTGarbageCollector>)garbageCollector {
return [self initWithPersistence:persistence
garbageCollector:garbageCollector
initialUser:[FSTUser unauthenticatedUser]
outstandingWrites:@{}];
initialUser:User::Unauthenticated()
outstandingWrites:{}];
}

- (instancetype)initWithPersistence:(id<FSTPersistence>)persistence
garbageCollector:(id<FSTGarbageCollector>)garbageCollector
initialUser:(FSTUser *)initialUser
outstandingWrites:(FSTOutstandingWriteQueues *)outstandingWrites {
initialUser:(const User &)initialUser
outstandingWrites:(const FSTOutstandingWriteQueues &)outstandingWrites {
if (self = [super init]) {
// Create mutable copy of outstandingWrites.
_outstandingWrites = [NSMutableDictionary dictionary];
[outstandingWrites enumerateKeysAndObjectsUsingBlock:^(
FSTUser *user, NSArray<FSTOutstandingWrite *> *writes, BOOL *stop) {
_outstandingWrites[user] = [writes mutableCopy];
}];
// Do a deep copy.
for (const auto &pair : outstandingWrites) {
_outstandingWrites[pair.first] = [pair.second mutableCopy];
}

_events = [NSMutableArray array];

Expand Down Expand Up @@ -139,9 +142,12 @@ - (instancetype)initWithPersistence:(id<FSTPersistence>)persistence
return self;
}

- (NSDictionary<FSTUser *, NSMutableArray<FSTOutstandingWrite *> *> *)outstandingWrites {
return static_cast<NSDictionary<FSTUser *, NSMutableArray<FSTOutstandingWrite *> *> *>(
_outstandingWrites);
- (const FSTOutstandingWriteQueues &)outstandingWrites {
return _outstandingWrites;
}

- (const User &)currentUser {
return _currentUser;
}

- (void)applyChangedOnlineState:(FSTOnlineState)onlineState {
Expand Down Expand Up @@ -200,8 +206,8 @@ - (void)enableNetwork {
[self.remoteStore enableNetwork];
}

- (void)changeUser:(FSTUser *)user {
self.currentUser = user;
- (void)changeUser:(const User &)user {
_currentUser = user;
[self.syncEngine userDidChange:user];
}

Expand Down Expand Up @@ -309,10 +315,10 @@ - (void)receiveWatchStreamError:(int)errorCode userInfo:(NSDictionary<NSString *
#pragma mark - Helper Methods

- (NSMutableArray<FSTOutstandingWrite *> *)currentOutstandingWrites {
NSMutableArray<FSTOutstandingWrite *> *writes = _outstandingWrites[self.currentUser];
NSMutableArray<FSTOutstandingWrite *> *writes = _outstandingWrites[_currentUser];
if (!writes) {
writes = [NSMutableArray array];
_outstandingWrites[self.currentUser] = writes;
_outstandingWrites[_currentUser] = writes;
}
return writes;
}
Expand Down
2 changes: 1 addition & 1 deletion Firestore/Source/API/FIRFirestore.mm
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

@interface FIRFirestore () {
/** The actual owned DatabaseId instance is allocated in FIRFirestore. */
firebase::firestore::model::DatabaseId _databaseID;
DatabaseId _databaseID;
}

@property(nonatomic, strong) NSString *persistenceKey;
Expand Down
Loading