|
| 1 | +// Copyright (c) 2026 Sidero Labs, Inc. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the LICENSE file. |
| 5 | + |
| 6 | +//go:build enterprise |
| 7 | + |
| 8 | +package auth_test |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "net/http" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/julienschmidt/httprouter" |
| 16 | + "github.com/siderolabs/gen/xerrors" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | + |
| 19 | + "github.com/siderolabs/image-factory/enterprise/auth" |
| 20 | + schematicpkg "github.com/siderolabs/image-factory/pkg/schematic" |
| 21 | +) |
| 22 | + |
| 23 | +func TestAuthProvider(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + |
| 26 | + provider, err := auth.NewProvider("testdata/auth.yaml") |
| 27 | + require.NoError(t, err) |
| 28 | + |
| 29 | + handler := func(t *testing.T, expectUser bool, expectUsername string) func(ctx context.Context, w http.ResponseWriter, r *http.Request, p httprouter.Params) error { |
| 30 | + return func(ctx context.Context, w http.ResponseWriter, r *http.Request, p httprouter.Params) error { |
| 31 | + username, ok := auth.GetAuthUsername(ctx) |
| 32 | + |
| 33 | + if expectUser { |
| 34 | + require.True(t, ok) |
| 35 | + require.Equal(t, expectUsername, username) |
| 36 | + } else { |
| 37 | + require.False(t, ok) |
| 38 | + } |
| 39 | + |
| 40 | + return nil |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + for _, test := range []struct { |
| 45 | + name string |
| 46 | + |
| 47 | + username string |
| 48 | + password string |
| 49 | + |
| 50 | + expectAuthError bool |
| 51 | + }{ |
| 52 | + { |
| 53 | + name: "no auth", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "valid user1/pass1", |
| 57 | + |
| 58 | + username: "user1", |
| 59 | + password: "pass1", |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "valid user1/pass1.1", |
| 63 | + |
| 64 | + username: "user1", |
| 65 | + password: "pass1.1", |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "valid user2/pass2", |
| 69 | + |
| 70 | + username: "user2", |
| 71 | + password: "pass2", |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "invalid user", |
| 75 | + |
| 76 | + username: "invalid", |
| 77 | + password: "pass", |
| 78 | + |
| 79 | + expectAuthError: true, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "invalid password for user1", |
| 83 | + |
| 84 | + username: "user1", |
| 85 | + password: "wrongpass", |
| 86 | + |
| 87 | + expectAuthError: true, |
| 88 | + }, |
| 89 | + } { |
| 90 | + t.Run(test.name, func(t *testing.T) { |
| 91 | + t.Parallel() |
| 92 | + |
| 93 | + ctx := t.Context() |
| 94 | + |
| 95 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/", nil) |
| 96 | + require.NoError(t, err) |
| 97 | + |
| 98 | + if test.username != "" && test.password != "" { |
| 99 | + req.SetBasicAuth(test.username, test.password) |
| 100 | + } |
| 101 | + |
| 102 | + middleware := provider.Middleware(handler(t, test.username != "", test.username)) |
| 103 | + |
| 104 | + err = middleware(ctx, nil, req, nil) |
| 105 | + if !test.expectAuthError { |
| 106 | + require.NoError(t, err) |
| 107 | + |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + require.Error(t, err) |
| 112 | + require.True(t, xerrors.TagIs[schematicpkg.RequiresAuthenticationTag](err)) |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
0 commit comments