-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathVENUserSpec.m
More file actions
288 lines (212 loc) · 10.5 KB
/
VENUserSpec.m
File metadata and controls
288 lines (212 loc) · 10.5 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#import "VENUser.h"
#import "VENCore.h"
#import "VENUserPayloadKeys.h"
#import "VENTestUtilities.h"
SpecBegin(VENUser)
NSDictionary *validUserDictionary1 = @{VENUserKeyUsername: @"PeterIsAZakin",
VENUserKeyInternalId: @"234234",
VENUserKeyExternalId: @"JLHDSJFIOHh23ioHLH",
VENUserKeyFirstName: @"Peter",
VENUserKeyLastName: @"Maddern",
VENUserKeyAbout: @"Happily married!"};
NSDictionary *validUserDictionary2 = @{VENUserKeyUsername: @"PetefadsrIsAZakin",
VENUserKeyInternalId: @"234223434",
VENUserKeyExternalId: @"JLHDSfadJfsdFIOHh23ioHLH",
VENUserKeyFirstName: @"Pefadster",
VENUserKeyLastName: @"Mafadsddern",
VENUserKeyAbout: @"Happifasdly married!"};
NSDictionary *invalidUserDictionary1 = @{VENUserKeyInternalId: @"234234",
VENUserKeyExternalId: @"JLHDSJFIOHh23ioHLH",
VENUserKeyFirstName: @"Peter",
VENUserKeyLastName: @"Maddern",
VENUserKeyAbout: @"Happily married!"};
// This does not contain the external ID
NSDictionary *invalidUserDictionary2 = @{VENUserKeyUsername: @"PeterIsAZakin",
VENUserKeyFirstName: @"Peter",
VENUserKeyLastName: @"Maddern",
VENUserKeyAbout: @"Happily married!"};
void(^assertUsersAreFieldwiseEqual)(VENUser *, VENUser *) = ^(VENUser *user1, VENUser *user2) {
expect(user1.username).to.equal(user2.username);
expect(user1.firstName).to.equal(user2.firstName);
expect(user1.lastName).to.equal(user2.lastName);
expect(user1.displayName).to.equal(user2.displayName);
expect(user1.about).to.equal(user2.about);
expect(user1.primaryPhone).to.equal(user2.primaryPhone);
expect(user1.primaryEmail).to.equal(user2.primaryEmail);
expect(user1.internalId).to.equal(user2.internalId);
expect(user1.externalId).to.equal(user2.externalId);
expect(user1.dateJoined).to.equal(user2.dateJoined);
expect(user1.profileImageUrl).to.equal(user2.profileImageUrl);
};
beforeAll(^{
VENCore *core = [[VENCore alloc] init];
[VENCore setDefaultCore:core];
[[LSNocilla sharedInstance] start];
});
afterAll(^{
[[LSNocilla sharedInstance] stop];
});
afterEach(^{
[[LSNocilla sharedInstance] clearStubs];
});
describe(@"Initialization", ^{
it(@"should succesfully create an empty object from init", ^{
VENUser *usr = [[VENUser alloc] init];
expect(usr).toNot.beNil();
expect(usr.username).to.beNil();
expect(usr.firstName).to.beNil();
expect(usr.lastName).to.beNil();
expect(usr.profileImageUrl).to.beNil();
});
it(@"should return NO to canInitWithDictionary for an invalid dictionary", ^{
BOOL canInit = [VENUser canInitWithDictionary:invalidUserDictionary1];
expect(canInit).to.beFalsy();
canInit = [VENUser canInitWithDictionary:invalidUserDictionary2];
expect(canInit).to.beFalsy();
});
it(@"should return NO to a nil or empty dictionary", ^{
BOOL canInit = [VENUser canInitWithDictionary:@{}];
expect(canInit).to.beFalsy();
canInit = [VENUser canInitWithDictionary:nil];
expect(canInit).to.beFalsy();
});
it(@"should return YES to canInitWithDictionary for a valid dictionary", ^{
BOOL canInit = [VENUser canInitWithDictionary:validUserDictionary1];
expect(canInit).to.beTruthy();
});
it(@"should create a user with minimally a username and externalId for dictionaries that return YES to canInitWithDictionary",^{
VENUser *user1 = [[VENUser alloc] initWithDictionary:validUserDictionary1];
expect(user1.username).to.equal(@"PeterIsAZakin");
expect(user1.externalId).to.equal(@"JLHDSJFIOHh23ioHLH");
VENUser *user2 = [[VENUser alloc] initWithDictionary:validUserDictionary2];
expect(user2.username).to.equal(@"PetefadsrIsAZakin");
expect(user2.externalId).to.equal(@"JLHDSfadJfsdFIOHh23ioHLH");
});
});
describe(@"Copying", ^{
it(@"should create a valid copy of any empty object", ^{
VENUser *usr = [[VENUser alloc] init];
VENUser *myOtherUser = [usr copy];
expect(myOtherUser).notTo.beNil();
expect(myOtherUser).to.beKindOf([usr class]);
});
it(@"should create a valid copy of a valid user", ^{
VENUser *user = [[VENUser alloc] initWithDictionary:validUserDictionary1];
VENUser *newUser = [user copy];
expect(user).to.equal(newUser);
});
});
describe(@"Equality", ^{
it(@"should correctly validate two equal objects", ^{
VENUser *user1 = [[VENUser alloc] initWithDictionary:validUserDictionary1];
VENUser *user2 = [user1 copy];
expect([user1 isEqual:user2]).to.beTruthy();
});
it(@"should not indicate that two different users are the same", ^{
VENUser *user1 = [[VENUser alloc] initWithDictionary:validUserDictionary1];
VENUser *user2 = [[VENUser alloc] initWithDictionary:validUserDictionary2];
expect([user1 isEqual:user2]).to.beFalsy();
});
it(@"should behave transitively", ^{
VENUser *user1 = [[VENUser alloc] initWithDictionary:validUserDictionary1];
VENUser *user2 = [[VENUser alloc] initWithDictionary:validUserDictionary2];
expect([user1 isEqual:user2]).to.beFalsy();
expect([user2 isEqual:user1]).to.beFalsy();
user1 = [[VENUser alloc] initWithDictionary:validUserDictionary1];
user2 = [user1 copy];
expect([user1 isEqual:user2]).to.beTruthy();
expect([user2 isEqual:user1]).to.beTruthy();
});
it(@"should follow the rule that two users are equal ONLY if their external Ids are the same", ^{
VENUser *user = [[VENUser alloc] init];
VENUser *myOtherUser = [user copy];
expect(user).toNot.equal(myOtherUser);
VENUser *invalidUser = [[VENUser alloc] initWithDictionary:invalidUserDictionary2];
VENUser *copiedInvalidUser = [invalidUser copy];
expect(invalidUser).toNot.equal(copiedInvalidUser);
});
});
describe(@"Dictionary Representation", ^{
it(@"should consider dictionary representations of equal users to be equal", ^{
VENUser *user1 = [[VENUser alloc] initWithDictionary:validUserDictionary1];
NSDictionary *user1Dictionary = [user1 dictionaryRepresentation];
VENUser *user2 = [[VENUser alloc] initWithDictionary:user1Dictionary];
expect([user1 isEqual:user2]).to.beTruthy();
assertUsersAreFieldwiseEqual(user1, user2);
});
});
describe(@"Fetching a User", ^{
it(@"should retrieve a pre-canned Chris user and create a valid user", ^AsyncBlock{
NSString *externalId = @"1106387358711808333";
NSString *baseURLString = [VENTestUtilities baseURLStringForCore:[VENCore defaultCore]];
NSString *urlToStub = [NSString stringWithFormat:@"%@/%@/%@?", baseURLString, VENAPIPathUsers, externalId];
[VENTestUtilities stubNetworkGET:urlToStub withStatusCode:200 andResponseFilePath:@"fetchChrisUser"];
[VENUser fetchUserWithExternalId:externalId success:^(VENUser *user) {
expect(user.externalId).to.equal(externalId);
done();
} failure:^(NSError *error) {
XCTFail();
done();
}];
});
it(@"should call failure when cannot find a user with that external Id", ^AsyncBlock{
NSString *externalId = @"1106387358711808339"; //invalid external id
NSString *baseURLString = [VENTestUtilities baseURLStringForCore:[VENCore defaultCore]];
NSString *urlToStub = [NSString stringWithFormat:@"%@/%@/%@?", baseURLString, VENAPIPathUsers, externalId];
[VENTestUtilities stubNetworkGET:urlToStub withStatusCode:400 andResponseFilePath:@"fetchInvalidUser"];
[VENUser fetchUserWithExternalId:externalId success:^(VENUser *user) {
XCTFail();
done();
} failure:^(NSError *error) {
expect([error localizedDescription]).to.equal(@"Resource not found.");
done();
}];
});
it(@"should call failure when not passed an external id", ^AsyncBlock{
[VENUser fetchUserWithExternalId:nil success:^(VENUser *user) {
XCTFail();
done();
} failure:^(NSError *error) {
expect(error).notTo.beNil();
done();
}];
});
it(@"should call failure when passed an empty-string external id", ^AsyncBlock{
[VENUser fetchUserWithExternalId:@"" success:^(VENUser *user) {
XCTFail();
done();
} failure:^(NSError *error) {
expect(error).notTo.beNil();
done();
}];
});
});
describe(@"SearchUsersWithSearchQuery", ^{
it(@"should fetch users with search string 'john'", ^AsyncBlock{
NSString *query = @"john";
NSString *baseURLString = [VENTestUtilities baseURLStringForCore:[VENCore defaultCore]];
NSString *urlToStub = [NSString stringWithFormat:@"%@/users?query=%@", baseURLString, query];
[VENTestUtilities stubNetworkGET:urlToStub withStatusCode:200 andResponseFilePath:@"searchUsers"];
[VENUser searchUsersWithQuery:query success:^(NSArray *users) {
expect([users count]).to.equal(@(20));
done();
}failure:^(NSError *error) {
XCTFail();
done();
}];
});
it(@"should return 20 users when given an empy string search query", ^AsyncBlock{
NSString *query = @"";
NSString *baseURLString = [VENTestUtilities baseURLStringForCore:[VENCore defaultCore]];
NSString *urlToStub = [NSString stringWithFormat:@"%@/users?query=%@", baseURLString, query];
[VENTestUtilities stubNetworkGET:urlToStub withStatusCode:200 andResponseFilePath:@"searchUsers"];
[VENUser searchUsersWithQuery:query success:^(NSArray *users) {
expect([users count]).to.equal(@(20));
done();
}failure:^(NSError *error) {
XCTFail();
done();
}];
});
});
SpecEnd