|
1 | 1 | package settings |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | 4 | "testing" |
6 | 5 |
|
7 | | - fberrors "github.com/thevickypedia/filebrowser/v2/errors" |
8 | 6 | "github.com/thevickypedia/filebrowser/v2/users" |
9 | 7 | ) |
10 | 8 |
|
11 | | -// stubStore is a minimal users.Store used to exercise CreateUserHome. |
12 | | -type stubStore struct { |
13 | | - byScope map[string]*users.User |
14 | | -} |
15 | | - |
16 | | -func (s *stubStore) Get(_ string, _ bool, _ interface{}) (*users.User, error) { |
17 | | - return nil, fberrors.ErrNotExist |
18 | | -} |
19 | | - |
20 | | -func (s *stubStore) GetByScope(scope string) (*users.User, error) { |
21 | | - if u, ok := s.byScope[scope]; ok { |
22 | | - return u, nil |
23 | | - } |
24 | | - return nil, fberrors.ErrNotExist |
25 | | -} |
26 | | - |
27 | | -func (s *stubStore) Gets(_ string, _ bool) ([]*users.User, error) { return nil, nil } |
28 | | -func (s *stubStore) Update(_ *users.User, _ ...string) error { return nil } |
29 | | -func (s *stubStore) Save(_ *users.User) error { return nil } |
30 | | -func (s *stubStore) Delete(_ interface{}) error { return nil } |
31 | | -func (s *stubStore) LastUpdate(_ uint) int64 { return 0 } |
32 | | - |
33 | 9 | // A user provisioned with CreateUserDir must receive a per-user home directory |
34 | 10 | // derived from its username, not the default scope which normalizes to the |
35 | 11 | // server root. |
36 | 12 | func TestCreateUserHomeDerivesPerUserScope(t *testing.T) { |
37 | 13 | s := &Settings{CreateUserDir: true, UserHomeBasePath: "/users"} |
38 | | - store := &stubStore{byScope: map[string]*users.User{}} |
39 | 14 |
|
40 | 15 | user := &users.User{Username: "alice", Scope: "."} |
41 | | - if err := s.CreateUserHome(user, store, t.TempDir(), false); err != nil { |
| 16 | + derived, err := s.CreateUserHome(user, t.TempDir(), false) |
| 17 | + if err != nil { |
42 | 18 | t.Fatalf("unexpected error: %v", err) |
43 | 19 | } |
| 20 | + if !derived { |
| 21 | + t.Error("expected the scope to be reported as derived") |
| 22 | + } |
44 | 23 | if user.Scope != "/users/alice" { |
45 | 24 | t.Errorf("expected derived scope /users/alice, got %q", user.Scope) |
46 | 25 | } |
47 | 26 | } |
48 | 27 |
|
49 | | -// When the derived scope is already owned by another user, provisioning must be |
50 | | -// rejected so distinct usernames cannot silently share one home directory. |
51 | | -func TestCreateUserHomeRejectsCollision(t *testing.T) { |
52 | | - s := &Settings{CreateUserDir: true, UserHomeBasePath: "/users"} |
53 | | - store := &stubStore{byScope: map[string]*users.User{"/users/alice": {Username: "alice"}}} |
54 | | - |
55 | | - user := &users.User{Username: "alice", Scope: "."} |
56 | | - if err := s.CreateUserHome(user, store, t.TempDir(), false); !errors.Is(err, fberrors.ErrExist) { |
57 | | - t.Fatalf("expected ErrExist on scope collision, got %v", err) |
58 | | - } |
59 | | -} |
60 | | - |
61 | 28 | // A scope explicitly supplied by the caller (e.g. returned by an auth hook) must |
62 | | -// be preserved instead of being replaced by a derived home directory. |
| 29 | +// be preserved instead of being replaced by a derived home directory, and must |
| 30 | +// not be reported as derived: it is legitimate for several users to share it. |
63 | 31 | func TestCreateUserHomePreservesExplicitScope(t *testing.T) { |
64 | 32 | s := &Settings{CreateUserDir: true, UserHomeBasePath: "/users"} |
65 | | - store := &stubStore{byScope: map[string]*users.User{"/users/alice": {Username: "alice"}}} |
66 | 33 |
|
67 | 34 | user := &users.User{Username: "alice", Scope: "/custom"} |
68 | | - if err := s.CreateUserHome(user, store, t.TempDir(), true); err != nil { |
| 35 | + derived, err := s.CreateUserHome(user, t.TempDir(), true) |
| 36 | + if err != nil { |
69 | 37 | t.Fatalf("unexpected error: %v", err) |
70 | 38 | } |
| 39 | + if derived { |
| 40 | + t.Error("an explicit scope must not be reported as derived") |
| 41 | + } |
71 | 42 | if user.Scope != "/custom" { |
72 | 43 | t.Errorf("explicit scope should be preserved, got %q", user.Scope) |
73 | 44 | } |
|
0 commit comments