Skip to content

Commit

Permalink
feat(GODT-2295): publish event when IMAP login fails. (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
xmichelo authored Feb 2, 2023
1 parent 18e0b89 commit 4fa6b6a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
8 changes: 8 additions & 0 deletions events/login_failed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package events

type LoginFailed struct {
eventBase

SessionID int
Username string
}
5 changes: 5 additions & 0 deletions internal/session/handle_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func (s *Session) handleLogin(ctx context.Context, tag string, cmd *proto.Login,

state, err := s.backend.GetState(ctx, cmd.GetUsername(), cmd.GetPassword(), s.sessionID)
if err != nil {
s.eventCh <- events.LoginFailed{
SessionID: s.sessionID,
Username: cmd.GetUsername(),
}

return err
}

Expand Down
21 changes: 20 additions & 1 deletion tests/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/ProtonMail/gluon/events"
"github.com/ProtonMail/gluon/wait"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -112,7 +113,7 @@ func TestLoginTooManyAttemps(t *testing.T) {
})
}

func TestLoginTooManyAttempsMany(t *testing.T) {
func TestLoginTooManyAttemptsMany(t *testing.T) {
runManyToOneTest(t, defaultServerOptions(t), []int{1, 2, 3}, func(c map[int]*testConnection, s *testSession) {
// 3 attempts.
c[1].C("A001 login user badpass").NO("A001")
Expand All @@ -136,6 +137,24 @@ func TestLoginTooManyAttempsMany(t *testing.T) {
})
}

func TestLoginEvents(t *testing.T) {
runOneToOneTest(t, defaultServerOptions(t), func(c *testConnection, s *testSession) {
require.IsType(t, events.UserAdded{}, <-s.eventCh)
require.IsType(t, events.ListenerAdded{}, <-s.eventCh)
require.IsType(t, events.SessionAdded{}, <-s.eventCh)

c.C("A001 login baduser badpass").NO("A001")
failedEvent, ok := (<-s.eventCh).(events.LoginFailed)
require.True(t, ok)
require.Equal(t, failedEvent.Username, "baduser")

c.C("A002 login user pass").OK("A002")
loginEvent, ok := (<-s.eventCh).(events.Login)
require.True(t, ok)
require.Equal(t, loginEvent.UserID, s.userIDs["user"])
})
}

func timeFunc(fn func()) time.Duration {
start := time.Now()

Expand Down

0 comments on commit 4fa6b6a

Please sign in to comment.