diff --git a/tests/Connection.Tests.ps1 b/tests/Connection.Tests.ps1 index ec93a5d..0c43e08 100755 --- a/tests/Connection.Tests.ps1 +++ b/tests/Connection.Tests.ps1 @@ -2,8 +2,7 @@ $ErrorActionPreference = "Stop" BeforeAll { - Import-Module (Join-Path $PSScriptRoot '..' 'src' ` - 'PSConfluencePublisher.psd1') + Import-Module "$PSScriptRoot/../src/PSConfluencePublisher.psd1" } diff --git a/tests/Manifest.Tests.ps1 b/tests/Manifest.Tests.ps1 index e360df2..48d8339 100755 --- a/tests/Manifest.Tests.ps1 +++ b/tests/Manifest.Tests.ps1 @@ -2,9 +2,7 @@ $ErrorActionPreference = "Stop" BeforeAll { - Import-Module (Join-Path $PSScriptRoot '..' 'src' ` - 'PSConfluencePublisher.psd1') - + Import-Module "$PSScriptRoot/../src/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 { - return '{"pagges":[], "attsdachments": []}' - } + InModuleScope Manifest ` + { + Mock Get-Content { + return '{"pagges":[], "attsdachments": []}' + } - #mocking Get-Content, therefore file name can be bogus - {Get-Manifest 'foobar.x'} | Should -Throw + #mocking Get-Content, therefore file name can be bogus + {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 - { - Set-Manifest ` - -Manifest $mockManifest ` - -File 'foobar.x' - } | Should -Throw + If ($PSVersionTable.PSEdition -eq 'Core') + { + It 'declines setting invalid schema' ` + { + InModuleScope Manifest ` + { + $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' - $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 diff --git a/tests/Page.Tests.ps1 b/tests/Page.Tests.ps1 index efb1c4d..7a7b0ce 100755 --- a/tests/Page.Tests.ps1 +++ b/tests/Page.Tests.ps1 @@ -56,7 +56,9 @@ Describe 'New-Page' ` $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 @@ -249,7 +251,9 @@ Describe 'New-Page' ` $result | Should -Be $mockPageMeta - $result.Version | Should -Be $null + ( + $result | Get-Member -Name 'Version' + ) | Should -Be $null Should -Invoke -CommandName 'Invoke-WebRequest' ` -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 ` + } + } +} diff --git a/tests/PageMeta.Tests.ps1 b/tests/PageMeta.Tests.ps1 index 4e18344..b3a89e9 100755 --- a/tests/PageMeta.Tests.ps1 +++ b/tests/PageMeta.Tests.ps1 @@ -2,80 +2,46 @@ $ErrorActionPreference = "Stop" BeforeAll { - Import-Module (Join-Path $PSScriptRoot '..' 'src' ` - '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 - } - } + Import-Module "$PSScriptRoot/../src/PSConfluencePublisher.psd1" } Describe 'Get-PageMeta' ` { + BeforeAll ` + { + Mock -ModuleName 'PageMeta' Get-PersonalAccessToken { + '012345678901234567890' + } + } + Context 'default' ` { - BeforeAll ` + + It 'uses index' ` { - Mock -ModuleName 'PageMeta' Get-PersonalAccessToken { - '012345678901234567890' + $mockPageMeta = @{ + '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' ` @@ -89,10 +55,6 @@ Describe 'Get-PageMeta' ` $mockPageMeta ) - Mock -ModuleName 'PageMeta' Get-PageMetaCache { - $mockPageMeta - } - $meta = Get-PageMeta ` -Host 'foobar' ` -Title 'foobar' ` @@ -100,159 +62,124 @@ Describe 'Get-PageMeta' ` -Manifest $mockManifest $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 } } -} - -Describe 'Update-PageMeta' ` -{ - Context 'default' ` + Context 'locally cached' ` { - It 'fails, if page meta index does not exist' ` + BeforeAll ` { - { - Update-PageMeta ` - -Id '0123456789' ` - -Title 'foobar' ` - -Manifest @{} - } | Should -Throw + $mockManifest = @( + @{ + 'Title' = 'page0' + 'Id' = 'id0' + }, + @{ + 'Title' = 'page1' + 'Id' = 'id1' + }, + @{ + 'Title' = 'page2' + 'Id' = 'id2' + } + ) } - It 'updates minimal' ` + It 'from parameter' ` { - $mockPageMeta = @{ - 'Title' = 'foobar' - } + $meta = Get-PageMeta ` + -Host 'foobar' ` + -Space 'foobar' ` + -Manifest $mockManifest - $mockManifest = @( - $mockPageMeta - ) - - $pageMeta = Update-PageMeta ` - -Title 'foobar' ` - -Id '0123456789' ` - -Manifest $mockManifest - - $mockPageMeta.Id | Should -Be '0123456789' + $meta.Count | Should -Be 3 } - It 'updates extended' ` + It 'from pipeline' ` { - $mockPageMeta = @{ - 'Title' = 'foobar' - } + $meta = $mockManifest | Get-PageMeta ` + -Host 'foobar' ` + -Space 'foobar' + $meta.Count | Should -Be 3 + } + } + + Context 'locally cached' ` + { + BeforeAll ` + { $mockManifest = @( - $mockPageMeta + @{ + 'Title' = 'page0' + 'Id' = 'id0' + }, + @{ + 'Title' = 'page1' + }, + @{ + 'Title' = 'page2' + 'Id' = 'id2' + } ) - Update-PageMeta ` - -Title 'foobar' ` - -Id 'pageId' ` - -Version 9001 ` - -AncestorTitle 'ancestorTitle' ` - -Hash 'hash' ` - -Manifest $mockManifest + Mock -ModuleName 'PageMeta' Invoke-WebRequest { + @{ + 'Content' = '{"results": [{"id": "remoteid", "_expandable": {"version": 1}}]}' + } + } + } - $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 } } } + diff --git a/tests/PersonalAccessToken.Tests.ps1 b/tests/PersonalAccessToken.Tests.ps1 index 7563c7d..cdf3e7d 100755 --- a/tests/PersonalAccessToken.Tests.ps1 +++ b/tests/PersonalAccessToken.Tests.ps1 @@ -2,8 +2,7 @@ $ErrorActionPreference = "Stop" BeforeAll { - Import-Module (Join-Path $PSScriptRoot '..' 'src' ` - 'PSConfluencePublisher.psd1') + Import-Module "$PSScriptRoot/../src/PSConfluencePublisher.psd1" $mockHost = 'confluence.contoso.com' diff --git a/tests/StringHelper.Tests.ps1 b/tests/StringHelper.Tests.ps1 index 00170d4..8ddf215 100755 --- a/tests/StringHelper.Tests.ps1 +++ b/tests/StringHelper.Tests.ps1 @@ -2,9 +2,7 @@ $ErrorActionPreference = "Stop" BeforeAll { - Import-Module (Join-Path $PSScriptRoot '..' 'src' ` - 'PSConfluencePublisher.psd1') - + Import-Module "$PSScriptRoot/../src/PSConfluencePublisher.psd1" } Describe 'Get-StringHash' `