mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 10:09:28 +02:00
91 lines
2.9 KiB
Go
91 lines
2.9 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 maloja_test
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/jarcoal/httpmock"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uploadedlobster.com/scotty/backends/maloja"
|
|
)
|
|
|
|
func TestNewClient(t *testing.T) {
|
|
serverUrl := "https://maloja.example.com"
|
|
token := "foobar123"
|
|
client := maloja.NewClient(serverUrl, token)
|
|
assert.Equal(t, serverUrl, client.HttpClient.BaseURL)
|
|
}
|
|
|
|
func TestGetScrobbles(t *testing.T) {
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
serverUrl := "https://maloja.example.com"
|
|
token := "thetoken"
|
|
client := maloja.NewClient(serverUrl, token)
|
|
setupHttpMock(t, client.HttpClient.GetClient(),
|
|
"https://maloja.example.com/apis/mlj_1/scrobbles",
|
|
"testdata/scrobbles.json")
|
|
|
|
result, err := client.GetScrobbles(0, 2)
|
|
require.NoError(t, err)
|
|
|
|
assert := assert.New(t)
|
|
require.Len(t, result.List, 2)
|
|
assert.Equal("Way to Eden", result.List[0].Track.Title)
|
|
assert.Equal(int64(558), result.List[0].Duration)
|
|
}
|
|
|
|
func TestNewScrobble(t *testing.T) {
|
|
server := "https://maloja.example.com"
|
|
client := maloja.NewClient(server, "thetoken")
|
|
httpmock.ActivateNonDefault(client.HttpClient.GetClient())
|
|
|
|
responder, err := httpmock.NewJsonResponder(200, httpmock.File("testdata/newscrobble-result.json"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
url := server + "/apis/mlj_1/newscrobble"
|
|
httpmock.RegisterResponder("POST", url, responder)
|
|
|
|
scrobble := maloja.NewScrobble{
|
|
Title: "Oweynagat",
|
|
Artist: "Dool",
|
|
Time: 1699574369,
|
|
}
|
|
result, err := client.NewScrobble(scrobble)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "success", result.Status)
|
|
}
|
|
|
|
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)
|
|
}
|