Skip to content

Commit 9fbd09d

Browse files
Fixes #211 Check SAME_AS_CONTAINER for container write access
* [Add] Check container write access in case of SAME_AS_CONTAINER ParticipantAccessRight * Add extra unit tests
1 parent 6e57453 commit 9fbd09d

File tree

2 files changed

+353
-9
lines changed

2 files changed

+353
-9
lines changed
Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="PermissionServiceTestFixture.cs" company="RHEA System S.A.">
3+
// Copyright (c) 2015-2021 RHEA System S.A.
4+
//
5+
// Author: Sam Gerené, Merlin Bieze, Alex Vorobiev, Naron Phou, Alexander van Delft.
6+
//
7+
// This file is part of CDP4 Web Services Community Edition.
8+
// The CDP4 Web Services Community Edition is the RHEA implementation of ECSS-E-TM-10-25 Annex A and Annex C.
9+
//
10+
// The CDP4 Web Services Community Edition is free software; you can redistribute it and/or
11+
// modify it under the terms of the GNU Affero General Public
12+
// License as published by the Free Software Foundation; either
13+
// version 3 of the License, or (at your option) any later version.
14+
//
15+
// The CDP4 Web Services Community Edition is distributed in the hope that it will be useful,
16+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
// Lesser General Public License for more details.
19+
//
20+
// You should have received a copy of the GNU Affero General Public License
21+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
// </copyright>
23+
// --------------------------------------------------------------------------------------------------------------------
24+
25+
namespace CDP4WebServices.API.Tests
26+
{
27+
using System;
28+
using System.Collections;
29+
using System.Collections.Generic;
30+
31+
using CDP4Authentication;
32+
33+
using CDP4Common.CommonData;
34+
using CDP4Common.DTO;
35+
36+
using CDP4Orm.Dao;
37+
using CDP4Orm.Dao.Resolve;
38+
39+
using CDP4WebServices.API.Services;
40+
using CDP4WebServices.API.Services.Authentication;
41+
using CDP4WebServices.API.Services.Authorization;
42+
43+
using Moq;
44+
45+
using Npgsql;
46+
47+
using NUnit.Framework;
48+
49+
using Definition = CDP4Common.DTO.Definition;
50+
using Thing = CDP4Common.DTO.Thing;
51+
52+
/// <summary>
53+
/// Test fixture for the <see cref="PermissionService"/> class
54+
/// </summary>
55+
[TestFixture]
56+
public class PermissionServiceTestFixture
57+
{
58+
/// <summary>
59+
/// The EngineeringModel partition.
60+
/// </summary>
61+
private const string SiteDirectoryPartition = "SiteDirectory";
62+
63+
/// <summary>
64+
/// The EngineeringModel partition.
65+
/// </summary>
66+
private const string EngineeringModelPartition = "EngineeringModel";
67+
68+
/// <summary>
69+
/// The Iteration partition.
70+
/// </summary>
71+
private const string IterationPartition = "Iteration";
72+
73+
private PermissionService permissionService;
74+
private Mock<IAccessRightKindService> accessRightKindService;
75+
private Mock<IResolveService> resolveService;
76+
private Mock<ParticipantDao> participantDao;
77+
78+
private AuthenticationPerson authenticationPerson;
79+
private static EngineeringModel engineeringModel = new(Guid.NewGuid(), 0);
80+
private static ParameterType parameterType = new TextParameterType(Guid.NewGuid(), 0);
81+
private static Iteration iteration = new(Guid.NewGuid(), 0);
82+
private static Requirement requirement = new(Guid.NewGuid(), 0);
83+
private static Definition definition = new(Guid.NewGuid(), 0);
84+
private static Definition definition2 = new(Guid.NewGuid(), 0);
85+
private static RequirementsSpecification requirementsSpecification = new(Guid.NewGuid(), 0);
86+
private static DomainOfExpertise domain = new(Guid.NewGuid(), 0);
87+
private static SiteDirectory siteDirectory = new(Guid.NewGuid(), 0);
88+
private Participant participant;
89+
90+
private Thing addContainerThingToCache = null;
91+
92+
[SetUp]
93+
public void TestSetup()
94+
{
95+
this.authenticationPerson = new AuthenticationPerson(Guid.NewGuid(), 0)
96+
{
97+
UserName = "TestRunner"
98+
};
99+
100+
this.permissionService = new PermissionService();
101+
102+
this.permissionService.Credentials = new Credentials
103+
{
104+
Person = this.authenticationPerson,
105+
EngineeringModelSetup = new EngineeringModelSetup(Guid.NewGuid(), 0)
106+
};
107+
108+
this.resolveService = new Mock<IResolveService>();
109+
110+
this.resolveService.Setup(x => x.ResolveItems(null, It.IsAny<string>(), It.IsAny<Dictionary<DtoInfo, DtoResolveHelper>>()))
111+
.Callback<NpgsqlTransaction, string, Dictionary<DtoInfo, DtoResolveHelper>>
112+
((npgsqlTransaction, partition, operationThingContainerCache) =>
113+
{
114+
if (this.addContainerThingToCache != null)
115+
{
116+
operationThingContainerCache.Add(new ContainerInfo(this.addContainerThingToCache.ClassKind.ToString(), this.addContainerThingToCache.Iid), new DtoResolveHelper(this.addContainerThingToCache));
117+
}
118+
});
119+
120+
this.permissionService.ResolveService = this.resolveService.Object;
121+
122+
this.accessRightKindService = new Mock<IAccessRightKindService>();
123+
124+
this.permissionService.AccessRightKindService = this.accessRightKindService.Object;
125+
126+
this.participant = new Participant(Guid.NewGuid(), 0)
127+
{
128+
Domain = new List<Guid> { domain.Iid },
129+
Person = this.authenticationPerson.Iid
130+
};
131+
132+
this.permissionService.Credentials.EngineeringModelSetup.Participant.Add(this.participant.Iid);
133+
134+
this.participantDao = new Mock<ParticipantDao>();
135+
136+
this.participantDao.Setup(
137+
x =>
138+
x.Read(null, It.IsAny<string>(), null, false))
139+
.Returns(new List<Participant>() { this.participant });
140+
141+
this.permissionService.ParticipantDao = this.participantDao.Object;
142+
143+
engineeringModel.Iteration.Add(iteration.Iid);
144+
requirement.Definition.Add(definition.Iid);
145+
parameterType.Definition.Add(definition2.Iid);
146+
siteDirectory.Domain.Add(domain.Iid);
147+
}
148+
149+
[Test]
150+
[TestCaseSource(nameof(TestCases))]
151+
public void VerifySameAsContainerPermissionAutorization(Thing containerThing, Thing thing, string partition)
152+
{
153+
//-------------------------------------------------------------
154+
// Setup
155+
//-------------------------------------------------------------
156+
this.addContainerThingToCache = containerThing;
157+
engineeringModel.Iteration.Add(iteration.Iid);
158+
159+
this.accessRightKindService.Setup(
160+
x =>
161+
x.QueryPersonAccessRightKind(It.IsAny<Credentials>(), thing.ClassKind.ToString()))
162+
.Returns(PersonAccessRightKind.SAME_AS_CONTAINER);
163+
164+
this.accessRightKindService.Setup(
165+
x =>
166+
x.QueryParticipantAccessRightKind(It.IsAny<Credentials>(), thing.ClassKind.ToString()))
167+
.Returns(ParticipantAccessRightKind.SAME_AS_CONTAINER);
168+
169+
var securityRequestContext = new RequestSecurityContext
170+
{
171+
ContainerReadAllowed = true, ContainerWriteAllowed = true
172+
};
173+
174+
//-------------------------------------------------------------
175+
176+
//-------------------------------------------------------------
177+
// container modify is allowed
178+
//-------------------------------------------------------------
179+
this.accessRightKindService.Setup(
180+
x =>
181+
x.QueryParticipantAccessRightKind(It.IsAny<Credentials>(), containerThing.ClassKind.ToString()))
182+
.Returns(ParticipantAccessRightKind.MODIFY);
183+
184+
this.accessRightKindService.Setup(
185+
x =>
186+
x.QueryPersonAccessRightKind(It.IsAny<Credentials>(), containerThing.ClassKind.ToString()))
187+
.Returns(PersonAccessRightKind.MODIFY);
188+
189+
Assert.IsTrue(
190+
this.permissionService.CanWrite(
191+
null,
192+
thing,
193+
thing.ClassKind.ToString(),
194+
partition,
195+
ServiceBase.UpdateOperation,
196+
securityRequestContext
197+
)
198+
);
199+
200+
//-------------------------------------------------------------
201+
202+
//-------------------------------------------------------------
203+
// container modify is NOT allowed
204+
//-------------------------------------------------------------
205+
this.accessRightKindService.Setup(
206+
x =>
207+
x.QueryParticipantAccessRightKind(It.IsAny<Credentials>(), containerThing.ClassKind.ToString()))
208+
.Returns(ParticipantAccessRightKind.READ);
209+
210+
this.accessRightKindService.Setup(
211+
x =>
212+
x.QueryPersonAccessRightKind(It.IsAny<Credentials>(), containerThing.ClassKind.ToString()))
213+
.Returns(PersonAccessRightKind.READ);
214+
215+
Assert.IsFalse(
216+
this.permissionService.CanWrite(
217+
null,
218+
thing,
219+
thing.ClassKind.ToString(),
220+
partition,
221+
ServiceBase.UpdateOperation,
222+
securityRequestContext
223+
)
224+
);
225+
226+
//-------------------------------------------------------------
227+
228+
//-------------------------------------------------------------
229+
// Create operation does not check container, but returns
230+
// RequestSecurityContext setting
231+
//-------------------------------------------------------------
232+
Assert.IsTrue(
233+
this.permissionService.CanWrite(
234+
null,
235+
thing,
236+
thing.ClassKind.ToString(),
237+
partition,
238+
ServiceBase.CreateOperation,
239+
securityRequestContext
240+
)
241+
);
242+
243+
//-------------------------------------------------------------
244+
245+
//-------------------------------------------------------------
246+
// container thing not found returns RequestSecurityContext setting
247+
//-------------------------------------------------------------
248+
this.addContainerThingToCache = null;
249+
250+
this.accessRightKindService.Setup(
251+
x =>
252+
x.QueryParticipantAccessRightKind(It.IsAny<Credentials>(), containerThing.ClassKind.ToString()))
253+
.Returns(ParticipantAccessRightKind.MODIFY);
254+
255+
Assert.IsFalse(
256+
this.permissionService.CanWrite(
257+
null,
258+
thing,
259+
thing.ClassKind.ToString(),
260+
partition,
261+
ServiceBase.UpdateOperation,
262+
securityRequestContext
263+
)
264+
);
265+
266+
//-------------------------------------------------------------
267+
}
268+
269+
/// <summary>
270+
/// Different Cases we want to check access rights for
271+
/// </summary>
272+
/// <returns>an <see cref="IEnumerable"/> of type <see cref="object[]"/>
273+
/// containing the <see cref="PermissionServiceTestFixture.VerifySameAsContainerPermissionAutorization"/> method's parameters.</returns>
274+
public static IEnumerable TestCases()
275+
{
276+
yield return new object[] { requirement, definition, IterationPartition };
277+
yield return new object[] { engineeringModel, iteration, EngineeringModelPartition };
278+
yield return new object[] { parameterType, definition2, SiteDirectoryPartition };
279+
yield return new object[] { iteration, requirementsSpecification, IterationPartition };
280+
yield return new object[] { siteDirectory, domain, SiteDirectoryPartition };
281+
}
282+
}
283+
}

0 commit comments

Comments
 (0)