|
1 | 1 | using System;
|
2 | 2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
| 3 | +using Moq; // Add Moq for mocking if not already added |
3 | 4 | using Parse.Infrastructure;
|
4 | 5 | using Parse.Platform.Objects;
|
| 6 | +using Parse.Abstractions.Infrastructure; |
| 7 | +using Parse.Abstractions.Platform.Objects; |
5 | 8 |
|
6 |
| -namespace Parse.Tests |
| 9 | +namespace Parse.Tests; |
| 10 | + |
| 11 | +[TestClass] |
| 12 | +public class ACLTests |
7 | 13 | {
|
8 |
| - [TestClass] |
9 |
| - public class ACLTests |
| 14 | + ParseClient Client { get; set; } |
| 15 | + |
| 16 | + Mock<IServiceHub> ServiceHubMock { get; set; } |
| 17 | + Mock<IParseObjectClassController> ClassControllerMock { get; set; } |
| 18 | + |
| 19 | + [TestInitialize] |
| 20 | + public void Initialize() |
10 | 21 | {
|
11 |
| - ParseClient Client { get; set; } = new ParseClient(new ServerConnectionData { Test = true }); |
| 22 | + // Mock ServiceHub |
| 23 | + ServiceHubMock = new Mock<IServiceHub>(); |
| 24 | + ClassControllerMock = new Mock<IParseObjectClassController>(); |
12 | 25 |
|
13 |
| - [TestInitialize] |
14 |
| - public void Initialize() |
15 |
| - { |
16 |
| - Client.AddValidClass<ParseUser>(); |
17 |
| - Client.AddValidClass<ParseSession>(); |
18 |
| - } |
| 26 | + // Mock ClassController behavior |
| 27 | + ServiceHubMock.Setup(hub => hub.ClassController).Returns(ClassControllerMock.Object); |
19 | 28 |
|
20 |
| - [TestCleanup] |
21 |
| - public void Clean() => (Client.Services as ServiceHub).Reset(); |
| 29 | + // Mock ClassController.Instantiate behavior |
| 30 | + ClassControllerMock.Setup(controller => controller.Instantiate(It.IsAny<string>(), It.IsAny<IServiceHub>())) |
| 31 | + .Returns<string, IServiceHub>((className, hub) => |
| 32 | + { |
| 33 | + var user = new ParseUser(); |
| 34 | + user.Bind(hub); // Ensure the object is bound to the service hub |
| 35 | + return user; |
| 36 | + }); |
22 | 37 |
|
23 |
| - [TestMethod] |
24 |
| - public void TestCheckPermissionsWithParseUserConstructor() |
| 38 | + // Set up ParseClient with the mocked ServiceHub |
| 39 | + Client = new ParseClient(new ServerConnectionData { Test = true }) |
25 | 40 | {
|
26 |
| - ParseUser owner = GenerateUser("OwnerUser"); |
27 |
| - ParseUser user = GenerateUser("OtherUser"); |
28 |
| - ParseACL acl = new ParseACL(owner); |
29 |
| - Assert.IsTrue(acl.GetReadAccess(owner.ObjectId)); |
30 |
| - Assert.IsTrue(acl.GetWriteAccess(owner.ObjectId)); |
31 |
| - Assert.IsTrue(acl.GetReadAccess(owner)); |
32 |
| - Assert.IsTrue(acl.GetWriteAccess(owner)); |
33 |
| - } |
34 |
| - |
35 |
| - [TestMethod] |
36 |
| - public void TestReadWriteMutationWithParseUserConstructor() |
37 |
| - { |
38 |
| - ParseUser owner = GenerateUser("OwnerUser"); |
39 |
| - ParseUser otherUser = GenerateUser("OtherUser"); |
40 |
| - ParseACL acl = new ParseACL(owner); |
41 |
| - acl.SetReadAccess(otherUser, true); |
42 |
| - acl.SetWriteAccess(otherUser, true); |
43 |
| - acl.SetReadAccess(owner.ObjectId, false); |
44 |
| - acl.SetWriteAccess(owner.ObjectId, false); |
45 |
| - Assert.IsTrue(acl.GetReadAccess(otherUser.ObjectId)); |
46 |
| - Assert.IsTrue(acl.GetWriteAccess(otherUser.ObjectId)); |
47 |
| - Assert.IsTrue(acl.GetReadAccess(otherUser)); |
48 |
| - Assert.IsTrue(acl.GetWriteAccess(otherUser)); |
49 |
| - Assert.IsFalse(acl.GetReadAccess(owner)); |
50 |
| - Assert.IsFalse(acl.GetWriteAccess(owner)); |
51 |
| - } |
52 |
| - |
53 |
| - [TestMethod] |
54 |
| - public void TestParseACLCreationWithNullObjectIdParseUser() => Assert.ThrowsException<ArgumentException>(() => new ParseACL(GenerateUser(default))); |
55 |
| - |
56 |
| - ParseUser GenerateUser(string objectID) => Client.GenerateObjectFromState<ParseUser>(new MutableObjectState { ObjectId = objectID }, "_User"); |
| 41 | + Services = ServiceHubMock.Object |
| 42 | + }; |
| 43 | + |
| 44 | + // Publicize the client to set ParseClient.Instance |
| 45 | + Client.Publicize(); |
| 46 | + |
| 47 | + // Add valid classes to the client |
| 48 | + Client.AddValidClass<ParseUser>(); |
| 49 | + Client.AddValidClass<ParseSession>(); |
| 50 | + } |
| 51 | + |
| 52 | + [TestCleanup] |
| 53 | + public void Clean() => (Client.Services as ServiceHub)?.Reset(); |
| 54 | + |
| 55 | + [TestMethod] |
| 56 | + public void TestCheckPermissionsWithParseUserConstructor() |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + ParseUser owner = GenerateUser("OwnerUser"); |
| 60 | + ParseUser user = GenerateUser("OtherUser"); |
| 61 | + |
| 62 | + // Act |
| 63 | + ParseACL acl = new ParseACL(owner); |
| 64 | + |
| 65 | + // Assert |
| 66 | + Assert.IsTrue(acl.GetReadAccess(owner.ObjectId)); |
| 67 | + Assert.IsTrue(acl.GetWriteAccess(owner.ObjectId)); |
| 68 | + Assert.IsTrue(acl.GetReadAccess(owner)); |
| 69 | + Assert.IsTrue(acl.GetWriteAccess(owner)); |
| 70 | + } |
| 71 | + |
| 72 | + [TestMethod] |
| 73 | + public void TestReadWriteMutationWithParseUserConstructor() |
| 74 | + { |
| 75 | + // Arrange |
| 76 | + ParseUser owner = GenerateUser("OwnerUser"); |
| 77 | + ParseUser otherUser = GenerateUser("OtherUser"); |
| 78 | + |
| 79 | + // Act |
| 80 | + ParseACL acl = new ParseACL(owner); |
| 81 | + acl.SetReadAccess(otherUser, true); |
| 82 | + acl.SetWriteAccess(otherUser, true); |
| 83 | + acl.SetReadAccess(owner.ObjectId, false); |
| 84 | + acl.SetWriteAccess(owner.ObjectId, false); |
| 85 | + |
| 86 | + // Assert |
| 87 | + Assert.IsTrue(acl.GetReadAccess(otherUser.ObjectId)); |
| 88 | + Assert.IsTrue(acl.GetWriteAccess(otherUser.ObjectId)); |
| 89 | + Assert.IsTrue(acl.GetReadAccess(otherUser)); |
| 90 | + Assert.IsTrue(acl.GetWriteAccess(otherUser)); |
| 91 | + Assert.IsFalse(acl.GetReadAccess(owner)); |
| 92 | + Assert.IsFalse(acl.GetWriteAccess(owner)); |
| 93 | + } |
| 94 | + |
| 95 | + [TestMethod] |
| 96 | + public void TestParseACLCreationWithNullObjectIdParseUser() |
| 97 | + { |
| 98 | + // Assert |
| 99 | + Assert.ThrowsException<ArgumentException>(() => new ParseACL(GenerateUser(default))); |
| 100 | + } |
| 101 | + |
| 102 | + ParseUser GenerateUser(string objectID) |
| 103 | + { |
| 104 | + // Use the mock to simulate generating a ParseUser |
| 105 | + var state = new MutableObjectState { ObjectId = objectID }; |
| 106 | + return Client.GenerateObjectFromState<ParseUser>(state, "_User"); |
57 | 107 | }
|
| 108 | + |
| 109 | + [TestMethod] |
| 110 | + public void TestGenerateObjectFromState() |
| 111 | + { |
| 112 | + // Arrange |
| 113 | + var state = new MutableObjectState { ObjectId = "123", ClassName = null }; |
| 114 | + var defaultClassName = "_User"; |
| 115 | + |
| 116 | + var serviceHubMock = new Mock<IServiceHub>(); |
| 117 | + var classControllerMock = new Mock<IParseObjectClassController>(); |
| 118 | + |
| 119 | + classControllerMock.Setup(controller => controller.Instantiate(It.IsAny<string>(), It.IsAny<IServiceHub>())) |
| 120 | + .Returns<string, IServiceHub>((className, hub) => new ParseUser()); |
| 121 | + |
| 122 | + // Act |
| 123 | + var user = classControllerMock.Object.GenerateObjectFromState<ParseUser>(state, defaultClassName, serviceHubMock.Object); |
| 124 | + |
| 125 | + // Assert |
| 126 | + Assert.IsNotNull(user); |
| 127 | + Assert.AreEqual(defaultClassName, user.ClassName); |
| 128 | + } |
| 129 | + |
58 | 130 | }
|
0 commit comments