e2e/aboutbackfill_test.go

v1.37.0
gitbay/e2e/aboutbackfill_test.go history · blame · raw

70 lines · 2061 bytes

 1package e2e
 2
 3import (
 4	"path/filepath"
 5	"strings"
 6	"testing"
 7
 8	"gitbay.org/gitbay/internal/store"
 9)
10
11// The about text parked by migration 0058 becomes a file in the owner's
12// .gitbay repository. Running it twice writes nothing the second time.
13func TestMigrateProfileAbout(t *testing.T) {
14	t.Parallel()
15	inst := startInstance(t)
16	aliceKey := inst.newKey(t, "alice")
17	inst.admin(t, "admin", "user", "create", "alice",
18		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
19
20	// Seed the holding table the way the migration would have.
21	dbPath := filepath.Join(inst.root, "gitbay.db")
22	st, err := store.Open(dbPath)
23	if err != nil {
24		t.Fatal(err)
25	}
26	_, err = st.DB.Exec(
27		"INSERT INTO profile_about_backfill (owner_kind, owner_id, about, about_format) "+
28			"VALUES ('user', (SELECT id FROM users WHERE username='alice'), ?, 'org')",
29		"* alice\n\ntext from the database\n")
30	st.Close()
31	if err != nil {
32		t.Fatal(err)
33	}
34
35	inst.admin(t, "admin", "migrate-profile-about")
36
37	out, _, code := inst.ssh(t, aliceKey, "", "profile", "show", "alice", "--json")
38	if code != 0 {
39		t.Fatalf("profile show: %d", code)
40	}
41	if !strings.Contains(out, "text from the database") {
42		t.Errorf("about not moved into the repository: %s", out)
43	}
44	if !strings.Contains(out, `"about_path":"profile/README.org"`) {
45		t.Errorf("about not written at the recorded format: %s", out)
46	}
47
48	// The repository it made is public, so nothing that was world-readable
49	// became hidden.
50	out, _, _ = inst.ssh(t, aliceKey, "", "repo", "show", "alice/.gitbay", "--json")
51	if !strings.Contains(out, `"visibility":"public"`) {
52		t.Errorf("backfilled repository is not public: %s", out)
53	}
54
55	// Idempotent: a second run is a no-op and the table stays empty.
56	inst.admin(t, "admin", "migrate-profile-about")
57	st, err = store.Open(dbPath)
58	if err != nil {
59		t.Fatal(err)
60	}
61	var n int
62	err = st.DB.QueryRow("SELECT count(*) FROM profile_about_backfill").Scan(&n)
63	st.Close()
64	if err != nil {
65		t.Fatal(err)
66	}
67	if n != 0 {
68		t.Errorf("holding table still has %d row(s)", n)
69	}
70}