Skip to content

Commit 156893d

Browse files
authored
Merge pull request #15 from rreinold/staging
Staging - Added 2 nil checks in CBUser's #getCurrentUserInfoWithError, Added 2 Unit Tests
2 parents 5fa431d + 5aadaed commit 156893d

File tree

2 files changed

+127
-5
lines changed

2 files changed

+127
-5
lines changed

CBAPITests/CBUserTests.m

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,18 @@ -(void)testGetAllUsers {
142142
/**
143143
An expired token returns an HTML Status Code 400
144144
*/
145-
-(void)testGetUserInfoWithExpiredToken {
145+
-(void)testFailedGetUserInfoWithExpiredToken {
146146

147147
XCTestExpectation* expectation = [self expectationWithDescription:@"testGetUserInfoWithExpiredToken"];
148148

149149
NSString * email = @"rob@clearblade.com";
150+
NSString* password = @"clearblade";
150151
NSString* token = @"D2UuYhlQp1H5OcWjrP4SuQ63E7RcKdNXzwI4h57KLC-YjeW74o2u2-O_mLA_sBejjogMqbHjGp_JKZLT_A==";
151152
NSDictionary* options = @{
152153
CBSettingsOptionServerAddress: PLATFORM_ADDRESS,
153154
CBSettingsOptionLoggingLevel: @(TEST_LOGGING_LEVEL),
154155
CBSettingsOptionEmail:email,
155-
CBSettingsOptionPassword:@"clearblade"};
156+
CBSettingsOptionPassword:password};
156157

157158
[ClearBlade
158159
initSettingsWithSystemKey:TOKEN_APP_KEY
@@ -187,17 +188,18 @@ -(void)testGetUserInfoWithExpiredToken {
187188
An expired token returns an HTML Status Code 400
188189
*/
189190

190-
-(void)testGetUserInfoWithInvalidToken {
191+
-(void)testFailedGetUserInfoWithInvalidToken {
191192

192193
XCTestExpectation* expectation = [self expectationWithDescription:@"testGetUserInfoWithInvalidToken"];
193194

194195
NSString * email = @"rob@clearblade.com";
196+
NSString* password = @"clearblade";
195197
NSString* token = @"Invalidtoken";
196198
NSDictionary* options = @{
197199
CBSettingsOptionServerAddress: PLATFORM_ADDRESS,
198200
CBSettingsOptionLoggingLevel: @(TEST_LOGGING_LEVEL),
199201
CBSettingsOptionEmail:email,
200-
CBSettingsOptionPassword:@"clearblade"};
202+
CBSettingsOptionPassword:password};
201203

202204
[ClearBlade
203205
initSettingsWithSystemKey:TOKEN_APP_KEY
@@ -228,6 +230,109 @@ -(void)testGetUserInfoWithInvalidToken {
228230

229231
[self waitForExpectationsWithTimeout:10.0 handler:nil];
230232
}
233+
234+
/**
235+
A nil token returns an Status Code 400. If ClearBlade is init'd before use, an auth token
236+
should never be nil.
237+
*/
238+
239+
-(void)testFailedGetUserInfoWithNilToken {
240+
241+
XCTestExpectation* expectation = [self expectationWithDescription:@"testGetUserInfoWithNilToken"];
242+
243+
NSString * email = @"rob@clearblade.com";
244+
NSString* password = @"clearblade";
245+
NSString* token = nil;
246+
NSDictionary* options = @{
247+
CBSettingsOptionServerAddress: PLATFORM_ADDRESS,
248+
CBSettingsOptionLoggingLevel: @(TEST_LOGGING_LEVEL),
249+
CBSettingsOptionEmail:email,
250+
CBSettingsOptionPassword:password};
251+
252+
[ClearBlade
253+
initSettingsWithSystemKey:TOKEN_APP_KEY
254+
withSystemSecret:TOKEN_APP_SECRET
255+
withOptions:options
256+
withSuccessCallback:^(ClearBlade *settings) {
257+
258+
CBUser *user = [CBUser authenticatedUserWithEmail:email withAuthToken:token];
259+
settings.mainUser = user;
260+
261+
NSError *getUserInfoError;
262+
[user getCurrentUserInfoWithError:&getUserInfoError];
263+
if (getUserInfoError != nil) {
264+
// We expect an error here because the token is nil
265+
[expectation fulfill];
266+
}
267+
else{
268+
XCTFail(@"Platform accepted an invalid token. We succeeded when we should have failed.");
269+
}
270+
}
271+
withErrorCallback:^(NSError * error) {
272+
273+
XCTFail(@"Failed to init ClearBlade. Error: <%@>", error);
274+
275+
276+
}
277+
];
278+
279+
[self waitForExpectationsWithTimeout:10.0 handler:nil];
280+
}
281+
282+
/**
283+
Note: Tokens expire, so you may need to re-up this token for running tests.
284+
*/
285+
286+
-(void)testGetUserInfoWithValidToken {
287+
288+
XCTestExpectation* expectation = [self expectationWithDescription:@"testGetUserInfoWithValidToken"];
289+
290+
NSString * email = @"rob@clearblade.com";
291+
NSString* password = @"clearblade";
292+
NSString* token = @"5bTng3_OJw76G7BTO2PVIw0QPuRCH4c0mcQ2iO4heyP9Pbk9sjKbM1RkemwKUejlTInaIfZT4E1jmLwOZw==";
293+
NSDictionary* options = @{
294+
CBSettingsOptionServerAddress: PLATFORM_ADDRESS,
295+
CBSettingsOptionLoggingLevel: @(TEST_LOGGING_LEVEL),
296+
CBSettingsOptionEmail:email,
297+
CBSettingsOptionPassword:password};
298+
299+
[ClearBlade
300+
initSettingsWithSystemKey:TOKEN_APP_KEY
301+
withSystemSecret:TOKEN_APP_SECRET
302+
withOptions:options
303+
withSuccessCallback:^(ClearBlade *settings) {
304+
305+
CBUser *user = [CBUser authenticatedUserWithEmail:email withAuthToken:token];
306+
settings.mainUser = user;
307+
308+
NSError *getUserInfoError;
309+
NSDictionary* userInfo = [user getCurrentUserInfoWithError:&getUserInfoError];
310+
if (getUserInfoError != nil) {
311+
312+
XCTFail(@"Platform denied a valid token.");
313+
}
314+
else{
315+
XCTAssertNotNil([userInfo objectForKey:@"email"]);
316+
XCTAssertNotNil([userInfo objectForKey:@"user_id"]);
317+
XCTAssertNotNil([userInfo objectForKey:@"creation_date"]);
318+
319+
[expectation fulfill];
320+
321+
}
322+
}
323+
withErrorCallback:^(NSError * error) {
324+
325+
XCTFail(@"Failed to init ClearBlade. Error: <%@>", error);
326+
327+
}
328+
];
329+
330+
[self waitForExpectationsWithTimeout:10.0 handler:nil];
331+
}
332+
333+
334+
335+
231336
/*
232337
Not testable at present
233338
-(void)testGetCount{

ClearBladeAPI/CBUser.m

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,26 @@ -(void)checkIsValidWithServerWithCallback:(CBUserIsValidCallback)isValidCallback
274274
}
275275

276276
-(NSDictionary *)getCurrentUserInfoWithError:(NSError *__autoreleasing *)error {
277+
NSDictionary *userInfo = @{};
278+
279+
if(!self.authToken){
280+
281+
*error = [NSError errorWithDomain:@"Unable to complete request because auth token was not set. You must initialize ClearBlade before use."
282+
code:400
283+
userInfo:nil];
284+
return userInfo;
285+
}
286+
277287
CBHTTPRequest* request = [CBUser getUserInfoRequestWithSettings:[ClearBlade settings] withToken:self.authToken];
288+
289+
if(!request){
290+
*error = [NSError errorWithDomain:@"Unable to complete request because invalid settings were found."
291+
code:400
292+
userInfo:nil];
293+
return userInfo; }
294+
278295
NSData *data = [request executeWithError:error];
279-
NSDictionary *userInfo = @{};
296+
280297
if (*error) {
281298
CBLogError(@"Failed to get user info of user <%@> because of error <%@>", self, *error);
282299
}

0 commit comments

Comments
 (0)