2025-11-14 22:15:43 -05:00
|
|
|
package subsonic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
2026-11-17 20:58:17 -05:00
|
|
|
"net/http"
|
2025-11-14 22:15:43 -05:00
|
|
|
"net/http/httptest"
|
|
|
|
|
|
|
|
|
|
"github.com/navidrome/navidrome/model"
|
|
|
|
|
"github.com/navidrome/navidrome/model/request"
|
|
|
|
|
"github.com/navidrome/navidrome/server/subsonic/responses"
|
|
|
|
|
"github.com/navidrome/navidrome/tests"
|
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var _ = Describe("LibraryScanning", func() {
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
var ctx context.Context
|
2025-11-14 22:15:43 -05:00
|
|
|
var api *Router
|
|
|
|
|
var ms *tests.MockScanner
|
|
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
ctx = GinkgoT().Context()
|
2025-11-14 22:15:43 -05:00
|
|
|
ms = tests.NewMockScanner()
|
|
|
|
|
api = &Router{scanner: ms}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Describe("StartScan", func() {
|
|
|
|
|
It("triggers a full scan with no parameters", func() {
|
|
|
|
|
// Create admin user
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
userCtx := request.WithUser(ctx, model.User{
|
2025-11-14 22:15:43 -05:00
|
|
|
ID: "admin-id",
|
|
|
|
|
IsAdmin: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create request with no parameters
|
2026-11-17 20:58:17 -05:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/rest/startScan", nil)
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
r = r.WithContext(userCtx)
|
2025-11-14 22:15:43 -05:00
|
|
|
|
|
|
|
|
// Call endpoint
|
|
|
|
|
response, err := api.StartScan(r)
|
|
|
|
|
|
|
|
|
|
// Should succeed
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(response).ToNot(BeNil())
|
|
|
|
|
|
|
|
|
|
// Verify ScanAll was called (eventually, since it's in a goroutine)
|
|
|
|
|
Eventually(func() int {
|
|
|
|
|
return ms.GetScanAllCallCount()
|
|
|
|
|
}).Should(BeNumerically(">", 0))
|
|
|
|
|
calls := ms.GetScanAllCalls()
|
|
|
|
|
Expect(calls).To(HaveLen(1))
|
|
|
|
|
Expect(calls[0].FullScan).To(BeFalse())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("triggers a full scan with fullScan=true", func() {
|
|
|
|
|
// Create admin user
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
userCtx := request.WithUser(ctx, model.User{
|
2025-11-14 22:15:43 -05:00
|
|
|
ID: "admin-id",
|
|
|
|
|
IsAdmin: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create request with fullScan parameter
|
2026-11-17 20:58:17 -05:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/rest/startScan?fullScan=true", nil)
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
r = r.WithContext(userCtx)
|
2025-11-14 22:15:43 -05:00
|
|
|
|
|
|
|
|
// Call endpoint
|
|
|
|
|
response, err := api.StartScan(r)
|
|
|
|
|
|
|
|
|
|
// Should succeed
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(response).ToNot(BeNil())
|
|
|
|
|
|
|
|
|
|
// Verify ScanAll was called with fullScan=true
|
|
|
|
|
Eventually(func() int {
|
|
|
|
|
return ms.GetScanAllCallCount()
|
|
|
|
|
}).Should(BeNumerically(">", 0))
|
|
|
|
|
calls := ms.GetScanAllCalls()
|
|
|
|
|
Expect(calls).To(HaveLen(1))
|
|
|
|
|
Expect(calls[0].FullScan).To(BeTrue())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("triggers a selective scan with single target parameter", func() {
|
|
|
|
|
// Setup mocks
|
|
|
|
|
mockUserRepo := tests.CreateMockUserRepo()
|
2026-09-25 18:06:10 -04:00
|
|
|
_ = mockUserRepo.SetUserLibraries(GinkgoT().Context(), "admin-id", []int{1, 2})
|
2025-11-14 22:15:43 -05:00
|
|
|
mockDS := &tests.MockDataStore{MockedUser: mockUserRepo}
|
|
|
|
|
api.ds = mockDS
|
|
|
|
|
|
|
|
|
|
// Create admin user
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
userCtx := request.WithUser(ctx, model.User{
|
2025-11-14 22:15:43 -05:00
|
|
|
ID: "admin-id",
|
|
|
|
|
IsAdmin: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create request with single target parameter
|
2026-11-17 20:58:17 -05:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/rest/startScan?target=1:Music/Rock", nil)
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
r = r.WithContext(userCtx)
|
2025-11-14 22:15:43 -05:00
|
|
|
|
|
|
|
|
// Call endpoint
|
|
|
|
|
response, err := api.StartScan(r)
|
|
|
|
|
|
|
|
|
|
// Should succeed
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(response).ToNot(BeNil())
|
|
|
|
|
|
|
|
|
|
// Verify ScanFolders was called with correct targets
|
|
|
|
|
Eventually(func() int {
|
|
|
|
|
return ms.GetScanFoldersCallCount()
|
|
|
|
|
}).Should(BeNumerically(">", 0))
|
|
|
|
|
calls := ms.GetScanFoldersCalls()
|
|
|
|
|
Expect(calls).To(HaveLen(1))
|
|
|
|
|
targets := calls[0].Targets
|
|
|
|
|
Expect(targets).To(HaveLen(1))
|
|
|
|
|
Expect(targets[0].LibraryID).To(Equal(1))
|
|
|
|
|
Expect(targets[0].FolderPath).To(Equal("Music/Rock"))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("triggers a selective scan with multiple target parameters", func() {
|
|
|
|
|
// Setup mocks
|
|
|
|
|
mockUserRepo := tests.CreateMockUserRepo()
|
2026-09-25 18:06:10 -04:00
|
|
|
_ = mockUserRepo.SetUserLibraries(GinkgoT().Context(), "admin-id", []int{1, 2})
|
2025-11-14 22:15:43 -05:00
|
|
|
mockDS := &tests.MockDataStore{MockedUser: mockUserRepo}
|
|
|
|
|
api.ds = mockDS
|
|
|
|
|
|
|
|
|
|
// Create admin user
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
userCtx := request.WithUser(ctx, model.User{
|
2025-11-14 22:15:43 -05:00
|
|
|
ID: "admin-id",
|
|
|
|
|
IsAdmin: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create request with multiple target parameters
|
2026-11-17 20:58:17 -05:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/rest/startScan?target=1:Music/Reggae&target=2:Classical/Bach", nil)
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
r = r.WithContext(userCtx)
|
2025-11-14 22:15:43 -05:00
|
|
|
|
|
|
|
|
// Call endpoint
|
|
|
|
|
response, err := api.StartScan(r)
|
|
|
|
|
|
|
|
|
|
// Should succeed
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(response).ToNot(BeNil())
|
|
|
|
|
|
|
|
|
|
// Verify ScanFolders was called with correct targets
|
|
|
|
|
Eventually(func() int {
|
|
|
|
|
return ms.GetScanFoldersCallCount()
|
|
|
|
|
}).Should(BeNumerically(">", 0))
|
|
|
|
|
calls := ms.GetScanFoldersCalls()
|
|
|
|
|
Expect(calls).To(HaveLen(1))
|
|
|
|
|
targets := calls[0].Targets
|
|
|
|
|
Expect(targets).To(HaveLen(2))
|
|
|
|
|
Expect(targets[0].LibraryID).To(Equal(1))
|
|
|
|
|
Expect(targets[0].FolderPath).To(Equal("Music/Reggae"))
|
|
|
|
|
Expect(targets[1].LibraryID).To(Equal(2))
|
|
|
|
|
Expect(targets[1].FolderPath).To(Equal("Classical/Bach"))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("triggers a selective full scan with target and fullScan parameters", func() {
|
|
|
|
|
// Setup mocks
|
|
|
|
|
mockUserRepo := tests.CreateMockUserRepo()
|
2026-09-25 18:06:10 -04:00
|
|
|
_ = mockUserRepo.SetUserLibraries(GinkgoT().Context(), "admin-id", []int{1})
|
2025-11-14 22:15:43 -05:00
|
|
|
mockDS := &tests.MockDataStore{MockedUser: mockUserRepo}
|
|
|
|
|
api.ds = mockDS
|
|
|
|
|
|
|
|
|
|
// Create admin user
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
userCtx := request.WithUser(ctx, model.User{
|
2025-11-14 22:15:43 -05:00
|
|
|
ID: "admin-id",
|
|
|
|
|
IsAdmin: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create request with target and fullScan parameters
|
2026-11-17 20:58:17 -05:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/rest/startScan?target=1:Music/Jazz&fullScan=true", nil)
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
r = r.WithContext(userCtx)
|
2025-11-14 22:15:43 -05:00
|
|
|
|
|
|
|
|
// Call endpoint
|
|
|
|
|
response, err := api.StartScan(r)
|
|
|
|
|
|
|
|
|
|
// Should succeed
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(response).ToNot(BeNil())
|
|
|
|
|
|
|
|
|
|
// Verify ScanFolders was called with fullScan=true
|
|
|
|
|
Eventually(func() int {
|
|
|
|
|
return ms.GetScanFoldersCallCount()
|
|
|
|
|
}).Should(BeNumerically(">", 0))
|
|
|
|
|
calls := ms.GetScanFoldersCalls()
|
|
|
|
|
Expect(calls).To(HaveLen(1))
|
|
|
|
|
Expect(calls[0].FullScan).To(BeTrue())
|
|
|
|
|
targets := calls[0].Targets
|
|
|
|
|
Expect(targets).To(HaveLen(1))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("returns error for invalid target format", func() {
|
|
|
|
|
// Create admin user
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
userCtx := request.WithUser(ctx, model.User{
|
2025-11-14 22:15:43 -05:00
|
|
|
ID: "admin-id",
|
|
|
|
|
IsAdmin: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create request with invalid target format (missing colon)
|
2026-11-17 20:58:17 -05:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/rest/startScan?target=1MusicRock", nil)
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
r = r.WithContext(userCtx)
|
2025-11-14 22:15:43 -05:00
|
|
|
|
|
|
|
|
// Call endpoint
|
|
|
|
|
response, err := api.StartScan(r)
|
|
|
|
|
|
|
|
|
|
// Should return error
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
|
Expect(response).To(BeNil())
|
|
|
|
|
var subErr subError
|
|
|
|
|
ok := errors.As(err, &subErr)
|
|
|
|
|
Expect(ok).To(BeTrue())
|
|
|
|
|
Expect(subErr.code).To(Equal(responses.ErrorGeneric))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("returns error for invalid library ID in target", func() {
|
|
|
|
|
// Create admin user
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
userCtx := request.WithUser(ctx, model.User{
|
2025-11-14 22:15:43 -05:00
|
|
|
ID: "admin-id",
|
|
|
|
|
IsAdmin: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create request with invalid library ID
|
2026-11-17 20:58:17 -05:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/rest/startScan?target=0:Music/Rock", nil)
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
r = r.WithContext(userCtx)
|
2025-11-14 22:15:43 -05:00
|
|
|
|
|
|
|
|
// Call endpoint
|
|
|
|
|
response, err := api.StartScan(r)
|
|
|
|
|
|
|
|
|
|
// Should return error
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
|
Expect(response).To(BeNil())
|
|
|
|
|
var subErr subError
|
|
|
|
|
ok := errors.As(err, &subErr)
|
|
|
|
|
Expect(ok).To(BeTrue())
|
|
|
|
|
Expect(subErr.code).To(Equal(responses.ErrorGeneric))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("returns error when library does not exist", func() {
|
|
|
|
|
// Setup mocks - user has access to library 1 and 2 only
|
|
|
|
|
mockUserRepo := tests.CreateMockUserRepo()
|
2026-09-25 18:06:10 -04:00
|
|
|
_ = mockUserRepo.SetUserLibraries(GinkgoT().Context(), "admin-id", []int{1, 2})
|
2025-11-14 22:15:43 -05:00
|
|
|
mockDS := &tests.MockDataStore{MockedUser: mockUserRepo}
|
|
|
|
|
api.ds = mockDS
|
|
|
|
|
|
|
|
|
|
// Create admin user
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
userCtx := request.WithUser(ctx, model.User{
|
2025-11-14 22:15:43 -05:00
|
|
|
ID: "admin-id",
|
|
|
|
|
IsAdmin: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create request with library ID that doesn't exist
|
2026-11-17 20:58:17 -05:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/rest/startScan?target=999:Music/Rock", nil)
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
r = r.WithContext(userCtx)
|
2025-11-14 22:15:43 -05:00
|
|
|
|
|
|
|
|
// Call endpoint
|
|
|
|
|
response, err := api.StartScan(r)
|
|
|
|
|
|
|
|
|
|
// Should return ErrorDataNotFound
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
|
Expect(response).To(BeNil())
|
|
|
|
|
var subErr subError
|
|
|
|
|
ok := errors.As(err, &subErr)
|
|
|
|
|
Expect(ok).To(BeTrue())
|
|
|
|
|
Expect(subErr.code).To(Equal(responses.ErrorDataNotFound))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("calls ScanAll when single library with empty path and only one library exists", func() {
|
|
|
|
|
// Setup mocks - single library in DB
|
|
|
|
|
mockUserRepo := tests.CreateMockUserRepo()
|
2026-09-25 18:06:10 -04:00
|
|
|
_ = mockUserRepo.SetUserLibraries(GinkgoT().Context(), "admin-id", []int{1})
|
2025-11-14 22:15:43 -05:00
|
|
|
mockLibraryRepo := &tests.MockLibraryRepo{}
|
|
|
|
|
mockLibraryRepo.SetData(model.Libraries{
|
|
|
|
|
{ID: 1, Name: "Music Library", Path: "/music"},
|
|
|
|
|
})
|
|
|
|
|
mockDS := &tests.MockDataStore{
|
|
|
|
|
MockedUser: mockUserRepo,
|
|
|
|
|
MockedLibrary: mockLibraryRepo,
|
|
|
|
|
}
|
|
|
|
|
api.ds = mockDS
|
|
|
|
|
|
|
|
|
|
// Create admin user
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
userCtx := request.WithUser(ctx, model.User{
|
2025-11-14 22:15:43 -05:00
|
|
|
ID: "admin-id",
|
|
|
|
|
IsAdmin: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create request with single library and empty path
|
2026-11-17 20:58:17 -05:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/rest/startScan?target=1:", nil)
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
r = r.WithContext(userCtx)
|
2025-11-14 22:15:43 -05:00
|
|
|
|
|
|
|
|
// Call endpoint
|
|
|
|
|
response, err := api.StartScan(r)
|
|
|
|
|
|
|
|
|
|
// Should succeed
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(response).ToNot(BeNil())
|
|
|
|
|
|
|
|
|
|
// Verify ScanAll was called instead of ScanFolders
|
|
|
|
|
Eventually(func() int {
|
|
|
|
|
return ms.GetScanAllCallCount()
|
|
|
|
|
}).Should(BeNumerically(">", 0))
|
|
|
|
|
Expect(ms.GetScanFoldersCallCount()).To(Equal(0))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("calls ScanFolders when single library with empty path but multiple libraries exist", func() {
|
|
|
|
|
// Setup mocks - multiple libraries in DB
|
|
|
|
|
mockUserRepo := tests.CreateMockUserRepo()
|
2026-09-25 18:06:10 -04:00
|
|
|
_ = mockUserRepo.SetUserLibraries(GinkgoT().Context(), "admin-id", []int{1, 2})
|
2025-11-14 22:15:43 -05:00
|
|
|
mockLibraryRepo := &tests.MockLibraryRepo{}
|
|
|
|
|
mockLibraryRepo.SetData(model.Libraries{
|
|
|
|
|
{ID: 1, Name: "Music Library", Path: "/music"},
|
|
|
|
|
{ID: 2, Name: "Audiobooks", Path: "/audiobooks"},
|
|
|
|
|
})
|
|
|
|
|
mockDS := &tests.MockDataStore{
|
|
|
|
|
MockedUser: mockUserRepo,
|
|
|
|
|
MockedLibrary: mockLibraryRepo,
|
|
|
|
|
}
|
|
|
|
|
api.ds = mockDS
|
|
|
|
|
|
|
|
|
|
// Create admin user
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
userCtx := request.WithUser(ctx, model.User{
|
2025-11-14 22:15:43 -05:00
|
|
|
ID: "admin-id",
|
|
|
|
|
IsAdmin: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create request with single library and empty path
|
2026-11-17 20:58:17 -05:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/rest/startScan?target=1:", nil)
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
r = r.WithContext(userCtx)
|
2025-11-14 22:15:43 -05:00
|
|
|
|
|
|
|
|
// Call endpoint
|
|
|
|
|
response, err := api.StartScan(r)
|
|
|
|
|
|
|
|
|
|
// Should succeed
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(response).ToNot(BeNil())
|
|
|
|
|
|
|
|
|
|
// Verify ScanFolders was called (not ScanAll)
|
|
|
|
|
Eventually(func() int {
|
|
|
|
|
return ms.GetScanFoldersCallCount()
|
|
|
|
|
}).Should(BeNumerically(">", 0))
|
|
|
|
|
calls := ms.GetScanFoldersCalls()
|
|
|
|
|
Expect(calls).To(HaveLen(1))
|
|
|
|
|
targets := calls[0].Targets
|
|
|
|
|
Expect(targets).To(HaveLen(1))
|
|
|
|
|
Expect(targets[0].LibraryID).To(Equal(1))
|
|
|
|
|
Expect(targets[0].FolderPath).To(Equal(""))
|
|
|
|
|
})
|
2026-03-09 14:19:53 -04:00
|
|
|
|
|
|
|
|
It("returns correct scanType in response when fullScan=false", func() {
|
|
|
|
|
// Setup mock to update status when scan starts (simulating the real scanner)
|
|
|
|
|
ms.SetScanStatusFunc(func(fullScan bool, targets []model.ScanTarget) *model.ScannerStatus {
|
|
|
|
|
scanType := "quick"
|
|
|
|
|
if fullScan {
|
|
|
|
|
scanType = "full"
|
|
|
|
|
}
|
|
|
|
|
return &model.ScannerStatus{
|
|
|
|
|
Scanning: true,
|
|
|
|
|
ScanType: scanType,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
userCtx := request.WithUser(ctx, model.User{
|
2026-03-09 14:19:53 -04:00
|
|
|
ID: "admin-id",
|
|
|
|
|
IsAdmin: true,
|
|
|
|
|
})
|
|
|
|
|
|
2026-11-17 20:58:17 -05:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/rest/startScan", nil)
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
r = r.WithContext(userCtx)
|
2026-03-09 14:19:53 -04:00
|
|
|
|
|
|
|
|
response, err := api.StartScan(r)
|
|
|
|
|
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(response).ToNot(BeNil())
|
|
|
|
|
Expect(response.ScanStatus).ToNot(BeNil())
|
|
|
|
|
Expect(response.ScanStatus.Scanning).To(BeTrue())
|
|
|
|
|
Expect(response.ScanStatus.ScanType).To(Equal("quick"))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("returns correct scanType in response when fullScan=true", func() {
|
|
|
|
|
// Setup mock to update status when scan starts (simulating the real scanner)
|
|
|
|
|
ms.SetScanStatusFunc(func(fullScan bool, targets []model.ScanTarget) *model.ScannerStatus {
|
|
|
|
|
scanType := "quick"
|
|
|
|
|
if fullScan {
|
|
|
|
|
scanType = "full"
|
|
|
|
|
}
|
|
|
|
|
return &model.ScannerStatus{
|
|
|
|
|
Scanning: true,
|
|
|
|
|
ScanType: scanType,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
userCtx := request.WithUser(ctx, model.User{
|
2026-03-09 14:19:53 -04:00
|
|
|
ID: "admin-id",
|
|
|
|
|
IsAdmin: true,
|
|
|
|
|
})
|
|
|
|
|
|
2026-11-17 20:58:17 -05:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/rest/startScan?fullScan=true", nil)
|
chore(lint): ban context.Background in tests, flag unused nolints (#6474)
* chore(lint): ban context.Background in tests, flag unused nolints
Add a forbidigo rule that rejects context.Background and context.TODO in
_test.go files. The lint message points to the right fix: the spec's ctx
from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and
benchmarks. AGENTS.md already asks for this, but nothing enforced it, so
new tests kept adding Background calls. All 465 existing calls are
replaced, with one ctx per top-level Describe set at the top of its
BeforeEach. The migration forbidigo rules keep their old scope through an
exclusion matched on their message text.
Also enable nolintlint with require-specific and allow-unused: false.
It found 41 //nolint directives that suppressed nothing, or named no
linter, or had a leading space that made them inert. They are removed or
fixed. The sweep also fixed a middleware test that wrote a user with a nil
ctx before ctx was set.
* test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx
The ROLLBACK cleanups in the library, plugin and initial setup tests must
run even if the spec context is already cancelled, so they use
context.WithoutCancel. The same goes for the context handed to the
process-wide scanner singleton in the GetInstance test. Also document that
the forbidigo exclusions are scoped by message text.
* test: pass the caller's ctx to tests.ClearDB
ClearDB was the last context.Background call in shared test code. It now
takes a ctx, and every caller passes its spec ctx. The forbidigo rule for
tests also covers the tests/ helper package now, so new helpers there
cannot add Background calls either.
2026-11-15 17:03:17 -05:00
|
|
|
r = r.WithContext(userCtx)
|
2026-03-09 14:19:53 -04:00
|
|
|
|
|
|
|
|
response, err := api.StartScan(r)
|
|
|
|
|
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(response).ToNot(BeNil())
|
|
|
|
|
Expect(response.ScanStatus).ToNot(BeNil())
|
|
|
|
|
Expect(response.ScanStatus.Scanning).To(BeTrue())
|
|
|
|
|
Expect(response.ScanStatus.ScanType).To(Equal("full"))
|
|
|
|
|
})
|
2025-11-14 22:15:43 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Describe("GetScanStatus", func() {
|
|
|
|
|
It("returns scan status", func() {
|
|
|
|
|
// Setup mock scanner status
|
|
|
|
|
ms.SetStatusResponse(&model.ScannerStatus{
|
|
|
|
|
Scanning: false,
|
|
|
|
|
Count: 100,
|
|
|
|
|
FolderCount: 10,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create request
|
2026-11-17 20:58:17 -05:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/rest/getScanStatus", nil)
|
2025-11-14 22:15:43 -05:00
|
|
|
r = r.WithContext(ctx)
|
|
|
|
|
|
|
|
|
|
// Call endpoint
|
|
|
|
|
response, err := api.GetScanStatus(r)
|
|
|
|
|
|
|
|
|
|
// Should succeed
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(response).ToNot(BeNil())
|
|
|
|
|
Expect(response.ScanStatus).ToNot(BeNil())
|
|
|
|
|
Expect(response.ScanStatus.Scanning).To(BeFalse())
|
|
|
|
|
Expect(response.ScanStatus.Count).To(Equal(int64(100)))
|
|
|
|
|
Expect(response.ScanStatus.FolderCount).To(Equal(int64(10)))
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|