~bigbes/sr-ht-dolt

ref: 587483e8afcc5f82a89c2e4875c383cfc0913f0d sr-ht-dolt/db/repos_test.go -rw-r--r-- 6.8 KiB
587483e8 — Eugene Blikh bd: point the Dolt remote at dolt.srht.bigb.es and enable auto-push 8 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package db

import (
	"context"
	"errors"
	"testing"

	"sourcecraft.dev/bigbes/sr-ht-dolt/core"
)

func TestCreateAndGetRepo(t *testing.T) {
	s, db, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	insertUser(t, db, 1, "alice", core.UserTypeUser)
	repo := mkRepo(t, s, ctx, 1, "alice", "widgets", core.VisibilityPublic)
	if repo.ID == 0 {
		t.Fatal("expected non-zero repo id")
	}

	got, err := s.GetRepoByOwnerAndName(ctx, "alice", "widgets")
	if err != nil {
		t.Fatalf("get by owner/name: %v", err)
	}
	if got.ID != repo.ID || got.OwnerName != "alice" || got.Visibility != core.VisibilityPublic {
		t.Fatalf("unexpected repo: %+v", got)
	}

	byID, err := s.GetRepoByID(ctx, repo.ID)
	if err != nil {
		t.Fatalf("get by id: %v", err)
	}
	if byID.Name != "widgets" || byID.OwnerName != "alice" {
		t.Fatalf("unexpected repo by id: %+v", byID)
	}
}

// TestCreateRepoDuplicateName covers both unique indexes a same-name re-create
// can trip. The "same path" case is the one production actually takes — Path is
// derived from (owner, name), so a real duplicate violates both indexes and
// Postgres reports the lower-OID repository_path_key. The "different path" case
// only reaches uq_repo_owner_id_name; it needs the repos root to have moved
// after the first row was written.
func TestCreateRepoDuplicateName(t *testing.T) {
	for _, tc := range []struct {
		name string
		path string
	}{
		{"same path (both indexes, path reported)", "/var/lib/dolt/~alice/widgets"},
		{"different path (name index only)", "/srv/dolt/~alice/widgets"},
	} {
		t.Run(tc.name, func(t *testing.T) {
			s, db, cleanup := newTestStore(t)
			defer cleanup()
			ctx := context.Background()

			insertUser(t, db, 1, "alice", core.UserTypeUser)
			mkRepo(t, s, ctx, 1, "alice", "widgets", core.VisibilityPublic)

			_, err := s.CreateRepo(ctx, &core.Repo{
				Name: "widgets", OwnerID: 1, OwnerName: "alice",
				Path: tc.path, Visibility: core.VisibilityPrivate,
			})
			if !errors.Is(err, ErrNameTaken) {
				t.Fatalf("expected ErrNameTaken, got %v", err)
			}
		})
	}
}

func TestGetRepoNotFound(t *testing.T) {
	s, _, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	if _, err := s.GetRepoByOwnerAndName(ctx, "nobody", "nope"); !errors.Is(err, ErrNotFound) {
		t.Fatalf("expected ErrNotFound, got %v", err)
	}
	if _, err := s.GetRepoByID(ctx, 999); !errors.Is(err, ErrNotFound) {
		t.Fatalf("expected ErrNotFound, got %v", err)
	}
}

func TestUpdateAndDeleteRepo(t *testing.T) {
	s, db, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	insertUser(t, db, 1, "alice", core.UserTypeUser)
	repo := mkRepo(t, s, ctx, 1, "alice", "widgets", core.VisibilityPublic)

	if err := s.UpdateRepo(ctx, repo.ID, "now with docs", core.VisibilityPrivate); err != nil {
		t.Fatalf("update: %v", err)
	}
	got, err := s.GetRepoByID(ctx, repo.ID)
	if err != nil {
		t.Fatalf("get: %v", err)
	}
	if got.Description != "now with docs" || got.Visibility != core.VisibilityPrivate {
		t.Fatalf("update not reflected: %+v", got)
	}

	if err := s.UpdateRepo(ctx, 999, "x", core.VisibilityPublic); !errors.Is(err, ErrNotFound) {
		t.Fatalf("expected ErrNotFound updating missing repo, got %v", err)
	}

	if err := s.DeleteRepo(ctx, repo.ID); err != nil {
		t.Fatalf("delete: %v", err)
	}
	if _, err := s.GetRepoByID(ctx, repo.ID); !errors.Is(err, ErrNotFound) {
		t.Fatalf("expected ErrNotFound after delete, got %v", err)
	}
	if err := s.DeleteRepo(ctx, repo.ID); !errors.Is(err, ErrNotFound) {
		t.Fatalf("expected ErrNotFound deleting twice, got %v", err)
	}
}

// TestListingVisibility exercises the listing rule: owners and ACL holders see
// everything; everyone else (incl. anonymous) sees only PUBLIC; UNLISTED is
// never listed to non-owners without an ACL.
func TestListingVisibility(t *testing.T) {
	s, db, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	owner := insertUser(t, db, 1, "alice", core.UserTypeUser)
	aclUser := insertUser(t, db, 2, "bob", core.UserTypeUser)
	stranger := insertUser(t, db, 3, "carol", core.UserTypeUser)

	mkRepo(t, s, ctx, owner, "alice", "pub", core.VisibilityPublic)
	mkRepo(t, s, ctx, owner, "alice", "unl", core.VisibilityUnlisted)
	priv := mkRepo(t, s, ctx, owner, "alice", "priv", core.VisibilityPrivate)

	// bob gets RO on the private repo.
	if err := s.UpsertACL(ctx, priv.ID, aclUser, core.AccessRO); err != nil {
		t.Fatalf("grant acl: %v", err)
	}

	names := func(repos []*core.Repo) map[string]bool {
		m := map[string]bool{}
		for _, r := range repos {
			m[r.Name] = true
		}
		return m
	}

	// Owner sees all three.
	ownerCaller := &core.Caller{UserID: owner, Username: "alice", UserType: core.UserTypeUser}
	got, err := s.ListReposByOwner(ctx, "alice", ownerCaller)
	if err != nil {
		t.Fatalf("list owner: %v", err)
	}
	if n := names(got); !n["pub"] || !n["unl"] || !n["priv"] || len(got) != 3 {
		t.Fatalf("owner should see all three, got %v", n)
	}

	// Anonymous sees only PUBLIC.
	got, err = s.ListReposByOwner(ctx, "alice", nil)
	if err != nil {
		t.Fatalf("list anon: %v", err)
	}
	if n := names(got); !n["pub"] || n["unl"] || n["priv"] || len(got) != 1 {
		t.Fatalf("anon should see only pub, got %v", n)
	}

	// Stranger (no ACL) sees only PUBLIC.
	strangerCaller := &core.Caller{UserID: stranger, Username: "carol", UserType: core.UserTypeUser}
	got, err = s.ListReposByOwner(ctx, "alice", strangerCaller)
	if err != nil {
		t.Fatalf("list stranger: %v", err)
	}
	if n := names(got); !n["pub"] || n["unl"] || n["priv"] || len(got) != 1 {
		t.Fatalf("stranger should see only pub, got %v", n)
	}

	// ACL holder sees PUBLIC plus the private repo they were granted (not UNLISTED).
	aclCaller := &core.Caller{UserID: aclUser, Username: "bob", UserType: core.UserTypeUser}
	got, err = s.ListReposByOwner(ctx, "alice", aclCaller)
	if err != nil {
		t.Fatalf("list acl user: %v", err)
	}
	if n := names(got); !n["pub"] || !n["priv"] || n["unl"] || len(got) != 2 {
		t.Fatalf("acl user should see pub+priv, got %v", n)
	}
}

func TestListReposForDashboard(t *testing.T) {
	s, db, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	owner := insertUser(t, db, 1, "alice", core.UserTypeUser)
	other := insertUser(t, db, 2, "bob", core.UserTypeUser)

	own := mkRepo(t, s, ctx, owner, "alice", "mine", core.VisibilityPrivate)
	shared := mkRepo(t, s, ctx, other, "bob", "shared", core.VisibilityPrivate)
	mkRepo(t, s, ctx, other, "bob", "hidden", core.VisibilityPrivate)

	if err := s.UpsertACL(ctx, shared.ID, owner, core.AccessRW); err != nil {
		t.Fatalf("grant: %v", err)
	}

	got, err := s.ListReposForDashboard(ctx, owner)
	if err != nil {
		t.Fatalf("dashboard: %v", err)
	}
	found := map[string]bool{}
	for _, r := range got {
		found[r.Name] = true
	}
	if !found["mine"] || !found["shared"] || found["hidden"] || len(got) != 2 {
		t.Fatalf("dashboard should list own+acl only, got %v", found)
	}
	_ = own
}