Skip to content

Commit

Permalink
added in start of DirectoryService classes for mocking during Pester …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
scrthq committed Oct 28, 2018
1 parent 1ca7c98 commit 4202772
Showing 1 changed file with 189 additions and 0 deletions.
189 changes: 189 additions & 0 deletions Tests/Classes/DirectoryService.MockClass.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
Import-Module '.\out\PSGSuite\2.17.0\PSGSuite.psd1' -Force

$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
}

$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$($_)@domain.com"
OrgUnitPath = "/Users/$_"
}))
foreach ($sub in (1..2)) {
[Void]$global:Users.Add((New-Object 'Google.Apis.Admin.Directory.directory_v1.Data.User' -Property @{
PrimaryEmail = "user$($_).$($sub)@domain.com"
OrgUnitPath = "/Users/$_/$sub"
}))
}
}
[Void]$global:Users.Add((New-Object 'Google.Apis.Admin.Directory.directory_v1.Data.User' -Property @{
PrimaryEmail = "[email protected]"
OrgUnitPath = "/Users"
}))

#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() {
return $true
}
[Object[]] ExecuteAsStream() {
return $true
}
[Object[]] ExecuteAsStreamAsync() {
return $true
}
[Object[]] ExecuteAsync() {
return $true
}
}

class DirectoryUsersListRequest : GoogleRequest {
DirectoryUsersListRequest() {

}
DirectoryUsersListRequest([String] $Query) : base ($Query) {

}
[Object[]] Execute() {
if ( -not [String]::IsNullOrEmpty($this.Query)) {
Write-Verbose "Query:`n$($this.Query.Trim())"
$filter = $this.Query.Trim()
$i = 0
if ($null -eq $filter.Split('=',2)[$i].Trim()) {
$i++
}
$left = $filter.Split('=',2)[$i].Trim()
$right = $filter.Split('=',2)[$i+1].Trim("'")
$filtered = $global:Users | Where-Object {$_.$left -like "$right*"}
return ([PSCustomObject]@{
UsersValue = $filtered
})
}
else {
return ([PSCustomObject]@{
UsersValue = $global:Users
})
}
}
}

class DirectoryUsersGetRequest : GoogleRequest {
[String] $UserKey
DirectoryUsersGetRequest([String] $UserKey) {
$this.UserKey = $UserKey
}
[Object] Execute() {
return ($global:Users | Where-Object {$_.PrimaryEmail -eq $this.UserKey -or $_.Id -eq $this.UserKey})
}
}
#endregion

#region: Resources
class DirectoryUsersAliasesResource {
DirectoryUsersAliasesResource() {

}
}

class DirectoryUsersPhotosResource {
DirectoryUsersPhotosResource() {

}
}

class DirectoryGroupsAliasesResource {
DirectoryGroupsAliasesResource() {

}
}

class DirectoryUsersResource {
[DirectoryUsersAliasesResource] $Aliases
[DirectoryUsersPhotosResource] $Photos

DirectoryUsersResource() {
$this.Aliases = [DirectoryUsersAliasesResource]::new()
$this.Photos = [DirectoryUsersPhotosResource]::new()
}

[DirectoryUsersListRequest] List() {
return [DirectoryUsersListRequest]::new()
}

[DirectoryUsersGetRequest] Get([String] $UserKey) {
return [DirectoryUsersGetRequest]::new($UserKey)
}
}
class DirectoryGroupsResource {
[DirectoryGroupsAliasesResource] $Aliases

DirectoryGroupsResource() {
$this.Aliases = [DirectoryGroupsAliasesResource]::new()
}

}
#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() {

}
}

class DirectoryService : GoogleService {
[DirectoryUsersResource] $Users
[DirectoryGroupsResource] $Groups
[String] $ApplicationName = $null

DirectoryService() {
$this.Users = [DirectoryUsersResource]::new()
$this.Groups = [DirectoryGroupsResource]::new()
$this.ApplicationName = $null
}
}
#endregion

0 comments on commit 4202772

Please sign in to comment.