Tests/TablePermissionsExist.Tests.ps1

Describe "Test-TablePermissionsExist" {
    function Create-SampleALProject {
        param (
            # path to create sample project in
            [Parameter(Mandatory=$true)]
            [string]
            $Path,
            # Determine whether to create a permissions file or not
            [Parameter(Mandatory=$true)]
            [string]
            [ValidateSet('None','Incomplete','Complete')]
            $PermissionsFile,
            # whether to include a sample table in the project
            [Parameter(Mandatory=$false)]
            [switch]
            $CreateTable,
            # whether to create the table in a Tests folder
            [Parameter(Mandatory=$false)]
            [switch]
            $CreateTableInTestsFolder
        )

        if ($CreateTableInTestsFolder.IsPresent) {
            New-Item -Path (Join-Path $Path 'Tests') -ItemType Directory
            $TablePath = Join-Path $Path 'Tests'
        }
        else {
            $TablePath = $Path
        }
    
        if ($CreateTable.IsPresent) {
            Set-Content -Value 'table 50000 "Permissions Test"
            {
            }'
 -Path (Join-Path $TablePath 'Tab50000.PermissionsTest.al')
        }
    
        switch ($PermissionsFile) {
            'None' {<#do nothing#>}
            'Incomplete' {Set-Content -Value '<PermissionSets><PermissionSet RoleID="TEST" RoleName="Test"></PermissionSet></PermissionSets>' -Path (Join-Path $Path 'extensionPermissionSet.xml')}
            'Complete' {Set-Content -Value '<PermissionSets><PermissionSet RoleID="TEST" RoleName="Test"><Permission><ObjectType>0</ObjectType><ObjectID>50000</ObjectID></Permission></PermissionSet></PermissionSets>' -Path (Join-Path $Path 'extensionPermissionSet.xml')}
        }
    }

    Context 'Complete permissions file' {
        Create-SampleALProject -Path $TestDrive -PermissionsFile Complete -CreateTable
        
        It "should return true" {
            Test-TablePermissionsExist -SourcePath $TestDrive | should be $true
        }
    }

    Context 'No permissions file' {
        Create-SampleALProject -Path $TestDrive -PermissionsFile None -CreateTable
        It "Should return false" {
            {Test-TablePermissionsExist -SourcePath $TestDrive -SuppressError} | Should throw 'Permissions file not found'
        }
    }

    Context 'Incomplete permissions file without surpressing the error' {
        Create-SampleALProject -Path $TestDrive -PermissionsFile Incomplete -CreateTable
        It "Should return false" {
            {Test-TablePermissionsExist -SourcePath $TestDrive} | should throw "Permission for table 50000 could not be found in permission file"
        }
    }

    Context 'Incomplete permissions file and surpress the error' {
        Create-SampleALProject -Path $TestDrive -PermissionsFile Incomplete -CreateTable
        It "Should return false" {
            Test-TablePermissionsExist -SourcePath $TestDrive -SuppressError | should be $false
        }
    }

    Context 'No tables added by project' {
        Create-SampleALProject -Path $TestDrive -PermissionsFile None
        It "Should return true" {
            Test-TablePermissionsExist -SourcePath $TestDrive | should be $true
        }
    }

    Context 'Table in Tests folder' {
        Create-SampleALProject -Path $TestDrive -PermissionsFile None -CreateTable -CreateTableInTestsFolder
        It "Should return true" {
            Test-TablePermissionsExist -SourcePath $TestDrive | should be $true
        }
    }
}