1- using System ;
21using 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 ;
63using Parse . Abstractions . Infrastructure ;
74using 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 ;
1010
1111[ TestClass ]
1212public class ACLTests
@@ -35,6 +35,7 @@ public void Initialize()
3535 return user ;
3636 } ) ;
3737
38+
3839 // Set up ParseClient with the mocked ServiceHub
3940 Client = new ParseClient ( new ServerConnectionData { Test = true } )
4041 {
@@ -47,13 +48,23 @@ public void Initialize()
4748 // Add valid classes to the client
4849 Client . AddValidClass < ParseUser > ( ) ;
4950 Client . AddValidClass < ParseSession > ( ) ;
51+ Client . AddValidClass < ParseRole > ( ) ;
5052 }
5153
5254 [ TestCleanup ]
5355 public void Clean ( ) => ( Client . Services as ServiceHub ) ? . Reset ( ) ;
5456
5557 [ 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
5768 {
5869 // Arrange
5970 ParseUser owner = GenerateUser ( "OwnerUser" ) ;
@@ -70,7 +81,8 @@ public void TestCheckPermissionsWithParseUserConstructor()
7081 }
7182
7283 [ TestMethod ]
73- public void TestReadWriteMutationWithParseUserConstructor ( )
84+ [ Description ( "Tests that users permission change accordingly" ) ]
85+ public void TestReadWriteMutationWithParseUserConstructor ( ) // 1
7486 {
7587 // Arrange
7688 ParseUser owner = GenerateUser ( "OwnerUser" ) ;
@@ -93,7 +105,8 @@ public void TestReadWriteMutationWithParseUserConstructor()
93105 }
94106
95107 [ TestMethod ]
96- public void TestParseACLCreationWithNullObjectIdParseUser ( )
108+ [ Description ( "Tests if throws if try to instantiate using a ParseUser without objectId." ) ]
109+ public void TestParseACLCreationWithNullObjectIdParseUser ( ) // 1
97110 {
98111 // Assert
99112 Assert . ThrowsException < ArgumentException > ( ( ) => new ParseACL ( GenerateUser ( default ) ) ) ;
@@ -102,22 +115,25 @@ public void TestParseACLCreationWithNullObjectIdParseUser()
102115 ParseUser GenerateUser ( string objectID )
103116 {
104117 // Use the mock to simulate generating a ParseUser
105- var state = new MutableObjectState { ObjectId = objectID } ;
118+ var state = new MutableObjectState { ObjectId = objectID , ClassName = "_User" } ;
106119 return Client . GenerateObjectFromState < ParseUser > ( state , "_User" ) ;
120+
107121 }
108122
109123 [ 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
111126 {
112127 // Arrange
113128 var state = new MutableObjectState { ObjectId = "123" , ClassName = null } ;
114129 var defaultClassName = "_User" ;
115130
131+
116132 var serviceHubMock = new Mock < IServiceHub > ( ) ;
117133 var classControllerMock = new Mock < IParseObjectClassController > ( ) ;
118134
119135 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 ( ) ) ;
121137
122138 // Act
123139 var user = classControllerMock . Object . GenerateObjectFromState < ParseUser > ( state , defaultClassName , serviceHubMock . Object ) ;
@@ -126,5 +142,137 @@ public void TestGenerateObjectFromState()
126142 Assert . IsNotNull ( user ) ;
127143 Assert . AreEqual ( defaultClassName , user . ClassName ) ;
128144 }
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+ }
129219
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