-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathACLTests.cs
130 lines (106 loc) · 4.26 KB
/
ACLTests.cs
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
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq; // Add Moq for mocking if not already added
using Parse.Infrastructure;
using Parse.Platform.Objects;
using Parse.Abstractions.Infrastructure;
using Parse.Abstractions.Platform.Objects;
namespace Parse.Tests;
[TestClass]
public class ACLTests
{
ParseClient Client { get; set; }
Mock<IServiceHub> ServiceHubMock { get; set; }
Mock<IParseObjectClassController> ClassControllerMock { get; set; }
[TestInitialize]
public void Initialize()
{
// Mock ServiceHub
ServiceHubMock = new Mock<IServiceHub>();
ClassControllerMock = new Mock<IParseObjectClassController>();
// Mock ClassController behavior
ServiceHubMock.Setup(hub => hub.ClassController).Returns(ClassControllerMock.Object);
// Mock ClassController.Instantiate behavior
ClassControllerMock.Setup(controller => controller.Instantiate(It.IsAny<string>(), It.IsAny<IServiceHub>()))
.Returns<string, IServiceHub>((className, hub) =>
{
var user = new ParseUser();
user.Bind(hub); // Ensure the object is bound to the service hub
return user;
});
// Set up ParseClient with the mocked ServiceHub
Client = new ParseClient(new ServerConnectionData { Test = true })
{
Services = ServiceHubMock.Object
};
// Publicize the client to set ParseClient.Instance
Client.Publicize();
// Add valid classes to the client
Client.AddValidClass<ParseUser>();
Client.AddValidClass<ParseSession>();
}
[TestCleanup]
public void Clean() => (Client.Services as ServiceHub)?.Reset();
[TestMethod]
public void TestCheckPermissionsWithParseUserConstructor()
{
// Arrange
ParseUser owner = GenerateUser("OwnerUser");
ParseUser user = GenerateUser("OtherUser");
// Act
ParseACL acl = new ParseACL(owner);
// Assert
Assert.IsTrue(acl.GetReadAccess(owner.ObjectId));
Assert.IsTrue(acl.GetWriteAccess(owner.ObjectId));
Assert.IsTrue(acl.GetReadAccess(owner));
Assert.IsTrue(acl.GetWriteAccess(owner));
}
[TestMethod]
public void TestReadWriteMutationWithParseUserConstructor()
{
// Arrange
ParseUser owner = GenerateUser("OwnerUser");
ParseUser otherUser = GenerateUser("OtherUser");
// Act
ParseACL acl = new ParseACL(owner);
acl.SetReadAccess(otherUser, true);
acl.SetWriteAccess(otherUser, true);
acl.SetReadAccess(owner.ObjectId, false);
acl.SetWriteAccess(owner.ObjectId, false);
// Assert
Assert.IsTrue(acl.GetReadAccess(otherUser.ObjectId));
Assert.IsTrue(acl.GetWriteAccess(otherUser.ObjectId));
Assert.IsTrue(acl.GetReadAccess(otherUser));
Assert.IsTrue(acl.GetWriteAccess(otherUser));
Assert.IsFalse(acl.GetReadAccess(owner));
Assert.IsFalse(acl.GetWriteAccess(owner));
}
[TestMethod]
public void TestParseACLCreationWithNullObjectIdParseUser()
{
// Assert
Assert.ThrowsException<ArgumentException>(() => new ParseACL(GenerateUser(default)));
}
ParseUser GenerateUser(string objectID)
{
// Use the mock to simulate generating a ParseUser
var state = new MutableObjectState { ObjectId = objectID };
return Client.GenerateObjectFromState<ParseUser>(state, "_User");
}
[TestMethod]
public void TestGenerateObjectFromState()
{
// Arrange
var state = new MutableObjectState { ObjectId = "123", ClassName = null };
var defaultClassName = "_User";
var serviceHubMock = new Mock<IServiceHub>();
var classControllerMock = new Mock<IParseObjectClassController>();
classControllerMock.Setup(controller => controller.Instantiate(It.IsAny<string>(), It.IsAny<IServiceHub>()))
.Returns<string, IServiceHub>((className, hub) => new ParseUser());
// Act
var user = classControllerMock.Object.GenerateObjectFromState<ParseUser>(state, defaultClassName, serviceHubMock.Object);
// Assert
Assert.IsNotNull(user);
Assert.AreEqual(defaultClassName, user.ClassName);
}
}