| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/rastrillo/rastrillo/budget" |
| 11 | "amadan.net/rastrillo/rastrillo/dbtest" |
| 12 | ) |
| 13 | |
| 14 | var template = dbtest.FromSet(BootSchema) |
| 15 | |
| 16 | func TestMain(m *testing.M) { |
| 17 | code := budget.Main(m) |
| 18 | template.Remove() |
| 19 | homeTemplate.Remove() |
| 20 | os.Exit(code) |
| 21 | } |
| 22 | |
| 23 | func open(t *testing.T) *sql.DB { return template.Open(t) } |
| 24 | |
| 25 | func TestBootstrapSeedsOwnerOnce(t *testing.T) { |
| 26 | ctx, db := context.Background(), open(t) |
| 27 | if err := Bootstrap(ctx, db, "owner@example.com"); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if err := Bootstrap(ctx, db, "Owner@Example.com "); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | ms, err := Members(ctx, db) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | if len(ms) != 1 || ms[0].Role != "owner" || ms[0].Email != "owner@example.com" { |
| 38 | t.Fatalf("members = %+v", ms) |
| 39 | } |
| 40 | if ms[0].ID == "" || ms[0].ID == ms[0].Email { |
| 41 | t.Errorf("ID %q must be random, not the email", ms[0].ID) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // BASE_OWNER is the operator's word on who owns Base: a new address is |
| 46 | // added as an owner, an existing member is promoted, and nobody is demoted |
| 47 | // or removed. |
| 48 | func TestBootstrapMakesANewOwnerAddressAnOwner(t *testing.T) { |
| 49 | ctx, db := context.Background(), open(t) |
| 50 | if err := Bootstrap(ctx, db, "first@example.com"); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if _, _, err := Invite(ctx, db, "member@example.com"); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if err := Bootstrap(ctx, db, "member@example.com"); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | if err := Bootstrap(ctx, db, "new@example.com"); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | ms, err := Members(ctx, db) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | roles := map[string]string{} |
| 67 | for _, m := range ms { |
| 68 | roles[m.Email] = m.Role |
| 69 | } |
| 70 | want := map[string]string{"first@example.com": "owner", "member@example.com": "owner", "new@example.com": "owner"} |
| 71 | if len(roles) != len(want) { |
| 72 | t.Fatalf("members = %+v", ms) |
| 73 | } |
| 74 | for email, role := range want { |
| 75 | if roles[email] != role { |
| 76 | t.Errorf("%s role = %q, want %q", email, roles[email], role) |
| 77 | } |
| 78 | } |
| 79 | for _, m := range ms { |
| 80 | if m.Email == "new@example.com" { |
| 81 | if n, err := MemberNumber(ctx, db, m.ID); err != nil || n == 0 { |
| 82 | t.Errorf("new owner has no member number: n=%d err=%v", n, err) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestClaimInstallOnlyOnce(t *testing.T) { |
| 89 | ctx, db := context.Background(), open(t) |
| 90 | first, err := ClaimInstall(ctx, db, "scratchpad", "acme-scratchpad.apps.example.com") |
| 91 | if err != nil || !first { |
| 92 | t.Fatalf("first claim = %v, %v", first, err) |
| 93 | } |
| 94 | second, err := ClaimInstall(ctx, db, "scratchpad", "acme-scratchpad.apps.example.com") |
| 95 | if err != nil || second { |
| 96 | t.Fatalf("second claim = %v, %v", second, err) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestClaimInstallRetriesFailed(t *testing.T) { |
| 101 | ctx, db := context.Background(), open(t) |
| 102 | if ok, err := ClaimInstall(ctx, db, "a", "h"); err != nil || !ok { |
| 103 | t.Fatalf("first claim = %v, %v", ok, err) |
| 104 | } |
| 105 | if err := SetInstallState(ctx, db, "a", "failed", "boom"); err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | if ok, err := ClaimInstall(ctx, db, "a", "h"); err != nil || !ok { |
| 109 | t.Fatalf("claim after failure = %v, %v", ok, err) |
| 110 | } |
| 111 | if err := SetInstallState(ctx, db, "a", "installed", ""); err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | if ok, err := ClaimInstall(ctx, db, "a", "h"); err != nil || ok { |
| 115 | t.Fatalf("claim of installed app = %v, %v; want false, nil", ok, err) |
| 116 | } |
| 117 | is, err := Installs(ctx, db) |
| 118 | if err != nil || len(is) != 1 || is[0].State != "installed" { |
| 119 | t.Fatalf("installs = %+v, %v", is, err) |
| 120 | } |
| 121 | if busy, err := DeleteInstall(ctx, db, "a", false); err != nil || busy { |
| 122 | t.Fatalf("delete = %v, %v", busy, err) |
| 123 | } |
| 124 | is, err = Installs(ctx, db) |
| 125 | if err != nil || len(is) != 0 { |
| 126 | t.Fatalf("installs after delete = %+v, %v", is, err) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestInviteAndJoin(t *testing.T) { |
| 131 | ctx, db := context.Background(), open(t) |
| 132 | m, created, err := Invite(ctx, db, " Ana@Example.com ") |
| 133 | if err != nil || !created || m.Email != "ana@example.com" || m.Role != "member" || m.JoinedAt != nil { |
| 134 | t.Fatalf("invite = %+v %v %v", m, created, err) |
| 135 | } |
| 136 | again, created, err := Invite(ctx, db, "ana@example.com") |
| 137 | if err != nil || created || again.ID != m.ID { |
| 138 | t.Fatalf("re-invite = %+v %v %v", again, created, err) |
| 139 | } |
| 140 | if err := MarkJoined(ctx, db, m.ID); err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | got, ok, err := MemberByEmail(ctx, db, "ana@example.com") |
| 144 | if err != nil || !ok || got.JoinedAt == nil { |
| 145 | t.Fatalf("member = %+v %v %v", got, ok, err) |
| 146 | } |
| 147 | if _, ok, err := MemberByEmail(ctx, db, "nobody@example.com"); err != nil || ok { |
| 148 | t.Errorf("stranger lookup = %v, %v; want false, nil", ok, err) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | func TestDeleteInstallRefusesInstalling(t *testing.T) { |
| 153 | ctx, db := context.Background(), open(t) |
| 154 | ClaimInstall(ctx, db, "a", "h") |
| 155 | if busy, err := DeleteInstall(ctx, db, "a", false); err != nil || !busy { |
| 156 | t.Fatalf("delete = %v, %v; want busy", busy, err) |
| 157 | } |
| 158 | if is, _ := Installs(ctx, db); len(is) != 1 { |
| 159 | t.Fatal("row was removed") |
| 160 | } |
| 161 | if busy, err := DeleteInstall(ctx, db, "missing", false); err != nil || busy { |
| 162 | t.Fatalf("delete of missing = %v, %v", busy, err) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | func TestSetInstallStateMissingRow(t *testing.T) { |
| 167 | if err := SetInstallState(context.Background(), open(t), "nope", "installed", ""); !errors.Is(err, ErrNoInstall) { |
| 168 | t.Fatalf("err = %v", err) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | func TestResetInterrupted(t *testing.T) { |
| 173 | ctx, db := context.Background(), open(t) |
| 174 | ClaimInstall(ctx, db, "a", "h") |
| 175 | ClaimInstall(ctx, db, "b", "h") |
| 176 | SetInstallState(ctx, db, "b", "installed", "") |
| 177 | if err := ResetInterrupted(ctx, db); err != nil { |
| 178 | t.Fatal(err) |
| 179 | } |
| 180 | is, _ := Installs(ctx, db) |
| 181 | if is[0].State != "failed" || is[0].Error != "Interrupted. Try again." || is[1].State != "installed" { |
| 182 | t.Fatalf("installs = %+v", is) |
| 183 | } |
| 184 | if ok, _ := ClaimInstall(ctx, db, "a", "h"); !ok { |
| 185 | t.Fatal("interrupted install cannot be reclaimed") |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | func TestRemovedDefaultIsRemembered(t *testing.T) { |
| 190 | ctx, db := context.Background(), open(t) |
| 191 | if removed, err := DefaultRemoved(ctx, db, "welcome"); err != nil || removed { |
| 192 | t.Fatalf("fresh = %v, %v", removed, err) |
| 193 | } |
| 194 | ClaimInstall(ctx, db, "welcome", "h") |
| 195 | // An installing row is busy, and nothing is recorded. |
| 196 | if busy, err := DeleteInstall(ctx, db, "welcome", true); err != nil || !busy { |
| 197 | t.Fatalf("delete while installing = %v, %v", busy, err) |
| 198 | } |
| 199 | if removed, _ := DefaultRemoved(ctx, db, "welcome"); removed { |
| 200 | t.Fatal("removal recorded while installing") |
| 201 | } |
| 202 | SetInstallState(ctx, db, "welcome", "installed", "") |
| 203 | if busy, err := DeleteInstall(ctx, db, "welcome", true); err != nil || busy { |
| 204 | t.Fatalf("delete = %v, %v", busy, err) |
| 205 | } |
| 206 | if removed, err := DefaultRemoved(ctx, db, "welcome"); err != nil || !removed { |
| 207 | t.Fatalf("after delete = %v, %v", removed, err) |
| 208 | } |
| 209 | if err := ClearDefaultRemoved(ctx, db, "welcome"); err != nil { |
| 210 | t.Fatal(err) |
| 211 | } |
| 212 | if removed, _ := DefaultRemoved(ctx, db, "welcome"); removed { |
| 213 | t.Fatal("still removed after clearing") |
| 214 | } |
| 215 | } |
| 216 | |