Skip to content

Commit

Permalink
kyc/quiz: return Failure for continue on expired session
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-dionysos committed Jan 15, 2024
1 parent 286f963 commit c705cb7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
14 changes: 11 additions & 3 deletions kyc/quiz/quiz.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ func (r *repositoryImpl) validateKycStep(user *users.User) error {

func (r *repositoryImpl) SkipQuizSession(ctx context.Context, userID UserID) error { //nolint:funlen //.
// $1: user_id.
// $2: max session duration (seconds).
const stmt = `
select
started_at,
started_at + make_interval(secs => $2) as deadline,
ended_at is not null as finished,
ended_successfully
from
Expand All @@ -120,9 +122,10 @@ func (r *repositoryImpl) SkipQuizSession(ctx context.Context, userID UserID) err

data, err := storage.ExecOne[struct {
StartedAt *time.Time `db:"started_at"`
Deadline *time.Time `db:"deadline"`
Finished bool `db:"finished"`
Success bool `db:"ended_successfully"`
}](ctx, tx, stmt, userID)
}](ctx, tx, stmt, userID, r.config.MaxSessionDurationSeconds)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return wrapErrorInTx(ErrUnknownSession)
Expand All @@ -135,7 +138,7 @@ func (r *repositoryImpl) SkipQuizSession(ctx context.Context, userID UserID) err
case data.StartedAt == nil:
return wrapErrorInTx(ErrUnknownSession)

case data.StartedAt.Add(stdlibtime.Duration(r.config.MaxSessionDurationSeconds) * stdlibtime.Second).Before(*now.Time):
case data.Deadline.Before(*now.Time):
return wrapErrorInTx(ErrSessionExpired)

case data.Finished:
Expand Down Expand Up @@ -555,7 +558,7 @@ func (r *repositoryImpl) modifyUser(ctx context.Context, success bool, now *time
return errors.Wrapf(r.Users.ModifyUser(ctx, usr, nil), "failed to modify user %#v", usr)
}

func (r *repositoryImpl) ContinueQuizSession( //nolint:funlen,revive //.
func (r *repositoryImpl) ContinueQuizSession( //nolint:funlen,revive,gocognit //.
ctx context.Context,
userID UserID,
question, answer uint8,
Expand All @@ -564,6 +567,11 @@ func (r *repositoryImpl) ContinueQuizSession( //nolint:funlen,revive //.
now := stdlibtime.Now().Truncate(stdlibtime.Second).UTC()
progress, pErr := r.CheckUserRunningSession(ctx, userID, now, tx)
if pErr != nil {
if errors.Is(pErr, ErrSessionExpired) {
quiz = &Quiz{Result: FailureResult}
pErr = r.UserMarkSessionAsFinished(ctx, userID, now, tx, false, false)
}

return wrapErrorInTx(pErr)
}
_, err = r.CheckQuestionNumber(ctx, progress.Lang, progress.Questions, question, tx)
Expand Down
9 changes: 7 additions & 2 deletions kyc/quiz/quiz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,13 @@ func testManagerSessionContinueErrors(ctx context.Context, t *testing.T, r *repo
data, err := r.StartQuizSession(ctx, "bogus", "en")
require.NoError(t, err)
helperForceResetSessionStartedAt(t, r, "bogus")
_, err = r.ContinueQuizSession(ctx, "bogus", data.Progress.NextQuestion.Number, 1)
require.ErrorIs(t, err, ErrSessionExpired)
data, err = r.ContinueQuizSession(ctx, "bogus", data.Progress.NextQuestion.Number, 1)
require.NoError(t, err)
require.Nil(t, data.Progress)
require.Equal(t, FailureResult, data.Result)

_, err = r.StartQuizSession(ctx, "bogus", "en")
require.ErrorIs(t, err, ErrSessionFinishedWithError)
})

t.Run("UnknownQuestionNumber", func(t *testing.T) {
Expand Down

0 comments on commit c705cb7

Please sign in to comment.