/
nekstep
/
gvcli
Обзор
Документация
Войти
/
nekstep
/
gvcli
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
internal/debug/logger_test.go
84 строки
3 KB
Oleg Chirukhin
refactoring: huge UI changes
18 июл 2025, 07:16
18 июл 2025, 07:16
ed0c8cb
Код
Авторство
О чём код?
package debug import ( "bytes" "log" "os" "testing" "github.com/stretchr/testify/assert" ) // TestLogger_Debug verifies that the Debug method prints formatted debug messages only when debug is enabled. // Expected: When enabled, the message should appear in the output. When disabled, nothing should be printed. // Not expected: The message should not appear if debug is disabled. func TestLogger_Debug(t *testing.T) { // We redirect the log output to a buffer to capture the output for assertion. var buf bytes.Buffer log.SetOutput(&buf) logger := NewLogger(true, false, false) logger.Debug("test %d", 123) output := buf.String() // The output should contain the formatted debug message. assert.Contains(t, output, "test 123") // If debug was disabled, output should be empty (not tested here, but that's the negative case). } // TestLogger_DebugHTTP checks that DebugHTTP prints HTTP debug messages only when HTTP debug is enabled. // Expected: The output should include the [DEBUG_HTTP] prefix and the formatted message. // Not expected: If httpEnabled is false, nothing should be printed (not tested here). func TestLogger_DebugHTTP(t *testing.T) { var buf bytes.Buffer log.SetOutput(&buf) logger := NewLogger(false, false, true) logger.DebugHTTP("http %s", "call") output := buf.String() // The output should contain the [DEBUG_HTTP] prefix and the message. assert.Contains(t, output, "[DEBUG_HTTP] http call") } // TestLogger_MaskToken ensures that MaskToken returns a masked version of the token unless passwordsEnabled is true. // Expected: For a long token, the output should show the first 4 and last 4 characters, with asterisks in the middle. // // For a short token, the output should be just asterisks. // If passwordsEnabled is true, the token should be returned as-is. // // Not expected: The full token should never be shown unless passwordsEnabled is true. func TestLogger_MaskToken(t *testing.T) { logger := NewLogger(false, false, false) masked := logger.MaskToken("1234567890abcdef") // Should show first 4, then ****, then last 4 characters assert.Equal(t, "1234****cdef", masked) shortMasked := logger.MaskToken("123") // Short tokens should be fully masked assert.Equal(t, "****", shortMasked) loggerPwd := NewLogger(false, true, false) // If passwordsEnabled, the token is not masked assert.Equal(t, "sensitive", loggerPwd.MaskToken("sensitive")) } // TestLogger_LogTokenBytes checks that LogTokenBytes prints the raw bytes of the token and warns about non-printable characters. // Expected: The output should include a line with the byte values of the token, and for any non-printable or whitespace characters, // // a warning message should be printed with the byte index and value. // // Not expected: If debug is not enabled, nothing should be printed (not tested here). func TestLogger_LogTokenBytes(t *testing.T) { // We capture os.Stdout to check the output of fmt.Printf. r, w, _ := os.Pipe() old := os.Stdout os.Stdout = w logger := NewLogger(true, false, false) logger.LogTokenBytes("abc\n\tdef") // Contains newline and tab (non-printable) w.Close() os.Stdout = old var buf bytes.Buffer _, _ = buf.ReadFrom(r) output := buf.String() // Should print the byte array and at least one warning for non-printable characters assert.Contains(t, output, "[DEBUG] JWT token bytes:") assert.Contains(t, output, "[DEBUG] WARNING: Non-printable or whitespace character") }