Skip to content

Commit

Permalink
updated unit tests for DirectoryService
Browse files Browse the repository at this point in the history
  • Loading branch information
scrthq committed Oct 29, 2018
1 parent 9319cf9 commit 33b6fd4
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 115 deletions.
66 changes: 66 additions & 0 deletions Tests/Mocks/Core.Mocks.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#region: Mock a config and load it for other functions to use
Mock 'Get-PSGSuiteConfig' -ModuleName PSGSuite -MockWith {
Write-Verbose "Getting mocked PSGSuite config"
$script:PSGSuite = [PSCustomObject][Ordered]@{
ConfigName = 'Pester'
P12KeyPath = ([System.IO.Path]::Combine($PSScriptRoot,"fake.p12"))
ClientSecretsPath = ([System.IO.Path]::Combine($PSScriptRoot,"fake_client_secrets.json"))
AppEmail = "[email protected]"
AdminEmail = "[email protected]"
CustomerId = 'Cxxxxxxxxx'
Domain = 'domain.com'
Preference = 'CustomerID'
ServiceAccountClientID = '1111111111111111111111111'
}
}
Get-PSGSuiteConfig -Verbose
#endregion

#region purpose: Base classes
class GoogleRequest {
[String] $Customer
[String] $CustomFieldMask
[String] $Domain
[String] $Fields
[String] $Key
[Int] $MaxResults
[String] $OauthToken
[String] $PageToken
[Bool] $PrettyPrint
[String] $Query
[String] $QuotaUser
[String] $ShowDeleted

GoogleRequest() {

}

[Object[]] Execute() {
throw "Must Override Method"
}
[Object[]] ExecuteAsStream() {
throw "Must Override Method"
}
[Object[]] ExecuteAsStreamAsync() {
throw "Must Override Method"
}
[Object[]] ExecuteAsync() {
throw "Must Override Method"
}
}
class GoogleService {
[String] $APIKey
[String] $ApplicationName = $null
[String] $BasePath
[String] $BaseUri
[String] $BatchPath
[String] $BatchUri
[System.Collections.Generic.List[String]] $Features
[Bool] $GZipEnabled
[String] $Name

GoogleService() {

}
}
#endregion
Original file line number Diff line number Diff line change
@@ -1,27 +1,4 @@
<#
Import-Module PSGSuite
$STypes = @(
'Google.Apis.Admin.DataTransfer.datatransfer_v1.DataTransferService'
'Google.Apis.Admin.Directory.directory_v1.DirectoryService'
'Google.Apis.Admin.Reports.reports_v1.ReportsService'
'Google.Apis.Calendar.v3.CalendarService'
'Google.Apis.Classroom.v1.ClassroomService'
'Google.Apis.Drive.v3.DriveService'
'Google.Apis.Gmail.v1.GmailService'
'Google.Apis.Groupssettings.v1.GroupssettingsService'
'Google.Apis.HangoutsChat.v1.HangoutsChatService'
'Google.Apis.Licensing.v1.LicensingService'
'Google.Apis.Sheets.v4.SheetsService'
'Google.Apis.Tasks.v1.TasksService'
'Google.Apis.Urlshortener.v1.UrlshortenerService'
)
$STypes | ForEach-Object {
$shortName = $_.Split('.')[-1]
New-Variable -Name $shortName -Value (New-Object $_) -Force
}
#>

#region: Test object collections
$global:Users = New-Object System.Collections.ArrayList
1..2 | ForEach-Object {
[Void]$global:Users.Add((New-Object 'Google.Apis.Admin.Directory.directory_v1.Data.User' -Property @{
Expand All @@ -35,44 +12,19 @@ $global:Users = New-Object System.Collections.ArrayList
}))
}
}
1..2 | ForEach-Object {
[Void]$global:Users.Add((New-Object 'Google.Apis.Admin.Directory.directory_v1.Data.User' -Property @{
PrimaryEmail = "user$($_)@domain2.com"
OrgUnitPath = "/Users/$_"
}))
}
[Void]$global:Users.Add((New-Object 'Google.Apis.Admin.Directory.directory_v1.Data.User' -Property @{
PrimaryEmail = "[email protected]"
OrgUnitPath = "/Users"
}))
#endregion

#region: Requests
class GoogleRequest {
[String] $Customer
[String] $CustomFieldMask
[String] $Domain
[String] $Fields
[String] $Key
[Int] $MaxResults
[String] $OauthToken
[String] $PageToken
[Bool] $PrettyPrint
[String] $Query
[String] $QuotaUser
[String] $ShowDeleted

GoogleRequest() {

}

[Object[]] Execute() {
throw "Must Override Method"
}
[Object[]] ExecuteAsStream() {
throw "Must Override Method"
}
[Object[]] ExecuteAsStreamAsync() {
throw "Must Override Method"
}
[Object[]] ExecuteAsync() {
throw "Must Override Method"
}
}

