/
ChuckPadSupportTests.m
137 lines (106 loc) · 5.35 KB
/
ChuckPadSupportTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// ChuckPadSupportTests.m
// hello-chuckpad
//
// Created by Mark Cerqueira on 8/1/16.
//
// Tests classes used to support chuckpad-social-ios that don't directly interact with the chuckpad-social API
// service.
#include <stdlib.h>
#import "ChuckPadBaseTest.h"
@interface ChuckPadSupportTests : ChuckPadBaseTest
@end
@implementation ChuckPadSupportTests
- (void)setUp {
[super setUp];
}
- (void)tearDown {
[super tearDown];
}
- (void)testChuckPadKeychain {
NSArray *environmentEnums = @[@(Production), @(Stage), @(Local)];
NSArray *environmentUrls = [[NSArray alloc] initWithObjects:EnvironmentHostUrls];
for (NSInteger i = 0; i < [environmentEnums count]; i++) {
[[ChuckPadSocial sharedInstance] setEnvironment:(Environment)[[environmentEnums objectAtIndex:i] integerValue]];
NSString *environmentUrl = (NSString *)[environmentUrls objectAtIndex:i];
User *user = [[User alloc] init];
user.userId = arc4random_uniform(9001);
user.username = [NSString stringWithFormat:@"%@%@", environmentUrl, @"username"];
user.email = [NSString stringWithFormat:@"%@%@", environmentUrl, @"email"];
user.authToken = [NSString stringWithFormat:@"%@%@", environmentUrl, @"authToken"];
[[ChuckPadKeychain sharedInstance] authSucceededWithUser:user];
XCTAssertTrue([[ChuckPadSocial sharedInstance] isLoggedIn]);
}
for (NSInteger i = 0; i < [environmentEnums count]; i++) {
[[ChuckPadSocial sharedInstance] setEnvironment:(Environment)[[environmentEnums objectAtIndex:i] integerValue]];
XCTAssertTrue([[ChuckPadSocial sharedInstance] isLoggedIn]);
[[ChuckPadSocial sharedInstance] localLogOut];
}
for (NSInteger i = 0; i < [environmentEnums count]; i++) {
[[ChuckPadSocial sharedInstance] setEnvironment:(Environment)[[environmentEnums objectAtIndex:i] integerValue]];
XCTAssertFalse([[ChuckPadSocial sharedInstance] isLoggedIn]);
}
}
- (void)testToggleEnvironmentUrl {
// urlOne is a production URL
NSString *urlOne = [[ChuckPadSocial sharedInstance] getBaseUrl];
[[ChuckPadSocial sharedInstance] toggleEnvironment];
// urlTwo is a stage URL
NSString *urlTwo = [[ChuckPadSocial sharedInstance] getBaseUrl];
[[ChuckPadSocial sharedInstance] toggleEnvironment];
// urlThree is a production URL
NSString *urlThree = [[ChuckPadSocial sharedInstance] getBaseUrl];
[[ChuckPadSocial sharedInstance] toggleEnvironment];
XCTAssertFalse([urlOne isEqualToString:urlTwo]);
XCTAssertFalse([urlOne isEqualToString:urlThree]);
XCTAssertFalse([urlTwo isEqualToString:urlThree]);
}
- (void)testChangingPatchTypeDisallowed {
// We are boostrapped to MiniAudicle. We should not be able to change that.
BOOL exceptionThrown1 = NO;
@try {
// In the setUp method we bootstrapped to MiniAudicle so this method should throw an exception.
[ChuckPadSocial bootstrapForPatchType:Auraglyph];
} @catch (NSException *exception) {
exceptionThrown1 = YES;
} @finally {
XCTAssertTrue(exceptionThrown1);
}
// If we try to bootstrap to what we're already bootstrapped (i.e. MiniAudicle) that is okay.
BOOL exceptionThrown2 = NO;
@try {
// In the setUp method we bootstrapped to MiniAudicle so this method should throw an exception.
[ChuckPadSocial bootstrapForPatchType:MiniAudicle];
} @catch (NSException *exception) {
exceptionThrown2 = YES;
} @finally {
XCTAssertFalse(exceptionThrown2);
}
}
- (void)testPatchCache {
[[PatchCache sharedInstance] setObject:@"World" forKey:@"1-seconds" expire:1];
[[PatchCache sharedInstance] setObject:@"World" forKey:@"4-seconds" expire:4];
[[PatchCache sharedInstance] setObject:@"World" forKey:@"60-seconds" expire:60];
// A key that does not map to anything should return nil
XCTAssertNil([[PatchCache sharedInstance] objectForKey:@"Non-existent key"]);
// No time has advanced so all these should return valid, non-nil objects
XCTAssertNotNil([[PatchCache sharedInstance] objectForKey:@"1-seconds"]);
XCTAssertNotNil([[PatchCache sharedInstance] objectForKey:@"4-seconds"]);
XCTAssertNotNil([[PatchCache sharedInstance] objectForKey:@"60-seconds"]);
[NSThread sleepForTimeInterval:2];
// Our object for "1-seconds" should have expired because 2 seconds have passed
XCTAssertNil([[PatchCache sharedInstance] objectForKey:@"1-seconds"]);
XCTAssertNotNil([[PatchCache sharedInstance] objectForKey:@"4-seconds"]);
XCTAssertNotNil([[PatchCache sharedInstance] objectForKey:@"60-seconds"]);
[NSThread sleepForTimeInterval:3];
// Now our object for "4-seconds" should also have expired because 5 seconds have passed
XCTAssertNil([[PatchCache sharedInstance] objectForKey:@"1-seconds"]);
XCTAssertNil([[PatchCache sharedInstance] objectForKey:@"4-seconds"]);
XCTAssertNotNil([[PatchCache sharedInstance] objectForKey:@"60-seconds"]);
[[PatchCache sharedInstance] removeAllObjects];
// All objects removed so everything should return nil
XCTAssertNil([[PatchCache sharedInstance] objectForKey:@"1-seconds"]);
XCTAssertNil([[PatchCache sharedInstance] objectForKey:@"4-seconds"]);
XCTAssertNil([[PatchCache sharedInstance] objectForKey:@"60-seconds"]);
}
@end