mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 18:19:28 +02:00
98 lines
3.4 KiB
Go
98 lines
3.4 KiB
Go
/*
|
|
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package funkwhale_test
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/jarcoal/httpmock"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uploadedlobster.com/scotty/internal/backends/funkwhale"
|
|
)
|
|
|
|
func TestNewClient(t *testing.T) {
|
|
serverUrl := "https://funkwhale.example.com"
|
|
token := "foobar123"
|
|
client := funkwhale.NewClient(serverUrl, token)
|
|
assert.Equal(t, serverUrl, client.HttpClient.BaseURL)
|
|
assert.Equal(t, token, client.HttpClient.Token)
|
|
}
|
|
|
|
func TestGetHistoryListenings(t *testing.T) {
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
serverUrl := "https://funkwhale.example.com"
|
|
token := "thetoken"
|
|
client := funkwhale.NewClient(serverUrl, token)
|
|
setupHttpMock(t, client.HttpClient.GetClient(),
|
|
"https://funkwhale.example.com/api/v1/history/listenings",
|
|
"testdata/listenings.json")
|
|
|
|
result, err := client.GetHistoryListenings("outsidecontext", 0, 2)
|
|
require.NoError(t, err)
|
|
|
|
assert := assert.New(t)
|
|
assert.Equal(2204, result.Count)
|
|
require.Len(t, result.Results, 2)
|
|
listen1 := result.Results[0]
|
|
assert.Equal("2023-11-09T23:59:29.022005Z", listen1.CreationDate)
|
|
assert.Equal("Way to Eden", listen1.Track.Title)
|
|
assert.Equal("Hazeshuttle", listen1.Track.Album.Title)
|
|
assert.Equal("Hazeshuttle", listen1.Track.Artist.Name)
|
|
assert.Equal("phw", listen1.User.UserName)
|
|
}
|
|
|
|
func TestGetFavoriteTracks(t *testing.T) {
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
token := "thetoken"
|
|
serverUrl := "https://funkwhale.example.com"
|
|
client := funkwhale.NewClient(serverUrl, token)
|
|
setupHttpMock(t, client.HttpClient.GetClient(),
|
|
"https://funkwhale.example.com/api/v1/favorites/tracks",
|
|
"testdata/favorite-tracks.json")
|
|
|
|
result, err := client.GetFavoriteTracks(0, 2)
|
|
require.NoError(t, err)
|
|
|
|
assert := assert.New(t)
|
|
assert.Equal(76, result.Count)
|
|
require.Len(t, result.Results, 2)
|
|
fav1 := result.Results[0]
|
|
assert.Equal("2023-11-05T20:32:32.339738Z", fav1.CreationDate)
|
|
assert.Equal("Reign", fav1.Track.Title)
|
|
assert.Equal("Home Economics", fav1.Track.Album.Title)
|
|
assert.Equal("Prinzhorn Dance School", fav1.Track.Artist.Name)
|
|
assert.Equal("phw", fav1.User.UserName)
|
|
}
|
|
|
|
func setupHttpMock(t *testing.T, client *http.Client, url string, testDataPath string) {
|
|
httpmock.ActivateNonDefault(client)
|
|
|
|
responder, err := httpmock.NewJsonResponder(200, httpmock.File(testDataPath))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
httpmock.RegisterResponder("GET", url, responder)
|
|
}
|