From ed01946acea0c42b449b579f0e0fc269f3b35e40 Mon Sep 17 00:00:00 2001
From: Deluan <deluan@navidrome.org>
Date: Thu, 27 May 2021 21:04:03 -0400
Subject: [PATCH] Embed Last.FM error responses, making the tests faster

---
 core/agents/lastfm_test.go               | 25 ++++++++++++------------
 tests/fixtures/lastfm.artist.error3.json |  1 -
 tests/fixtures/lastfm.artist.error6.json |  1 -
 3 files changed, 13 insertions(+), 14 deletions(-)
 delete mode 100644 tests/fixtures/lastfm.artist.error3.json
 delete mode 100644 tests/fixtures/lastfm.artist.error6.json

diff --git a/core/agents/lastfm_test.go b/core/agents/lastfm_test.go
index eddeef794..63db4c3fc 100644
--- a/core/agents/lastfm_test.go
+++ b/core/agents/lastfm_test.go
@@ -1,8 +1,10 @@
 package agents
 
 import (
+	"bytes"
 	"context"
 	"errors"
+	"io/ioutil"
 	"net/http"
 	"os"
 
@@ -14,6 +16,11 @@ import (
 	. "github.com/onsi/gomega"
 )
 
+const (
+	lastfmError3 = `{"error":3,"message":"Invalid Method - No method with that name in this package","links":[]}`
+	lastfmError6 = `{"error":6,"message":"The artist you supplied could not be found","links":[]}`
+)
+
 var _ = Describe("lastfmAgent", func() {
 	Describe("lastFMConstructor", func() {
 		It("uses default api key and language if not configured", func() {
@@ -59,8 +66,7 @@ var _ = Describe("lastfmAgent", func() {
 		})
 
 		It("returns an error if Last.FM call returns an error", func() {
-			f, _ := os.Open("tests/fixtures/lastfm.artist.error3.json")
-			httpClient.Res = http.Response{Body: f, StatusCode: 200}
+			httpClient.Res = http.Response{Body: ioutil.NopCloser(bytes.NewBufferString(lastfmError3)), StatusCode: 200}
 			_, err := agent.GetBiography("123", "U2", "mbid-1234")
 			Expect(err).To(HaveOccurred())
 			Expect(httpClient.RequestCount).To(Equal(1))
@@ -76,8 +82,7 @@ var _ = Describe("lastfmAgent", func() {
 				Expect(httpClient.SavedRequest.URL.Query().Get("mbid")).To(BeEmpty())
 			})
 			It("calls again when last.fm returns an error 6", func() {
-				f, _ := os.Open("tests/fixtures/lastfm.artist.error6.json")
-				httpClient.Res = http.Response{Body: f, StatusCode: 200}
+				httpClient.Res = http.Response{Body: ioutil.NopCloser(bytes.NewBufferString(lastfmError6)), StatusCode: 200}
 				_, _ = agent.GetBiography("123", "U2", "mbid-1234")
 				Expect(httpClient.RequestCount).To(Equal(2))
 				Expect(httpClient.SavedRequest.URL.Query().Get("mbid")).To(BeEmpty())
@@ -115,8 +120,7 @@ var _ = Describe("lastfmAgent", func() {
 		})
 
 		It("returns an error if Last.FM call returns an error", func() {
-			f, _ := os.Open("tests/fixtures/lastfm.artist.error3.json")
-			httpClient.Res = http.Response{Body: f, StatusCode: 200}
+			httpClient.Res = http.Response{Body: ioutil.NopCloser(bytes.NewBufferString(lastfmError3)), StatusCode: 200}
 			_, err := agent.GetSimilar("123", "U2", "mbid-1234", 2)
 			Expect(err).To(HaveOccurred())
 			Expect(httpClient.RequestCount).To(Equal(1))
@@ -132,8 +136,7 @@ var _ = Describe("lastfmAgent", func() {
 				Expect(httpClient.SavedRequest.URL.Query().Get("mbid")).To(BeEmpty())
 			})
 			It("calls again when last.fm returns an error 6", func() {
-				f, _ := os.Open("tests/fixtures/lastfm.artist.error6.json")
-				httpClient.Res = http.Response{Body: f, StatusCode: 200}
+				httpClient.Res = http.Response{Body: ioutil.NopCloser(bytes.NewBufferString(lastfmError6)), StatusCode: 200}
 				_, _ = agent.GetSimilar("123", "U2", "mbid-1234", 2)
 				Expect(httpClient.RequestCount).To(Equal(2))
 				Expect(httpClient.SavedRequest.URL.Query().Get("mbid")).To(BeEmpty())
@@ -171,8 +174,7 @@ var _ = Describe("lastfmAgent", func() {
 		})
 
 		It("returns an error if Last.FM call returns an error", func() {
-			f, _ := os.Open("tests/fixtures/lastfm.artist.error3.json")
-			httpClient.Res = http.Response{Body: f, StatusCode: 200}
+			httpClient.Res = http.Response{Body: ioutil.NopCloser(bytes.NewBufferString(lastfmError3)), StatusCode: 200}
 			_, err := agent.GetTopSongs("123", "U2", "mbid-1234", 2)
 			Expect(err).To(HaveOccurred())
 			Expect(httpClient.RequestCount).To(Equal(1))
@@ -188,8 +190,7 @@ var _ = Describe("lastfmAgent", func() {
 				Expect(httpClient.SavedRequest.URL.Query().Get("mbid")).To(BeEmpty())
 			})
 			It("calls again when last.fm returns an error 6", func() {
-				f, _ := os.Open("tests/fixtures/lastfm.artist.error6.json")
-				httpClient.Res = http.Response{Body: f, StatusCode: 200}
+				httpClient.Res = http.Response{Body: ioutil.NopCloser(bytes.NewBufferString(lastfmError6)), StatusCode: 200}
 				_, _ = agent.GetTopSongs("123", "U2", "mbid-1234", 2)
 				Expect(httpClient.RequestCount).To(Equal(2))
 				Expect(httpClient.SavedRequest.URL.Query().Get("mbid")).To(BeEmpty())
diff --git a/tests/fixtures/lastfm.artist.error3.json b/tests/fixtures/lastfm.artist.error3.json
deleted file mode 100644
index bf51136fc..000000000
--- a/tests/fixtures/lastfm.artist.error3.json
+++ /dev/null
@@ -1 +0,0 @@
-{"error":3,"message":"Invalid Method - No method with that name in this package","links":[]}
diff --git a/tests/fixtures/lastfm.artist.error6.json b/tests/fixtures/lastfm.artist.error6.json
deleted file mode 100644
index b70a6b7f9..000000000
--- a/tests/fixtures/lastfm.artist.error6.json
+++ /dev/null
@@ -1 +0,0 @@
-{"error":6,"message":"The artist you supplied could not be found","links":[]}
\ No newline at end of file