74 lines
2.9 KiB
Go
74 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNewJensTodoMessage(t *testing.T) {
|
|
message := newJensTodoMessage(4711)
|
|
if message.Topic != jensTodoPushTopic {
|
|
t.Fatalf("topic=%q", message.Topic)
|
|
}
|
|
if message.Notification == nil || message.Notification.Title != "Neue Aufgabe von Jens" || message.Notification.Body != "Eine neue Landwelle-IT-Aufgabe wurde eingetragen." {
|
|
t.Fatalf("unexpected notification: %#v", message.Notification)
|
|
}
|
|
if message.Data["type"] != "todo" || message.Data["todo_id"] != "4711" || message.Data["author"] != "Jens" {
|
|
t.Fatalf("unexpected data: %#v", message.Data)
|
|
}
|
|
if message.Android == nil || message.Android.Priority != "high" || message.Android.Notification == nil || message.Android.Notification.ChannelID != jensTodoAndroidChannel {
|
|
t.Fatalf("unexpected android config: %#v", message.Android)
|
|
}
|
|
}
|
|
|
|
func TestValidTodoCreateRequestID(t *testing.T) {
|
|
if !validTodoCreateRequestID("abcdefghijklmnopqrstuvwx_123-ABC") {
|
|
t.Fatal("expected generated-style token to be valid")
|
|
}
|
|
for _, invalid := range []string{"short", "invalid with spaces 123456789", "../../firebase-service-account.json"} {
|
|
if validTodoCreateRequestID(invalid) {
|
|
t.Fatalf("expected %q to be invalid", invalid)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSanitizeFirebaseError(t *testing.T) {
|
|
path := "/etc/landwelle/firebase-service-account.json"
|
|
got := sanitizeFirebaseError(errors.New("read "+path+": private_key=secret\nBearer abc access_token=token123"), path)
|
|
for _, forbidden := range []string{path, "secret", "abc", "token123"} {
|
|
if strings.Contains(got, forbidden) {
|
|
t.Fatalf("sensitive value %q leaked: %q", forbidden, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTodoWasCreatedByJensUsesConfiguredUserID(t *testing.T) {
|
|
t.Setenv("LANDWELLE_TODO_JENS_USER_ID", "42")
|
|
handler := &adminHandler{}
|
|
jensID := int64(42)
|
|
otherID := int64(7)
|
|
|
|
if !handler.todoWasCreatedByJens(context.Background(), todoInsertResult{AuthorUserID: &jensID}, adminSession{}) {
|
|
t.Fatal("expected configured author user ID to identify Jens")
|
|
}
|
|
if !handler.todoWasCreatedByJens(context.Background(), todoInsertResult{CreatedByUserID: &jensID}, adminSession{}) {
|
|
t.Fatal("expected configured creator user ID to identify Jens")
|
|
}
|
|
if handler.todoWasCreatedByJens(context.Background(), todoInsertResult{Author: "Jens", AuthorUserID: &otherID}, adminSession{Username: "Jens"}) {
|
|
t.Fatal("configured user ID must take precedence over mutable names")
|
|
}
|
|
}
|
|
|
|
func TestTodoWasCreatedByJensControlledNameFallback(t *testing.T) {
|
|
t.Setenv("LANDWELLE_TODO_JENS_USER_ID", "")
|
|
handler := &adminHandler{}
|
|
if !handler.todoWasCreatedByJens(context.Background(), todoInsertResult{Author: " Jens "}, adminSession{}) {
|
|
t.Fatal("expected normalized author fallback to identify Jens without a configured ID")
|
|
}
|
|
if handler.todoWasCreatedByJens(context.Background(), todoInsertResult{Author: "Jensen"}, adminSession{}) {
|
|
t.Fatal("must not match partial names")
|
|
}
|
|
}
|