Tests/Unit/MSFT_xSQLServerDatabaseRole.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
$script:DSCModuleName      = 'xSQLServer'
$script:DSCResourceName    = 'MSFT_xSQLServerDatabaseRole'

#region HEADER

# Unit Test Template Version: 1.2.0
$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
     (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
    & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\'))
}

Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force

$TestEnvironment = Initialize-TestEnvironment `
    -DSCModuleName $script:DSCModuleName `
    -DSCResourceName $script:DSCResourceName `
    -TestType Unit

#endregion HEADER

function Invoke-TestSetup {
    # Loading mocked classes
    Add-Type -Path (Join-Path -Path $script:moduleRoot -ChildPath 'Tests\Unit\Stubs\SMO.cs')
}

function Invoke-TestCleanup {
    Restore-TestEnvironment -TestEnvironment $TestEnvironment
}

# Begin Testing
try
{
    Invoke-TestSetup

    InModuleScope $script:DSCResourceName {
        $mockSqlServerName                          = 'localhost'
        $mockSqlServerInstanceName                  = 'MSSQLSERVER'
        $mockSqlDatabaseName                        = 'AdventureWorks'
        $mockSqlServerLogin                         = 'John'
        $mockSqlServerLoginOne                      = 'CONTOSO\KingJulian'
        $mockSqlServerLoginTwo                      = 'CONTOSO\SQLAdmin'
        $mockSqlServerLoginType                     = 'WindowsUser'
        $mockSqlDatabaseRole                        = 'MyRole'
        $mockSqlDatabaseRoleSecond                  = 'MySecondRole'
        $mockExpectedSqlDatabaseRole                = 'MyRole'
        $mockInvalidOperationForAddMemberMethod     = $false
        $mockInvalidOperationForDropMemberMethod    = $false
        $mockInvalidOperationForCreateMethod        = $false
        $mockExpectedForAddMemberMethod             = 'MySecondRole'
        $mockExpectedForDropMemberMethod            = 'MyRole'
        $mockExpectedForCreateMethod                = 'John'

        # Default parameters that are used for the It-blocks
        $mockDefaultParameters = @{
            SQLInstanceName = $mockSqlServerInstanceName
            SQLServer       = $mockSqlServerName
        }

        #region Function mocks
        $mockConnectSQL = {
            return @(
                (
                    New-Object Object |
                        Add-Member -MemberType NoteProperty -Name InstanceName -Value $mockSqlServerInstanceName -PassThru |
                        Add-Member -MemberType NoteProperty -Name ComputerNamePhysicalNetBIOS -Value $mockSqlServerName -PassThru |
                        Add-Member -MemberType ScriptProperty -Name Databases -Value {
                            return @{
                                $mockSqlDatabaseName = @((
                                    New-Object Object |
                                        Add-Member -MemberType NoteProperty -Name Name -Value $mockSqlDatabaseName -PassThru |
                                        Add-Member -MemberType ScriptProperty -Name Users -Value {
                                            return @{
                                                $mockSqlServerLoginOne = @((
                                                    New-Object Object |
                                                        Add-Member -MemberType ScriptMethod -Name IsMember -Value {
                                                            param(
                                                                [String]
                                                                $mockSqlDatabaseRole
                                                            )
                                                            if ( $mockSqlDatabaseRole -eq $mockExpectedSqlDatabaseRole )
                                                            {
                                                                return $true
                                                            }
                                                            else
                                                            {
                                                                return $false
                                                            }
                                                        } -PassThru
                                                ))
                                                $mockSqlServerLoginTwo = @((
                                                    New-Object Object |
                                                        Add-Member -MemberType ScriptMethod -Name IsMember -Value {
                                                                return $true
                                                        } -PassThru
                                                ))
                                            }
                                        } -PassThru |
                                        Add-Member -MemberType ScriptProperty -Name Roles -Value {
                                            return @{
                                                $mockSqlDatabaseRole = @((
                                                    New-Object Object |
                                                        Add-Member -MemberType NoteProperty -Name Name -Value $mockSqlDatabaseRole -PassThru |
                                                        Add-Member -MemberType ScriptMethod -Name AddMember -Value {
                                                            param(
                                                                [String]
                                                                $mockSqlServerLogin
                                                            )
                                                            if ($mockInvalidOperationForAddMemberMethod)
                                                            {
                                                                throw 'Mock AddMember Method was called with invalid operation.'
                                                            }
                                                            if ( $this.Name -ne $mockExpectedForAddMemberMethod )
                                                            {
                                                                throw "Called mocked AddMember() method without adding the right user. Expected '{0}'. But was '{1}'." `
                                                                        -f $mockExpectedForAddMemberMethod, $this.Name
                                                            }
                                                        } -PassThru |
                                                        Add-Member -MemberType ScriptMethod -Name DropMember -Value {
                                                            param(
                                                                [String]
                                                                $mockSqlServerLogin
                                                            )
                                                            if ($mockInvalidOperationForDropMemberMethod)
                                                            {
                                                                throw 'Mock DropMember Method was called with invalid operation.'
                                                            }
                                                            if ( $this.Name -ne $mockExpectedForDropMemberMethod )
                                                            {
                                                                throw "Called mocked Drop() method without adding the right user. Expected '{0}'. But was '{1}'." `
                                                                        -f $mockExpectedForDropMemberMethod, $this.Name
                                                            }
                                                        } -PassThru
                                                ))
                                                $mockSqlDatabaseRoleSecond = @((
                                                    New-Object Object |
                                                        Add-Member -MemberType NoteProperty -Name Name -Value $mockSqlDatabaseRoleSecond -PassThru |
                                                        Add-Member -MemberType ScriptMethod -Name AddMember -Value {
                                                            param(
                                                                [String]
                                                                $mockSqlServerLogin
                                                            )
                                                            if ($mockInvalidOperationForAddMemberMethod)
                                                            {
                                                                throw 'Mock AddMember Method was called with invalid operation.'
                                                            }
                                                            if ( $this.Name -ne $mockExpectedForAddMemberMethod )
                                                            {
                                                                throw "Called mocked AddMember() method without adding the right user. Expected '{0}'. But was '{1}'." `
                                                                        -f $mockExpectedForAddMemberMethod, $this.Name
                                                            }
                                                        } -PassThru |
                                                        Add-Member -MemberType ScriptMethod -Name DropMember -Value {
                                                            param(
                                                                [String]
                                                                $mockSqlServerLogin
                                                            )
                                                            if ($mockInvalidOperationForDropMemberMethod)
                                                            {
                                                                throw 'Mock DropMember Method was called with invalid operation.'
                                                            }
                                                            if ( $this.Name -ne $mockExpectedForDropMemberMethod )
                                                            {
                                                                throw "Called mocked Drop() method without adding the right user. Expected '{0}'. But was '{1}'." `
                                                                        -f $mockExpectedForDropMemberMethod, $this.Name
                                                            }
                                                        } -PassThru
                                                ))
                                            }
                                        }-PassThru -Force
                                    ))
                                }
                            } -PassThru -Force |
                        Add-Member -MemberType ScriptProperty -Name Logins -Value {
                            return @{
                                $mockSqlServerLoginOne = @((
                                    New-Object Object |
                                        Add-Member -MemberType NoteProperty -Name LoginType -Value $mockSqlServerLoginType -PassThru
                                ))
                                $mockSqlServerLoginTwo = @((
                                    New-Object Object |
                                        Add-Member -MemberType NoteProperty -Name LoginType -Value $mockSqlServerLoginType -PassThru
                                ))
                                $mockSqlServerLogin = @((
                                    New-Object Object |
                                        Add-Member -MemberType NoteProperty -Name LoginType -Value $mockSqlServerLoginType -PassThru
                                ))
                            }
                        } -PassThru -Force

                )
            )
        }

        $mockNewObjectUser = {
            return @(
                (
                    New-Object Object |
                        Add-Member -MemberType NoteProperty -Name Name -Value $mockSqlServerLogin -PassThru |
                        Add-Member -MemberType NoteProperty -Name Login -Value $mockSqlServerLogin -PassThru |
                        Add-Member -MemberType ScriptMethod -Name Create -Value {
                            if ($mockInvalidOperationForCreateMethod)
                            {
                                throw 'Mock Create Method was called with invalid operation.'
                            }
                            if ( $this.Name -ne $mockExpectedForCreateMethod )
                            {
                                throw "Called mocked Create() method without adding the right user. Expected '{0}'. But was '{1}'." `
                                        -f $mockExpectedForCreateMethod, $this.Name
                            }
                        } -PassThru -Force
                )
            )
        }
        #endregion

        Describe "MSFT_xSQLServerDatabaseRole\Get-TargetResource" -Tag 'Get'{
            BeforeEach {
                Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable
            }

            Context 'When passing values to parameters and database name does not exist' {
                It 'Should throw the correct error' {
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLoginOne
                        Database    = 'unknownDatabaseName'
                        Role        = $mockSqlDatabaseRole
                    }

                    $throwInvalidOperation = ("Database 'unknownDatabaseName' does not exist " + `
                                              "on SQL server 'localhost\MSSQLSERVER'.")

                    { Get-TargetResource @testParameters } | Should -Throw $throwInvalidOperation
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }
            }

            Context 'When passing values to parameters and role does not exist' {
                It 'Should throw the correct error' {
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLoginOne
                        Database    = $mockSqlDatabaseName
                        Role        = 'unknownRoleName'
                    }

                     $throwInvalidOperation = ("Role 'unknownRoleName' does not exist on database " + `
                                               "'AdventureWorks' on SQL server 'localhost\MSSQLSERVER'.")

                    { Get-TargetResource @testParameters } | Should -Throw $throwInvalidOperation
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }
            }

            Context 'When passing values to parameters and multiple values to Role parameter' {
                It 'Should not throw' {
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLoginOne
                        Database    = $mockSqlDatabaseName
                        Role        = @($mockSqlDatabaseRole,$mockSqlDatabaseRoleSecond)
                    }

                    { Get-TargetResource @testParameters } | Should -Not -Throw
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }
            }

            Context 'When passing values to parameters and login does not exist' {
                It 'Should throw the correct error' {
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = 'unknownLoginName'
                        Database    = $mockSqlDatabaseName
                        Role        = $mockSqlDatabaseRole
                    }

                    $throwInvalidOperation = ("Login 'unknownLoginName' does not exist " + `
                                              "on SQL server 'localhost\MSSQLSERVER'.")

                    { Get-TargetResource @testParameters } | Should -Throw $throwInvalidOperation
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }
            }

            Context 'When the system is not in the desired state, with one role' {
                $testParameters = $mockDefaultParameters
                $testParameters += @{
                    Name        = $mockSqlServerLoginOne
                    Database    = $mockSqlDatabaseName
                    Role        = $mockSqlDatabaseRoleSecond
                }

                It 'Should return the state as absent' {
                    $result = Get-TargetResource @testParameters
                    $result.Ensure | Should -Be 'Absent'

                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It
                }

                It 'Should not return any granted roles' {
                    $result = Get-TargetResource @testParameters
                    $result.Role | Should -Be $null

                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It
                }

                It 'Should return the same values as passed as parameters' {
                    $result = Get-TargetResource @testParameters
                    $result.SQLServer | Should -Be $testParameters.SQLServer
                    $result.SQLInstanceName | Should -Be $testParameters.SQLInstanceName
                    $result.Database | Should -Be $testParameters.Database
                    $result.Name | Should -Be $testParameters.Name

                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It
                }
            }

            Context 'When the system is not in the desired state, with two roles' {
                $testParameters = $mockDefaultParameters
                $testParameters += @{
                    Name        = $mockSqlServerLoginOne
                    Database    = $mockSqlDatabaseName
                    Role        = @($mockSqlDatabaseRole,$mockSqlDatabaseRoleSecond)
                }

                It 'Should return the state as absent' {
                    $result = Get-TargetResource @testParameters
                    $result.Ensure | Should -Be 'Absent'

                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It
                }

                It 'Should only return the one granted role' {
                    $result = Get-TargetResource @testParameters
                    $result.Role | Should -Be $mockSqlDatabaseRole

                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It
                }

                It 'Should return the same values as passed as parameters' {
                    $result = Get-TargetResource @testParameters
                    $result.SQLServer | Should -Be $testParameters.SQLServer
                    $result.SQLInstanceName | Should -Be $testParameters.SQLInstanceName
                    $result.Database | Should -Be $testParameters.Database
                    $result.Name | Should -Be $testParameters.Name

                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It
                }
            }

            Context 'When the system is not in the desired state, and login is not a member of the database' {
                $testParameters = $mockDefaultParameters
                $testParameters += @{
                    Name        = $mockSqlServerLogin
                    Database    = $mockSqlDatabaseName
                    Role        = $mockSqlDatabaseRole
                }

                It 'Should return the state as absent' {
                    $result = Get-TargetResource @testParameters
                    $result.Ensure | Should -Be 'Absent'

                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It
                }

                It 'Should return the same values as passed as parameters' {
                    $result = Get-TargetResource @testParameters
                    $result.SQLServer | Should -Be $testParameters.SQLServer
                    $result.SQLInstanceName | Should -Be $testParameters.SQLInstanceName
                    $result.Database | Should -Be $testParameters.Database
                    $result.Name | Should -Be $testParameters.Name

                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It
                }
            }

            Context 'When the system is in the desired state for a Windows user' {
                $testParameters = $mockDefaultParameters
                $testParameters += @{
                    Name        = $mockSqlServerLoginOne
                    Database    = $mockSqlDatabaseName
                    Role        = $mockSqlDatabaseRole
                }

                It 'Should return the state as absent' {
                    $result = Get-TargetResource @testParameters
                    $result.Ensure | Should -Be 'Present'

                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It
                }

                It 'Should return the same values as passed as parameters' {
                    $result = Get-TargetResource @testParameters
                    $result.SQLServer | Should -Be $testParameters.SQLServer
                    $result.SQLInstanceName | Should -Be $testParameters.SQLInstanceName
                    $result.Database | Should -Be $testParameters.Database
                    $result.Name | Should -Be $testParameters.Name
                    $result.Role | Should -Be $testParameters.Role

                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope It
                }
            }

            Assert-VerifiableMock
        }

        Describe "MSFT_xSQLServerDatabaseRole\Test-TargetResource" -Tag 'Test'{
            BeforeEach {
                Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable
            }

            Context 'When the system is not in the desired state and Ensure is set to Present' {
                It 'Should return the state as false when one desired role is not configured' {
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLoginOne
                        Database    = $mockSqlDatabaseName
                        Role        = $mockSqlDatabaseRoleSecond
                        Ensure      = 'Present'
                    }

                    $result = Test-TargetResource @testParameters
                    $result | Should -Be $false
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }
            }

            Context 'When the system is not in the desired state and Ensure is set to Present' {
                It 'Should return the state as false when two desired roles are not configured' {
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLoginOne
                        Database    = $mockSqlDatabaseName
                        Role        = @($mockSqlDatabaseRole,$mockSqlDatabaseRoleSecond)
                        Ensure      = 'Present'
                    }

                    $result = Test-TargetResource @testParameters
                    $result | Should -Be $false
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }
            }

            Context 'When the system is not in the desired state and Ensure is set to Absent' {
                It 'Should return the state as false when undesired roles are not configured' {
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLoginTwo
                        Database    = $mockSqlDatabaseName
                        Role        = @($mockSqlDatabaseRole,$mockSqlDatabaseRoleSecond)
                        Ensure      = 'Absent'
                    }

                    $result = Test-TargetResource @testParameters
                    $result | Should -Be $false
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }
            }

            Context 'When the system is in the desired state and Ensure is set to Present' {
                It 'Should return the state as true when one desired role is correctly configured' {
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLoginOne
                        Database    = $mockSqlDatabaseName
                        Role        = $mockSqlDatabaseRole
                        Ensure      = 'Present'
                    }

                    $result = Test-TargetResource @testParameters
                    $result | Should -Be $true
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }
            }

            Context 'When the system is in the desired state and Ensure is set to Present' {
                It 'Should return the state as true when two desired role are correctly configured' {
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLoginTwo
                        Database    = $mockSqlDatabaseName
                        Role        = @($mockSqlDatabaseRole,$mockSqlDatabaseRoleSecond)
                        Ensure      = 'Present'
                    }

                    $result = Test-TargetResource @testParameters
                    $result | Should -Be $true
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }
            }

            Context 'When the system is in the desired state and Ensure is set to Absent' {
                It 'Should return the state as true when two desired role are correctly configured' {
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLoginOne
                        Database    = $mockSqlDatabaseName
                        Role        = $mockSqlDatabaseRoleSecond
                        Ensure      = 'Absent'
                    }

                    $result = Test-TargetResource @testParameters
                    $result | Should -Be $true
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }
            }

            Assert-VerifiableMock
        }

        Describe "MSFT_xSQLServerDatabaseRole\Set-TargetResource" -Tag 'Set'{
            BeforeEach {
                Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable
                Mock -CommandName New-Object -MockWith $mockNewObjectUser -ParameterFilter {
                    $TypeName -eq 'Microsoft.SqlServer.Management.Smo.User'
                } -Verifiable
            }

            Context 'When the system is not in the desired state, Ensure is set to Present and Login does not exist' {
                It 'Should Not Throw when Ensure parameter is set to Present' {
                    $mockExpectedForAddMemberMethod    = 'MyRole'
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLogin
                        Database    = $mockSqlDatabaseName
                        Role        = $mockSqlDatabaseRole
                        Ensure      = 'Present'
                    }

                    { Set-TargetResource @testParameters } | Should -Not -Throw
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }

                It 'Should call the mock function New-Object with TypeName equal to Microsoft.SqlServer.Management.Smo.User' {
                    Assert-MockCalled New-Object -Exactly -Times 1 -ParameterFilter {
                        $TypeName -eq 'Microsoft.SqlServer.Management.Smo.User'
                    } -Scope Context
                }
            }

            Context 'When the system is not in the desired state, Ensure is set to Present and Login does not exist' {
                It 'Should Throw the correct error when Ensure parameter is set to Present' {
                    $mockInvalidOperationForCreateMethod = $true
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLogin
                        Database    = $mockSqlDatabaseName
                        Role        = $mockSqlDatabaseRoleSecond
                        Ensure      = 'Present'
                    }

                    $throwInvalidOperation = ('Failed adding the login John as a user of the database AdventureWorks, on ' + `
                                              'the instance localhost\MSSQLSERVER. InnerException: Exception calling "Create" ' + `
                                              'with "0" argument(s): "Mock Create Method was called with invalid operation."')

                    { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }

                It 'Should call the mock function New-Object with TypeName equal to Microsoft.SqlServer.Management.Smo.User' {
                    Assert-MockCalled New-Object -Exactly -Times 1 -ParameterFilter {
                        $TypeName -eq 'Microsoft.SqlServer.Management.Smo.User'
                    } -Scope Context
                }
            }

            Context 'When the system is not in the desired state, Ensure is set to Present and Login already exist' {
                It 'Should Not Throw when Ensure parameter is set to Present' {
                    $mockExpectedForAddMemberMethod = 'MySecondRole'
                    $mockSqlServerLogin = $mockSqlServerLoginOne
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLogin
                        Database    = $mockSqlDatabaseName
                        Role        = $mockSqlDatabaseRoleSecond
                        Ensure      = 'Present'
                    }

                    { Set-TargetResource @testParameters } | Should -Not -Throw
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }

                It 'Should not call the mock function New-Object with TypeName equal to Microsoft.SqlServer.Management.Smo.User' {
                    Assert-MockCalled New-Object -Exactly -Times 0 -ParameterFilter {
                        $TypeName -eq 'Microsoft.SqlServer.Management.Smo.User'
                    } -Scope Context
                }
            }

            Context 'When the system is not in the desired state, Ensure is set to Present and Login already exist' {
                It 'Should Throw the correct error when Ensure parameter is set to Present' {
                    $mockInvalidOperationForAddMemberMethod = $true
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLoginOne
                        Database    = $mockSqlDatabaseName
                        Role        = $mockSqlDatabaseRoleSecond
                        Ensure      = 'Present'
                    }

                    $throwInvalidOperation = ('Failed adding the login CONTOSO\KingJulian to the role MySecondRole on ' + `
                                              'the database AdventureWorks, on the instance localhost\MSSQLSERVER. ' + `
                                              'InnerException: Exception calling "AddMember" with "1" argument(s): ' + `
                                              '"Mock AddMember Method was called with invalid operation."')

                    { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }

                It 'Should not call the mock function New-Object with TypeName equal to Microsoft.SqlServer.Management.Smo.User' {
                    Assert-MockCalled New-Object -Exactly -Times 0 -ParameterFilter {
                        $TypeName -eq 'Microsoft.SqlServer.Management.Smo.User'
                    } -Scope Context
                }
            }

            Context 'When the system is not in the desired state, Ensure is set to Absent' {
                It 'Should not throw the correct error when Ensure parameter is set to Absent' {
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLoginTwo
                        Database    = $mockSqlDatabaseName
                        Role        = $mockSqlDatabaseRole
                        Ensure      = 'Absent'
                    }

                    { Set-TargetResource @testParameters } | Should -Not -Throw
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }

                It 'Should not call the mock function New-Object with TypeName equal to Microsoft.SqlServer.Management.Smo.User' {
                    Assert-MockCalled New-Object -Exactly -Times 0 -ParameterFilter {
                        $TypeName -eq 'Microsoft.SqlServer.Management.Smo.User'
                    } -Scope Context
                }
            }

            $mockInvalidOperationForDropMemberMethod = $true

            Context 'When the system is not in the desired state, Ensure is set to Absent' {
                It 'Should not throw the correct error when Ensure parameter is set to Absent' {
                    $testParameters = $mockDefaultParameters
                    $testParameters += @{
                        Name        = $mockSqlServerLoginTwo
                        Database    = $mockSqlDatabaseName
                        Role        = $mockSqlDatabaseRole
                        Ensure      = 'Absent'
                    }

                    $throwInvalidOperation = ('Failed removing the login CONTOSO\SQLAdmin from the role MyRole on ' + `
                                              'the database AdventureWorks, on the instance localhost\MSSQLSERVER. ' + `
                                              'InnerException: Exception calling "DropMember" with "1" argument(s): ' + `
                                              '"Mock DropMember Method was called with invalid operation."')

                    { Set-TargetResource @testParameters } | Should -Throw $throwInvalidOperation
                }

                It 'Should call the mock function Connect-SQL' {
                    Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
                }

                It 'Should not call the mock function New-Object with TypeName equal to Microsoft.SqlServer.Management.Smo.User' {
                    Assert-MockCalled New-Object -Exactly -Times 0 -ParameterFilter {
                        $TypeName -eq 'Microsoft.SqlServer.Management.Smo.User'
                    } -Scope Context
                }
            }

            Assert-VerifiableMock
        }
    }
}
finally
{
    Invoke-TestCleanup
}