From bbccc24ed1ea2c3e5f6bea1970649ac8df45daf6 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 12 Jan 2024 15:34:34 +0100 Subject: [PATCH] test federationinfo validation --- models/forgefed/federationinfo_test.go | 24 ++++++++++ .../{validateable.go => validatable.go} | 0 modules/validation/validatable_test.go | 44 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 models/forgefed/federationinfo_test.go rename modules/validation/{validateable.go => validatable.go} (100%) create mode 100644 modules/validation/validatable_test.go diff --git a/models/forgefed/federationinfo_test.go b/models/forgefed/federationinfo_test.go new file mode 100644 index 0000000000..b6185dddeb --- /dev/null +++ b/models/forgefed/federationinfo_test.go @@ -0,0 +1,24 @@ +// Copyright 2024 The forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package forgefed + +import ( + "testing" + + "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/validation" +) + +func Test_ValidateMaxLen(t *testing.T) { + sut := FederationInfo{ + HostFqdn: "host.do.main", + NodeInfo: NodeInfo{ + Source: "forgejo", + }, + LatestActivity: timeutil.TimeStampNow(), + } + if res, err := validation.IsValid(sut); !res { + t.Errorf("sut should be valid but was %q", err) + } +} diff --git a/modules/validation/validateable.go b/modules/validation/validatable.go similarity index 100% rename from modules/validation/validateable.go rename to modules/validation/validatable.go diff --git a/modules/validation/validatable_test.go b/modules/validation/validatable_test.go new file mode 100644 index 0000000000..2a0c46aad5 --- /dev/null +++ b/modules/validation/validatable_test.go @@ -0,0 +1,44 @@ +// Copyright 2024 The forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package validation + +import ( + "testing" + + "code.gitea.io/gitea/modules/timeutil" +) + +func Test_ValidateNotEmpty_ForString(t *testing.T) { + sut := "" + if len(ValidateNotEmpty(sut, "dummyField")) == 0 { + t.Errorf("sut should be invalid") + } + sut = "not empty" + if res := ValidateNotEmpty(sut, "dummyField"); len(res) > 0 { + t.Errorf("sut should be valid but was %q", res) + } +} + +func Test_ValidateNotEmpty_ForTimestamp(t *testing.T) { + sut := timeutil.TimeStamp(0) + if res := ValidateNotEmpty(sut, "dummyField"); len(res) == 0 { + t.Errorf("sut should be invalid") + } + sut = timeutil.TimeStampNow() + if res := ValidateNotEmpty(sut, "dummyField"); len(res) > 0 { + t.Errorf("sut should be valid but was %q", res) + } + +} + +func Test_ValidateMaxLen(t *testing.T) { + sut := "0123456789" + if len(ValidateMaxLen(sut, 9, "dummyField")) == 0 { + t.Errorf("sut should be invalid") + } + sut = "0123456789" + if res := ValidateMaxLen(sut, 11, "dummyField"); len(res) > 0 { + t.Errorf("sut should be valid but was %q", res) + } +}