-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #353 from viovanov/vlad/secondary-emails
support for secondary recovery emails emails
- Loading branch information
Showing
4 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package mocks | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/volatiletech/authboss/v3" | ||
) | ||
|
||
type UserWithSecondaryEmails struct { | ||
User | ||
SecondaryEmails []string | ||
} | ||
|
||
// GetSecondaryEmails for the user | ||
func (u *UserWithSecondaryEmails) GetSecondaryEmails() []string { | ||
return u.SecondaryEmails | ||
} | ||
|
||
type ServerStorerWithSecondaryEmails struct { | ||
BasicStorer *ServerStorer | ||
} | ||
|
||
func (s ServerStorerWithSecondaryEmails) Load(ctx context.Context, key string) (authboss.User, error) { | ||
user, err := s.BasicStorer.Load(ctx, key) | ||
if err != nil { | ||
return user, err | ||
} | ||
|
||
mockedUser := user.(*User) | ||
|
||
return &UserWithSecondaryEmails{ | ||
User: *mockedUser, | ||
SecondaryEmails: []string{"[email protected]", "[email protected]"}, | ||
}, nil | ||
} | ||
|
||
func (s ServerStorerWithSecondaryEmails) Save(ctx context.Context, user authboss.User) error { | ||
if u, ok := user.(*UserWithSecondaryEmails); ok { | ||
user = &u.User | ||
} | ||
|
||
return s.BasicStorer.Save(ctx, user) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package recover | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/volatiletech/authboss/v3" | ||
"github.com/volatiletech/authboss/v3/mocks" | ||
) | ||
|
||
func testSetupWithSecondaryEmails() *testHarness { | ||
harness := &testHarness{} | ||
|
||
harness.ab = authboss.New() | ||
harness.bodyReader = &mocks.BodyReader{} | ||
harness.mailer = &mocks.Emailer{} | ||
harness.redirector = &mocks.Redirector{} | ||
harness.renderer = &mocks.Renderer{} | ||
harness.responder = &mocks.Responder{} | ||
harness.session = mocks.NewClientRW() | ||
harness.storer = mocks.NewServerStorer() | ||
|
||
harness.ab.Paths.RecoverOK = "/recover/ok" | ||
harness.ab.Modules.MailNoGoroutine = true | ||
|
||
harness.ab.Config.Core.BodyReader = harness.bodyReader | ||
harness.ab.Config.Core.Logger = mocks.Logger{} | ||
harness.ab.Config.Core.Mailer = harness.mailer | ||
harness.ab.Config.Core.Redirector = harness.redirector | ||
harness.ab.Config.Core.MailRenderer = harness.renderer | ||
harness.ab.Config.Core.Responder = harness.responder | ||
harness.ab.Config.Storage.SessionState = harness.session | ||
harness.ab.Config.Storage.Server = mocks.ServerStorerWithSecondaryEmails{ | ||
BasicStorer: harness.storer, | ||
} | ||
|
||
harness.recover = &Recover{harness.ab} | ||
|
||
return harness | ||
} | ||
|
||
func TestSecondaryEmails(t *testing.T) { | ||
t.Parallel() | ||
|
||
h := testSetupWithSecondaryEmails() | ||
|
||
h.bodyReader.Return = &mocks.Values{ | ||
PID: "[email protected]", | ||
} | ||
h.storer.Users["[email protected]"] = &mocks.User{ | ||
Email: "[email protected]", | ||
Password: "i can't recall, doesn't seem like something bcrypted though", | ||
} | ||
|
||
r := mocks.Request("GET") | ||
w := httptest.NewRecorder() | ||
|
||
if err := h.recover.StartPost(w, r); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if w.Code != http.StatusTemporaryRedirect { | ||
t.Error("code was wrong:", w.Code) | ||
} | ||
if h.redirector.Options.RedirectPath != h.ab.Config.Paths.RecoverOK { | ||
t.Error("page was wrong:", h.responder.Page) | ||
} | ||
if len(h.redirector.Options.Success) == 0 { | ||
t.Error("expected a nice success message") | ||
} | ||
|
||
if h.mailer.Email.To[0] != "[email protected]" { | ||
t.Error("e-mail to address is wrong:", h.mailer.Email.To) | ||
} | ||
if !strings.HasSuffix(h.mailer.Email.Subject, "Password Reset") { | ||
t.Error("e-mail subject line is wrong:", h.mailer.Email.Subject) | ||
} | ||
if len(h.renderer.Data[DataRecoverURL].(string)) == 0 { | ||
t.Errorf("the renderer's url in data was missing: %#v", h.renderer.Data) | ||
} | ||
|
||
if len(h.mailer.Email.To) != 3 { | ||
t.Errorf("should have sent 3 e-mails out, but sent %d", len(h.mailer.Email.To)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters