~bigbes/sr-ht-dolt

ref: 231b7764d90a18c5f1bc6608fff9bd0534bb603d sr-ht-dolt/db/repos_test.go -rw-r--r-- 10.8 KiB
231b7764 — Eugene Blikh web: give bd memories their own view 5 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package db

import (
	"context"
	"errors"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"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)
	}
}

// TestListReposForViewer exercises the instance-wide listing rule across
// several owners: PUBLIC is listed to everyone including an anonymous viewer,
// while UNLISTED and PRIVATE reach only their owner and the users holding an ACL
// entry on them. The expectations are exact ordered sets — "owner/name", newest
// first — so both the membership rule and the ordering are checked.
func TestListReposForViewer(t *testing.T) {
	s, db, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	alice := insertUser(t, db, 1, "alice", core.UserTypeUser)
	bob := insertUser(t, db, 2, "bob", core.UserTypeUser)
	carol := insertUser(t, db, 3, "carol", core.UserTypeUser)
	dave := insertUser(t, db, 4, "dave", core.UserTypeUser)

	// Two owners, each with one repository per visibility. Creation order is
	// also the expected reverse of the listing order.
	mkRepo(t, s, ctx, alice, "alice", "pub", core.VisibilityPublic)
	mkRepo(t, s, ctx, alice, "alice", "unl", core.VisibilityUnlisted)
	alicePriv := mkRepo(t, s, ctx, alice, "alice", "priv", core.VisibilityPrivate)
	bobPub := mkRepo(t, s, ctx, bob, "bob", "pub", core.VisibilityPublic)
	bobUnl := mkRepo(t, s, ctx, bob, "bob", "unl", core.VisibilityUnlisted)
	mkRepo(t, s, ctx, bob, "bob", "priv", core.VisibilityPrivate)

	// dave is the ACL grantee: RO on one owner's PRIVATE repo, RW on the
	// other's UNLISTED one. Neither grant may leak any other repository.
	require.NoError(t, s.UpsertACL(ctx, alicePriv.ID, dave, core.AccessRO))
	require.NoError(t, s.UpsertACL(ctx, bobUnl.ID, dave, core.AccessRW))
	// An ACL on a PUBLIC repo must not duplicate it in the result.
	require.NoError(t, s.UpsertACL(ctx, bobPub.ID, carol, core.AccessRO))

	caller := func(id int, name string) *core.Caller {
		return &core.Caller{UserID: id, Username: name, UserType: core.UserTypeUser}
	}

	for _, tc := range []struct {
		name   string
		viewer *core.Caller
		want   []string
	}{
		{
			name:   "anonymous sees every PUBLIC repo and nothing else",
			viewer: nil,
			want:   []string{"bob/pub", "alice/pub"},
		},
		{
			name:   "stranger sees the same PUBLIC set as anonymous",
			viewer: caller(carol, "carol"),
			want:   []string{"bob/pub", "alice/pub"},
		},
		{
			name:   "ACL grantee additionally sees the repos granted to them",
			viewer: caller(dave, "dave"),
			want:   []string{"bob/unl", "bob/pub", "alice/priv", "alice/pub"},
		},
		{
			name:   "owner sees all of their own plus other owners' PUBLIC",
			viewer: caller(alice, "alice"),
			want:   []string{"bob/pub", "alice/priv", "alice/unl", "alice/pub"},
		},
		{
			name:   "the other owner sees their own plus the first owner's PUBLIC",
			viewer: caller(bob, "bob"),
			want:   []string{"bob/priv", "bob/unl", "bob/pub", "alice/pub"},
		},
		{
			name:   "a caller with no rows of their own still sees PUBLIC",
			viewer: &core.Caller{UserID: 999, Username: "ghost", UserType: core.UserTypeUser},
			want:   []string{"bob/pub", "alice/pub"},
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			got, err := s.ListReposForViewer(ctx, tc.viewer)
			require.NoError(t, err)
			assert.Equal(t, tc.want, qualifiedNames(got))
		})
	}
}

// TestListReposForViewerEmpty checks that an instance with no repositories at
// all is an empty result rather than an error, for both an anonymous and a
// signed-in viewer.
func TestListReposForViewerEmpty(t *testing.T) {
	s, db, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	alice := insertUser(t, db, 1, "alice", core.UserTypeUser)

	got, err := s.ListReposForViewer(ctx, nil)
	require.NoError(t, err)
	assert.Empty(t, got)

	got, err = s.ListReposForViewer(ctx, &core.Caller{UserID: alice, Username: "alice", UserType: core.UserTypeUser})
	require.NoError(t, err)
	assert.Empty(t, got)
}

// qualifiedNames renders a listing as "owner/name" entries, preserving order.
func qualifiedNames(repos []*core.Repo) []string {
	out := make([]string, 0, len(repos))
	for _, r := range repos {
		out = append(out, r.OwnerName+"/"+r.Name)
	}
	return out
}

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
}