Tests/Private/Get-TeamAndChannel.Tests.ps1

# Module: TeamsFunctions
# Function: Test
# Author: David Eberhardt
# Updated: 24-JUL-2022





InModuleScope TeamsFunctions {
  BeforeAll {
    # Mocking basic connection commands to avoid connectivity related errors
    Mock Assert-AzureADConnection { return $true }
    Mock Assert-MicrosoftTeamsConnection { return $true }
    Mock Test-MicrosoftTeamsConnection { return $true }

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

    # Dot Sourcing Mock Objects
    . "$(Get-ParentDirectoryPath -TargetParentDirectory 'Tests' -DirectoryName $PSScriptRoot)\Public\Testing-MockedObjects.ps1"
  }

  Describe -Tags ('Unit', 'Acceptance') "Function 'Get-TeamAndChannel'" {
    Context 'Team not found' {
      BeforeEach {
        Mock Get-Team { throw }
      }

      It 'should return neither a Team Object nor a Channel Object' {
        $Identity = 'Trunk Company\General'
        { $Team, $Channel = Get-TeamAndChannel -String $Identity @Params } | Should -Throw
        $Team.GroupId | Should -BeNullOrEmpty
        $Channel.Id | Should -BeNullOrEmpty

        $Identity = 'Trunk Company\19:6knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        { $Team, $Channel = Get-TeamAndChannel -String $Identity @Params } | Should -Throw
        $Team.GroupId | Should -BeNullOrEmpty
        $Channel.Id | Should -BeNullOrEmpty

        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\General'
        { $Team, $Channel = Get-TeamAndChannel -String $Identity @Params } | Should -Throw
        $Team.GroupId | Should -BeNullOrEmpty
        $Channel.Id | Should -BeNullOrEmpty

        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\19:6knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        { $Team, $Channel = Get-TeamAndChannel -String $Identity @Params } | Should -Throw
        $Team.GroupId | Should -BeNullOrEmpty
        $Channel.Id | Should -BeNullOrEmpty
      }
    }

    Context 'Team found but Channel not found' {
      BeforeEach {
        # Using Testing-MockedObjects.ps1: $TeamGood
        Mock Get-Team { return $TeamGood }
        Mock Get-TeamChannel { return }
      }

      It 'should return a Team Object but not a Channel Object' {
        $Identity = 'Night Watch\Bribes'
        { $Team, $Channel = Get-TeamAndChannel -String $Identity @Params } | Should -Throw

        $Identity = 'Night Watch\19:7knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        { $Team, $Channel = Get-TeamAndChannel -String $Identity @Params } | Should -Throw

        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\Bribes'
        { $Team, $Channel = Get-TeamAndChannel -String $Identity @Params } | Should -Throw

        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\19:7knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        { $Team, $Channel = Get-TeamAndChannel -String $Identity @Params } | Should -Throw
      }
    }

    Context 'Team and Channel found' {
      BeforeEach {
        # Using Testing-MockedObjects.ps1: $TeamGood and $ChannelGood
        Mock Get-Team { return $TeamGood }
        Mock Get-TeamChannel { return $TeamChannelGood }
      }

      It 'should find a Team by Name and a Channel by Name and return a Team Object and a Channel object' {
        $Identity = 'Night Watch\Night Watch - Duty Roster'
        { $Team, $Channel = Get-TeamAndChannel -String "$Identity" @Params} | Should -Not -Throw
        $Team, $Channel = Get-TeamAndChannel -String "$Identity" @Params
        $Team.GroupId | Should -Be $TeamGood.GroupId
        $Channel.Id | Should -Be $ChannelGood.Id
      }

      It 'should find a Team by Name and a Channel by Id and return a Team Object and a Channel object' {
        $Identity = 'Night Watch\19:5e61bcffc0e54c5d915b17f6a0796a42@thread.tacv2'
        { $Team, $Channel = Get-TeamAndChannel -String "$Identity" @Params} | Should -Not -Throw
        $Team, $Channel = Get-TeamAndChannel -String "$Identity" @Params
        $Team.GroupId | Should -Be $TeamGood.GroupId
        $Channel.Id | Should -Be $ChannelGood.Id
      }

      It 'should find a Team by Id and a Channel by Name and return a Team Object and a Channel object' {
        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\Night Watch - Duty Roster'
        { $Team, $Channel = Get-TeamAndChannel -String "$Identity" @Params} | Should -Not -Throw
        $Team, $Channel = Get-TeamAndChannel -String "$Identity" @Params
        $Team.GroupId | Should -Be $TeamGood.GroupId
        $Channel.Id | Should -Be $ChannelGood.Id
      }

      It 'should find a Team by Id and a Channel by Id and return a Team Object and a Channel object' {
        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\19:5e61bcffc0e54c5d915b17f6a0796a42@thread.tacv2'
        { $Team, $Channel = Get-TeamAndChannel -String "$Identity" @Params} | Should -Not -Throw
        $Team, $Channel = Get-TeamAndChannel -String "$Identity" @Params
        $Team.GroupId | Should -Be $TeamGood.GroupId
        $Channel.Id | Should -Be $ChannelGood.Id
      }
    }
  }
}