test: add new test suite for updated content

This commit is contained in:
Rodweil, Theodor 2023-08-14 05:10:57 +02:00
parent 4fa55b8602
commit 7e68b132b8
No known key found for this signature in database
GPG key ID: F8BC1B0EB1F9CCF5
6 changed files with 317 additions and 236 deletions

View file

@ -2,8 +2,7 @@
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
BeforeAll { BeforeAll {
Import-Module (Join-Path $PSScriptRoot '..' 'src' ` Import-Module "$PSScriptRoot/../src/PSConfluencePublisher.psd1"
'PSConfluencePublisher.psd1')
} }

View file

@ -2,9 +2,7 @@
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
BeforeAll { BeforeAll {
Import-Module (Join-Path $PSScriptRoot '..' 'src' ` Import-Module "$PSScriptRoot/../src/PSConfluencePublisher.psd1"
'PSConfluencePublisher.psd1')
} }
@ -30,16 +28,19 @@ Describe 'Get-Manifest' `
} }
} }
It 'throws on schema mismatch' ` If ($PSVersionTable.PSEdition -eq 'Core')
{ {
InModuleScope Manifest ` It 'throws on schema mismatch' `
{ {
Mock Get-Content { InModuleScope Manifest `
return '{"pagges":[], "attsdachments": []}' {
} Mock Get-Content {
return '{"pagges":[], "attsdachments": []}'
}
#mocking Get-Content, therefore file name can be bogus #mocking Get-Content, therefore file name can be bogus
{Get-Manifest 'foobar.x'} | Should -Throw {Get-Manifest 'foobar.x'} | Should -Throw
}
} }
} }
} }
@ -74,21 +75,25 @@ Describe 'Set-Manifest' `
} }
} }
It 'declines setting invalid schema' `
{
InModuleScope Manifest `
{
$mockManifest = @{
'pagges' = @()
'attachments' = @()
}
#mocking Get-Content, therefore file name can be bogus If ($PSVersionTable.PSEdition -eq 'Core')
{ {
Set-Manifest ` It 'declines setting invalid schema' `
-Manifest $mockManifest ` {
-File 'foobar.x' InModuleScope Manifest `
} | Should -Throw {
$mockManifest = @{
'pagges' = @()
'attachments' = @()
}
#mocking Get-Content, therefore file name can be bogus
{
Set-Manifest `
-Manifest $mockManifest `
-File 'foobar.x'
} | Should -Throw
}
} }
} }
} }
@ -146,7 +151,12 @@ Describe 'Set-Manifest' `
$Path | Should -Be 'foo/bar/foobar.x' $Path | Should -Be 'foo/bar/foobar.x'
$Destination | Should -Be 'foo/bar/foobar.x.bck' # we love PS, but the path handling is just awfully
# inconsistent.
$Destination | Should -BeIn @(
'foo/bar/foobar.x.bck',
'foo\bar\foobar.x.bck'
)
} }
#mocking Get-Content, therefore file name can be bogus #mocking Get-Content, therefore file name can be bogus

View file

@ -56,7 +56,9 @@ Describe 'New-Page' `
$body_.body.storage.representation | Should -Be 'storage' $body_.body.storage.representation | Should -Be 'storage'
$body_.body.storage.value | Should -Be $defaultMockContent $body_.body.storage.value | Should -Be (
$defaultMockContent | Out-String
)
$body_.space.key | Should -Be $defaultMockSpaceName $body_.space.key | Should -Be $defaultMockSpaceName
@ -249,7 +251,9 @@ Describe 'New-Page' `
$result | Should -Be $mockPageMeta $result | Should -Be $mockPageMeta
$result.Version | Should -Be $null (
$result | Get-Member -Name 'Version'
) | Should -Be $null
Should -Invoke -CommandName 'Invoke-WebRequest' ` Should -Invoke -CommandName 'Invoke-WebRequest' `
-ModuleName 'Page' ` -ModuleName 'Page' `
@ -318,3 +322,147 @@ Describe 'New-Page' `
} }
} }
} }
Describe 'Update-Page' `
{
BeforeEach `
{
$defaultMockContent = 'foobar content'
$defaultMockSpaceName = 'testitest'
$defaultMockTitle = 'foobar'
$defaultMockPageMeta = @{
'Title' = $defaultMockTitle
'Ref' = 'pages/320okffs.xml'
'Id' = 123
'Version' = 2
}
$defaultMockManifest = @(
$defaultMockPageMeta
)
$mockIndex = @{
$defaultMockTitle = 0
}
Mock -ModuleName 'Page' Get-Content {
$defaultMockContent
}
Mock -ModuleName 'Page' Get-PersonalAccessToken {
'01234567890123456789'
}
# TODO: write proper parameter filters, so that we can reuse this
# mock with more thorough/deep assertions on properties
Mock -ModuleName 'Page' Invoke-WebRequest {
# $Uri | Should -Be 'https://confluence.contoso.com/rest/api/content'
$body_ = $Body | ConvertFrom-JSON
$body_.type | Should -Be 'page'
$body_.body.storage.representation | Should -Be 'storage'
$body_.body.storage.value | Should -Be (
$defaultMockContent | Out-String
)
$body_.space.key | Should -Be $defaultMockSpaceName
# $body_.title | Should -Be $defaultMockTitle
@{
'Content' = 'DONT CARE ABOUT THE RESPONSE CONTENT'
}
}
Mock -ModuleName 'Page' Get-StringHash {
@{
'Hash' = 'NOTAREALHASH'
}
}
}
Context 'default' `
{
It 'accepts parameterized input' -Tag Now `
{
$result = Update-Page `
-Host 'confluence.contoso.com' `
-Space $defaultMockSpaceName `
-Title $defaultMockTitle `
-Manifest $defaultMockManifest `
-Index $mockIndex
$result | Should -Be $defaultMockPageMeta
}
It 'accepts pipeline input' `
{
$result = $defaultMockManifest | Update-Page `
-Host 'confluence.contoso.com' `
-Space $defaultMockSpaceName `
-Title $defaultMockTitle `
-Index $mockIndex
$result | Should -Be $defaultMockPageMeta
}
}
}
Describe 'Publish-Page' `
{
BeforeEach `
{
$defaultMockSpaceName = 'foobar-space'
$defaultMockTitle = 'foobar'
$defaultMockManifest = @(
@{},
@{},
@{}
)
$defaultMockIndex = @{}
Mock -ModuleName 'Page' New-Page {
$defaultMockManifest
}
Mock -ModuleName 'Page' Update-Page {
$defaultMockManifest
}
}
Context 'default' -Tag 'Now' `
{
It 'passes everything properly' `
{
$result = Publish-Page `
-Host 'confluence.contoso.com' `
-Space $defaultMockSpaceName `
-Title $defaultMockTitle `
-Index $defaultMockIndex `
-Manifest $defaultMockManifest
$result | Should -Be $defaultMockManifest
Should -Invoke -CommandName 'New-Page' `
-ModuleName 'Page' `
-Exactly `
-Times 1 `
Should -Invoke -CommandName 'Update-Page' `
-ModuleName 'Page' `
-Exactly `
-Times 1 `
}
}
}

View file

@ -2,80 +2,46 @@
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
BeforeAll { BeforeAll {
Import-Module (Join-Path $PSScriptRoot '..' 'src' ` Import-Module "$PSScriptRoot/../src/PSConfluencePublisher.psd1"
'PSConfluencePublisher.psd1')
}
Describe 'Get-PageMetaCache' `
{
Context 'default' `
{
It 'uses index' `
{
$mockPageMeta = @{
'Title' = 'foobar'
}
$mockManifest = @(
$mockPageMeta
)
$mockIndex = @{
'foobar' = 0
}
$meta = Get-PageMetaCache `
-Title 'foobar' `
-Manifest $mockManifest `
-Index $mockIndex
$meta | Should -Be $mockPageMeta
}
It 'returns page meta when title exists' `
{
$mockPageMeta = @{
'Title' = 'foobar'
}
$mockManifest = @(
$mockPageMeta
)
$meta = Get-PageMetaCache `
-Title 'foobar' `
-Manifest $mockManifest
$meta | Should -Be $mockPageMeta
}
It 'returns null, if page with supplied title does not exist' `
{
$mockManifest = @(
@{}
)
$meta = Get-PageMetaCache `
-Title 'foobar' `
-Manifest $mockManifest
$meta | Should -Be $null
}
}
} }
Describe 'Get-PageMeta' ` Describe 'Get-PageMeta' `
{ {
BeforeAll `
{
Mock -ModuleName 'PageMeta' Get-PersonalAccessToken {
'012345678901234567890'
}
}
Context 'default' ` Context 'default' `
{ {
BeforeAll `
It 'uses index' `
{ {
Mock -ModuleName 'PageMeta' Get-PersonalAccessToken { $mockPageMeta = @{
'012345678901234567890' 'Title' = 'foobar'
'Id' = '0123456789'
} }
$mockManifest = @(
@{},
$mockPageMeta
)
$mockIndex = @{
'foobar' = 1
}
$meta = Get-PageMeta `
-Host 'foobar' `
-Title 'foobar' `
-Space 'foobar' `
-Index $mockIndex `
-Manifest $mockManifest
$meta | Should -Be $mockPageMeta
} }
It 'returns cache when page id present' ` It 'returns cache when page id present' `
@ -89,10 +55,6 @@ Describe 'Get-PageMeta' `
$mockPageMeta $mockPageMeta
) )
Mock -ModuleName 'PageMeta' Get-PageMetaCache {
$mockPageMeta
}
$meta = Get-PageMeta ` $meta = Get-PageMeta `
-Host 'foobar' ` -Host 'foobar' `
-Title 'foobar' ` -Title 'foobar' `
@ -100,159 +62,124 @@ Describe 'Get-PageMeta' `
-Manifest $mockManifest -Manifest $mockManifest
$meta | Should -Be $mockPageMeta $meta | Should -Be $mockPageMeta
Should -Invoke -CommandName 'Get-PageMetaCache' `
-ModuleName 'PageMeta' `
-Exact `
-Times 1
}
It 'gets a page id remotely if there is exactly one result' `
{
$mockPageMeta = @{
'Version' = 'version'
'Hash' = 'hash'
'Ref' = 'ref'
}
Mock -ModuleName 'PageMeta' Get-PageMetaCache {
$mockPageMeta
}
Mock -ModuleName 'PageMeta' Update-PageMeta {
$Id | Should -Be '123'
$Version | Should -Be 9
$Title | Should -Be 'foobar'
$mockPageMeta
}
Mock -ModuleName 'PageMeta' Invoke-WebRequest {
@{
'Content' = '{"results": [{"id": "123","_expandable":{"version": 9}}]}'
}
}
$meta = Get-PageMeta `
-Host 'confluence.contoso.com' `
-Title 'foobar' `
-Space 'foobar' `
-Manifest @{'Pages'= {}}
$meta | Should -Be $mockPageMeta
Should -Invoke 'Get-PageMetaCache' `
-ModuleName 'PageMeta' `
-Exactly `
-Times 1
Should -Invoke 'Invoke-WebRequest' `
-ModuleName 'PageMeta' `
-Exactly `
-Times 1
Should -Invoke 'Update-PageMeta' `
-ModuleName 'PageMeta' `
-Exactly `
-Times 1
}
It 'throws an exception, if there is more than one result' `
{
Mock -ModuleName 'PageMeta' Invoke-WebRequest {
@{
'Content' = '{"results": [{}, {}]}'
}
}
{
Get-PageMeta `
-Host 'confluence.contoso.com' `
-Title 'foobar' `
-Space 'foobar' `
-Manifest @{'Pages'= {}}
} | Should -Throw 'more than one result for query*'
}
It 'throws an exception, if there is no result' `
{
Mock -ModuleName 'PageMeta' Invoke-WebRequest {
@{
'Content' = '{"results": []}'
}
}
$result = Get-PageMeta `
-Host 'confluence.contoso.com' `
-Title 'foobar' `
-Space 'foobar' `
-Manifest @{'Pages'= {}}
$result | Should -Be $null
} }
} }
}
Context 'locally cached' `
Describe 'Update-PageMeta' `
{
Context 'default' `
{ {
It 'fails, if page meta index does not exist' ` BeforeAll `
{ {
{ $mockManifest = @(
Update-PageMeta ` @{
-Id '0123456789' ` 'Title' = 'page0'
-Title 'foobar' ` 'Id' = 'id0'
-Manifest @{} },
} | Should -Throw @{
'Title' = 'page1'
'Id' = 'id1'
},
@{
'Title' = 'page2'
'Id' = 'id2'
}
)
} }
It 'updates minimal' ` It 'from parameter' `
{ {
$mockPageMeta = @{ $meta = Get-PageMeta `
'Title' = 'foobar' -Host 'foobar' `
} -Space 'foobar' `
-Manifest $mockManifest
$mockManifest = @( $meta.Count | Should -Be 3
$mockPageMeta
)
$pageMeta = Update-PageMeta `
-Title 'foobar' `
-Id '0123456789' `
-Manifest $mockManifest
$mockPageMeta.Id | Should -Be '0123456789'
} }
It 'updates extended' ` It 'from pipeline' `
{ {
$mockPageMeta = @{ $meta = $mockManifest | Get-PageMeta `
'Title' = 'foobar' -Host 'foobar' `
} -Space 'foobar'
$meta.Count | Should -Be 3
}
}
Context 'locally cached' `
{
BeforeAll `
{
$mockManifest = @( $mockManifest = @(
$mockPageMeta @{
'Title' = 'page0'
'Id' = 'id0'
},
@{
'Title' = 'page1'
},
@{
'Title' = 'page2'
'Id' = 'id2'
}
) )
Update-PageMeta ` Mock -ModuleName 'PageMeta' Invoke-WebRequest {
-Title 'foobar' ` @{
-Id 'pageId' ` 'Content' = '{"results": [{"id": "remoteid", "_expandable": {"version": 1}}]}'
-Version 9001 ` }
-AncestorTitle 'ancestorTitle' ` }
-Hash 'hash' ` }
-Manifest $mockManifest
$mockPageMeta.Id | Should -Be 'pageId' It 'only gets remote if necesary' `
{
$meta = Get-PageMeta `
-Host 'foobar' `
-Space 'foobar' `
-Manifest $mockManifest
$mockPageMeta.Version | Should -Be 9001 Should -Invoke -CommandName Invoke-WebRequest `
-ModuleName 'PageMeta' `
-Exactly `
-Times 1
$mockPageMeta.AncestorTitle | Should -Be 'ancestorTitle' $meta.Count | Should -Be 3
$mockPageMeta.Hash | Should -Be 'hash' $meta[0].Id | Should -Be 'id0'
$meta[0].Version | Should -Be $null
$meta[1].Id | Should -Be 'remoteid'
$meta[1].Version | Should -Be 1
}
It 'forcefully gets remote' `
{
$meta = Get-PageMeta `
-Host 'foobar' `
-Space 'foobar' `
-Force `
-Manifest $mockManifest
Should -Invoke -CommandName Invoke-WebRequest `
-ModuleName 'PageMeta' `
-Exactly `
-Times 3
$meta.Count | Should -Be 3
$meta[0].Id | Should -Be 'remoteid'
$meta[0].Version | Should -Be 1
$meta[1].Id | Should -Be 'remoteid'
$meta[1].Version | Should -Be 1
$meta[2].Id | Should -Be 'remoteid'
$meta[2].Version | Should -Be 1
} }
} }
} }

View file

@ -2,8 +2,7 @@
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
BeforeAll { BeforeAll {
Import-Module (Join-Path $PSScriptRoot '..' 'src' ` Import-Module "$PSScriptRoot/../src/PSConfluencePublisher.psd1"
'PSConfluencePublisher.psd1')
$mockHost = 'confluence.contoso.com' $mockHost = 'confluence.contoso.com'

View file

@ -2,9 +2,7 @@
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
BeforeAll { BeforeAll {
Import-Module (Join-Path $PSScriptRoot '..' 'src' ` Import-Module "$PSScriptRoot/../src/PSConfluencePublisher.psd1"
'PSConfluencePublisher.psd1')
} }
Describe 'Get-StringHash' ` Describe 'Get-StringHash' `