From da6fc606524e94e87684298d54a450091db864b2 Mon Sep 17 00:00:00 2001 From: Rob Archibald Date: Sat, 8 Oct 2016 15:07:05 -0700 Subject: [PATCH] test fixes --- authStore_test.go | 38 ++++++++++++++++++++++++++++---------- mailer_test.go | 3 ++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/authStore_test.go b/authStore_test.go index 867203e..f644cd2 100644 --- a/authStore_test.go +++ b/authStore_test.go @@ -17,11 +17,11 @@ import ( var futureTime = time.Now().Add(5 * time.Minute) var pastTime = time.Now().Add(-5 * time.Minute) -func getAuthStore(createSessionReturn *SessionReturn, emailCookieToReturn *emailCookie, hasCookieGetError, hasCookiePutError bool, backend *MockBackend) *authStore { +func getAuthStore(createSessionReturn *SessionReturn, emailCookieToReturn *emailCookie, hasCookieGetError, hasCookiePutError bool, mailErr error, backend *MockBackend) *authStore { r := &http.Request{} cookieStore := NewMockCookieStore(map[string]interface{}{emailCookieName: emailCookieToReturn}, hasCookieGetError, hasCookiePutError) sessionStore := MockSessionStore{CreateSessionReturn: createSessionReturn} - return &authStore{backend, &sessionStore, &TextMailer{}, cookieStore, r} + return &authStore{backend, &sessionStore, &TextMailer{Err: mailErr}, cookieStore, r} } func TestNewAuthStore(t *testing.T) { @@ -149,7 +149,7 @@ var loginTests = []struct { func TestAuthLogin(t *testing.T) { for i, test := range loginTests { backend := &MockBackend{GetUserLoginReturn: test.GetUserLoginReturn, ErrReturn: test.ErrReturn} - store := getAuthStore(test.CreateSessionReturn, nil, true, false, backend) // cookie get error so we don't try to invalidate old session or rememberme + store := getAuthStore(test.CreateSessionReturn, nil, true, false, nil, backend) // cookie get error so we don't try to invalidate old session or rememberme val, err := store.login(test.Email, test.Password, test.RememberMe) methods := store.backend.(*MockBackend).MethodsCalled if (err == nil && test.ExpectedErr != "" || err != nil && test.ExpectedErr != err.Error()) || @@ -188,7 +188,7 @@ var registerTests = []struct { func TestAuthRegister(t *testing.T) { for i, test := range registerTests { backend := &MockBackend{AddUserReturn: test.AddUserReturn} - store := getAuthStore(nil, nil, false, false, backend) + store := getAuthStore(nil, nil, false, false, nil, backend) err := store.register(test.Email) methods := store.backend.(*MockBackend).MethodsCalled if (err == nil && test.ExpectedErr != "" || err != nil && test.ExpectedErr != err.Error()) || @@ -246,7 +246,7 @@ var createProfileTests = []struct { func TestAuthCreateProfile(t *testing.T) { for i, test := range createProfileTests { backend := &MockBackend{CreateLoginReturn: test.CreateLoginReturn} - store := getAuthStore(test.CreateSessionReturn, test.EmailCookie, test.HasCookieGetError, test.HasCookiePutError, backend) + store := getAuthStore(test.CreateSessionReturn, test.EmailCookie, test.HasCookieGetError, test.HasCookiePutError, nil, backend) err := store.createProfile("name", "organization", "password", "path") methods := store.backend.(*MockBackend).MethodsCalled if (err == nil && test.ExpectedErr != "" || err != nil && test.ExpectedErr != err.Error()) || @@ -259,7 +259,9 @@ func TestAuthCreateProfile(t *testing.T) { var verifyEmailTests = []struct { Scenario string EmailVerificationCode string + HasCookiePutError bool VerifyEmailReturn *VerifyEmailReturn + MailErr error MethodsCalled []string ExpectedErr string }{ @@ -276,6 +278,22 @@ var verifyEmailTests = []struct { MethodsCalled: []string{"VerifyEmail"}, ExpectedErr: "Failed to verify email", }, + { + Scenario: "Cookie Save Error", + EmailVerificationCode: "nfwRDzfxxJj2_HY-_mLz6jWyWU7bF0zUlIUUVkQgbZ0", + VerifyEmailReturn: verifyEmailSuccess(), + HasCookiePutError: true, + MethodsCalled: []string{"VerifyEmail"}, + ExpectedErr: "Failed to save email cookie", + }, + { + Scenario: "Mail Error", + EmailVerificationCode: "nfwRDzfxxJj2_HY-_mLz6jWyWU7bF0zUlIUUVkQgbZ0", + VerifyEmailReturn: verifyEmailSuccess(), + MethodsCalled: []string{"VerifyEmail"}, + MailErr: errors.New("test"), + ExpectedErr: "Failed to send welcome email", + }, { Scenario: "Email sent", EmailVerificationCode: "nfwRDzfxxJj2_HY-_mLz6jWyWU7bF0zUlIUUVkQgbZ0", @@ -287,7 +305,7 @@ var verifyEmailTests = []struct { func TestAuthVerifyEmail(t *testing.T) { for i, test := range verifyEmailTests { backend := &MockBackend{VerifyEmailReturn: test.VerifyEmailReturn} - store := getAuthStore(nil, nil, false, false, backend) + store := getAuthStore(nil, nil, false, test.HasCookiePutError, test.MailErr, backend) err := store.verifyEmail(test.EmailVerificationCode) methods := store.backend.(*MockBackend).MethodsCalled if (err == nil && test.ExpectedErr != "" || err != nil && test.ExpectedErr != err.Error()) || @@ -312,7 +330,7 @@ func TestRegisterPub(t *testing.T) { buf.WriteString(`{"Email":"bogus"}`) r := &http.Request{Body: ioutil.NopCloser(&buf)} backend := &MockBackend{} - store := getAuthStore(nil, nil, true, false, backend) + store := getAuthStore(nil, nil, true, false, nil, backend) store.r = r err := store.Register() if err == nil || err.Error() != "Invalid email" { @@ -341,7 +359,7 @@ func TestVerifyEmailPub(t *testing.T) { buf.WriteString(`{"EmailVerificationCode":"nfwRDzfxxJj2_HY-_mLz6jWyWU7bF0zUlIUUVkQgbZ0"}`) // random valid base64 encoded data r := &http.Request{Body: ioutil.NopCloser(&buf)} backend := &MockBackend{VerifyEmailReturn: verifyEmailErr()} - store := getAuthStore(nil, nil, true, false, backend) + store := getAuthStore(nil, nil, true, false, nil, backend) store.r = r err := store.VerifyEmail() if err == nil || err.Error() != "Failed to verify email" { @@ -370,7 +388,7 @@ func TestLoginJson(t *testing.T) { buf.WriteString(`{"Email":"email", "Password":"password", "RememberMe":true}`) r := &http.Request{Body: ioutil.NopCloser(&buf)} backend := &MockBackend{} - store := getAuthStore(nil, nil, true, false, backend) + store := getAuthStore(nil, nil, true, false, nil, backend) store.r = r err := store.Login() if err == nil || err.Error() != "Please enter a valid email address." { @@ -419,7 +437,7 @@ func TestCreateProfilePub(t *testing.T) { r, _ := http.NewRequest("PUT", "url", &buf) r.Header.Add("Content-Type", w.FormDataContentType()) backend := &MockBackend{} - store := getAuthStore(nil, nil, true, false, backend) + store := getAuthStore(nil, nil, true, false, nil, backend) store.r = r err := store.CreateProfile() if err == nil || err.Error() != "Unable to get email verification cookie" { diff --git a/mailer_test.go b/mailer_test.go index 6ed6eb1..0bbaeae 100644 --- a/mailer_test.go +++ b/mailer_test.go @@ -86,6 +86,7 @@ func (s *NilSender) Send(to, subject, body string) error { } type TextMailer struct { + Err error Mailer MessageTo string MessageData interface{} @@ -118,5 +119,5 @@ func (t *TextMailer) SendPasswordChanged(to string, data interface{}) error { func (t *TextMailer) send(to string, data interface{}) error { t.MessageTo = to t.MessageData = data - return nil + return t.Err }