summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--handler_test.go133
2 files changed, 135 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index f3ee5e1..f6aa928 100644
--- a/Makefile
+++ b/Makefile
@@ -100,7 +100,9 @@ pack: gen_config
echo "\n/$(APP).tar.gz is ready." || \
(echo "Run \"make build\" first." && exit 1)
-test: import _info_test _run_test
+test: test_all
+
+test_new: import _info_test _run_test
_info_test:
@echo "Please note: We are testing only changes."
diff --git a/handler_test.go b/handler_test.go
index 92088bf..2ea7494 100644
--- a/handler_test.go
+++ b/handler_test.go
@@ -341,6 +341,9 @@ func BenchmarkPasswordHandler(b *testing.B) {
}
func TestChangePasswordHandler(t *testing.T) {
+
+ // Test with the correct form
+
form := url.Values{}
if err := CreateNewEntry("test@localhost", Md5Hash("Password")); err != nil {
@@ -385,7 +388,135 @@ func TestChangePasswordHandler(t *testing.T) {
t.Fatal("Expected success message.")
}
- t.Log(flash.Success)
+ t.Log("Success message:", flash.Success[0])
+
+ // Test with incorrect form to check validation
+ // Check Password and ConfirmPassword validation
+
+ form = url.Values{}
+
+ if err = CreateNewEntry("test1@localhost", Md5Hash("Password")); err != nil {
+ t.Fatal(err)
+ }
+
+ form.Set("Email", "test1@localhost")
+ form.Set("Password", "Password")
+ form.Set("ConfirmPassword", "Password")
+ form.Set("NewPassword", "password")
+
+ request, err = http.NewRequest("POST", "/changePassword", strings.NewReader(form.Encode()))
+ if err != nil {
+ t.Log("Error creating new http request. (/changePassword)", err)
+ }
+ request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
+ response = httptest.NewRecorder()
+
+ ChangePasswordHandler(response, request)
+
+ if response.Code != 302 {
+ t.Log(response.Code)
+ t.Fatal("Expected 302 as status code.")
+ }
+
+ flash = Flash{}
+ session, err = store.Get(request, "_SID")
+ if err != nil {
+ t.Log(err)
+ }
+ flash.Error = session.Flashes("error")
+ flash.Success = session.Flashes("success")
+
+ if len(flash.Success) > 0 {
+ t.Fatal("Got success message, expected failure.")
+ }
+
+ for _, v := range flash.Error {
+ t.Log(v)
+ }
+
+ // Test with incorrect form to check validation
+ // Check password validation
+
+ form = url.Values{}
+
+ if err = CreateNewEntry("test2@localhost", Md5Hash("Password")); err != nil {
+ t.Fatal(err)
+ }
+
+ form.Set("Email", "test2@localhost")
+ form.Set("Password", "password")
+ form.Set("ConfirmPassword", "Password")
+ form.Set("NewPassword", "Password")
+
+ request, err = http.NewRequest("POST", "/changePassword", strings.NewReader(form.Encode()))
+ if err != nil {
+ t.Log("Error creating new http request. (/changePassword)", err)
+ }
+ request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
+ response = httptest.NewRecorder()
+
+ ChangePasswordHandler(response, request)
+
+ if response.Code != 302 {
+ t.Log(response.Code)
+ t.Fatal("Expected 302 as status code.")
+ }
+
+ flash = Flash{}
+ session, err = store.Get(request, "_SID")
+ if err != nil {
+ t.Log(err)
+ }
+ flash.Error = session.Flashes("error")
+ flash.Success = session.Flashes("success")
+
+ if len(flash.Success) > 0 {
+ t.Fatal("Got success message, expected failure.")
+ }
+
+ for _, v := range flash.Error {
+ t.Log(v)
+ }
+
+ // Test with incorrect form to check validation
+ // Check non-existent e-mail
+
+ form = url.Values{}
+
+ form.Set("Email", "nonexistent@localhost")
+ form.Set("Password", "password")
+ form.Set("ConfirmPassword", "Password")
+ form.Set("NewPassword", "Password")
+
+ request, err = http.NewRequest("POST", "/changePassword", strings.NewReader(form.Encode()))
+ if err != nil {
+ t.Log("Error creating new http request. (/changePassword)", err)
+ }
+ request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
+ response = httptest.NewRecorder()
+
+ ChangePasswordHandler(response, request)
+
+ if response.Code != 302 {
+ t.Log(response.Code)
+ t.Fatal("Expected 302 as status code.")
+ }
+
+ flash = Flash{}
+ session, err = store.Get(request, "_SID")
+ if err != nil {
+ t.Log(err)
+ }
+ flash.Error = session.Flashes("error")
+ flash.Success = session.Flashes("success")
+
+ if len(flash.Success) > 0 {
+ t.Fatal("Got success message, expected failure.")
+ }
+
+ for _, v := range flash.Error {
+ t.Log(v)
+ }
}
func TestChangeLocaleHandler(t *testing.T) {