authelia/internal/logging/logger_test.go
James Elliott 8f05846e21
feat: webauthn (#2707)
This implements Webauthn. Old devices can be used to authenticate via the appid compatibility layer which should be automatic. New devices will be registered via Webauthn, and devices which do not support FIDO2 will no longer be able to be registered. At this time it does not fully support multiple devices (backend does, frontend doesn't allow registration of additional devices). Does not support passwordless.
2022-03-03 22:20:43 +11:00

116 lines
2.8 KiB
Go

package logging
import (
"fmt"
"io"
"log"
"os"
"runtime"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/authelia/authelia/v4/internal/configuration/schema"
)
func TestShouldWriteLogsToFile(t *testing.T) {
dir, err := os.MkdirTemp("/tmp", "logs-dir")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)
path := fmt.Sprintf("%s/authelia.log", dir)
err = InitializeLogger(schema.LogConfiguration{Format: "text", FilePath: path, KeepStdout: false}, false)
require.NoError(t, err)
Logger().Info("This is a test")
f, err := os.OpenFile(path, os.O_RDONLY, 0)
require.NoError(t, err)
b, err := io.ReadAll(f)
require.NoError(t, err)
assert.Contains(t, string(b), "level=info msg=\"This is a test\"\n")
}
func TestShouldWriteLogsToFileAndStdout(t *testing.T) {
dir, err := os.MkdirTemp("/tmp", "logs-dir")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)
path := fmt.Sprintf("%s/authelia.log", dir)
err = InitializeLogger(schema.LogConfiguration{Format: "text", FilePath: path, KeepStdout: true}, false)
require.NoError(t, err)
Logger().Info("This is a test")
f, err := os.OpenFile(path, os.O_RDONLY, 0)
require.NoError(t, err)
b, err := io.ReadAll(f)
require.NoError(t, err)
assert.Contains(t, string(b), "level=info msg=\"This is a test\"\n")
}
func TestShouldFormatLogsAsJSON(t *testing.T) {
dir, err := os.MkdirTemp("/tmp", "logs-dir")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)
path := fmt.Sprintf("%s/authelia.log", dir)
err = InitializeLogger(schema.LogConfiguration{Format: "json", FilePath: path, KeepStdout: false}, false)
require.NoError(t, err)
Logger().Info("This is a test")
f, err := os.OpenFile(path, os.O_RDONLY, 0)
require.NoError(t, err)
b, err := io.ReadAll(f)
require.NoError(t, err)
assert.Contains(t, string(b), "{\"level\":\"info\",\"msg\":\"This is a test\",")
}
func TestShouldRaiseErrorOnInvalidFile(t *testing.T) {
err := InitializeLogger(schema.LogConfiguration{FilePath: "/not/a/valid/path/to.log"}, false)
switch runtime.GOOS {
case "windows":
assert.EqualError(t, err, "open /not/a/valid/path/to.log: The system cannot find the path specified.")
default:
assert.EqualError(t, err, "open /not/a/valid/path/to.log: no such file or directory")
}
}
func TestSetLevels(t *testing.T) {
assert.Equal(t, logrus.InfoLevel, logrus.GetLevel())
setLevelStr("error", false)
assert.Equal(t, logrus.ErrorLevel, logrus.GetLevel())
setLevelStr("warn", false)
assert.Equal(t, logrus.WarnLevel, logrus.GetLevel())
setLevelStr("info", false)
assert.Equal(t, logrus.InfoLevel, logrus.GetLevel())
setLevelStr("debug", false)
assert.Equal(t, logrus.DebugLevel, logrus.GetLevel())
setLevelStr("trace", false)
assert.Equal(t, logrus.TraceLevel, logrus.GetLevel())
}