internal/control/audit_test.go

v1.43.1
gitbay/internal/control/audit_test.go history · blame · raw

65 lines · 2143 bytes

 1package control
 2
 3import (
 4	"testing"
 5	"time"
 6)
 7
 8func TestParseSince(t *testing.T) {
 9	now := time.Date(2026, 9, 1, 12, 0, 0, 0, time.UTC)
10	cases := map[string]time.Time{
11		"30m":                  now.Add(-30 * time.Minute),
12		"24h":                  now.Add(-24 * time.Hour),
13		"7d":                   now.Add(-7 * 24 * time.Hour),
14		"2026-08-01":           time.Date(2026, 8, 1, 0, 0, 0, 0, time.UTC),
15		"2026-08-01T10:00:00Z": time.Date(2026, 8, 1, 10, 0, 0, 0, time.UTC),
16	}
17	for in, want := range cases {
18		got, ok := parseSince(in, now)
19		if !ok || !got.Equal(want) {
20			t.Errorf("parseSince(%q) = %v, %v; want %v", in, got, ok, want)
21		}
22	}
23	for _, bad := range []string{"", "yesterday", "-1h", "x d", "2026-13-01"} {
24		if _, ok := parseSince(bad, now); ok {
25			t.Errorf("parseSince(%q) accepted", bad)
26		}
27	}
28}
29
30// TestAuditArgsDropFlagValues: prose reaches argv through --body and
31// --message, and the audit log kept it verbatim for a repository that may
32// be private. Identifiers are positional and survive; flag names survive
33// so the entry still says what shape the command had (#122).
34func TestAuditArgsDropFlagValues(t *testing.T) {
35	cases := []struct {
36		in   []string
37		want []string
38	}{
39		{[]string{"krz/gitbay", "--title", "a bug", "--body", "the whole issue text"},
40			[]string{"krz/gitbay", "--title", "--body"}},
41		{[]string{"krz/gitbay", "7", "--add", "bug", "--add", "ui"},
42			[]string{"krz/gitbay", "7", "--add", "--add"}},
43		// A switch takes no value, so the next argument is not swallowed.
44		{[]string{"krz/gitbay", "--private", "--name", "x"},
45			[]string{"krz/gitbay", "--private", "--name"}},
46		// After "--" everything is positional, including a token that
47		// looks like a flag.
48		{[]string{"krz/gitbay", "--", "--not-a-flag"},
49			[]string{"krz/gitbay", "--", "--not-a-flag"}},
50		{nil, []string{}},
51	}
52	for _, tc := range cases {
53		got := auditArgs(tc.in)
54		if len(got) != len(tc.want) {
55			t.Errorf("auditArgs(%v) = %v, want %v", tc.in, got, tc.want)
56			continue
57		}
58		for i := range got {
59			if got[i] != tc.want[i] {
60				t.Errorf("auditArgs(%v) = %v, want %v", tc.in, got, tc.want)
61				break
62			}
63		}
64	}
65}