summaryrefslogtreecommitdiff
path: root/handler_test.go
diff options
context:
space:
mode:
authorHorus32015-05-15 20:19:01 +0200
committerHorus32015-05-15 20:19:01 +0200
commit5d48193b6988f20b4ed94331350262d07754bd4d (patch)
treeee9914f39f095a995967031c14fb7779914df908 /handler_test.go
parent8532a24f6bc2069eff755d7174021a1ae5bc09c9 (diff)
parent53b71a44c901b4630d247604b82a221e19a7d01d (diff)
downloadfreemail-5d48193b6988f20b4ed94331350262d07754bd4d.tar.gz
Merge branch 'master' of git.iamfabulous.de:freemail
Diffstat (limited to 'handler_test.go')
-rw-r--r--handler_test.go133
1 files changed, 132 insertions, 1 deletions
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) {