From e3079d81ea07c80a6f6b11ca2089d3f442ec99bf Mon Sep 17 00:00:00 2001 From: Deluan Date: Mon, 27 Mar 2023 20:36:23 -0400 Subject: [PATCH] More tests --- server/auth_test.go | 46 +++++++++++- server/serve_index_test.go | 93 ++++++++++++++++++++++++ server/{serve_test.go => server_test.go} | 0 3 files changed, 138 insertions(+), 1 deletion(-) rename server/{serve_test.go => server_test.go} (100%) diff --git a/server/auth_test.go b/server/auth_test.go index 951a0053..dbaa79e1 100644 --- a/server/auth_test.go +++ b/server/auth_test.go @@ -56,6 +56,7 @@ var _ = Describe("Auth", func() { Expect(parsed["token"]).ToNot(BeEmpty()) }) }) + Describe("Login from HTTP headers", func() { fs := os.DirFS("tests/fixtures") @@ -133,8 +134,8 @@ var _ = Describe("Auth", func() { // Request Header authentication should not generate a JWT token Expect(parsed).ToNot(HaveKey("token")) }) - }) + Describe("login", func() { BeforeEach(func() { req = httptest.NewRequest("POST", "/login", strings.NewReader(`{"username":"janedoe", "password":"abc123"}`)) @@ -178,4 +179,47 @@ var _ = Describe("Auth", func() { Expect(w.Code).To(Equal(200)) }) }) + + Describe("validateIPAgainstList", func() { + Context("when provided with empty inputs", func() { + It("should return false", func() { + Expect(validateIPAgainstList("", "")).To(BeFalse()) + Expect(validateIPAgainstList("192.168.1.1", "")).To(BeFalse()) + Expect(validateIPAgainstList("", "192.168.0.0/16")).To(BeFalse()) + }) + }) + + Context("when provided with invalid IP inputs", func() { + It("should return false", func() { + Expect(validateIPAgainstList("invalidIP", "192.168.0.0/16")).To(BeFalse()) + }) + }) + + Context("when provided with valid inputs", func() { + It("should return true when IP is in the list", func() { + Expect(validateIPAgainstList("192.168.1.1", "192.168.0.0/16,10.0.0.0/8")).To(BeTrue()) + Expect(validateIPAgainstList("10.0.0.1", "192.168.0.0/16,10.0.0.0/8")).To(BeTrue()) + }) + + It("should return false when IP is not in the list", func() { + Expect(validateIPAgainstList("172.16.0.1", "192.168.0.0/16,10.0.0.0/8")).To(BeFalse()) + }) + }) + + Context("when provided with invalid CIDR notation in the list", func() { + It("should ignore invalid CIDR and return the correct result", func() { + Expect(validateIPAgainstList("192.168.1.1", "192.168.0.0/16,invalidCIDR")).To(BeTrue()) + Expect(validateIPAgainstList("10.0.0.1", "invalidCIDR,10.0.0.0/8")).To(BeTrue()) + Expect(validateIPAgainstList("172.16.0.1", "192.168.0.0/16,invalidCIDR")).To(BeFalse()) + }) + }) + + Context("when provided with IP:port format", func() { + It("should handle IP:port format correctly", func() { + Expect(validateIPAgainstList("192.168.1.1:8080", "192.168.0.0/16,10.0.0.0/8")).To(BeTrue()) + Expect(validateIPAgainstList("10.0.0.1:1234", "192.168.0.0/16,10.0.0.0/8")).To(BeTrue()) + Expect(validateIPAgainstList("172.16.0.1:9999", "192.168.0.0/16,10.0.0.0/8")).To(BeFalse()) + }) + }) + }) }) diff --git a/server/serve_index_test.go b/server/serve_index_test.go index 95a35126..01f36a79 100644 --- a/server/serve_index_test.go +++ b/server/serve_index_test.go @@ -3,11 +3,13 @@ package server import ( "encoding/json" "fmt" + "net/http" "net/http/httptest" "os" "regexp" "strconv" "strings" + "time" "github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/conf/configtest" @@ -429,6 +431,97 @@ var _ = Describe("serveIndex", func() { }) }) +var _ = Describe("addShareData", func() { + var ( + r *http.Request + data map[string]interface{} + shareInfo *model.Share + ) + + BeforeEach(func() { + data = make(map[string]interface{}) + r = httptest.NewRequest("GET", "/", nil) + }) + + Context("when shareInfo is nil or has an empty ID", func() { + It("should not modify data", func() { + addShareData(r, data, nil) + Expect(data).To(BeEmpty()) + + shareInfo = &model.Share{} + addShareData(r, data, shareInfo) + Expect(data).To(BeEmpty()) + }) + }) + + Context("when shareInfo is not nil and has a non-empty ID", func() { + BeforeEach(func() { + shareInfo = &model.Share{ + ID: "testID", + Description: "Test description", + Downloadable: true, + Tracks: []model.MediaFile{ + { + ID: "track1", + Title: "Track 1", + Artist: "Artist 1", + Album: "Album 1", + Duration: 100, + UpdatedAt: time.Date(2023, time.Month(3), 27, 0, 0, 0, 0, time.UTC), + }, + { + ID: "track2", + Title: "Track 2", + Artist: "Artist 2", + Album: "Album 2", + Duration: 200, + UpdatedAt: time.Date(2023, time.Month(3), 26, 0, 0, 0, 0, time.UTC), + }, + }, + Contents: "Test contents", + URL: "https://example.com/share/testID", + ImageURL: "https://example.com/share/testID/image", + } + }) + + It("should populate data with shareInfo data", func() { + addShareData(r, data, shareInfo) + + Expect(data["ShareDescription"]).To(Equal(shareInfo.Description)) + Expect(data["ShareURL"]).To(Equal(shareInfo.URL)) + Expect(data["ShareImageURL"]).To(Equal(shareInfo.ImageURL)) + + var shareData shareData + err := json.Unmarshal([]byte(data["ShareInfo"].(string)), &shareData) + Expect(err).NotTo(HaveOccurred()) + Expect(shareData.ID).To(Equal(shareInfo.ID)) + Expect(shareData.Description).To(Equal(shareInfo.Description)) + Expect(shareData.Downloadable).To(Equal(shareInfo.Downloadable)) + + Expect(shareData.Tracks).To(HaveLen(len(shareInfo.Tracks))) + for i, track := range shareData.Tracks { + Expect(track.ID).To(Equal(shareInfo.Tracks[i].ID)) + Expect(track.Title).To(Equal(shareInfo.Tracks[i].Title)) + Expect(track.Artist).To(Equal(shareInfo.Tracks[i].Artist)) + Expect(track.Album).To(Equal(shareInfo.Tracks[i].Album)) + Expect(track.Duration).To(Equal(shareInfo.Tracks[i].Duration)) + Expect(track.UpdatedAt).To(Equal(shareInfo.Tracks[i].UpdatedAt)) + } + }) + + Context("when shareInfo has an empty description", func() { + BeforeEach(func() { + shareInfo.Description = "" + }) + + It("should use shareInfo.Contents as ShareDescription", func() { + addShareData(r, data, shareInfo) + Expect(data["ShareDescription"]).To(Equal(shareInfo.Contents)) + }) + }) + }) +}) + var appConfigRegex = regexp.MustCompile(`(?m)window.__APP_CONFIG__=(.*);`) func extractAppConfig(body string) map[string]interface{} { diff --git a/server/serve_test.go b/server/server_test.go similarity index 100% rename from server/serve_test.go rename to server/server_test.go