Skip to content

Commit

Permalink
Merge pull request #301 from lipkau/fix/#299-ResolveUserString
Browse files Browse the repository at this point in the history
Fixed the way a string of an user is resolved
  • Loading branch information
lipkau authored Jul 9, 2018
2 parents fa89dd0 + 3e8160b commit b1f3ca1
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 38 deletions.
2 changes: 1 addition & 1 deletion JiraPS/Public/Remove-JiraGroupMember.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function Remove-JiraGroupMember {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Processing [$_user]"
Write-Debug "[$($MyInvocation.MyCommand.Name)] Processing `$_user [$_user]"

$userObj = Get-JiraUser -InputObject $_user -Credential $Credential -ErrorAction Stop
$userObj = Get-JiraUser -UserName $_user -Credential $Credential -ErrorAction Stop

# if ($groupMembers -contains $userObj.Name) {
# TODO: test what jira says
Expand Down
183 changes: 146 additions & 37 deletions Tests/Remove-JiraGroupMember.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,86 +1,103 @@
Describe "Remove-JiraGroupMember" {

Import-Module "$PSScriptRoot/../JiraPS" -Force -ErrorAction Stop
BeforeAll {
Remove-Module JiraPS -ErrorAction SilentlyContinue
Import-Module "$PSScriptRoot/../JiraPS" -Force -ErrorAction Stop
}

InModuleScope JiraPS {

. "$PSScriptRoot/Shared.ps1"

#region Definitions
$jiraServer = 'http://jiraserver.example.com'

$testGroupName = 'testGroup'
$testUsername1 = 'testUsername1'
$testUsername2 = 'testUsername2'
#endregion Definitions

#region Mocks
Mock Get-JiraConfigServer -ModuleName JiraPS {
Write-Output $jiraServer
}

Mock Get-JiraGroup -ModuleName JiraPS {
$object = [PSCustomObject] @{
'Name' = $testGroupName
'Size' = 2
[PSCustomObject]@{
PSTypeName = "JiraPS.Group"
Name = $testGroupName
Size = 2
}
$object.PSObject.TypeNames.Insert(0, 'JiraPS.Group')
return $object
}

Mock Get-JiraUser -ModuleName JiraPS {
$object = [PSCustomObject] @{
'Name' = "$InputObject"
if ($InputObject) {
$obj = [PSCustomObject]@{
PSTypeName = "JiraPS.User"
Name = "$InputObject"
}
}
else {
$obj = [PSCustomObject]@{
PSTypeName = "JiraPS.User"
Name = "$UserName"
}
}
$obj | Add-Member -MemberType ScriptMethod -Name "ToString" -Force -Value {
Write-Output "$($this.Name)"
}
$object.PSObject.TypeNames.Insert(0, 'JiraPS.User')
return $object
$obj
}

Mock Get-JiraGroupMember -ModuleName JiraPS {
$object = [PSCustomObject] @{
'Name' = $testUsername1
[PSCustomObject]@{
PSTypeName = "JiraPS.Group"
Name = $testUsername1
}
$object.PSObject.TypeNames.Insert(0, 'JiraPS.Group')
return $object
}

Mock Invoke-JiraMethod -ModuleName JiraPS {
ShowMockInfo 'Invoke-JiraMethod' 'Method', 'Uri'
}
#endregion Mocks

#############
# Tests
#############
Context "Sanity checking" {
$command = Get-Command -Name Remove-JiraGroupMember

It "Accepts a group name as a String to the -Group parameter" {
{ Remove-JiraGroupMember -Group $testGroupName -User $testUsername1 -Force } | Should Not Throw
Assert-MockCalled -CommandName Invoke-JiraMethod -ParameterFilter {$URI -match $testGroupName} -Exactly -Times 1 -Scope It
}

It "Accepts a JiraPS.Group object to the -Group parameter" {
$group = Get-JiraGroup -GroupName $testGroupName
{ Remove-JiraGroupMember -Group $testGroupName -User $testUsername1 -Force } | Should Not Throw
Assert-MockCalled -CommandName Invoke-JiraMethod -ParameterFilter {$URI -match $testGroupName} -Exactly -Times 1 -Scope It
}

It "Accepts pipeline input from Get-JiraGroup" {
{ Get-JiraGroup -GroupName $testGroupName | Remove-JiraGroupMember -User $testUsername1 -Force} | Should Not Throw
Assert-MockCalled -CommandName Invoke-JiraMethod -ParameterFilter {$URI -match $testGroupName} -Exactly -Times 1 -Scope It
}
defParam $command 'Group'
defParam $command 'User'
defParam $command 'Credential'
defParam $command 'PassThru'
defParam $command 'Force'
}

Context "Behavior testing" {

It "Tests to see if a provided user is currently a member of the provided JIRA group before attempting to remove them" {
{ Remove-JiraGroupMember -Group $testGroupName -User $testUsername1 -Force } | Should Not Throw
Assert-MockCalled -CommandName Get-JiraGroup -Exactly -Times 1 -Scope It

Assert-MockCalled -CommandName Get-JiraGroup -ModuleName "JiraPS" -Exactly -Times 1 -Scope It
}

It "Removes a user from a JIRA group if the user is a member" {
{ Remove-JiraGroupMember -Group $testGroupName -User $testUsername1 -Force } | Should Not Throw
Assert-MockCalled -CommandName Invoke-JiraMethod -ParameterFilter {$Method -eq 'Delete' -and $URI -like "$jiraServer/rest/api/*/group/user?groupname=$testGroupName&username=$testUsername1"} -Exactly -Times 1 -Scope It

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = "JiraPS"
ParameterFilter = {
$Method -eq 'Delete' -and
$URI -like "$jiraServer/rest/api/*/group/user?groupname=$testGroupName&username=$testUsername1"
}
Exactly = $true
Times = 1
Scope = 'It'
}
Assert-MockCalled @assertMockCalledSplat
}

It "Removes multiple users to a JIRA group if they are passed to the -User parameter" {

# Override our previous mock so we have two group members
Mock Get-JiraGroupMember -ModuleName JiraPS {
@(
Expand All @@ -95,12 +112,104 @@ Describe "Remove-JiraGroupMember" {

# Should use the REST method twice, since at present, you can only delete one group member per API call
{ Remove-JiraGroupMember -Group $testGroupName -User $testUsername1, $testUsername2 -Force } | Should Not Throw
Assert-MockCalled -CommandName Invoke-JiraMethod -ParameterFilter {$Method -eq 'Delete' -and $URI -like "$jiraServer/rest/api/*/group/user?groupname=$testGroupName&username=*"} -Exactly -Times 2 -Scope It

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = "JiraPS"
ParameterFilter = {
$Method -eq 'Delete' -and
$URI -like "$jiraServer/rest/api/*/group/user?groupname=$testGroupName&username=*"
}
Exactly = $true
Times = 2
Scope = 'It'
}
Assert-MockCalled @assertMockCalledSplat
}
}

# Context "Error checking" {
Context "Input testing" {
It "Accepts a group name as a String to the -Group parameter" {
{ Remove-JiraGroupMember -Group $testGroupName -User $testUsername1 -Force } | Should Not Throw

# }
$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = "JiraPS"
ParameterFilter = {
$Method -eq "Delete" -and
$URI -like "*/rest/api/*/group/user*" -and
$URI -match "groupname=$testGroupName" -and
$URI -match "username=$testUsername1"
}
Exactly = $true
Times = 1
Scope = 'It'
}
Assert-MockCalled @assertMockCalledSplat
}

It "Accepts a JiraPS.Group object to the -Group parameter" {
{
$group = Get-JiraGroup -GroupName $testGroupName
Remove-JiraGroupMember -Group $group -User $testUsername1 -Force
} | Should Not Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = "JiraPS"
ParameterFilter = {
$Method -eq "Delete" -and
$URI -like "*/rest/api/*/group/user*" -and
$URI -match "groupname=$testGroupName" -and
$URI -match "username=$testUsername1"
}
Exactly = $true
Times = 1
Scope = 'It'
}
Assert-MockCalled @assertMockCalledSplat
}

It "Accepts pipeline input from Get-JiraGroup" {
{ Get-JiraGroup -GroupName $testGroupName | Remove-JiraGroupMember -User $testUsername1 -Force} | Should Not Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = "JiraPS"
ParameterFilter = {
$Method -eq "Delete" -and
$URI -like "*/rest/api/*/group/user*" -and
$URI -match "groupname=$testGroupName" -and
$URI -match "username=$testUsername1"
}
Exactly = $true
Times = 1
Scope = 'It'
}
Assert-MockCalled @assertMockCalledSplat
}

It "Accepts a JiraPS.User as input for -User parameter" {
{
$user = Get-JiraUser -UserName $testUsername1
Remove-JiraGroupMember -Group $testGroupName -User $user -Force
} | Should Not Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = "JiraPS"
ParameterFilter = {
$Method -eq "Delete" -and
$URI -like "*/rest/api/*/group/user*" -and
$URI -match "groupname=$testGroupName" -and
$URI -match "username=$testUsername1"
}
Exactly = $true
Times = 1
Scope = 'It'
}
Assert-MockCalled @assertMockCalledSplat
}
}
}
}

0 comments on commit b1f3ca1

Please sign in to comment.