Tests/UserInfo.steps.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
Then 'a Spotify UserProfile object should be returned' {
    $Profile | Should -BeOfType PSSpotify.UserProfile
}

And 'the Spotify UserProfile should have id (?<user>.+)' {
    param($user)

    if ($user -eq "current") {
        $user = $global:SpotifySession.CurrentUser.Id
    }

    $Profile.id | should -Be $user
}

And 'get (?<user>.+) userprofile (?<online>online|mocked)' {
    param($user, $online)

    $param = @{}
    if ($user -ne "current") {
        $param.add("id", $user)
    }
    else {
        $user = $global:SpotifySession.CurrentUser.Id
    }

    if ($online -ne "online") {
        mock Invoke-RestMethod -ParameterFilter {
            $Uri -eq "$global:RootUrl/me" -or 
            $Uri -like "$global:RootUrl/users/*"
        } -MockWith ([scriptblock]::create("[PSSpotify.UserProfile]@{id = '$user'}"))
    }

    $Profile = Get-SpotifyProfile @param
}

And 'mock users top (?<number>.+) played artists' {
    param($number)

    $mockscript = @()

    for ($i = 0; $i -lt $number; $i++) {
        $id = get-random -Maximum 999
        $mockscript += "([pscustomobject]@{Items = [pscustomobject]@{Id = '$id'}})"
    }

    mock Invoke-RestMethod ([scriptblock]::create("@(" + ($mockscript -join ",") + ")"))
    
    mock Get-SpotifyArtist {
        param([parameter(ValueFromPipelineByPropertyName)]$id)
        new-object PSSpotify.Artist -Property @{
            id = $id
        }
    }
}

Then 'users top (?<number>.+) played artists should be returned' {
    param($number)
    
    $Artists = Get-SpotifyUsersTopArtist -limit $number
    $Artists[0] | should -BeOfType PSSpotify.Artist
    $Artists.count | should -Be $number
}

And 'mock users top (?<number>.+) played tracks' {
    param($number)
    
    $mockscript = @()

    for ($i = 0; $i -lt $number; $i++) {
        $id = get-random -Maximum 999
        $mockscript += "[pscustomobject]@{Items = [pscustomobject]@{Id = '$id'}}"
    }
    mock Invoke-RestMethod ([scriptblock]::create($mockscript -join ';'))
    
    mock Get-SpotifyTrack {
        param([parameter(ValueFromPipelineByPropertyName)]$id)
        new-object PSSpotify.Track -Property @{
            id = $id
        }
    }
}

Then 'users top (?<number>.+) played tracks should be returned' {
    param($number)
    
    $Tracks = Get-SpotifyUsersTopTrack -limit $number
    $Tracks[0] | should -BeOfType PSSpotify.Track
    $Tracks.count | should -Be $number
}

Then 'return users top (?<return>.+) played tracks but skip the first (?<skip>.+)' {
    param($Return, $Skip)

    $Tracks = Get-SpotifyUsersTopTrack -limit $return -offset $Skip
    $Tracks[0] | should -BeOfType PSSpotify.Track
    $Tracks.count | should -Be $return
    Compare-Object ($MockedTracks | select -First $Return -Skip $Skip) $Tracks | should -BeNullOrEmpty
}

Then 'return users top (?<return>.+) played artists but skip the first (?<skip>.+)' {
    param($Return, $Skip)

    $Artists = Get-SpotifyUsersTopArtist -limit $return -offset $Skip
    $Artists[0] | should -BeOfType PSSpotify.Artist
    $Artists.count | should -Be $return
    Compare-Object ($MockedArtists | select -First $Return -Skip $Skip) $Artists | should -BeNullOrEmpty
}

And 'mock users top (?<number>.+) played artists with limit of (?<return>.+) and skip of (?<skip>.+)' {
    param($number, $return, $skip)

    $MockedArtists = @()
    $mockscript = @()

    for ($i = 0; $i -lt $number; $i++) {
        $id = get-random -Maximum 999
        $mockscript += "([pscustomobject]@{Items = [pscustomobject]@{Id = '$id'}})"
        $MockedArtists += ([pscustomobject]@{Items = [pscustomobject]@{Id = $id}})
    }
    mock Invoke-RestMethod -ParameterFilter {
        $uri -like "*limit=$return*" -and
        $uri -like "*offset=$skip*"
    } -MockWith ([scriptblock]::create("@(" +($mockscript -join ',') + ") | select -first $return -skip $skip"))
}

And 'mock users top (?<number>.+) played tracks with limit of (?<return>.+) and skip of (?<skip>.+)' {
    param($number, $return, $skip)
    
    $MockedTracks = @()
    $mockscript = @()

    for ($i = 0; $i -lt $number; $i++) {
        $id = get-random -Maximum 999
        $mockscript += "([pscustomobject]@{Items = [pscustomobject]@{Id = '$id'}})"
        $MockedTracks += ([pscustomobject]@{Items = [pscustomobject]@{Id = $id}})
    }
    mock Invoke-RestMethod -ParameterFilter {
        $uri -like "*limit=$return*" -and
        $uri -like "*offset=$skip*"
    } -MockWith ([scriptblock]::create("@(" +($mockscript -join ',') + ") | select -first $return -skip $skip"))
}