From 3325a7c8623ecf9fd39416baf61d9fbe2d00b051 Mon Sep 17 00:00:00 2001
From: Michael Eischer <michael.eischer@fau.de>
Date: Thu, 8 Jun 2023 15:17:00 +0200
Subject: [PATCH] location: extract backend specific part of StripPassword

The tests for the rest backend now reside there.
---
 .../backend/location/display_location_test.go | 115 ++++--------------
 internal/backend/rest/config_test.go          |  68 +++++++++++
 2 files changed, 92 insertions(+), 91 deletions(-)

diff --git a/internal/backend/location/display_location_test.go b/internal/backend/location/display_location_test.go
index 30d3cc286..4a4055a84 100644
--- a/internal/backend/location/display_location_test.go
+++ b/internal/backend/location/display_location_test.go
@@ -1,96 +1,29 @@
-package location
+package location_test
 
-import "testing"
+import (
+	"testing"
 
-var passwordTests = []struct {
-	input    string
-	expected string
-}{
-	{
-		"local:/srv/repo",
-		"local:/srv/repo",
-	},
-	{
-		"/dir1/dir2",
-		"/dir1/dir2",
-	},
-	{
-		`c:\dir1\foobar\dir2`,
-		`c:\dir1\foobar\dir2`,
-	},
-	{
-		"sftp:user@host:/srv/repo",
-		"sftp:user@host:/srv/repo",
-	},
-	{
-		"s3://eu-central-1/bucketname",
-		"s3://eu-central-1/bucketname",
-	},
-	{
-		"swift:container17:/prefix97",
-		"swift:container17:/prefix97",
-	},
-	{
-		"b2:bucketname:/prefix",
-		"b2:bucketname:/prefix",
-	},
-	{
-		"rest:",
-		"rest:/",
-	},
-	{
-		"rest:localhost/",
-		"rest:localhost/",
-	},
-	{
-		"rest::123/",
-		"rest::123/",
-	},
-	{
-		"rest:http://",
-		"rest:http://",
-	},
-	{
-		"rest:http://hostname.foo:1234/",
-		"rest:http://hostname.foo:1234/",
-	},
-	{
-		"rest:http://user@hostname.foo:1234/",
-		"rest:http://user@hostname.foo:1234/",
-	},
-	{
-		"rest:http://user:@hostname.foo:1234/",
-		"rest:http://user:***@hostname.foo:1234/",
-	},
-	{
-		"rest:http://user:p@hostname.foo:1234/",
-		"rest:http://user:***@hostname.foo:1234/",
-	},
-	{
-		"rest:http://user:pppppaaafhhfuuwiiehhthhghhdkjaoowpprooghjjjdhhwuuhgjsjhhfdjhruuhsjsdhhfhshhsppwufhhsjjsjs@hostname.foo:1234/",
-		"rest:http://user:***@hostname.foo:1234/",
-	},
-	{
-		"rest:http://user:password@hostname",
-		"rest:http://user:***@hostname/",
-	},
-	{
-		"rest:http://user:password@:123",
-		"rest:http://user:***@:123/",
-	},
-	{
-		"rest:http://user:password@",
-		"rest:http://user:***@/",
-	},
-}
+	"github.com/restic/restic/internal/backend/location"
+	"github.com/restic/restic/internal/restic"
+	"github.com/restic/restic/internal/test"
+)
 
 func TestStripPassword(t *testing.T) {
-	for i, test := range passwordTests {
-		t.Run(test.input, func(t *testing.T) {
-			result := StripPassword(test.input)
-			if result != test.expected {
-				t.Errorf("test %d: expected '%s' but got '%s'", i, test.expected, result)
-			}
-		})
-	}
+	registry := location.NewRegistry()
+	registry.Register("test",
+		location.NewHTTPBackendFactory[any, restic.Backend](nil,
+			func(s string) string {
+				return "cleaned"
+			}, nil, nil,
+		),
+	)
+
+	t.Run("valid", func(t *testing.T) {
+		clean := location.StripPassword(registry, "test:secret")
+		test.Equals(t, "cleaned", clean)
+	})
+	t.Run("unknown", func(t *testing.T) {
+		clean := location.StripPassword(registry, "invalid:secret")
+		test.Equals(t, "invalid:secret", clean)
+	})
 }
diff --git a/internal/backend/rest/config_test.go b/internal/backend/rest/config_test.go
index 8cfc78407..23ea9095b 100644
--- a/internal/backend/rest/config_test.go
+++ b/internal/backend/rest/config_test.go
@@ -36,3 +36,71 @@ var configTests = []test.ConfigTestData[Config]{
 func TestParseConfig(t *testing.T) {
 	test.ParseConfigTester(t, ParseConfig, configTests)
 }
+
+var passwordTests = []struct {
+	input    string
+	expected string
+}{
+	{
+		"rest:",
+		"rest:/",
+	},
+	{
+		"rest:localhost/",
+		"rest:localhost/",
+	},
+	{
+		"rest::123/",
+		"rest::123/",
+	},
+	{
+		"rest:http://",
+		"rest:http://",
+	},
+	{
+		"rest:http://hostname.foo:1234/",
+		"rest:http://hostname.foo:1234/",
+	},
+	{
+		"rest:http://user@hostname.foo:1234/",
+		"rest:http://user@hostname.foo:1234/",
+	},
+	{
+		"rest:http://user:@hostname.foo:1234/",
+		"rest:http://user:***@hostname.foo:1234/",
+	},
+	{
+		"rest:http://user:p@hostname.foo:1234/",
+		"rest:http://user:***@hostname.foo:1234/",
+	},
+	{
+		"rest:http://user:pppppaaafhhfuuwiiehhthhghhdkjaoowpprooghjjjdhhwuuhgjsjhhfdjhruuhsjsdhhfhshhsppwufhhsjjsjs@hostname.foo:1234/",
+		"rest:http://user:***@hostname.foo:1234/",
+	},
+	{
+		"rest:http://user:password@hostname",
+		"rest:http://user:***@hostname/",
+	},
+	{
+		"rest:http://user:password@:123",
+		"rest:http://user:***@:123/",
+	},
+	{
+		"rest:http://user:password@",
+		"rest:http://user:***@/",
+	},
+}
+
+func TestStripPassword(t *testing.T) {
+	// Make sure that the factory uses the correct method
+	StripPassword := NewFactory().StripPassword
+
+	for i, test := range passwordTests {
+		t.Run(test.input, func(t *testing.T) {
+			result := StripPassword(test.input)
+			if result != test.expected {
+				t.Errorf("test %d: expected '%s' but got '%s'", i, test.expected, result)
+			}
+		})
+	}
+}