spotify_test.go 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package spotify
  2. import (
  3. "os"
  4. "strings"
  5. "testing"
  6. )
  7. func getAuthorization() (string, string) {
  8. envVars := os.Environ()
  9. for _, envVar := range envVars {
  10. key_val := strings.Split(envVar, "=")
  11. if key_val[0] == "SPOTIFY_TOKEN" {
  12. username_secret := strings.Split(key_val[1], ":")
  13. return username_secret[0], username_secret[1]
  14. }
  15. }
  16. return "", ""
  17. }
  18. func createClient(t *testing.T) *SpotifyClient {
  19. username, secret := getAuthorization()
  20. if username == "" {
  21. t.Fatal("Spotify authorization token not available, please use it as env variable SPOTIFY_TOKEN")
  22. }
  23. client := NewClient(username, secret)
  24. err := client.Authenticate()
  25. if err != nil {
  26. t.Fatal("Spotify authentication failed", err)
  27. }
  28. return client
  29. }
  30. func TestGetTrackInfo(t *testing.T) {
  31. client := createClient(t)
  32. track, err := client.GetTrack("5fLiaYOzIAk0PpwJ2VQUpT")
  33. if err != nil {
  34. t.Error("Error while reading Track", err)
  35. }
  36. if track.Artists[0].Name != "Vesala" {
  37. t.Error("Expected track artist to be 'Vesala'")
  38. }
  39. if track.Name != "Rakkaus ja maailmanloppu" {
  40. t.Error("Expected track artist to be 'Rakkaus ja maailmanloppu'")
  41. }
  42. if track.ExternalUrls.Spotify != "https://open.spotify.com/track/5fLiaYOzIAk0PpwJ2VQUpT" {
  43. t.Error("Expected track artist to be 'https://open.spotify.com/track/5fLiaYOzIAk0PpwJ2VQUpT'")
  44. }
  45. }