1
- using System ;
2
1
using Microsoft . VisualStudio . TestTools . UnitTesting ;
3
- using Moq ; // Add Moq for mocking if not already added
4
- using Parse . Infrastructure ;
5
- using Parse . Platform . Objects ;
2
+ using Moq ;
6
3
using Parse . Abstractions . Infrastructure ;
7
4
using Parse . Abstractions . Platform . Objects ;
8
-
9
- namespace Parse . Tests ;
5
+ using Parse . Infrastructure ;
6
+ using Parse . Platform . Objects ;
7
+ using Parse ;
8
+ using System . Collections . Generic ;
9
+ using System ;
10
10
11
11
[ TestClass ]
12
12
public class ACLTests
@@ -35,6 +35,7 @@ public void Initialize()
35
35
return user ;
36
36
} ) ;
37
37
38
+
38
39
// Set up ParseClient with the mocked ServiceHub
39
40
Client = new ParseClient ( new ServerConnectionData { Test = true } )
40
41
{
@@ -47,13 +48,23 @@ public void Initialize()
47
48
// Add valid classes to the client
48
49
Client . AddValidClass < ParseUser > ( ) ;
49
50
Client . AddValidClass < ParseSession > ( ) ;
51
+ Client . AddValidClass < ParseRole > ( ) ;
50
52
}
51
53
52
54
[ TestCleanup ]
53
55
public void Clean ( ) => ( Client . Services as ServiceHub ) ? . Reset ( ) ;
54
56
55
57
[ TestMethod ]
56
- public void TestCheckPermissionsWithParseUserConstructor ( )
58
+ [ Description ( "Tests if default ParseACL is created without errors." ) ]
59
+ public void TestParseACLDefaultConstructor ( ) // 1
60
+ {
61
+ var acl = new ParseACL ( ) ;
62
+ Assert . IsNotNull ( acl ) ;
63
+
64
+ }
65
+ [ TestMethod ]
66
+ [ Description ( "Tests ACL creation using ParseUser constructor." ) ]
67
+ public void TestCheckPermissionsWithParseUserConstructor ( ) // 1
57
68
{
58
69
// Arrange
59
70
ParseUser owner = GenerateUser ( "OwnerUser" ) ;
@@ -70,7 +81,8 @@ public void TestCheckPermissionsWithParseUserConstructor()
70
81
}
71
82
72
83
[ TestMethod ]
73
- public void TestReadWriteMutationWithParseUserConstructor ( )
84
+ [ Description ( "Tests that users permission change accordingly" ) ]
85
+ public void TestReadWriteMutationWithParseUserConstructor ( ) // 1
74
86
{
75
87
// Arrange
76
88
ParseUser owner = GenerateUser ( "OwnerUser" ) ;
@@ -93,7 +105,8 @@ public void TestReadWriteMutationWithParseUserConstructor()
93
105
}
94
106
95
107
[ TestMethod ]
96
- public void TestParseACLCreationWithNullObjectIdParseUser ( )
108
+ [ Description ( "Tests if throws if try to instantiate using a ParseUser without objectId." ) ]
109
+ public void TestParseACLCreationWithNullObjectIdParseUser ( ) // 1
97
110
{
98
111
// Assert
99
112
Assert . ThrowsException < ArgumentException > ( ( ) => new ParseACL ( GenerateUser ( default ) ) ) ;
@@ -102,22 +115,25 @@ public void TestParseACLCreationWithNullObjectIdParseUser()
102
115
ParseUser GenerateUser ( string objectID )
103
116
{
104
117
// Use the mock to simulate generating a ParseUser
105
- var state = new MutableObjectState { ObjectId = objectID } ;
118
+ var state = new MutableObjectState { ObjectId = objectID , ClassName = "_User" } ;
106
119
return Client . GenerateObjectFromState < ParseUser > ( state , "_User" ) ;
120
+
107
121
}
108
122
109
123
[ TestMethod ]
110
- public void TestGenerateObjectFromState ( )
124
+ [ Description ( "Tests to create a ParseUser via IParseClassController, that is set when calling Bind." ) ]
125
+ public void TestGenerateObjectFromState ( ) // 1
111
126
{
112
127
// Arrange
113
128
var state = new MutableObjectState { ObjectId = "123" , ClassName = null } ;
114
129
var defaultClassName = "_User" ;
115
130
131
+
116
132
var serviceHubMock = new Mock < IServiceHub > ( ) ;
117
133
var classControllerMock = new Mock < IParseObjectClassController > ( ) ;
118
134
119
135
classControllerMock . Setup ( controller => controller . Instantiate ( It . IsAny < string > ( ) , It . IsAny < IServiceHub > ( ) ) )
120
- . Returns < string , IServiceHub > ( ( className , hub ) => new ParseUser ( ) ) ;
136
+ . Returns < string , IServiceHub > ( ( className , hub ) => new ParseUser ( ) ) ;
121
137
122
138
// Act
123
139
var user = classControllerMock . Object . GenerateObjectFromState < ParseUser > ( state , defaultClassName , serviceHubMock . Object ) ;
@@ -126,5 +142,137 @@ public void TestGenerateObjectFromState()
126
142
Assert . IsNotNull ( user ) ;
127
143
Assert . AreEqual ( defaultClassName , user . ClassName ) ;
128
144
}
145
+ [ TestMethod ]
146
+ [ Description ( "Tests for public read and write access values." ) ]
147
+ public void TestPublicReadWriteAccessValues ( ) // 1
148
+ {
149
+ var acl = new ParseACL ( ) ;
150
+ Assert . IsFalse ( acl . PublicReadAccess ) ;
151
+ Assert . IsFalse ( acl . PublicWriteAccess ) ;
152
+
153
+ acl . PublicReadAccess = true ;
154
+ acl . PublicWriteAccess = true ;
155
+ Assert . IsTrue ( acl . PublicReadAccess ) ;
156
+ Assert . IsTrue ( acl . PublicWriteAccess ) ;
157
+ }
158
+
159
+ [ TestMethod ]
160
+ [ Description ( "Tests that sets and gets properly for string UserIds." ) ]
161
+ public void TestSetGetAccessWithStringId ( ) // 1
162
+ {
163
+ var acl = new ParseACL ( ) ;
164
+ var testUser = GenerateUser ( "test" ) ;
165
+ acl . SetReadAccess ( testUser . ObjectId , true ) ;
166
+ acl . SetWriteAccess ( testUser . ObjectId , true ) ;
167
+
168
+ Assert . IsTrue ( acl . GetReadAccess ( testUser . ObjectId ) ) ;
169
+ Assert . IsTrue ( acl . GetWriteAccess ( testUser . ObjectId ) ) ;
170
+
171
+ acl . SetReadAccess ( testUser . ObjectId , false ) ;
172
+ acl . SetWriteAccess ( testUser . ObjectId , false ) ;
173
+
174
+ Assert . IsFalse ( acl . GetReadAccess ( testUser . ObjectId ) ) ;
175
+ Assert . IsFalse ( acl . GetWriteAccess ( testUser . ObjectId ) ) ;
176
+ }
177
+
178
+ [ TestMethod ]
179
+ [ Description ( "Tests that methods thow exceptions if user id is null." ) ]
180
+ public void SetGetAccessThrowsForNull ( ) // 1
181
+ {
182
+ var acl = new ParseACL ( ) ;
183
+
184
+ Assert . ThrowsException < ArgumentException > ( ( ) => acl . SetReadAccess ( userId : null , false ) ) ;
185
+ Assert . ThrowsException < ArgumentException > ( ( ) => acl . SetWriteAccess ( userId : null , false ) ) ;
186
+ Assert . ThrowsException < ArgumentException > ( ( ) => acl . GetReadAccess ( userId : null ) ) ;
187
+ Assert . ThrowsException < ArgumentException > ( ( ) => acl . GetWriteAccess ( userId : null ) ) ;
188
+
189
+ }
190
+ [ TestMethod ]
191
+ [ Description ( "Tests that a Get access using a ParseUser is correct." ) ]
192
+ public void TestSetGetAccessWithParseUser ( ) // 1
193
+ {
194
+ var acl = new ParseACL ( ) ;
195
+ ParseUser test = GenerateUser ( "test" ) ;
196
+
197
+ acl . SetReadAccess ( test , true ) ;
198
+ acl . SetWriteAccess ( test , true ) ;
199
+ Assert . IsTrue ( acl . GetReadAccess ( test ) ) ;
200
+ Assert . IsTrue ( acl . GetWriteAccess ( test ) ) ;
201
+
202
+ acl . SetReadAccess ( test , false ) ;
203
+ acl . SetWriteAccess ( test , false ) ;
204
+
205
+ Assert . IsFalse ( acl . GetReadAccess ( test ) ) ;
206
+ Assert . IsFalse ( acl . GetWriteAccess ( test ) ) ;
207
+
208
+ }
209
+
210
+ [ TestMethod ]
211
+ [ Description ( "Tests that the default ParseACL returns correct roles for read/write" ) ]
212
+ public void TestDefaultRolesForReadAndWriteAccess ( ) // 1
213
+ {
214
+ var acl = new ParseACL ( ) ;
215
+ Assert . IsFalse ( acl . GetRoleReadAccess ( "role" ) ) ;
216
+ Assert . IsFalse ( acl . GetRoleWriteAccess ( "role" ) ) ;
217
+
218
+ }
129
219
130
- }
220
+ [ TestMethod ]
221
+ [ Description ( "Tests role read/write access with role names correctly and get methods." ) ]
222
+ public void TestSetGetRoleReadWriteAccessWithRoleName ( ) // 1
223
+ {
224
+ var acl = new ParseACL ( ) ;
225
+ acl . SetRoleReadAccess ( "test" , true ) ;
226
+ acl . SetRoleWriteAccess ( "test" , true ) ;
227
+ Assert . IsTrue ( acl . GetRoleReadAccess ( "test" ) ) ;
228
+ Assert . IsTrue ( acl . GetRoleWriteAccess ( "test" ) ) ;
229
+
230
+ acl . SetRoleReadAccess ( "test" , false ) ;
231
+ acl . SetRoleWriteAccess ( "test" , false ) ;
232
+ Assert . IsFalse ( acl . GetRoleReadAccess ( "test" ) ) ;
233
+ Assert . IsFalse ( acl . GetRoleWriteAccess ( "test" ) ) ;
234
+ }
235
+
236
+ [ TestMethod ]
237
+ [ Description ( "Tests ACL can use and correctly convert to JSON object via ConvertToJSON." ) ]
238
+ public void TestConvertToJSON ( ) // 3
239
+ {
240
+ var acl = new ParseACL ( ) ;
241
+ ParseUser user = GenerateUser ( "test" ) ;
242
+
243
+ acl . SetReadAccess ( user , true ) ;
244
+ acl . SetWriteAccess ( user , false ) ;
245
+ acl . SetRoleReadAccess ( "test" , true ) ;
246
+ var json = ( acl as IJsonConvertible ) . ConvertToJSON ( ) ;
247
+ Assert . IsInstanceOfType ( json , typeof ( IDictionary < string , object > ) ) ;
248
+
249
+ var jsonObject = json as IDictionary < string , object > ;
250
+ Assert . IsTrue ( jsonObject . ContainsKey ( user . ObjectId ) ) ;
251
+ Assert . IsTrue ( jsonObject . ContainsKey ( "role:test" ) ) ;
252
+ var test = jsonObject [ user . ObjectId ] as Dictionary < string , object > ;
253
+ Assert . AreEqual ( 1 , test . Count ) ;
254
+ }
255
+
256
+
257
+ [ TestMethod ]
258
+ [ Description ( "Tests that ProcessAclData can handle invalid values for public key." ) ]
259
+ public void TestProcessAclData_HandlesInvalidDataForPublic ( ) // 1
260
+ {
261
+ var aclData = new Dictionary < string , object > { { "*" , 123 } } ;
262
+ var acl = new ParseACL ( aclData ) ;
263
+ Assert . IsFalse ( acl . PublicReadAccess ) ;
264
+ Assert . IsFalse ( acl . PublicWriteAccess ) ;
265
+ }
266
+ [ TestMethod ]
267
+ [ Description ( "Tests if ACL skips keys that don't represent valid JSON data dictionaries" ) ]
268
+ public void TestProcessAclData_SkipsInvalidKeys ( ) // 1
269
+ {
270
+ var aclData = new Dictionary < string , object > {
271
+ { "userId" , 123 }
272
+ } ;
273
+ var acl = new ParseACL ( aclData ) ;
274
+
275
+ Assert . IsFalse ( acl . GetReadAccess ( "userId" ) ) ;
276
+ Assert . IsFalse ( acl . GetWriteAccess ( "userId" ) ) ;
277
+ }
278
+ }
0 commit comments