|
| 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