internal/store/deps_test.go

6521399c1c78577eb00f1bcf8947c0900b9cb62e
gitbay/internal/store/deps_test.go

59 lines · 1753 bytes

 1package store
 2
 3import "testing"
 4
 5// The bot's name was not reserved before migration 0028, so an instance
 6// upgrading from v1.0.x may already have an owner holding it. The migration
 7// has to survive that: a daemon that will not start is worse than a
 8// dependency check that cannot open an issue.
 9func TestBotNameCollisionDoesNotBlockMigration(t *testing.T) {
10	for _, tc := range []struct {
11		name   string
12		occupy func(*Store) error
13	}{
14		{"user", func(s *Store) error {
15			_, err := s.DB.Exec("INSERT INTO users (username, is_admin) VALUES (?, 0)", BotUsername)
16			return err
17		}},
18		{"org", func(s *Store) error {
19			_, err := s.DB.Exec("INSERT INTO orgs (name) VALUES (?)", BotUsername)
20			return err
21		}},
22	} {
23		t.Run(tc.name, func(t *testing.T) {
24			s := open(t)
25			if err := s.MigrateTo(27); err != nil {
26				t.Fatal(err)
27			}
28			if err := tc.occupy(s); err != nil {
29				t.Fatal(err)
30			}
31			if err := s.MigrateTo(28); err != nil {
32				t.Fatalf("migration 0028 failed with %s %q present: %v", tc.name, BotUsername, err)
33			}
34			var n int
35			if err := s.DB.QueryRow("SELECT count(*) FROM users WHERE username = ?", BotUsername).Scan(&n); err != nil {
36				t.Fatal(err)
37			}
38			want := 0
39			if tc.name == "user" {
40				want = 1 // the pre-existing account, not a second one
41			}
42			if n != want {
43				t.Errorf("users named %q = %d, want %d", BotUsername, n, want)
44			}
45		})
46	}
47}
48
49// On a clean instance the account is created, which is what every other
50// dependency test assumes.
51func TestBotAccountCreatedWhenNameIsFree(t *testing.T) {
52	s := open(t)
53	if err := s.MigrateUp(); err != nil {
54		t.Fatal(err)
55	}
56	if _, err := s.UserByUsername(BotUsername); err != nil {
57		t.Fatalf("no %s account after a clean migration: %v", BotUsername, err)
58	}
59}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form