| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func goodEnv() map[string]string { |
| 11 | return map[string]string{ |
| 12 | "MAPA_DEK": base64.StdEncoding.EncodeToString( |
| 13 | []byte(strings.Repeat("k", 32))), |
| 14 | "MAPA_INSTANCE_KEY": "instance", |
| 15 | "MAPA_OWNERS": " Ana@Example.com ,bo@example.com,", |
| 16 | "MAPA_ALLOW_LOGGED_MAIL": "1", |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func load(env map[string]string) (Config, error) { |
| 21 | return LoadConfig(func(key string) string { return env[key] }) |
| 22 | } |
| 23 | |
| 24 | func TestAGoodEnvironmentLoads(t *testing.T) { |
| 25 | config, err := load(goodEnv()) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if strings.Join(config.Owners, ",") != "ana@example.com,bo@example.com" { |
| 30 | t.Fatalf("owners are %v; they must be normalised", config.Owners) |
| 31 | } |
| 32 | if config.TimeZone.String() != "UTC" || !config.UsedDefaultOrigin() { |
| 33 | t.Fatalf("defaults: zone %v, origin %s", config.TimeZone, |
| 34 | config.Origin) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestEveryRefusal(t *testing.T) { |
| 39 | for name, change := range map[string]map[string]string{ |
| 40 | "no DEK": {"MAPA_DEK": ""}, |
| 41 | "a bad DEK": {"MAPA_DEK": "not base64!"}, |
| 42 | "a short DEK": {"MAPA_DEK": "c2hvcnQ="}, |
| 43 | "no instance key": {"MAPA_INSTANCE_KEY": ""}, |
| 44 | "no owners": {"MAPA_OWNERS": " , "}, |
| 45 | "logged mail off loopback": { |
| 46 | "MAPA_ORIGIN": "https://paulca.mapa.oficina.works"}, |
| 47 | "no mail at all": {"MAPA_ALLOW_LOGGED_MAIL": ""}, |
| 48 | "the suite shape": {"MAPA_SHAPE": "suite"}, |
| 49 | "an unknown shape": {"MAPA_SHAPE": "both"}, |
| 50 | "a bad time zone": {"MAPA_TIME_ZONE": "Mars/Olympus"}, |
| 51 | "the host's zone": {"MAPA_TIME_ZONE": "Local"}, |
| 52 | "a misspelt locale": {"MAPA_LOCALE": "en_IE"}, |
| 53 | "a locale in lower case": {"MAPA_LOCALE": "en-ie"}, |
| 54 | "a locale Mapa does not offer": {"MAPA_LOCALE": "pt-PT"}, |
| 55 | "a directory with a path": { |
| 56 | "HOME_DIRECTORY_ORIGIN": "https://home.example/x"}, |
| 57 | "a plain-http directory": { |
| 58 | "HOME_DIRECTORY_ORIGIN": "http://home.example"}, |
| 59 | } { |
| 60 | env := goodEnv() |
| 61 | for key, value := range change { |
| 62 | env[key] = value |
| 63 | } |
| 64 | if _, err := load(env); err == nil { |
| 65 | t.Errorf("%s: booted", name) |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestTheSuiteShapeSaysWhy(t *testing.T) { |
| 71 | env := goodEnv() |
| 72 | env["MAPA_SHAPE"] = "suite" |
| 73 | if _, err := load(env); !errors.Is(err, ErrSuiteNotBuilt) { |
| 74 | t.Fatalf("got %v", err) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestTheTeamTimeZoneIsRead(t *testing.T) { |
| 79 | env := goodEnv() |
| 80 | env["MAPA_TIME_ZONE"] = "Europe/Dublin" |
| 81 | env["HOME_DIRECTORY_ORIGIN"] = "https://home.oficina.works/" |
| 82 | config, err := load(env) |
| 83 | if err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | if config.TimeZone.String() != "Europe/Dublin" || |
| 87 | config.Directory != "https://home.oficina.works" { |
| 88 | t.Fatalf("zone %v, directory %q", config.TimeZone, |
| 89 | config.Directory) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // MAPA_IMPORTERS names people who may import from Notion, each of whom |
| 94 | // must be an owner (Notion import spec §7, D4). Unset, nobody may. |
| 95 | func TestImportersMustBeOwners(t *testing.T) { |
| 96 | env := goodEnv() |
| 97 | config, err := load(env) |
| 98 | if err != nil || len(config.Importers) != 0 || |
| 99 | config.Importer("ana@example.com") { |
| 100 | t.Fatalf("unset importers: %v, %v", config.Importers, err) |
| 101 | } |
| 102 | env["MAPA_IMPORTERS"] = " ANA@example.com " |
| 103 | config, err = load(env) |
| 104 | if err != nil || !config.Importer("Ana@Example.com") || |
| 105 | config.Importer("bo@example.com") { |
| 106 | t.Fatalf("importers %v, %v", config.Importers, err) |
| 107 | } |
| 108 | env["MAPA_IMPORTERS"] = "ana@example.com,cy@example.com" |
| 109 | if _, err := load(env); err == nil || |
| 110 | !strings.Contains(err.Error(), "cy@example.com") { |
| 111 | t.Fatalf("an importer who is not an owner booted: %v", err) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestTheTeamLocaleIsRead(t *testing.T) { |
| 116 | env := goodEnv() |
| 117 | config, err := load(env) |
| 118 | if err != nil || config.Locale != "en-IE" { |
| 119 | t.Fatalf("unset: %q, %v", config.Locale, err) |
| 120 | } |
| 121 | env["MAPA_LOCALE"] = "en-US" |
| 122 | if config, err = load(env); err != nil || config.Locale != "en-US" { |
| 123 | t.Fatalf("en-US: %q, %v", config.Locale, err) |
| 124 | } |
| 125 | env["MAPA_LOCALE"] = "en_US" |
| 126 | if _, err = load(env); err == nil || |
| 127 | !strings.Contains(err.Error(), `write "en-US"`) { |
| 128 | t.Fatalf("en_US: %v", err) |
| 129 | } |
| 130 | } |
| 131 | |