From 8e54e79e5d856f0a56357110e456545626d2af1c Mon Sep 17 00:00:00 2001 From: Davide Salerno Date: Wed, 28 Jun 2023 21:47:19 +0200 Subject: [PATCH] Added unit test for WithAuth KO case Signed-off-by: Davide Salerno --- modules/dockerregistry/dockerregistry_test.go | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/modules/dockerregistry/dockerregistry_test.go b/modules/dockerregistry/dockerregistry_test.go index b5352ebdd9..ff495cf8f9 100644 --- a/modules/dockerregistry/dockerregistry_test.go +++ b/modules/dockerregistry/dockerregistry_test.go @@ -110,8 +110,6 @@ func TestDockerRegistryWithData(t *testing.T) { t.Cleanup(cancel) } -/**/ - func TestDockerRegistryWithAuth(t *testing.T) { ctx := context.Background() wd, err := os.Getwd() @@ -141,6 +139,35 @@ func TestDockerRegistryWithAuth(t *testing.T) { t.Cleanup(cancel) } +func TestDockerRegistryWithAuthWithUnauthorizedRequest(t *testing.T) { + ctx := context.Background() + wd, err := os.Getwd() + assert.NoError(t, err) + container, err := RunContainer(ctx, WithAuthentication(wd+"/../../testdata/auth")) + + if err != nil { + t.Fatal(err) + } + + // Clean up the container after the test is complete + t.Cleanup(func() { + if err := container.Terminate(ctx); err != nil { + t.Fatalf("failed to terminate container: %s", err) + } + }) + + port, ipAddress := getRegistryPortAndAddress(t, err, container, ctx) + + // Let's simply check that the registry is up and running with a GET to http://localhost:5000/v2/_catalog + h := http.Client{} + req, _ := http.NewRequest("GET", "http://"+ipAddress+":"+port.Port()+"/v2/_catalog", nil) + resp, err := h.Do(req) + require.Equal(t, resp.StatusCode, 401) + defer resp.Body.Close() + _, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) +} + func TestDockerRegistryWithAuthAndData(t *testing.T) { t.Cleanup(func() { os.Setenv("DOCKER_AUTH_CONFIG", originalDockerAuthConfig)