package banner_test import ( "bytes" "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "sneak.berlin/go/webhooker/internal/banner" ) // TestCredentials_IsFindableByEye pins the properties that make the // block worth having: rules above and below it, the two fields on // their own lines, and blank lines separating it from whatever the // surrounding log wrote. func TestCredentials_IsFindableByEye(t *testing.T) { t.Parallel() var out bytes.Buffer require.NoError(t, banner.Credentials( &out, "HEADLINE", "admin", "s3cret", "NOTE", )) got := out.String() lines := strings.Split(strings.Trim(got, "\n"), "\n") require.GreaterOrEqual(t, len(lines), 3) assert.Equal(t, lines[0], lines[len(lines)-1], "rules must match") assert.Greater( t, len(lines[0]), 40, "the rule must be visible at a glance", ) assert.Equal(t, strings.Repeat("=", len(lines[0])), lines[0]) assert.Contains(t, got, "\n username: admin\n") assert.Contains(t, got, "\n password: s3cret\n") assert.Contains(t, got, "HEADLINE") assert.Contains(t, got, "NOTE") assert.True(t, strings.HasPrefix(got, "\n")) } // failingWriter reports the write error a banner must not swallow: it // is the one copy of a password that will never be shown again. type failingWriter struct{} func (failingWriter) Write([]byte) (int, error) { return 0, assert.AnError } func TestCredentials_ReportsAWriteFailure(t *testing.T) { t.Parallel() err := banner.Credentials( failingWriter{}, "HEADLINE", "admin", "s3cret", "NOTE", ) require.ErrorIs(t, err, assert.AnError) }