Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/utils/validator/validator_test.go

156 lignes
4,3 Kio
Go
Brut Vue normale Historique

package validator
import (
"net/http"
"path"
"testing"
"github.com/NyaaPantsu/nyaa/config"
2017-08-03 03:38:07 +02:00
"github.com/NyaaPantsu/nyaa/models"
msg "github.com/NyaaPantsu/nyaa/utils/messages"
First batch of changes for the refactor (#1078) * First batch of changes for the refactor Added the support of gin in routes and other services/utils Begining implementation of JetHTML * Remove os folder * Move scrapers to own repo * Second batch of changes All .jet.html are the working templates. You can now test this PR, the index Page and upload works. If you want to complete the other html templates, you're welcome * Move captcha to util * Move uploadService to utils * Use govalidator instead of regex * Third batch of changes All the front end should as previously. I also fixed some minor things unrelated to the refactor (mostly style issues on static pages) Now errors can be accessed by importing the "errors" helpers and using the `yield errors(name="xxx")` command in templates. Same for infos. Templates are now more hierarchized with a base template "base.jet.html" which is extended depending on the context in "index_site" or "index_admin" layouts. Those layouts are extended than in every pages. Other helpers are captcha to render a captcha `yield captcha(captchaid="xxx")` And also csrf, with the command `yield csrf_field()` To translate, you don't have anymore to do `call $.T "xxx"`, you just have to do `T("xxx")`. Pages for the website part are in folders in the folder "templates/site". Pages for the admin part are in "templates/admin". Layouts are separated in "templates/layouts". Helpers and menu are in "templates/layouts/helpers" and "templates/layouts/menu". Error pages should be put in "templates/errors" * Added test on templates When adding a new template, you have to tell to template_test.go, the context of the new template (if it doesn't use the common context) * Panel admin works Now the templating part should work. The PR can now be fully tested. I think we should push the templating PR and do the routes/controllers/removal of services in another branch. So we know that this one is functional * Updated dependencies * Fixed test for modelhelper * Fix testing for commentlist * Fix travis :') * Just renamed router and removed network * Applying same SEO fix * Update form_validator.go * Added back regexp package
2017-06-28 13:42:38 +02:00
"github.com/gin-gonic/gin"
)
// run before config/parse.go:init()
var _ = func() (_ struct{}) {
config.Configpaths[1] = path.Join("..", "..", config.Configpaths[1])
config.Configpaths[0] = path.Join("..", "..", config.Configpaths[0])
config.Reload()
2017-08-03 03:38:07 +02:00
config.Get().DBType = models.SqliteType
config.Get().DBParams = ":memory:?cache=shared&mode=memory"
models.ORM, _ = models.GormInit(models.DefaultLogger)
return
}()
type TestForm struct {
2017-07-01 02:59:39 +02:00
DefaultVal int `validate:"default=3,required"`
ConfirmVal string `validate:"eqfield=ConfirmeVal,min=7,max=8,required"`
ConfirmeVal string `validate:"required"`
2017-07-01 19:44:36 +02:00
Optional string
}
func TestValidateForm(t *testing.T) {
t.Parallel()
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
First batch of changes for the refactor (#1078) * First batch of changes for the refactor Added the support of gin in routes and other services/utils Begining implementation of JetHTML * Remove os folder * Move scrapers to own repo * Second batch of changes All .jet.html are the working templates. You can now test this PR, the index Page and upload works. If you want to complete the other html templates, you're welcome * Move captcha to util * Move uploadService to utils * Use govalidator instead of regex * Third batch of changes All the front end should as previously. I also fixed some minor things unrelated to the refactor (mostly style issues on static pages) Now errors can be accessed by importing the "errors" helpers and using the `yield errors(name="xxx")` command in templates. Same for infos. Templates are now more hierarchized with a base template "base.jet.html" which is extended depending on the context in "index_site" or "index_admin" layouts. Those layouts are extended than in every pages. Other helpers are captcha to render a captcha `yield captcha(captchaid="xxx")` And also csrf, with the command `yield csrf_field()` To translate, you don't have anymore to do `call $.T "xxx"`, you just have to do `T("xxx")`. Pages for the website part are in folders in the folder "templates/site". Pages for the admin part are in "templates/admin". Layouts are separated in "templates/layouts". Helpers and menu are in "templates/layouts/helpers" and "templates/layouts/menu". Error pages should be put in "templates/errors" * Added test on templates When adding a new template, you have to tell to template_test.go, the context of the new template (if it doesn't use the common context) * Panel admin works Now the templating part should work. The PR can now be fully tested. I think we should push the templating PR and do the routes/controllers/removal of services in another branch. So we know that this one is functional * Updated dependencies * Fixed test for modelhelper * Fix testing for commentlist * Fix travis :') * Just renamed router and removed network * Applying same SEO fix * Update form_validator.go * Added back regexp package
2017-06-28 13:42:38 +02:00
c := &gin.Context{Request: req}
messages := msg.GetMessages(c)
testform := TestForm{}
ValidateForm(&testform, messages)
if !messages.HasErrors() {
t.Errorf("No errors when parsing empty invalid form: %v", testform)
}
messages.ClearAllErrors()
2017-07-01 19:44:36 +02:00
testform.DefaultVal, testform.ConfirmVal, testform.ConfirmeVal, testform.Optional = 1, "testingl", "testingl", "test"
ValidateForm(&testform, messages)
if messages.HasErrors() {
t.Errorf("Errors when parsing valid form %v\n with errors %v", testform, messages.GetAllErrors())
}
messages.ClearAllErrors()
2017-07-01 19:44:36 +02:00
testform.Optional = ""
ValidateForm(&testform, messages)
if messages.HasErrors() {
t.Errorf("Errors when testing an empty optional field in form %v\n with errors %v", testform, messages.GetAllErrors())
}
messages.ClearAllErrors()
testform.ConfirmVal = "test"
testform.ConfirmeVal = "test"
ValidateForm(&testform, messages)
2017-07-01 02:59:39 +02:00
if len(messages.GetErrors("ConfirmVal")) == 0 {
t.Errorf("No errors on minimal length test when parsing invalid form: %v", testform)
}
messages.ClearAllErrors()
testform.ConfirmVal, testform.ConfirmeVal = "testing", "testind"
ValidateForm(&testform, messages)
2017-07-01 02:59:39 +02:00
if len(messages.GetErrors("ConfirmVal")) == 0 {
t.Errorf("No errors on equal test when parsing invalid form: %v", testform)
}
messages.ClearAllErrors()
testform.ConfirmVal, testform.ConfirmeVal = "", "testing"
ValidateForm(&testform, messages)
2017-07-01 02:59:39 +02:00
if len(messages.GetErrors("ConfirmVal")) == 0 {
t.Errorf("No errors on needed test when parsing invalid form: %v", testform)
}
messages.ClearAllErrors()
testform.ConfirmVal, testform.ConfirmeVal = "azertyuid", "azertyuid"
ValidateForm(&testform, messages)
2017-07-01 02:59:39 +02:00
if len(messages.GetErrors("ConfirmVal")) == 0 {
t.Errorf("No errors on maximal length test when parsing invalid form %v", testform)
}
messages.ClearAllErrors()
testform.DefaultVal = 0
ValidateForm(&testform, messages)
if testform.DefaultVal == 0 {
t.Errorf("Default value are not assigned on int with notnull specified: %v", testform)
}
messages.ClearAllErrors()
testform.DefaultVal = 1
ValidateForm(&testform, messages)
if testform.DefaultVal != 1 {
t.Errorf("Default value are assigned on int with non null value: %v", testform)
}
messages.ClearAllErrors()
}
func TestIsUTFLetterNumeric(t *testing.T) {
t.Parallel()
var tests = []struct {
param string
expected bool
}{
{"\n", false},
{"\r", false},
{"Ⅸ", true},
{"", true},
{" fooo ", false},
{"abc!!!", false},
{"abc1", true},
{"abc〩", true},
{"abc", true},
{"소주", true},
{"ABC", true},
{"FoObAr", true},
{"소aBC", true},
{"소", true},
{"달기&Co.", false},
{"〩Hours", true},
{"\ufff0", false},
{"\u0070", true}, //UTF-8(ASCII): p
{"\u0026", false}, //UTF-8(ASCII): &
{"\u0030", true}, //UTF-8(ASCII): 0
{"123", true},
{"0123", true},
{"-00123", false},
{"0", true},
{"-0", false},
{"123.123", false},
{" ", false},
{".", false},
{"-1¾", false},
{"1¾", true},
{"〥〩", true},
{"모자", true},
{"ix", true},
{"۳۵۶۰", true},
{"1--", false},
{"1-1", false},
{"-", false},
{"--", false},
{"1++", false},
{"1+1", false},
{"+", false},
{"++", false},
{"+1", false},
}
for _, test := range tests {
actual := IsUTFLetterNumeric(test.param)
if actual != test.expected {
t.Errorf("Expected IsUTFLetterNumeric(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
}