Skip to content

Commit 4dd33d5

Browse files
committed
Update Manage API Keys examples
1 parent 886934f commit 4dd33d5

16 files changed

+551
-195
lines changed

examples/ios/Examples/AccessMongoDB.swift

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,7 @@
11
import XCTest
22
import RealmSwift
33

4-
class AccessMongoDB: XCTestCase {
5-
6-
override func setUp() {
7-
let expectation = XCTestExpectation(description: "logs in")
8-
app.login(credentials: Credentials.anonymous) { (user, error) in
9-
guard error == nil else {
10-
fatalError("Login failed: \(error!.localizedDescription)")
11-
}
12-
expectation.fulfill()
13-
}
14-
wait(for: [expectation], timeout: 10)
15-
}
16-
17-
override func tearDown() {
18-
let expectation = XCTestExpectation(description: "logs out")
19-
app.currentUser!.logOut { (error) in
20-
guard error == nil else {
21-
fatalError("Failed to log out: \(error!.localizedDescription)")
22-
}
23-
expectation.fulfill()
24-
}
25-
wait(for: [expectation], timeout: 10)
26-
}
27-
4+
class AccessMongoDB: AnonymouslyLoggedInTestCase {
285
func testRemoteMongoDB() {
296
let expectation = XCTestExpectation(description: "it completes")
307

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import XCTest
2+
import RealmSwift
3+
4+
// Derive from this class whenever you need to use
5+
// app.currentUser with an existing anonymous login.
6+
class AnonymouslyLoggedInTestCase: XCTestCase {
7+
override func setUp() {
8+
let expectation = XCTestExpectation(description: "logs in")
9+
app.login(credentials: Credentials.anonymous) { (user, error) in
10+
guard error == nil else {
11+
fatalError("Login failed: \(error!.localizedDescription)")
12+
}
13+
expectation.fulfill()
14+
}
15+
wait(for: [expectation], timeout: 10)
16+
}
17+
18+
override func tearDown() {
19+
let expectation = XCTestExpectation(description: "logs out")
20+
app.currentUser!.remove { (error) in
21+
guard error == nil else {
22+
fatalError("Failed to log out: \(error!.localizedDescription)")
23+
}
24+
expectation.fulfill()
25+
}
26+
wait(for: [expectation], timeout: 10)
27+
}
28+
29+
30+
}

examples/ios/Examples/Functions.swift

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,7 @@
11
import XCTest
22
import RealmSwift
33

4-
class Functions: XCTestCase {
5-
6-
override func setUp() {
7-
let expectation = XCTestExpectation(description: "logs in")
8-
app.login(credentials: Credentials.anonymous) { (user, error) in
9-
guard error == nil else {
10-
fatalError("Login failed: \(error!.localizedDescription)")
11-
}
12-
expectation.fulfill()
13-
}
14-
wait(for: [expectation], timeout: 10)
15-
}
16-
17-
override func tearDown() {
18-
let expectation = XCTestExpectation(description: "logs out")
19-
app.currentUser!.logOut { (error) in
20-
guard error == nil else {
21-
fatalError("Failed to log out: \(error!.localizedDescription)")
22-
}
23-
expectation.fulfill()
24-
}
25-
wait(for: [expectation], timeout: 10)
26-
}
4+
class Functions: AnonymouslyLoggedInTestCase {
275

286
func testCallFunction() {
297
let expectation = XCTestExpectation(description: "it completes")

examples/ios/Examples/ManageApiKeys.m

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#import <XCTest/XCTest.h>
2+
#import <Realm/Realm.h>
3+
#import "MyRealmApp.h"
4+
5+
@interface ManageApiKeys : XCTestCase
6+
7+
@end
8+
9+
@implementation ManageApiKeys
10+
11+
- (void)setUp {
12+
XCTestExpectation *expectation = [self expectationWithDescription:@"registers and logs in"];
13+
14+
RLMApp *app = [RLMApp appWithId:YOUR_REALM_APP_ID];
15+
RLMEmailPasswordAuth *client = [app emailPasswordAuth];
16+
NSString *email = @"[email protected]";
17+
NSString *password = @"123456";
18+
// User will be deleted by TestSetup after entire suite
19+
[client registerUserWithEmail:email password:password completion:^(NSError *error) {
20+
// Ignore error. May have registered on previous test.
21+
[app loginWithCredential:[RLMCredentials credentialsWithEmail:email password:password] completion:^(RLMUser *user, NSError *error) {
22+
[expectation fulfill];
23+
}];
24+
}];
25+
// :code-block-end:
26+
[self waitForExpectations:@[expectation] timeout:10.0];
27+
}
28+
29+
- (void)tearDown {
30+
XCTestExpectation *expectation = [self expectationWithDescription:@"Log out"];
31+
[[[MyRealmApp app] currentUser] logOutWithCompletion:^(NSError *error) {
32+
[expectation fulfill];
33+
}];
34+
[self waitForExpectations:@[expectation] timeout:10.0];
35+
}
36+
37+
- (void)testCreateApiKey {
38+
XCTestExpectation *expectation = [self expectationWithDescription:@"it completes"];
39+
// :code-block-start: create-api-key
40+
RLMApp *app = [RLMApp appWithId:YOUR_REALM_APP_ID];
41+
// ... log in ...
42+
RLMUser *user = [app currentUser];
43+
RLMAPIKeyAuth *client = [user apiKeysAuth];
44+
45+
// Create the API key
46+
[client createAPIKeyWithName:@"someKeyName" completion:^(RLMUserAPIKey *apiKey, NSError *error) {
47+
if (error != nil) {
48+
// ... handle Error ...
49+
} else {
50+
// ... use apiKey ...
51+
// :hide-start:
52+
[client deleteAPIKey:[apiKey objectId] completion:^(NSError *error) {
53+
[expectation fulfill];
54+
}];
55+
// :hide-end:
56+
}
57+
}];
58+
// :code-block-end:
59+
[self waitForExpectations:@[expectation] timeout:10.0];
60+
}
61+
62+
- (void)testLookUpApiKey {
63+
XCTestExpectation *fetchOneExpectation = [self expectationWithDescription:@"fetch one completes"];
64+
XCTestExpectation *fetchAllExpectation = [self expectationWithDescription:@"fetch all completes"];
65+
// :code-block-start: look-up-api-key
66+
RLMApp *app = [RLMApp appWithId:YOUR_REALM_APP_ID];
67+
// ... log in ...
68+
RLMUser *user = [app currentUser];
69+
RLMAPIKeyAuth *client = [user apiKeysAuth];
70+
71+
// Fetch API key by a specific ObjectId
72+
NSError *error = nil;
73+
RLMObjectId *objectId = [[RLMObjectId alloc] initWithString:@"someObjectId" error:&error];
74+
[client fetchAPIKey:objectId completion:^(RLMUserAPIKey *apiKey, NSError *error) {
75+
if (error != nil) {
76+
// ... handle error ...
77+
} else {
78+
// ... use apiKey ...
79+
}
80+
// :hide-start:
81+
[fetchOneExpectation fulfill];
82+
// :hide-end:
83+
}];
84+
85+
// Fetch all API keys
86+
[client fetchAPIKeysWithCompletion:^(NSArray<RLMUserAPIKey *> *keys, NSError *error) {
87+
if (error != nil) {
88+
// ... handle error ...
89+
} else {
90+
for (RLMUserAPIKey *key in keys) {
91+
// ... use key ...
92+
}
93+
}
94+
// :hide-start:
95+
[fetchAllExpectation fulfill];
96+
// :hide-end:
97+
}];
98+
// :code-block-end:
99+
[self waitForExpectations:@[fetchOneExpectation, fetchAllExpectation] timeout:10.0];
100+
}
101+
102+
- (void)testEnableDisableApiKey {
103+
XCTestExpectation *enableExpectation = [self expectationWithDescription:@"enable completes"];
104+
XCTestExpectation *disableExpectation = [self expectationWithDescription:@"disable completes"];
105+
// :code-block-start: enable-disable-api-key
106+
RLMApp *app = [RLMApp appWithId:YOUR_REALM_APP_ID];
107+
// ... log in ...
108+
RLMUser *user = [app currentUser];
109+
RLMAPIKeyAuth *client = [user apiKeysAuth];
110+
111+
// Enable the API key with specific objectId
112+
RLMObjectId *objectId = [[RLMObjectId alloc] initWithString:@"00112233445566778899aabb" error:nil];
113+
[client enableAPIKey:objectId completion:^(NSError *error) {
114+
// Handle error if any. Otherwise, enable was successful.
115+
// :hide-start:
116+
[enableExpectation fulfill];
117+
// :hide-end:
118+
}];
119+
120+
RLMUserAPIKey *apiKey;
121+
122+
// ... Get an API key ...
123+
// :hide-start:
124+
apiKey = [[RLMUserAPIKey alloc] init];
125+
// :hide-end:
126+
127+
// Disable the API key
128+
[client disableAPIKey:[apiKey objectId] completion:^(NSError *error) {
129+
// Handle error if any. Otherwise, disable was successful.
130+
// :hide-start:
131+
[disableExpectation fulfill];
132+
// :hide-end:
133+
}];
134+
// :code-block-end:
135+
[self waitForExpectations:@[enableExpectation, disableExpectation] timeout:10.0];
136+
}
137+
138+
- (void)testDeleteApiKey {
139+
XCTestExpectation *expectation = [self expectationWithDescription:@"it completes"];
140+
// :code-block-start: delete-api-key
141+
RLMApp *app = [RLMApp appWithId:YOUR_REALM_APP_ID];
142+
// ... log in ...
143+
RLMUser *user = [app currentUser];
144+
RLMAPIKeyAuth *client = [user apiKeysAuth];
145+
146+
RLMUserAPIKey *apiKey;
147+
148+
// ... Get an API key ...
149+
// :hide-start:
150+
apiKey = [[RLMUserAPIKey alloc] init];
151+
// :hide-end:
152+
153+
[client deleteAPIKey:[apiKey objectId] completion:^(NSError *error) {
154+
// Handle error if any. Otherwise, delete was successful.
155+
// :hide-start:
156+
[expectation fulfill];
157+
// :hide-end:
158+
}];
159+
// :code-block-end:
160+
[self waitForExpectations:@[expectation] timeout:10.0];
161+
}
162+
163+
@end

0 commit comments

Comments
 (0)