forked from parse-community/Parse-SDK-iOS-OSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACLTests.m
253 lines (186 loc) · 8.07 KB
/
ACLTests.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
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
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <OCMock/OCMock.h>
#import "PFACLPrivate.h"
#import "PFMacros.h"
#import "PFObjectPrivate.h"
#import "PFRole.h"
#import "PFUnitTestCase.h"
#import "PFUserPrivate.h"
@interface ACLTests : PFUnitTestCase
@end
@implementation ACLTests
- (void)testConstructors {
id mockedUser = PFStrictClassMock([PFUser class]);
OCMStub([mockedUser objectId]).andReturn(@"1337");
PFACL *acl = [PFACL ACL];
XCTAssertNotNil(acl);
XCTAssertFalse([acl getReadAccessForUser:mockedUser]);
XCTAssertFalse([acl getWriteAccessForUser:mockedUser]);
acl = [PFACL ACLWithUser:mockedUser];
XCTAssertNotNil(acl);
XCTAssertTrue([acl getReadAccessForUser:mockedUser]);
XCTAssertTrue([acl getWriteAccessForUser:mockedUser]);
}
- (void)testShared {
PFACL *acl = [PFACL ACL];
XCTAssertFalse(acl.isShared);
[acl setShared:YES];
XCTAssertTrue(acl.isShared);
}
- (void)testPublicAccess {
PFACL *acl = [PFACL ACL];
XCTAssertFalse([acl getPublicReadAccess]);
XCTAssertFalse([acl getPublicWriteAccess]);
[acl setPublicReadAccess:YES];
XCTAssertTrue([acl getPublicReadAccess]);
[acl setPublicWriteAccess:YES];
XCTAssertTrue([acl getPublicWriteAccess]);
}
- (void)testReadAccess {
PFRole *mockedRole = PFStrictClassMock([PFRole class]);
PFUser *mockedUser = PFStrictClassMock([PFUser class]);
OCMStub(mockedRole.name).andReturn(@"aRoleName");
OCMStub(mockedRole.objectId).andReturn(@"aRoleID");
OCMStub(mockedUser.objectId).andReturn(@"aUserID");
PFACL *acl = [PFACL ACL];
XCTAssertFalse([acl getReadAccessForUserId:@"someUserID"]);
XCTAssertFalse([acl getReadAccessForUser:mockedUser]);
XCTAssertFalse([acl getReadAccessForRoleWithName:@"someRoleName"]);
XCTAssertFalse([acl getReadAccessForRole:mockedRole]);
[acl setReadAccess:YES forUserId:@"someUserID"];
XCTAssertTrue([acl getReadAccessForUserId:@"someUserID"]);
[acl setReadAccess:YES forUser:mockedUser];
XCTAssertTrue([acl getReadAccessForUser:mockedUser]);
[acl setReadAccess:YES forRoleWithName:@"someRoleName"];
XCTAssertTrue([acl getReadAccessForRoleWithName:@"someRoleName"]);
[acl setReadAccess:YES forRole:mockedRole];
XCTAssertTrue([acl getReadAccessForRole:mockedRole]);
}
- (void)testWriteAccess {
PFRole *mockedRole = PFStrictClassMock([PFRole class]);
PFUser *mockedUser = PFStrictClassMock([PFUser class]);
OCMStub(mockedRole.name).andReturn(@"aRoleName");
OCMStub(mockedRole.objectId).andReturn(@"aRoleID");
OCMStub(mockedUser.objectId).andReturn(@"aUserID");
PFACL *acl = [PFACL ACL];
XCTAssertFalse([acl getWriteAccessForUserId:@"someUserID"]);
XCTAssertFalse([acl getWriteAccessForUser:mockedUser]);
XCTAssertFalse([acl getWriteAccessForRoleWithName:@"someRoleName"]);
XCTAssertFalse([acl getWriteAccessForRole:mockedRole]);
[acl setWriteAccess:YES forUserId:@"someUserID"];
XCTAssertTrue([acl getWriteAccessForUserId:@"someUserID"]);
[acl setWriteAccess:YES forUser:mockedUser];
XCTAssertTrue([acl getWriteAccessForUser:mockedUser]);
[acl setWriteAccess:YES forRoleWithName:@"someRoleName"];
XCTAssertTrue([acl getWriteAccessForRoleWithName:@"someRoleName"]);
[acl setWriteAccess:YES forRole:mockedRole];
XCTAssertTrue([acl getWriteAccessForRole:mockedRole]);
}
- (void)testLazyUser {
PFUser *lazyUser = PFStrictClassMock([PFUser class]);
__block NSString *userId = nil;
OCMStub(lazyUser.objectId).andDo(^(NSInvocation *invocation) {
[invocation setReturnValue:&userId];
});
OCMStub(lazyUser._lazy).andReturn(YES);
__block void (^saveListener)(id, NSError *) = nil;
OCMStub([lazyUser registerSaveListener:[OCMArg checkWithBlock:^BOOL(id obj) {
saveListener = [obj copy];
return obj != nil;
}]]);
OCMStub([lazyUser unregisterSaveListener:[OCMArg checkWithBlock:^BOOL(id obj) {
return [obj isEqual:saveListener];
}]]);
PFACL *acl = [PFACL ACL];
XCTAssertFalse([acl hasUnresolvedUser]);
[acl setReadAccess:YES forUser:lazyUser];
[acl setWriteAccess:YES forUser:lazyUser];
XCTAssertTrue([acl hasUnresolvedUser]);
XCTAssertTrue([acl getReadAccessForUser:lazyUser]);
XCTAssertTrue([acl getWriteAccessForUser:lazyUser]);
XCTAssertFalse([acl getReadAccessForUserId:@"userID"]);
XCTAssertFalse([acl getWriteAccessForUserId:@"userID"]);
userId = @"userID";
saveListener(lazyUser, nil);
XCTAssertFalse([acl hasUnresolvedUser]);
XCTAssertTrue([acl getReadAccessForUserId:@"userID"]);
XCTAssertTrue([acl getWriteAccessForUserId:@"userID"]);
}
- (void)testEquality {
PFACL *a = [PFACL ACL];
PFACL *b = [PFACL ACL];
XCTAssertFalse([a isEqual:nil]);
XCTAssertFalse([a isEqual:@"Hello, World!"]);
XCTAssertTrue([a isEqual:a]);
XCTAssertTrue([a isEqual:b]);
[b setPublicWriteAccess:YES];
XCTAssertFalse([a isEqual:b]);
}
- (void)testHash {
PFACL *acl = [PFACL ACL];
NSUInteger oldHash = [acl hash];
[acl setReadAccess:YES forUserId:@"foo"];
NSUInteger newHash = [acl hash];
XCTAssertNotEqual(oldHash, newHash);
}
- (void)testCopy {
PFACL *aclA = [PFACL ACL];
PFACL *aclB = [aclA copy];
XCTAssertNotEqual(aclA, aclB);
XCTAssertEqualObjects(aclA, aclB);
[aclB setPublicWriteAccess:YES];
XCTAssertFalse([aclA getPublicWriteAccess]);
}
- (void)testUnsharedCopy {
PFACL *sharedACL = [PFACL ACL];
[sharedACL setShared:YES];
[sharedACL setPublicReadAccess:YES];
PFACL *unsharedACL = [sharedACL createUnsharedCopy];
XCTAssertFalse([unsharedACL isShared]);
XCTAssertTrue([unsharedACL getPublicReadAccess]);
}
- (void)testACLRequiresObjectId {
PFACL *acl = [PFACL ACL];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
XCTAssertThrowsSpecificNamed([acl setReadAccess:YES forUserId:nil],
NSException,
NSInvalidArgumentException,
@"Should not be able to give permissions to nil ids.");
XCTAssertThrowsSpecificNamed([acl setWriteAccess:YES forUserId:nil],
NSException,
NSInvalidArgumentException,
@"Should not be able to give permissions to nil ids.");
#pragma clang diagnostic pop
PFUser *user = [PFUser user];
XCTAssertThrowsSpecificNamed([acl setReadAccess:YES forUser:user],
NSException,
NSInvalidArgumentException,
@"Should not be able to give permissions to unsaved users.");
XCTAssertThrowsSpecificNamed([acl setWriteAccess:YES forUser:user],
NSException,
NSInvalidArgumentException,
@"Should not be able to give permissions to unsaved users.");
}
- (void)testNSCoding {
PFACL *acl = [PFACL ACL];
[acl setReadAccess:NO forUserId:@"a"];
[acl setReadAccess:YES forUserId:@"b"];
[acl setWriteAccess:NO forUserId:@"c"];
[acl setWriteAccess:YES forUserId:@"d"];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:acl];
XCTAssertTrue([data length] > 0, @"Encoded data should not be empty");
PFACL *decodedACL = [NSKeyedUnarchiver unarchiveObjectWithData:data];
XCTAssertFalse([decodedACL getReadAccessForUserId:@"a"], @"Decoded value should be the same as the encoded one.");
XCTAssertTrue([decodedACL getReadAccessForUserId:@"b"], @"Decoded value should be the same as the encoded one.");
XCTAssertFalse([decodedACL getWriteAccessForUserId:@"c"], @"Decoded value should be the same as the encoded one.");
XCTAssertTrue([decodedACL getWriteAccessForUserId:@"d"], @"Decoded value should be the same as the encoded one.");
}
@end