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