From a107dcd60089ef90bbdbe0fe75537f2164aaac86 Mon Sep 17 00:00:00 2001 From: Xeronith Date: Wed, 5 Jul 2023 18:31:04 +0330 Subject: [PATCH] feat(app): :safety_vest: add new validators --- app/validators/email_validator.go | 11 +++ app/validators/email_validator_test.go | 84 ++++++++++++++++++++++ app/validators/webfinger_validator.go | 11 +++ app/validators/webfinger_validator_test.go | 56 +++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 app/validators/email_validator.go create mode 100644 app/validators/email_validator_test.go create mode 100644 app/validators/webfinger_validator.go create mode 100644 app/validators/webfinger_validator_test.go diff --git a/app/validators/email_validator.go b/app/validators/email_validator.go new file mode 100644 index 0000000..a6f993a --- /dev/null +++ b/app/validators/email_validator.go @@ -0,0 +1,11 @@ +package validators + +import ( + "regexp" + + . "github.com/reiver/greatape/components/constants" +) + +func EmailIsValid(input string) bool { + return regexp.MustCompile(EMAIL).MatchString(input) +} diff --git a/app/validators/email_validator_test.go b/app/validators/email_validator_test.go new file mode 100644 index 0000000..47d1bef --- /dev/null +++ b/app/validators/email_validator_test.go @@ -0,0 +1,84 @@ +package validators_test + +import ( + "testing" + + "github.com/reiver/greatape/app/validators" +) + +func TestEmailValidator(test *testing.T) { + type arguments struct { + email string + } + + testCases := []struct { + name string + expectation bool + arguments arguments + }{ + { + "Case1", + false, + arguments{ + email: "", + }, + }, + { + "Case2", + false, + arguments{ + email: "user", + }, + }, + { + "Case3", + true, + arguments{ + email: "user@domain.com", + }, + }, + { + "Case4", + true, + arguments{ + email: "user+plus@gmail.com", + }, + }, + { + "Case5", + true, + arguments{ + email: "susan235@gmail.com", + }, + }, + { + "Case6", + true, + arguments{ + email: "new_user@icloud.com", + }, + }, + { + "Case7", + true, + arguments{ + email: "someone@somewhere.co.uk", + }, + }, + { + "Case8", + true, + arguments{ + email: "north.star@space.social", + }, + }, + } + + for _, testCase := range testCases { + test.Run(testCase.name, func(test *testing.T) { + if result := validators.EmailIsValid(testCase.arguments.email); result != testCase.expectation { + test.Errorf("EmailIsValid() = %v, expected %v", result, testCase.expectation) + } + }) + } +} diff --git a/app/validators/webfinger_validator.go b/app/validators/webfinger_validator.go new file mode 100644 index 0000000..252cd63 --- /dev/null +++ b/app/validators/webfinger_validator.go @@ -0,0 +1,11 @@ +package validators + +import ( + "regexp" + + . "github.com/reiver/greatape/components/constants" +) + +func WebfingerIsValid(webfinger string) bool { + return regexp.MustCompile(WEBFINGER).MatchString(webfinger) +} diff --git a/app/validators/webfinger_validator_test.go b/app/validators/webfinger_validator_test.go new file mode 100644 index 0000000..a9d73a4 --- /dev/null +++ b/app/validators/webfinger_validator_test.go @@ -0,0 +1,56 @@ +package validators_test + +import ( + "testing" + + "github.com/reiver/greatape/app/validators" +) + +func TestWebfingerValidator(test *testing.T) { + type arguments struct { + webfinger string + } + + testCases := []struct { + name string + expectation bool + arguments arguments + }{ + { + "Case1", + false, + arguments{ + webfinger: "somebody@somewhere.xyz", + }, + }, + { + "Case2", + true, + arguments{ + webfinger: "acct:somebody@somewhere.xyz", + }, + }, + { + "Case3", + true, + arguments{ + webfinger: "acct:user@sub.domain.com", + }, + }, + { + "Case4", + false, + arguments{ + webfinger: "acct:user", + }, + }, + } + + for _, testCase := range testCases { + test.Run(testCase.name, func(test *testing.T) { + if result := validators.WebfingerIsValid(testCase.arguments.webfinger); result != testCase.expectation { + test.Errorf("WebfingerIsValid() = %v, expected %v", result, testCase.expectation) + } + }) + } +}