Skip to content
Open
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
7 changes: 7 additions & 0 deletions RadarSDK/Radar.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ + (void)nativeSetup:(RadarInitializeOptions *)options {
+ (void)initializeWithPublishableKey:(NSString *)publishableKey options:(RadarInitializeOptions *)options {
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelInfo type:RadarLogTypeSDKCall message:@"initialize()"];

// Check for secret key usage
if ([publishableKey containsString:@"_sk_"]) {
@throw [NSException exceptionWithName:NSInvalidArgumentException
reason:@"Secret keys are not allowed. Please use your Radar publishable key."
userInfo:nil];
}

Class RadarSDKMotion = NSClassFromString(@"RadarSDKMotion");
if (RadarSDKMotion) {
id radarSDKMotion = [[RadarSDKMotion alloc] init];
Expand Down
28 changes: 28 additions & 0 deletions RadarSDKTests/RadarSDKTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,34 @@ - (void)test_Radar_initialize {
XCTAssertEqualObjects(kPublishableKey, [RadarSettings publishableKey]);
}

- (void)test_Radar_initialize_throwsExceptionForSecretKey {
NSString *secretKey = @"prj_test_sk_0000000000000000000000000000000000000000";

XCTAssertThrows([Radar initializeWithPublishableKey:secretKey options:nil],
@"Should throw exception when secret key is used");
}

- (void)test_Radar_initialize_throwsCorrectExceptionTypeForSecretKey {
NSString *secretKey = @"prj_test_sk_0000000000000000000000000000000000000000";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this test would cover test_Radar_initialize_throwsExceptionForSecretKey

I think it would cover all cases with just this test that verify it throws and throws the right error. But it's probably fine to have more tests.


@try {
[Radar initializeWithPublishableKey:secretKey options:nil];
XCTFail(@"Expected NSInvalidArgumentException to be thrown");
} @catch (NSException *exception) {
XCTAssertEqualObjects(exception.name, NSInvalidArgumentException,
@"Exception should be NSInvalidArgumentException");
XCTAssertTrue([exception.reason containsString:@"Secret keys are not allowed"],
@"Exception reason should mention secret keys are not allowed");
}
}

- (void)test_Radar_initialize_acceptsValidPublishableKey {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this case is covered by test_Radar_initialize, no?

NSString *validKey = @"prj_test_pk_0000000000000000000000000000000000000000";

XCTAssertNoThrow([Radar initializeWithPublishableKey:validKey options:nil],
@"Should not throw exception for valid publishable key");
}

- (void)test_Radar_setUserId {
NSString *userId = @"userId";
[Radar setUserId:userId];
Expand Down
Loading