| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "net/http/httptest" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/registry" |
| 10 | ) |
| 11 | |
| 12 | func TestValidActivityMessage(t *testing.T) { |
| 13 | for _, message := range []string{"", "ship the new checkout", strings.Repeat("界", activityMessageMaxRunes)} { |
| 14 | if !validActivityMessage(message) { |
| 15 | t.Errorf("validActivityMessage(%q) = false", message) |
| 16 | } |
| 17 | } |
| 18 | for _, message := range []string{ |
| 19 | strings.Repeat("x", activityMessageMaxRunes+1), |
| 20 | "two\nlines", "tab\tseparated", "nul\x00byte", string([]byte{0xff}), |
| 21 | } { |
| 22 | if validActivityMessage(message) { |
| 23 | t.Errorf("validActivityMessage(%q) = true", message) |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func TestActivityDoesNotLinkPlatformAliasAsInstance(t *testing.T) { |
| 29 | fx := newConsoleFixture(t) |
| 30 | c := signInPaul(t, fx) |
| 31 | host := "platform-alias.example.com" |
| 32 | fx.addRoute(t, registry.Route{Host: host, App: "kass", Account: fx.sqid, Kind: "static", Platform: true}) |
| 33 | if err := fx.st.RecordActivity(OpsAccountID, "kass", "paul@keymail.dev", Activity{ |
| 34 | Action: "instance-create", TargetType: "instance", Target: host, Detail: "created instance " + host, |
| 35 | }, time.Now()); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | |
| 39 | status, _, body := get(t, c, fx.u("/apps/kass/activity")) |
| 40 | if status != 200 { |
| 41 | t.Fatalf("status = %d", status) |
| 42 | } |
| 43 | if !strings.Contains(body, host) { |
| 44 | t.Fatalf("activity target missing: %s", body) |
| 45 | } |
| 46 | if strings.Contains(body, `/instances/`+host) { |
| 47 | t.Fatalf("platform alias target linked as an instance: %s", body) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestCheckedActivityMessageRejectsTrailingControl(t *testing.T) { |
| 52 | for _, message := range []string{"description\n", "\tdescription", "description\r"} { |
| 53 | w := httptest.NewRecorder() |
| 54 | if _, ok := checkedActivityMessage(w, message); ok || w.Code != 400 { |
| 55 | t.Errorf("accepted %q: status %d", message, w.Code) |
| 56 | } |
| 57 | } |
| 58 | if got, ok := checkedActivityMessage(httptest.NewRecorder(), " description "); !ok || got != "description" { |
| 59 | t.Fatalf("space trimming = %q, %v", got, ok) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestActivityRendersDescriptionAndCompleteAction(t *testing.T) { |
| 64 | fx := newConsoleFixture(t) |
| 65 | c := signInPaul(t, fx) |
| 66 | for _, event := range []Activity{ |
| 67 | {Action: "promote", TargetType: "channel", Target: "edge", Message: "Fix <script>alert(1)</script>", Detail: "promoted v3 to edge (cli)"}, |
| 68 | {Action: "config", Message: "Update delivery configuration", Detail: "config: 2 set, 0 unset (relay) (cli)"}, |
| 69 | {Action: "ship", TargetType: "release", Target: "v3", Message: "Publish checkout fix", Detail: "shipped v3 (cli)"}, |
| 70 | } { |
| 71 | if err := fx.st.RecordActivity(OpsAccountID, "kass", "paul@keymail.dev", event, time.Now()); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | } |
| 75 | status, _, body := get(t, c, fx.u("/apps/kass/activity")) |
| 76 | if status != 200 { |
| 77 | t.Fatalf("status = %d", status) |
| 78 | } |
| 79 | for _, want := range []string{"Fix <script>alert(1)</script>", "promoted v3 to edge (cli)", "Update delivery configuration", "config: 2 set, 0 unset (relay) (cli)", `/apps/kass/releases#version-v3`, "shipped v3 (cli)"} { |
| 80 | if !strings.Contains(body, want) { |
| 81 | t.Errorf("missing %q", want) |
| 82 | } |
| 83 | } |
| 84 | if strings.Contains(body, "<script>alert(1)</script>") || strings.Contains(body, "CLI promotions don") { |
| 85 | t.Fatal("activity contains unescaped message or obsolete CLI exclusion") |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestActivityAppRestartBodyCompatibility(t *testing.T) { |
| 90 | for _, body := range []string{"", "{}", `{"message":"Reload delivery configuration"}`} { |
| 91 | t.Run(body, func(t *testing.T) { |
| 92 | fx, token := restartFixture(t, "activity-restart-body") |
| 93 | status, out := postBody(t, fx.ts.URL+"/api/cli/apps/bab/titogo/restart", token, body) |
| 94 | if status != 200 { |
| 95 | t.Fatalf("restart = %d %s", status, out) |
| 96 | } |
| 97 | events, err := fx.st.Events(OpsAccountID, "titogo", 10) |
| 98 | if err != nil || len(events) != 1 { |
| 99 | t.Fatalf("events = %+v, %v", events, err) |
| 100 | } |
| 101 | want := "" |
| 102 | if strings.Contains(body, "message") { |
| 103 | want = "Reload delivery configuration" |
| 104 | } |
| 105 | e := events[0] |
| 106 | if e.Action != "restart" || e.TargetType != "app" || e.Target != "titogo" || e.Message != want || e.Detail == "" { |
| 107 | t.Fatalf("event = %+v", e) |
| 108 | } |
| 109 | }) |
| 110 | } |
| 111 | } |
| 112 | |