forked from SCRT-HQ/PSGSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated unit tests for DirectoryService
- Loading branch information
Showing
9 changed files
with
235 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 @{ | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
}) | ||
} | ||
} | ||
|
||
|
@@ -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) | ||
} | ||
} | ||
} | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 |
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
@@ -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]' | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.