dnsimpleps.tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
param()

#. $PSScriptRoot\dnsimpleps.ps1
Import-Module $PSScriptRoot\dnsimpleps.psm1 -Force

InModuleScope 'dnsimpleps' {
    Describe 'Dnsimple cmdlets' {
        Context 'Token handling ' {
            It 'Should write access tokens to file' {

                $tempFile = join-path $TestDrive '.access.token'

                Write-AccessToken -Account 11111 -AccessToken thetoken `
                    -Path $tempFile

                [xml](Get-Content $tempFile) `
                    | Select-xml "//s:I32[@N='Key' and text()='11111']" `
                        -Namespace @{s='http://schemas.microsoft.com/powershell/2004/04'} `
                        | Should Not BeNullOrEmpty
            }

            It 'Should be able to read access tokens from file' {
                
                #Arrange
                $tempFile = join-path $TestDrive '.access.token'
                $account = 11111
                $accessToken = 'thetoken'
                Write-AccessToken -Account $account -AccessToken $accessToken `
                    -Path $tempFile

                #Act
                $result = Read-AccessToken $account -Path $tempFile

                #Assert
                $result.Account | Should Be $account
                (new-object pscredential 'a',$result.AccessToken).GetNetworkCredential().Password | Should Be $AccessToken
            }
        }

        Context 'Records handling' {
            Mock CallDnsimpleApi {
                    $bodyPS = ConvertFrom-Json $Data
                    return [PSCustomObject]@{
                        id=11278049
                        zone_id=$Zone
                        parent_id=$null
                        name=$bodyPS.name
                        content=$bodyPS.Content
                        ttl=3600
                        priority=$null
                        type=$bodyPS.type
                        regions=@('global')
                        system_record=$false
                        created_at='2017-03-24T11:53:27Z'
                        updated_at='2017-03-24T11:53:27Z'
                    }
                } -ParameterFilter { $Method -eq 'POST' -and $Uri -eq "https://api.dnsimple.com/v2/$account/zones/$zone/records" }

            $account = [pscustomobject]@{
                Account=11111
                AccessToken=(ConvertTo-SecureString 'thetoken' -AsPlainText -Force)
            }
            $zone = 'pwrsh.io'

            It 'should be able to add zone record' {
                $recordType = 'MX'
                $content = 'smtp.pwrsh.io'
                $name = 'record1'
                $result = $account | Add-ZoneRecord -Zone $zone -RecordType $recordType `
                    -Name $name -Content $content

                    $result.zone_id | Should be $zone
                    $result.content | Should be $content
                    $result.name | Should be $name
            }

            Mock Invoke-WebRequest {
                    return [pscustomobject]@{
                        StatusCode=204
                        StatusDescription='No Content'
                        Content=''
                        RawContentLength=0
                    }
                } -ParameterFilter { $Method -eq 'DELETE' -and $Uri -eq "https://api.dnsimple.com/v2/$Account/zones/$Zone/records/$id" } 

            It 'should be able to delete record' {
                $id = 1
                $result = $account | Remove-ZoneRecord -Zone $zone -Id $id

                $result.StatusCode | Should Be 204
            }

            Mock Invoke-RestMethod {
                    return [PSCustomObject]@{
                        data = [PSCustomObject]@{
                            id=11278049
                            zone_id=$Zone
                            parent_id=$null
                            name='name1'
                            content='content1'
                            ttl=3600
                            priority=$null
                            type='MX'
                            regions=@('global')
                            system_record=$false
                            created_at='2017-03-24T11:53:27Z'
                            updated_at='2017-03-24T11:53:27Z'
                        }
                    }
                } -ParameterFilter { $Method -eq 'Get' -and $Uri -eq "https://api.dnsimple.com/v2" }

            It 'should unwrap return value' {
                $result = CallDnsimpleApi 'Get' 'https://api.dnsimple.com/v2' `
                    $null (ConvertTo-SecureString 'thetoken' -AsPlainText -Force)
                $result.name | should be 'name1'
            }
        }
    }
}