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) + } +}