Tests/Unit/Public/CallableEntity/Get-TeamsCallableEntity.Tests.ps1

# Module: Orbit.Teams
# Function: Test
# Author: David Eberhardt
# Updated: 23-JUL-2022

# Script Analyzer Exceptions
#[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUserDeclaredVarsMoreThanAssignments', '', Justification = 'Context Boundaries')]


# Unit Tests
Describe -Tags ('Unit', 'Acceptance') "Function '$(((Split-Path -Leaf $PsCommandPath) -replace '\.Tests\.', '.') -replace '\.ps1', '')'" {
  InModuleScope -ModuleName 'Orbit.Teams' {
    BeforeAll {
      # Mocking basic connection commands to avoid connectivity related errors
      Mock Assert-GraphConnection -MockWith { $true }
      Mock Test-GraphConnection -MockWith { $true }
      Mock Assert-MicrosoftTeamsConnection -MockWith { $true }
      Mock Test-MicrosoftTeamsConnection -MockWith { $true }

      # Splatting Parameters
      $Params = @{
        WarningAction     = 'SilentlyContinue'
        InformationAction = 'SilentlyContinue'
      }

      # Dot Sourcing Mock Objects
      . "$(Split-Path -Parent ((Split-Path -Parent $PsScriptRoot) -split 'Tests')[0])\Orbit\Tests\Testing-MockedObjects.ps1"
    }

    Context 'Input' {
      # Pipeline, Position, etc.

    }

    Context 'Execution' {
      # Code Logic

    }

    Context 'Output' {
      #Properties, Values, Types

    }

    # OLD Context blocks
    Context 'Channel - Callable Entity is not found (Team not found)' {
      BeforeEach {
        Mock Get-TeamsTeamAndChannel { return $(throw), $(throw) }
        Mock Get-MgGroup -MockWith { $null }
        $global:OrbitQueryTenantGraphGroups = $GraphGroup
      }

      It 'should return an Object of type Unknown' {
        $Identity = 'Trunk Company\General'
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty

        $Identity = 'Trunk Company\19:7knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty

        $Identity = '7c105977-d636-419a-b5f3-4885375e432a\General'
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty

        $Identity = '7c105977-d636-419a-b5f3-4885375e432a\19:7knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty
      }
    }

    Context 'Channel - Callable Entity is not found (Team found but Channel not found)' {
      # Reference Object to be created by Get-TeamsCallableEntity - not used in Tests
      BeforeEach {
        Mock Get-TeamsTeamAndChannel { return $TeamGood, $(throw) }
        Mock Get-MgGroup -MockWith { $null }
        $global:OrbitQueryTenantGraphGroups = $GraphGroup
      }

      It 'should return an Object of type Unknown' {
        $Identity = 'Night Watch\Bribes'
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty

        $Identity = 'Night Watch\19:7knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty

        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\Bribes'
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty

        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\19:7knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty
      }
    }

    Context 'Channel - Callable Entity is found' {
      BeforeEach {
        # Using Testing-MockedObjects.ps1: $TeamGood and $ChannelGood
        Mock Get-TeamsTeamAndChannel { return $TeamGood, $ChannelGood }
        $global:OrbitQueryTenantGraphGroups = $GraphGroup
      }

      It 'should return an Object of type Channel' {
        $Identity = 'Night Watch\Night Watch - Duty Roster'
        (Get-TeamsCallableEntity -Identity $Identity).ObjectType | Should -Be 'Channel'

        $Identity = 'Night Watch\19:6knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        (Get-TeamsCallableEntity -Identity $Identity).ObjectType | Should -Be 'Channel'

        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\Night Watch - Duty Roster'
        (Get-TeamsCallableEntity -Identity $Identity).ObjectType | Should -Be 'Channel'

        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\19:6knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        (Get-TeamsCallableEntity -Identity $Identity).ObjectType | Should -Be 'Channel'
      }
    }

    Context 'LineURI - String is malformed' {
      It 'should return an Object of type Unknown' {
        # $script:OrbitRegexPhoneNumber matches 4 to 20 digits - Extensions between 3 and 8 digits - Testing out of bounds
        $Identity = 123
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty

        $Identity = '123;ext=12'
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty

        $Identity = '123456789012345678901'
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty

        $Identity = '12345678901234567890;ext=123456789' # $script:OrbitRegexPhoneNumber matches extensions 3 to 8 digits
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty
      }
    }

    Context 'LineURI - String is an acceptable string' {
      It 'should return an Object of type ExternalPstn' {
        # $script:OrbitRegexPhoneNumber matches 4 to 20 digits - Extensions between 3 and 8 digits
        $Identity = '15551234567'
        (Get-TeamsCallableEntity -Identity $Identity).Type | Should -Be 'ExternalPstn'

        $Identity = '+15551234567'
        (Get-TeamsCallableEntity -Identity $Identity).Type | Should -Be 'ExternalPstn'

        $Identity = '+1 (555) 1234567'
        (Get-TeamsCallableEntity -Identity $Identity).Type | Should -Be 'ExternalPstn'

        $Identity = '1 (555) 1234-567'
        (Get-TeamsCallableEntity -Identity $Identity).Type | Should -Be 'ExternalPstn'

        $Identity = '1 (555) 123-4567'
        (Get-TeamsCallableEntity -Identity $Identity).Type | Should -Be 'ExternalPstn'

        $Identity = '1 (555) 123-4-567'
        (Get-TeamsCallableEntity -Identity $Identity).Type | Should -Be 'ExternalPstn'

        $Identity = 'tel:+15551234567'
        (Get-TeamsCallableEntity -Identity $Identity).Type | Should -Be 'ExternalPstn'

        $Identity = '1234'
        (Get-TeamsCallableEntity -Identity $Identity).Type | Should -Be 'ExternalPstn'

        $Identity = 'tel:+12345678901234567890;ext=123'
        (Get-TeamsCallableEntity -Identity $Identity).Type | Should -Be 'ExternalPstn'

        $Identity = 'tel:+12345678901234567890'
        (Get-TeamsCallableEntity -Identity $Identity).Type | Should -Be 'ExternalPstn'

        $Identity = 'tel:+12345678901234567890;ext=12345678'
        (Get-TeamsCallableEntity -Identity $Identity).Type | Should -Be 'ExternalPstn'
      }
    }

    Context 'User - Callable Entity is not found' {
      BeforeEach {
        Mock Get-MgUser -MockWith { $null }
        Mock Get-MgGroup -MockWith { $null }
        Mock Get-CsOnlineApplicationInstance -MockWith { $null }
      }
      It 'should return an Object of type Unknown' {
        $Identity = 'TheLastElephant@discworld.onmicrosoft.com'
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty
      }
    }

    Context 'User - Callable Entity is found' {
      BeforeEach {
        # Using Testing-MockedObjects.ps1: $GraphUserUserGood1
        Mock Get-MgUser { return $GraphUserUserGood1 }
        Mock Get-CsOnlineApplicationInstance { throw }
      }
      It 'should return an Object of type User' {
        $Identity = 'SamVimes@discworld.onmicrosoft.com'
        (Get-TeamsCallableEntity -Identity $Identity @Params).ObjectType | Should -Be 'User'
      }
    }

    Context 'ApplicationInstance - Callable Entity is not found' {
      BeforeEach {
        Mock Get-MgUser -MockWith { $null }
        Mock Get-CsOnlineApplicationInstance -MockWith { $null }
        $global:OrbitQueryTenantGraphGroups = $GraphGroup
      }

      It 'should return an Object of type Unknown' {
        $Identity = 'NightWatchDisconnect@discworld.onmicrosoft.com'
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty
      }
    }

    Context 'ApplicationInstance - Callable Entity is found' {
      BeforeEach {
        # Using Testing-MockedObjects.ps1: $GraphUserRA, $CsOnlineApplicationInstanceGood1
        Mock Get-MgUser { return $GraphUserRAGood1 }
        Mock Get-CsOnlineApplicationInstance { return $CsOnlineApplicationInstanceGood1 }
      }
      Context 'ApplicationInstance is found' {
        It 'should return an Object of type ResourceAccount' {
          $Identity = 'NightWatchRA@discworld.onmicrosoft.com'
          $CallableEntity = Get-TeamsCallableEntity -Identity $Identity @Params
          $CallableEntity.Entity | Should -Be $Identity
          $CallableEntity.ObjectType | Should -Be 'ResourceAccount'
        }
      }
    }

    Context 'Group - Callable Entity is not found' {
      BeforeEach {
        Mock Get-MgUser -MockWith { $null }
        Mock Get-MgGroup -MockWith { $null }
        Mock Get-CsOnlineApplicationInstance -MockWith { $null }
        $global:OrbitQueryTenantGraphGroups = $GraphGroup
      }

      It 'should return an Object of type Unknown' {
        $Identity = 'Committee for Tall People'
        (Get-TeamsCallableEntity -Identity $Identity @Params).Identity | Should -BeNullOrEmpty
      }
    }

    Context 'Group - Callable Entity is found' {
      BeforeEach {
        # Using Testing-MockedObjects.ps1: $GraphGroup
        Mock Get-MgUser -MockWith { $null }
        Mock Get-MgGroup { return $GraphGroup }
        Mock Get-CsOnlineApplicationInstance -MockWith { $null }
      }

      It 'should return an Object of type Group' {
        $Identity = 'equalheights@discworld.onmicrosoft.com'
        (Get-TeamsCallableEntity -Identity $Identity @Params).ObjectType | Should -Be 'Group'

        $Identity = 'Committee for Equal Heights'
        (Get-TeamsCallableEntity -Identity $Identity @Params).ObjectType | Should -Be 'Group'
      }
    }
  }
}