Skip to content

Commit

Permalink
Update form functions and add form tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abneed committed May 23, 2022
1 parent f1f944d commit ca864c9
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 10 deletions.
15 changes: 5 additions & 10 deletions internal/forms/forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package forms

import (
"fmt"
"net/http"
"net/url"
"strings"

Expand Down Expand Up @@ -39,18 +38,14 @@ func (f *Form) Required(fields ...string) {
}

// Has checks if form field is in post and not empty
func (f *Form) Has(field string, r *http.Request) bool {
x := r.Form.Get(field)
if x == "" {

return false
}
return true
func (f *Form) Has(field string) bool {
x := f.Get(field)
return x != ""
}

// MinLength checks for strign minimum length
func (f *Form) MinLength(field string, length int, r *http.Request) bool {
x := r.Form.Get(field)
func (f *Form) MinLength(field string, length int) bool {
x := f.Get(field)
if len(x) < length {
f.Errors.Add(field, fmt.Sprintf("This field must be at least %d characters long", length))
return false
Expand Down
127 changes: 127 additions & 0 deletions internal/forms/forms_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package forms

import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)

func TestForm_Valid(t *testing.T) {
r := httptest.NewRequest("POST", "/whatever", nil)
form := New(r.PostForm)

isValid := form.Valid()
if !isValid {
t.Error("got invalid when should have been valid")
}
}

func TestForm_Required(t *testing.T) {
r := httptest.NewRequest("POST", "/whatever", nil)
form := New(r.PostForm)

form.Required("a", "b", "c")
if form.Valid() {
t.Error("form shows valid when required fields missing")
}

postedData := url.Values{}
postedData.Add("a", "a")
postedData.Add("b", "a")
postedData.Add("c", "a")

r, _ = http.NewRequest("POST", "/whatever", nil)

r.PostForm = postedData
form = New(r.PostForm)
form.Required("a", "b", "c")
if !form.Valid() {
t.Error("shows does not have required fields when it does")
}
}

func TestForm_Has(t *testing.T) {
r := httptest.NewRequest("POST", "/whatever", nil)
form := New(r.PostForm)

has := form.Has("whatever")
if has {
t.Error("form shows has field when it does not")
}

postedData := url.Values{}
postedData.Add("a", "a")
form = New(postedData)

has = form.Has("a")
if !has {
t.Error("shows form does not have field when it should")
}
}

func TestForm_MinLength(t *testing.T) {
r := httptest.NewRequest("POST", "/whatever", nil)
form := New(r.PostForm)

form.MinLength("x", 10)
if form.Valid() {
t.Error("form shows min length for non-existent field")
}

isError := form.Errors.Get("x")
if isError == "" {
t.Error("should have an error, but did not get one")
}

postedValues := url.Values{}
postedValues.Add("some_field", "some value")
form = New(postedValues)

form.MinLength("some_field", 100)
if form.Valid() {
t.Error("form shows min length of 100 met when data is shorter")
}

postedValues = url.Values{}
postedValues.Add("another_field", "abc123")
form = New(postedValues)

form.MinLength("another_field", 1)
if !form.Valid() {
t.Error("form shows min length of 1 is not met when it is")
}

isError = form.Errors.Get("another_field")
if isError != "" {
t.Error("should not have an error, but got one")
}
}

func TestForm_IsEmail(t *testing.T) {
postedValues := url.Values{}
form := New(postedValues)

form.IsEmail("not_email_field")
if form.Valid() {
t.Error("form shows valid email for non-existent field")
}

postedValues = url.Values{}
postedValues.Add("invalid_email", "abc")
form = New(postedValues)

form.IsEmail("invalid_email")
if form.Valid() {
t.Error("form shows valid email when it does not")
}

postedValues = url.Values{}
postedValues.Add("valid_email", "[email protected]")
form = New(postedValues)

form.IsEmail("valid_email")
if !form.Valid() {
t.Error("form shows invalid email when it does")
}
}
1 change: 1 addition & 0 deletions internal/forms/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package forms

0 comments on commit ca864c9

Please sign in to comment.