#region: Requests - These should inherit from the GoogleRequest core class
class DirectoryUsersListRequest : GoogleRequest {
[String] $Projection
[String] $Domain
Expand All @@ -93,7 +45,7 @@ class DirectoryUsersListRequest : GoogleRequest {

}
[Object[]] Execute() {
if ( -not [String]::IsNullOrEmpty($this.Query)) {
$results = if ( -not [String]::IsNullOrEmpty($this.Query)) {
Write-Verbose "Query: $($this.Query.Trim())"
$filter = $this.Query.Trim()
$i = 0
Expand All @@ -108,10 +60,14 @@ class DirectoryUsersListRequest : GoogleRequest {
})
}
else {
return ([PSCustomObject]@{
UsersValue = $global:Users
})
$global:Users
}
if ( -not [String]::IsNullOrEmpty($this.Domain)) {
$results = $results | Where-Object {$_.PrimaryEmail -like "*$($this.domain)"}
}
return ([PSCustomObject]@{
UsersValue = $results
})
}
}

Expand All @@ -135,7 +91,33 @@ class DirectoryUsersGetRequest : GoogleRequest {
return $user
}
else {
throw "User not found!"
throw "User $($this.UserKey) not found!"
}
}
}

class DirectoryUsersInsertRequest : GoogleRequest {
[Object] $Body
[String] $Projection
[String] $Domain
[String] $Customer
[String] $MaxResults
[String] $OrderBy
[String] $SortOrder
[String] $CustomFieldMask
[String] $ShowDeleted = $false
[String] $ViewType

DirectoryUsersInsertRequest([Object] $Body) {
$this.Body = $Body
}
[Object] Execute() {
if ($global:Users | Where-Object {$_.PrimaryEmail -eq $this.Body.PrimaryEmail}) {
throw "User $($this.Body.PrimaryEmail) already exists!"
}
else {
[Void]$global:Users.Add($this.Body)
return ($this.Body)
}
}
}
Expand Down Expand Up @@ -176,6 +158,10 @@ class DirectoryUsersResource {
[DirectoryUsersGetRequest] Get([String] $UserKey) {
return [DirectoryUsersGetRequest]::new($UserKey)
}

[DirectoryUsersInsertRequest] Insert([Object] $Body) {
return [DirectoryUsersInsertRequest]::new($Body)
}
}
class DirectoryGroupsResource {
[DirectoryGroupsAliasesResource] $Aliases
Expand All @@ -187,23 +173,7 @@ class DirectoryGroupsResource {
}
#endregion

#region: Services
class GoogleService {
[String] $APIKey
[String] $ApplicationName
[String] $BasePath
[String] $BaseUri
[String] $BatchPath
[String] $BatchUri
[System.Collections.Generic.List[String]] $Features
[Bool] $GZipEnabled
[String] $Name

GoogleService() {

}
}

#region: Service - This should inherit from the GoogleService core class
class DirectoryService : GoogleService {
[DirectoryUsersResource] $Users
[DirectoryGroupsResource] $Groups
Expand All @@ -216,3 +186,10 @@ class DirectoryService : GoogleService {
}
}
#endregion

#region: New-GoogleService mock
Mock 'New-GoogleService' -ModuleName PSGSuite -ParameterFilter {$ServiceType -eq 'Google.Apis.Admin.Directory.directory_v1.DirectoryService'} -MockWith {
Write-Verbose "Mocking New-GoogleService for ServiceType '$ServiceType' using the DirectoryService class"
return [DirectoryService]::new()
}
#endregion
7 changes: 0 additions & 7 deletions Tests/Mocks/Users.Mocks.ps1

This file was deleted.

Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
$PSVersion = $PSVersionTable.PSVersion.Major
$projectRoot = Resolve-Path "$PSScriptRoot\..\.."
$ModulePath = Resolve-Path "$projectRoot\BuildOutput\$($env:BHProjectName)"
$decompiledModulePath = Resolve-Path "$projectRoot\$($env:BHProjectName)"
Expand All @@ -16,7 +15,8 @@ $moduleRoot = Split-Path (Resolve-Path "$ModulePath\*\*.psd1")

Import-Module $ModulePath -Force -Verbose:$false

Describe "Module tests: $($env:BHProjectName)" {

Describe "Module tests: $($env:BHProjectName)" -Tag 'Module' {
Context "Confirm files are valid Powershell syntax" {
$scripts = Get-ChildItem $decompiledModulePath -Include *.ps1,*.psm1,*.psd1 -Recurse

Expand Down Expand Up @@ -60,7 +60,7 @@ Describe "Module tests: $($env:BHProjectName)" {
}
}

Describe "Function contents" {
Describe "Function contents" -Tag 'Module' {
Context "All non-helper public functions should use Write-Verbose" {
$scripts = Get-ChildItem "$decompiledModulePath\Public" -Include *.ps1 -Recurse | Where-Object {$_.FullName -notlike "*Helpers*"}
$testCase = $scripts | Foreach-Object {@{file = $_;Name = $_.BaseName}}
Expand Down
45 changes: 37 additions & 8 deletions Tests/Unit Tests/Users/PSGSuite.Get-GSUser.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
<#
* Unit test files should be wrapped with InModuleScope
* Each unit test file should have the following above the Describe block(s):
1. A call to dot-source the core mocks and classes
2. A call to dot-source the appropriate Service mock and classes for the function being tested
#>

InModuleScope PSGSuite {
Write-Verbose "Loading mocked versions of New-GoogleService"
. ([System.IO.Path]::Combine("$env:BHProjectPath","Tests","Mocks","Users.Mocks.ps1"))
Describe 'Get-GSUser mock tests' {

#region: Load service and config mocks and validate mock via $null ApplicationName
Write-Verbose "Loading Core mocks"
. ([System.IO.Path]::Combine("$env:BHProjectPath","Tests","Mocks","Core.Mocks.ps1"))
Write-Verbose "Loading DirectoryService mock"
. ([System.IO.Path]::Combine("$env:BHProjectPath","Tests","Mocks","DirectoryService.Mocks.ps1"))
Describe 'DirectoryService' -Tag 'Core' {
Context 'When a mocked Directory service is created' {
It 'ApplicationName should be $null' {
$service = New-GoogleService -ServiceType 'Google.Apis.Admin.Directory.directory_v1.DirectoryService' -Scope 'Mock' -User '[email protected]' -Verbose
$service.ApplicationName | Should -BeNullOrEmpty
}
}
}
#endregion

Describe 'Get-GSUser mock tests' -Tag 'Directory' {
Context 'When Get-GSUser lists users' {
$result = Get-GSUser -Filter * -Verbose
$testCase = @('[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]') | Foreach-Object {@{item = $_}}
$testCase = @('[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]') | Foreach-Object {@{item = $_}}
It "[Full list] PrimaryEmail should contain <item>" -TestCases $testCase {
param($item)
$result.PrimaryEmail | Should -Contain $item
Expand All @@ -24,14 +39,25 @@ InModuleScope PSGSuite {
param($item)
$result.PrimaryEmail | Should -Contain $item
}
$testCase = @('[email protected]','[email protected]','[email protected]','[email protected]') | Foreach-Object {@{item = $_}}
$testCase = @('[email protected]','[email protected]','[email protected]','[email protected]','[email protected]') | Foreach-Object {@{item = $_}}
It "[Filtered list] PrimaryEmail should not contain <item>" -TestCases $testCase {
param($item)
$result.PrimaryEmail | Should -Not -Contain $item
}
}
Context 'When Get-GSUser gets a user' {
$testCase = @('[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]') | Foreach-Object {@{item = $_}}
Context 'When Get-GSUser lists users from a specific domain' {
$result = Get-GSUser -Filter * -Domain domain2.com -Verbose
$testCase = @('[email protected]','[email protected]') | Foreach-Object {@{item = $_}}
It "[Domain list] PrimaryEmail should contain <item>" -TestCases $testCase {
param($item)
$result.PrimaryEmail | Should -Contain $item
}
It '[Domain list] PrimaryEmail should not contain "[email protected]"' {
$result.PrimaryEmail | Should -Not -Contain '[email protected]'
}
}
Context 'When Get-GSUser gets specific users' {
$testCase = @('[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]') | Foreach-Object {@{item = $_}}
It "Should not throw when getting <item>" -TestCases $testCase {
param($item)
{Get-GSUser -User $item -Verbose} | Should -Not -Throw
Expand All @@ -50,7 +76,10 @@ InModuleScope PSGSuite {
$result.OrgUnitPath | Should -BeExactly "/Users/$id"
}
It 'Should throw when getting [email protected]' {
{Get-GSUser -User '[email protected]' -Verbose} | Should -Throw "User not found!"
{Get-GSUser -User '[email protected]' -Verbose} | Should -Throw -ExpectedMessage "User [email protected] not found!"
}
It 'Should get the AdminEmail user when no user or filter is specified' {
(Get-GSUser -Verbose).PrimaryEmail | Should -BeExactly '[email protected]'
}
}
}
Expand Down
Loading

0 comments on commit 33b6fd4

Please sign in to comment.