diff options
| author | horus_arch | 2015-04-01 17:19:04 +0200 |
|---|---|---|
| committer | horus_arch | 2015-04-01 17:19:04 +0200 |
| commit | 6d41bdf2b99b239f691e033e17a17dfc50398006 (patch) | |
| tree | 4f697ffc30308cfe1b23c0caf46f6796ff2bd5cf | |
| parent | 9d3a4de291f7c7bb06ec2a956f23c07a67fcc7f9 (diff) | |
| download | freemail-6d41bdf2b99b239f691e033e17a17dfc50398006.tar.gz | |
Change default target for 'make test' to 'make test_all'. Add exhaustive integration test for 'ChangePasswordHandler'.
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | handler_test.go | 133 |
2 files changed, 135 insertions, 2 deletions
@@ -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) { |
