<commit_before>$svc = Enter-Appclusive LAB3;

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")

function Stop-Pester($message = "EMERGENCY: Script cannot continue.")
{
	$msg = $message;
	$e = New-CustomErrorRecord -msg $msg -cat OperationStopped -o $msg;
	$PSCmdlet.ThrowTerminatingError($e);
}

Describe -Tags "Acl.Tests" "Acl.Tests" {

	Mock Export-ModuleMember { return $null; }	
	. "$here\$sut"
	. "$here\DeleteNode.ps1"
	
	$entityPrefix = "TestItem-";
	$usedEntitySets = @("Nodes", "Aces", "Acls");
	
	Context "#CLOUDTCL-1871-AclTests" {
		
		BeforeEach {
			$moduleName = 'biz.dfch.PS.Appclusive.Client';
			Remove-Module $moduleName -ErrorAction:SilentlyContinue;
			Import-Module $moduleName;
			$svc = Enter-Appclusive LAB3;
		}
		
		AfterEach {
            $svc = Enter-Appclusive LAB3;
            $entityFilter = "startswith(Name, '{0}')" -f $entityPrefix;

            foreach ($entitySet in $usedEntitySets)
            {
                $entities = $svc.Core.$entitySet.AddQueryOption('$filter', $entityFilter) | Select;
         
                foreach ($entity in $entities)
                {
                    Remove-ApcEntity -svc $svc -Id $entity.Id -EntitySetName $entitySet -Confirm:$false;
                }
            }
        }
		<#
		It "CreateAndDeleteAcl" -Test {
		
			#ARRANGE
			$nodeName = $entityPrefix + "newtestnode";
			$aclName = $entityPrefix + "Acl";
			
			#ACT create node
			$newNode = Create-Node -svc $svc -Name $nodeName | select;
			
			#get Id of the node
			$nodeId = $newNode.Id;
			
			#ACT create acl
			$acl = Create-Acl -svc $svc -aclName $aclName -entityId $nodeId | select;
			
			#get Id of the acl
			$aclId = $acl.Id;
			
			#ACT - delete acl
			Delete-Acl -svc $svc -aclId $aclId;
		}
		
		It "Acl-UpdateNameAndDescripton" -Test {
			
			#ARRANGE
			$nodeName = $entityPrefix + "newtestnode";
			$aclName = $entityPrefix + "Acl";
			$newAclName = $aclName + "Updated";
			$newAclDescription = "Updated Description";
			
			#ACT create node
			$newNode = Create-Node -svc $svc -Name $nodeName | select;
			
			#get Id of the node
			$nodeId = $newNode.Id;
			
			#ACT create acl
			$acl = Create-Acl -svc $svc -aclName $aclName -entityId $nodeId | select;
			
			#get Id of the acl
			$aclId = $acl.Id;
								
			#ACT update acl
			$updatedAcl = Update-Acl -svc $svc -aclId $aclId -newAclName $newAclName -newAclDescription $newAclDescription;
			}
		#>
		It "DeleteAclThatIsReferencedOnAce" -Test {
			
			#ARRANGE
			$nodeName = $entityPrefix + "newtestnode";
			$aclName = $entityPrefix + "Acl";
			$aceName = $entityPrefix + "Ace";
			
			#ACT create node
			$newNode = Create-Node -svc $svc -Name $nodeName | select;
			
			#get Id of the node
			$nodeId = $newNode.Id;
			
			#ACT create acl
			$acl = Create-Acl -svc $svc -aclName $aclName -entityId $nodeId | select;
			
			#get Id of the acl
			$aclId = $acl.Id;
			
			#ACT Create Ace
			$ace = Create-Ace -svc $svc -aceName $aceName -aclId $aclId | select;
			
			#get the Id of the ace
			$aceId = $ace.Id;
			
			try
			{
				#ARRANGE get the acl
				$query = "Id eq {0}" -f $aclId;
				$acl = $svc.Core.Acls.AddQueryOption('$filter', $query) | select;
				
				#ACT Delete acl
				$svc.Core.DeleteObject($acl);
				{ $result = $svc.Core.SaveChanges(); } | Should Throw;	
			}
			
			catch 
			{
				$threwException = $true;
			}
		}
		
		It "Acl-GetAcesOfAcl" {
			
			#ARRANGE
			$nodeName = $entityPrefix + "newtestnode";
			$aclName = $entityPrefix + "Acl";
			$aceName1 = $entityPrefix + "Ace1";
			$aceName2 = $entityPrefix + "Ace2";
			
			#ACT create node
			$newNode = Create-Node -svc $svc -Name $nodeName | select;
			
			#get Id of the node
			$nodeId = $newNode.Id;
			
			#ACT create acl
			$acl = Create-Acl -svc $svc -aclName $aclName -entityId $nodeId | select;
			
			#get Id of the acl
			$aclId = $acl.Id;
			
			#ACT Create 2 Aces that reference the Acl
			$ace1 = Create-Ace -svc $svc -aceName $aceName1 -aclId $aclId | select;
			$ace2 = Create-Ace -svc $svc -aceName $aceName2 -aclId $aclId | select;

			#ACT Get Ace and load Aces
			$query = "Id eq {0}" -f $aclId;
			$acl = $svc.Core.Acls.AddQueryOption('$filter', $query) | select;
			$acesOfAcl = $svc.Core.LoadProperty($acl, 'Aces') | Select;
			
			#ASSERT
			$acesOfAcl.Id -contains $ace1.Id | Should be $True;
			$acesOfAcl.Id -contains $ace2.Id | Should be $True;
			}
		}
	
	<#
	Context "#CLOUDTCL-1872-AceTests" {
		BeforeEach {
			$moduleName = 'biz.dfch.PS.Appclusive.Client';
			Remove-Module $moduleName -ErrorAction:SilentlyContinue;
			Import-Module $moduleName;
			$svc = Enter-ApcServer;
			
			# Create Acl for Ace tests
			$aclName = "Test Acl for Ace tests";
			$aclDescription = "TestNode used in Test";		
			$acl = CreateAcl -aclName $aclName -aclDescription $aclDescription;	
			$svc.Core.AddToAcls($acl);
			$result = $svc.core.SaveChanges();
			$result.StatusCode | Should be 201;
			$acl.Id | Should Not Be 0;
			
			$result = $null;
		}
		
		AfterEach {
			# Cleanup Acl
			$svc = Enter-ApcServer;
			$svc.Core.AttachTo('Acls', $acl);
			$svc.Core.DeleteObject($acl);
			$result = $svc.Core.SaveChanges();
			$result.StatusCode | Should Be 204;
		}

		It "Ace-CreateAndDeleteAce" -Test {
			try {
				# Arrange
				$aceName = "Test Ace"
				$aceDescription = "Ace used in tests"
				$aceAclId = $acl.Id;
				$aceAction = "ALLOW";
				
				$ace = CreateAce -aceName $aceName -aceDescription $aceDescription -aceAclId $aceAclId -aceAction $aceAction;	
				
				# Act
				$svc.Core.AddToAces($ace);
				$result = $svc.core.SaveChanges();
				
				# Assert
				$result.StatusCode | Should be 201;
				$ace.Id | Should Not Be 0;
				
			} 			
			Finally {
				#Cleanup	
				$svc.Core.DeleteObject($ace);
				$result = $svc.Core.SaveChanges();
				$result.StatusCode | Should Be 204;
			}
		}
		
		It "Ace-UpdateNameDescription" -Test {
			try {
				# Arrange Create
				$aceName = "Test Ace"
				$aceDescription = "Ace used in tests"
				$aceAclId = $acl.Id;
				$aceAction = "ALLOW";
				
				$ace = CreateAce -aceName $aceName -aceDescription $aceDescription -aceAclId $aceAclId -aceAction $aceAction;	
				
				# Act Create
				$svc.Core.AddToAces($ace);
				$result = $svc.core.SaveChanges();
				
				# Assert Create
				$result.StatusCode | Should be 201;
				$ace.Id | Should Not Be 0;
				
				# Arrange Update
				$aceSetName = "Updated"
				$aceSetDescription = "Ace used in tests (updated)"
				$ace.Name = $aceSetName;
				$ace.Description = $aceSetDescription;
								
				# Act
				$svc.Core.UpdateObject($ace)
				$result = $svc.core.SaveChanges();	
				
				# Assert<commit_msg>* create and delete ace test fixed<commit_after>$svc = Enter-Appclusive LAB3;

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")

function Stop-Pester($message = "EMERGENCY: Script cannot continue.")
{
	$msg = $message;
	$e = New-CustomErrorRecord -msg $msg -cat OperationStopped -o $msg;
	$PSCmdlet.ThrowTerminatingError($e);
}

Describe -Tags "Acl.Tests" "Acl.Tests" {

	Mock Export-ModuleMember { return $null; }	
	. "$here\$sut"
	. "$here\DeleteNode.ps1"
	
	$entityPrefix = "TestItem-";
	$usedEntitySets = @("Nodes", "Aces", "Acls");
	
	Context "#CLOUDTCL-1871-AclTests" {
		
		BeforeEach {
			$moduleName = 'biz.dfch.PS.Appclusive.Client';
			Remove-Module $moduleName -ErrorAction:SilentlyContinue;
			Import-Module $moduleName;
			$svc = Enter-Appclusive LAB3;
		}
		
		AfterEach {
            $svc = Enter-Appclusive LAB3;
            $entityFilter = "startswith(Name, '{0}')" -f $entityPrefix;

            foreach ($entitySet in $usedEntitySets)
            {
                $entities = $svc.Core.$entitySet.AddQueryOption('$filter', $entityFilter) | Select;
         
                foreach ($entity in $entities)
                {
                    Remove-ApcEntity -svc $svc -Id $entity.Id -EntitySetName $entitySet -Confirm:$false;
                }
            }
        }
		
		It "CreateAndDeleteAcl" -Test {
		
			#ARRANGE
			$nodeName = $entityPrefix + "newtestnode";
			$aclName = $entityPrefix + "Acl";
			
			#ACT create node
			$newNode = Create-Node -svc $svc -Name $nodeName | select;
			
			#get Id of the node
			$nodeId = $newNode.Id;
			
			#ACT create acl
			$acl = Create-Acl -svc $svc -aclName $aclName -entityId $nodeId | select;
			
			#get Id of the acl
			$aclId = $acl.Id;
			
			#ACT - delete acl
			Delete-Acl -svc $svc -aclId $aclId;
		}
		
		It "Acl-UpdateNameAndDescripton" -Test {
			
			#ARRANGE
			$nodeName = $entityPrefix + "newtestnode";
			$aclName = $entityPrefix + "Acl";
			$newAclName = $aclName + "Updated";
			$newAclDescription = "Updated Description";
			
			#ACT create node
			$newNode = Create-Node -svc $svc -Name $nodeName | select;
			
			#get Id of the node
			$nodeId = $newNode.Id;
			
			#ACT create acl
			$acl = Create-Acl -svc $svc -aclName $aclName -entityId $nodeId | select;
			
			#get Id of the acl
			$aclId = $acl.Id;
								
			#ACT update acl
			$updatedAcl = Update-Acl -svc $svc -aclId $aclId -newAclName $newAclName -newAclDescription $newAclDescription;
			}
		
		It "DeleteAclThatIsReferencedOnAce" -Test {
			
			#ARRANGE
			$nodeName = $entityPrefix + "newtestnode";
			$aclName = $entityPrefix + "Acl";
			$aceName = $entityPrefix + "Ace";
			
			#ACT create node
			$newNode = Create-Node -svc $svc -Name $nodeName | select;
			
			#get Id of the node
			$nodeId = $newNode.Id;
			
			#ACT create acl
			$acl = Create-Acl -svc $svc -aclName $aclName -entityId $nodeId | select;
			
			#get Id of the acl
			$aclId = $acl.Id;
			
			#ACT Create Ace
			$ace = Create-Ace -svc $svc -aceName $aceName -aclId $aclId | select;
			
			#get the Id of the ace
			$aceId = $ace.Id;
			
			try
			{
				#ARRANGE get the acl
				$query = "Id eq {0}" -f $aclId;
				$acl = $svc.Core.Acls.AddQueryOption('$filter', $query) | select;
				
				#ACT Delete acl
				$svc.Core.DeleteObject($acl);
				{ $result = $svc.Core.SaveChanges(); } | Should Throw;	
			}
			
			catch 
			{
				$threwException = $true;
			}
		}
		
		It "Acl-GetAcesOfAcl" {
			
			#ARRANGE
			$nodeName = $entityPrefix + "newtestnode";
			$aclName = $entityPrefix + "Acl";
			$aceName1 = $entityPrefix + "Ace1";
			$aceName2 = $entityPrefix + "Ace2";
			
			#ACT create node
			$newNode = Create-Node -svc $svc -Name $nodeName | select;
			
			#get Id of the node
			$nodeId = $newNode.Id;
			
			#ACT create acl
			$acl = Create-Acl -svc $svc -aclName $aclName -entityId $nodeId;
						
			#get Id of the acl
			$aclId = $acl.Id;
			
			#ACT Create 2 Aces that reference the Acl
			$ace1 = Create-Ace -svc $svc -aceName $aceName1 -aclId $aclId | select;
			$ace2 = Create-Ace -svc $svc -aceName $aceName2 -aclId $aclId | select;
			
			$acesOfAcl = $svc.Core.LoadProperty($acl, 'Aces') | Select;
			
			#ASSERT
			$acesOfAcl.Id -contains $ace1.Id | Should be $True;
			$acesOfAcl.Id -contains $ace2.Id | Should be $True;
			}
		}
	
	
	Context "#CLOUDTCL-1872-AceTests" {
		
		BeforeEach {
			$moduleName = 'biz.dfch.PS.Appclusive.Client';
			Remove-Module $moduleName -ErrorAction:SilentlyContinue;
			Import-Module $moduleName;
			$svc = Enter-Appclusive LAB3;
		}
		
		AfterEach {
            $svc = Enter-Appclusive LAB3;
            $entityFilter = "startswith(Name, '{0}')" -f $entityPrefix;

            foreach ($entitySet in $usedEntitySets)
            {
                $entities = $svc.Core.$entitySet.AddQueryOption('$filter', $entityFilter) | Select;
         
                foreach ($entity in $entities)
                {
                    Remove-ApcEntity -svc $svc -Id $entity.Id -EntitySetName $entitySet -Confirm:$false;
                }
            }
        }
		
		It "Ace-CreateAndDeleteAce" -Test {
			#ARRANGE
			$nodeName = $entityPrefix + "newtestnode";
			$aclName = $entityPrefix + "Acl";
			$aceName = $entityPrefix + "Ace";
			
			#ACT create node
			$newNode = Create-Node -svc $svc -Name $nodeName | select;
			
			#get Id of the node
			$nodeId = $newNode.Id;
			
			#ACT create acl
			$acl = Create-Acl -svc $svc -aclName $aclName -entityId $nodeId | select;
			
			#get Id of the acl
			$aclId = $acl.Id;
			
			#ACT Create Ace
			$ace = Create-Ace -svc $svc -aceName $aceName -aclId $aclId | select;
			
			#get the Id of the ace
			$aceId = $ace.Id;
			
			#ACT Delete Ace
			Delete-Ace -svc $svc -aceId $aceId;
			}
		}
		<#
		It "Ace-UpdateNameDescription" -Test {
			try {
				# Arrange Create
				$aceName = "Test Ace"
				$aceDescription = "Ace used in tests"
				$aceAclId = $acl.Id;
				$aceAction = "ALLOW";
				
				$ace = CreateAce -aceName $aceName -aceDescription $aceDescription -aceAclId $aceAclId -aceAction $aceAction;	
				
				# Act Create
				$svc.Core.AddToAces($ace);
				$result = $svc.core.SaveChanges();
				
				# Assert Create
				$result.StatusCode | Should be 201;
				$ace.Id | Should Not Be 0;
				
				# Arrange Update
				$aceSetName = "Updated"
				$aceSetDescription = "Ace used in tests (updated)"
				$ace.Name = $aceSetName;
				$ace.Description = $aceSetDescription;
								
				# Act
				$svc.Core.UpdateObject($ace)
				$result = $svc.core.SaveChanges();	
				
				# Assert
{'fraction_non_alphanumeric': 0.13454162593066105, 'fraction_numerical': 0.005715574941716177, 'mean_word_length': 2.464825429911412, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2307664', 'n_tokens_mistral': 5893, 'n_tokens_neox': 4804, 'n_words': 1167}