| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | rdb "amadan.net/rastrillo/rastrillo/db" |
| 11 | "amadan.net/rastrillo/rastrillo/migrate" |
| 12 | |
| 13 | "amadan.net/oficina/home/teammode" |
| 14 | "amadan.net/oficina/home/teammode/teammodetest" |
| 15 | ) |
| 16 | |
| 17 | // The record is verified against bindings built only from configuration; |
| 18 | // change any one input and TeamSettings refuses. Team mode without Home |
| 19 | // refuses too: there is nobody to sign in. |
| 20 | func TestTeamConfigPinsTheProvisioningRecord(t *testing.T) { |
| 21 | f := newHomeFixture(t) |
| 22 | cfg, _ := teamConfigFor(t, teamTestBindings()) |
| 23 | if _, err := TeamSettings(cfg, &f.creds, testOrigin); err != nil { |
| 24 | t.Fatalf("valid team config refused: %v", err) |
| 25 | } |
| 26 | for name, change := range map[string]func(*teammode.Config){ |
| 27 | "team": func(c *teammode.Config) { c.TeamID = "TeamB" }, |
| 28 | "directory": func(c *teammode.Config) { c.DirectoryOrigin = "https://other.example.com" }, |
| 29 | "token": func(c *teammode.Config) { c.RosterToken += "=" }, |
| 30 | "record": func(c *teammode.Config) { c.ProvisioningBase64 += "\n" }, |
| 31 | "operator": func(c *teammode.Config) { c.OperatorKeysBase64 = "e30=" }, |
| 32 | "missing team": func(c *teammode.Config) { c.TeamID = "" }, |
| 33 | } { |
| 34 | bad := *cfg |
| 35 | change(&bad) |
| 36 | if _, err := TeamSettings(&bad, &f.creds, testOrigin); !errors.Is(err, errTeamConfig) { |
| 37 | t.Errorf("%s: changed team config accepted", name) |
| 38 | } |
| 39 | } |
| 40 | if _, err := TeamSettings(cfg, &f.creds, "https://elsewhere.example.com"); !errors.Is(err, errTeamConfig) { |
| 41 | t.Error("a record for another origin was accepted") |
| 42 | } |
| 43 | other := f.creds |
| 44 | other.ConsumerID = "someone-else" |
| 45 | if _, err := TeamSettings(cfg, &other, testOrigin); !errors.Is(err, errTeamConfig) { |
| 46 | t.Error("a record for another Home consumer was accepted") |
| 47 | } |
| 48 | if _, err := TeamSettings(cfg, nil, testOrigin); !errors.Is(err, errTeamConfig) { |
| 49 | t.Error("team mode without Home credentials was accepted") |
| 50 | } |
| 51 | plain, _ := appWithSchema(t, testOrigin, TeamBootSchema, signedInAddress) |
| 52 | plain.teamSettings = teamSettingsFor(t, cfg) |
| 53 | if err := plain.ConfigureTeam(); !errors.Is(err, errTeamConfig) { |
| 54 | t.Errorf("team mode without Home = %v, want refusal", err) |
| 55 | } |
| 56 | // Settings verified for another deployment (here, another origin) |
| 57 | // than the Home credentials the bridge was built with: refused. |
| 58 | elsewhere := teammodetest.Bindings("sheets", "sheets-preview", "https://elsewhere.example.com") |
| 59 | otherCfg, _ := teamConfigFor(t, elsewhere) |
| 60 | otherSettings, err := teammode.Verify(otherCfg, teammodetest.Home(elsewhere), "sheets", elsewhere.Audience) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | mismatched, _ := appWithSchema(t, testOrigin, TeamBootSchema, signedInAddress) |
| 65 | mismatched.teamSettings = otherSettings |
| 66 | homeOn(t, mismatched, "") |
| 67 | if err := mismatched.ConfigureTeam(); !errors.Is(err, errTeamConfig) || mismatched.team != nil { |
| 68 | t.Errorf("settings for another deployment = %v, want refusal", err) |
| 69 | } |
| 70 | // And for another Home consumer at this origin. |
| 71 | consumer := teammodetest.Bindings("sheets", "another-consumer", testOrigin) |
| 72 | consumerCfg, _ := teamConfigFor(t, consumer) |
| 73 | consumerSettings, err := teammode.Verify(consumerCfg, teammodetest.Home(consumer), "sheets", testOrigin) |
| 74 | if err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | wrongConsumer, _ := appWithSchema(t, testOrigin, TeamBootSchema, signedInAddress) |
| 78 | wrongConsumer.teamSettings = consumerSettings |
| 79 | homeOn(t, wrongConsumer, "") |
| 80 | if err := wrongConsumer.ConfigureTeam(); !errors.Is(err, errTeamConfig) { |
| 81 | t.Errorf("settings for another consumer = %v, want refusal", err) |
| 82 | } |
| 83 | // And for another Home issuer (Fable, #53 Required 1): without the |
| 84 | // check the census asks under the settings' issuer, finds nothing |
| 85 | // foreign, and activates. |
| 86 | issuer := teamTestBindings() |
| 87 | issuer.Issuer = "https://id.other.example" |
| 88 | issuerCfg, issuerDir := teamConfigFor(t, issuer) |
| 89 | issuerSettings, err := teammode.Verify(issuerCfg, teammodetest.Home(issuer), "sheets", testOrigin) |
| 90 | if err != nil { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | wrongIssuer, _ := appWithSchema(t, testOrigin, TeamBootSchema, signedInAddress) |
| 94 | wrongIssuer.teamSettings, wrongIssuer.teamTransport = issuerSettings, issuerDir |
| 95 | homeOn(t, wrongIssuer, "") |
| 96 | if err := wrongIssuer.ConfigureTeam(); !errors.Is(err, errTeamConfig) || wrongIssuer.team != nil { |
| 97 | t.Errorf("settings for another issuer = %v, want refusal", err) |
| 98 | } |
| 99 | none, _ := appWithSchema(t, testOrigin, TeamBootSchema, signedInAddress) |
| 100 | if err := none.ConfigureTeam(); err != nil || none.team != nil { |
| 101 | t.Error("no team settings changed something") |
| 102 | } |
| 103 | if s, err := TeamSettings(nil, &f.creds, testOrigin); s != nil || err != nil { |
| 104 | t.Error("no team config made team settings") |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // All five or none; any one set is team mode, and SHEETS_OWNERS stops |
| 109 | // being required. Without any, SHEETS_OWNERS is required exactly as it |
| 110 | // always was. |
| 111 | func TestTeamEnvIsAllOrNoneAndOwnersBecomeOptional(t *testing.T) { |
| 112 | for _, k := range []string{"SHEETS_TEAM_ID", "SHEETS_TEAM_DIRECTORY_ORIGIN", "SHEETS_TEAM_PROVISIONING_BASE64", "SHEETS_TEAM_OPERATOR_KEYS_BASE64", "SHEETS_TEAM_ROSTER_TOKEN"} { |
| 113 | t.Setenv(k, "") |
| 114 | } |
| 115 | t.Setenv("SHEETS_ORIGIN", "http://127.0.0.1:8080") |
| 116 | t.Setenv("SHEETS_INSTANCE_KEY", "k") |
| 117 | t.Setenv("SHEETS_OWNERS", "") |
| 118 | if teammode.ConfigFromEnv("SHEETS") != nil { |
| 119 | t.Fatal("no team variables made a team config") |
| 120 | } |
| 121 | if _, err := LoadAuthEnv(quietLogger()); err == nil || !strings.Contains(err.Error(), "SHEETS_OWNERS") { |
| 122 | t.Fatalf("allowlist mode without SHEETS_OWNERS = %v, want the old refusal", err) |
| 123 | } |
| 124 | t.Setenv("SHEETS_TEAM_ROSTER_TOKEN", "x") |
| 125 | env, err := LoadAuthEnv(quietLogger()) |
| 126 | if err != nil || env.Team == nil || env.Team.RosterToken != "x" || env.Team.TeamID != "" || len(env.Owners) != 0 { |
| 127 | t.Fatalf("one team variable was not a (partial) team config, or team mode still required SHEETS_OWNERS: %v", err) |
| 128 | } |
| 129 | f := newHomeFixture(t) |
| 130 | if _, err := TeamSettings(env.Team, &f.creds, testOrigin); !errors.Is(err, errTeamConfig) { |
| 131 | t.Error("a partial team config was accepted") |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // The marker: once activated, the database refuses to boot without team |
| 136 | // configuration, or with a different binding, before anything migrates. |
| 137 | // The triggers refuse rewriting it; a database never activated boots as |
| 138 | // before. |
| 139 | func TestTeamMarkerLocksTheDatabaseIntoTeamMode(t *testing.T) { |
| 140 | ctx := context.Background() |
| 141 | f := newTeamFixture(t) |
| 142 | d, err := f.app.db.DB() |
| 143 | if err != nil { |
| 144 | t.Fatal(err) |
| 145 | } |
| 146 | got, activated, err := teamState.Required(ctx, d) |
| 147 | if err != nil || !activated || len(got) != 43 { |
| 148 | t.Fatalf("ConfigureTeam committed no marker: %v", err) |
| 149 | } |
| 150 | if _, err := BootSchemaFor(ctx, d, nil); !errors.Is(err, errTeamConfig) { |
| 151 | t.Error("an activated database booted without team configuration") |
| 152 | } |
| 153 | if s, err := BootSchemaFor(ctx, d, f.app.teamSettings); err != nil || s != TeamBootSchema { |
| 154 | t.Error("an activated database refused its team configuration") |
| 155 | } |
| 156 | if _, err := d.Exec("UPDATE sheets_team_required SET bindings=?", strings.Repeat("A", 43)); err == nil { |
| 157 | t.Error("the marker was rewritten") |
| 158 | } |
| 159 | if _, err := d.Exec("DELETE FROM sheets_team_required"); err == nil { |
| 160 | t.Error("the marker was deleted") |
| 161 | } |
| 162 | |
| 163 | // Same database, another team's record: refused at boot, and at |
| 164 | // activation. |
| 165 | other := teamTestBindings() |
| 166 | other.Team = "TeamB" |
| 167 | cfg, _ := teamConfigFor(t, other) |
| 168 | otherSettings := teamSettingsFor(t, cfg) |
| 169 | if _, err := BootSchemaFor(ctx, d, otherSettings); !errors.Is(err, errTeamConfig) { |
| 170 | t.Error("an activated database booted with a different binding") |
| 171 | } |
| 172 | f.app.team, f.app.teamSettings = nil, otherSettings |
| 173 | if err := f.app.ConfigureTeam(); !errors.Is(err, errTeamConfig) { |
| 174 | t.Errorf("a different binding = %v, want refusal", err) |
| 175 | } |
| 176 | |
| 177 | // A database never activated: BootSchema, as before. |
| 178 | fresh, err := rdb.Open(filepath.Join(t.TempDir(), "fresh.db"), quietLogger()) |
| 179 | if err != nil { |
| 180 | t.Fatal(err) |
| 181 | } |
| 182 | t.Cleanup(func() { fresh.Close() }) |
| 183 | if s, err := BootSchemaFor(ctx, fresh.Writer(), nil); err != nil || s != BootSchema { |
| 184 | t.Error("a fresh database did not boot in allowlist mode") |
| 185 | } |
| 186 | // Team schema applied but never activated (a team boot refused after |
| 187 | // migrating): still allowlist-capable, and BootSchema re-applies. |
| 188 | if _, err := migrate.Apply(ctx, fresh, TeamBootSchema); err != nil { |
| 189 | t.Fatal(err) |
| 190 | } |
| 191 | if s, err := BootSchemaFor(ctx, fresh.Writer(), nil); err != nil || s != BootSchema { |
| 192 | t.Fatal("an unactivated team schema locked the database") |
| 193 | } |
| 194 | if _, err := migrate.Apply(ctx, fresh, BootSchema); err != nil { |
| 195 | t.Fatalf("BootSchema over an unactivated team schema: %v", err) |
| 196 | } |
| 197 | } |
| 198 | |