From 6d41bdf2b99b239f691e033e17a17dfc50398006 Mon Sep 17 00:00:00 2001 From: horus_arch Date: Wed, 1 Apr 2015 17:19:04 +0200 Subject: Change default target for 'make test' to 'make test_all'. Add exhaustive integration test for 'ChangePasswordHandler'. --- handler_test.go | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 1 deletion(-) (limited to 'handler_test.go') 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) { -- cgit v1.2.3