Tests/Unit/Public/Channel/Get-TeamsTeamAndChannel.Tests.ps1

# Module: Orbit.Teams
# Function: Test
# Author: David Eberhardt
# Updated: 24-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'
        ErrorAction       = 'Stop'
      }

      # 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

    }

    Context 'Team not found' {
      BeforeEach {
        Mock Get-Team { throw }
      }

      It 'should return neither a Team Object nor a Channel Object - with ParameterSet Team' {
        $TeamObject = $ChannelObject = $null
        $Team = 'Trunk Company'
        $Channel = 'General'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params } | Should -Throw
        $TeamObject.GroupId | Should -BeNullOrEmpty
        $ChannelObject.Id | Should -BeNullOrEmpty

        $TeamObject = $ChannelObject = $null
        $Team = 'Trunk Company'
        $Channel = '19:6knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params } | Should -Throw
        $TeamObject.GroupId | Should -BeNullOrEmpty
        $ChannelObject.Id | Should -BeNullOrEmpty

        $TeamObject = $ChannelObject = $null
        $Team = '6c105977-d636-419a-b5f3-4885375e432a'
        $Channel = 'General'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params } | Should -Throw
        $TeamObject.GroupId | Should -BeNullOrEmpty
        $ChannelObject.Id | Should -BeNullOrEmpty

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

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

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

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

        $TeamObject = $ChannelObject = $null
        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\19:6knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String $Identity @Params } | Should -Throw
        $TeamObject.GroupId | Should -BeNullOrEmpty
        $ChannelObject.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 - with ParameterSet Team' {
        $TeamObject = $ChannelObject = $null
        $Team = 'Night Watch'
        $Channel = 'Bribes'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params } | Should -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel -ErrorAction SilentlyContinue
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.Id | Should -BeNullOrEmpty

        $TeamObject = $ChannelObject = $null
        $Team = 'Night Watch'
        $Channel = '19:7knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params } | Should -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel -ErrorAction SilentlyContinue
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.Id | Should -BeNullOrEmpty

        $TeamObject = $ChannelObject = $null
        $Team = '6c105977-d636-419a-b5f3-4885375e432a'
        $Channel = 'Bribes'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params } | Should -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel -ErrorAction SilentlyContinue
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.Id | Should -BeNullOrEmpty

        $TeamObject = $ChannelObject = $null
        $Team = '6c105977-d636-419a-b5f3-4885375e432a'
        $Channel = '19:7knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params } | Should -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel -ErrorAction SilentlyContinue
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.Id | Should -BeNullOrEmpty
      }

      It 'should return a Team Object but not a Channel Object - with ParameterSet String' {
        $TeamObject = $ChannelObject = $null
        $Identity = 'Night Watch\Bribes'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String $Identity @Params } | Should -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String $Identity -ErrorAction SilentlyContinue
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.Id | Should -BeNullOrEmpty

        $TeamObject = $ChannelObject = $null
        $Identity = 'Night Watch\19:7knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String $Identity @Params } | Should -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String $Identity -ErrorAction SilentlyContinue
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.Id | Should -BeNullOrEmpty

        $TeamObject = $ChannelObject = $null
        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\Bribes'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String $Identity @Params } | Should -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String $Identity -ErrorAction SilentlyContinue
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.Id | Should -BeNullOrEmpty

        $TeamObject = $ChannelObject = $null
        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\19:7knN4Rzr3_NUiCsVwEtrHvYWbp_MzqbPp9fEyqujZVc1@thread.tacv2'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String $Identity @Params } | Should -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String $Identity -ErrorAction SilentlyContinue
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.Id | Should -BeNullOrEmpty
      }
    }

    Context 'Team and Channel found - with ParameterSet Team' {
      BeforeEach {
        # Using Testing-MockedObjects.ps1: $TeamGood and $TeamChannelGood
        # $TeamChannelGood returns three Objects and narrows down to one
        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' {
        $TeamObject = $ChannelObject = $null
        $Team = 'Night Watch'
        $Channel = 'Night Watch - Duty Roster'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params } | Should -Not -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.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' {
        $TeamObject = $ChannelObject = $null
        $Team = 'Night Watch'
        $Channel = '19:5e61bcffc0e54c5d915b17f6a0796a42@thread.tacv2'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params } | Should -Not -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.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' {
        $TeamObject = $ChannelObject = $null
        $Team = '6c105977-d636-419a-b5f3-4885375e432a'
        $Channel = 'Night Watch - Duty Roster'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params } | Should -Not -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.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' {
        $TeamObject = $ChannelObject = $null
        $Team = '6c105977-d636-419a-b5f3-4885375e432a'
        $Channel = '19:5e61bcffc0e54c5d915b17f6a0796a42@thread.tacv2'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params } | Should -Not -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -Team $Team -Channel $Channel @Params
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.Id | Should -Be $ChannelGood.Id
      }
    }

    Context 'Team and Channel found - with ParameterSet String' {
      BeforeEach {
        # Using Testing-MockedObjects.ps1: $TeamGood and $ChannelGood
        # $ChannelGood returns one Object as `Get-TeamTeamsChannel -String` is defined to find an exact object
        Mock Get-Team { return $TeamGood }
        Mock Get-TeamChannel { return $ChannelGood }
      }

      It 'should find a Team by Name and a Channel by Name and return a Team Object and a Channel object' {
        $TeamObject = $ChannelObject = $null
        $Identity = 'Night Watch\Night Watch - Duty Roster'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String "$Identity" @Params } | Should -Not -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String "$Identity" @Params
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.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' {
        $TeamObject = $ChannelObject = $null
        $Identity = 'Night Watch\19:5e61bcffc0e54c5d915b17f6a0796a42@thread.tacv2'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String "$Identity" @Params } | Should -Not -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String "$Identity" @Params
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.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' {
        $TeamObject = $ChannelObject = $null
        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\Night Watch - Duty Roster'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String "$Identity" @Params } | Should -Not -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String "$Identity" @Params
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.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' {
        $TeamObject = $ChannelObject = $null
        $Identity = '6c105977-d636-419a-b5f3-4885375e432a\19:5e61bcffc0e54c5d915b17f6a0796a42@thread.tacv2'
        { $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String "$Identity" @Params } | Should -Not -Throw
        $TeamObject, $ChannelObject = Get-TeamsTeamAndChannel -String "$Identity" @Params
        $TeamObject.GroupId | Should -Be $TeamGood.GroupId
        $ChannelObject.Id | Should -Be $ChannelGood.Id
      }
    }

  }
}