account-pin-block-v2 #256
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"path"
|
"path"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -907,37 +908,79 @@ func TestResetAccountAuthorized(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestIncorrectPinReset(t *testing.T) {
|
func TestIncorrectPinReset(t *testing.T) {
|
||||||
|
sessionId := "session123"
|
||||||
|
ctx, store := InitializeTestStore(t)
|
||||||
fm, err := NewFlagManager(flagsPath)
|
fm, err := NewFlagManager(flagsPath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
flag_incorrect_pin, _ := fm.parser.GetFlag("flag_incorrect_pin")
|
flag_incorrect_pin, _ := fm.parser.GetFlag("flag_incorrect_pin")
|
||||||
|
flag_account_blocked, _ := fm.parser.GetFlag("flag_account_blocked")
|
||||||
|
|
||||||
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
|
|
||||||
// Define test cases
|
// Define test cases
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
input []byte
|
input []byte
|
||||||
|
attempts uint8
|
||||||
expectedResult resource.Result
|
expectedResult resource.Result
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Test incorrect pin reset",
|
name: "Test when incorrect PIN attempts is 2",
|
||||||
input: []byte(""),
|
input: []byte(""),
|
||||||
expectedResult: resource.Result{
|
expectedResult: resource.Result{
|
||||||
FlagReset: []uint32{flag_incorrect_pin},
|
FlagReset: []uint32{flag_incorrect_pin},
|
||||||
|
Content: "1", //Expected remaining PIN attempts
|
||||||
},
|
},
|
||||||
|
attempts: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Test incorrect pin reset when incorrect PIN attempts is 1",
|
||||||
|
input: []byte(""),
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagReset: []uint32{flag_incorrect_pin},
|
||||||
|
Content: "2", //Expected remaining PIN attempts
|
||||||
|
},
|
||||||
|
attempts: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Test incorrect pin reset when incorrect PIN attempts is 1",
|
||||||
|
input: []byte(""),
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagReset: []uint32{flag_incorrect_pin},
|
||||||
|
Content: "2", //Expected remaining PIN attempts
|
||||||
|
},
|
||||||
|
attempts: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Test incorrect pin reset when incorrect PIN attempts is 3(account expected to be blocked)",
|
||||||
|
input: []byte(""),
|
||||||
|
expectedResult: resource.Result{
|
||||||
|
FlagReset: []uint32{flag_incorrect_pin},
|
||||||
|
FlagSet: []uint32{flag_account_blocked},
|
||||||
|
},
|
||||||
|
attempts: 3,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|
||||||
|
if err := store.WriteEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS, []byte(strconv.Itoa(int(tt.attempts)))); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
// Create the Handlers instance with the mock flag manager
|
// Create the Handlers instance with the mock flag manager
|
||||||
h := &Handlers{
|
h := &Handlers{
|
||||||
flagManager: fm.parser,
|
flagManager: fm.parser,
|
||||||
|
userdataStore: store,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call the method
|
// Call the method
|
||||||
res, err := h.ResetIncorrectPin(context.Background(), "reset_incorrect_pin", tt.input)
|
res, err := h.ResetIncorrectPin(ctx, "reset_incorrect_pin", tt.input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
@ -2190,3 +2233,55 @@ func TestGetVoucherDetails(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, expectedResult, res)
|
assert.Equal(t, expectedResult, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCountIncorrectPINAttempts(t *testing.T) {
|
||||||
|
ctx, store := InitializeTestStore(t)
|
||||||
|
sessionId := "session123"
|
||||||
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
|
attempts := uint8(2)
|
||||||
|
|
||||||
|
h := &Handlers{
|
||||||
|
userdataStore: store,
|
||||||
|
}
|
||||||
|
err := store.WriteEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS, []byte(strconv.Itoa(int(attempts))))
|
||||||
|
if err != nil {
|
||||||
|
t.Logf(err.Error())
|
||||||
|
}
|
||||||
|
err = h.countIncorrectPINAttempts(ctx, sessionId)
|
||||||
|
if err != nil {
|
||||||
|
t.Logf(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
attemptsAfterCount, err := store.ReadEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS)
|
||||||
|
if err != nil {
|
||||||
|
t.Logf(err.Error())
|
||||||
|
}
|
||||||
|
pinAttemptsValue, _ := strconv.ParseUint(string(attemptsAfterCount), 0, 64)
|
||||||
|
pinAttemptsCount := uint8(pinAttemptsValue)
|
||||||
|
expectedAttempts := attempts + 1
|
||||||
|
assert.Equal(t, pinAttemptsCount, expectedAttempts)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResetIncorrectPINAttempts(t *testing.T) {
|
||||||
|
ctx, store := InitializeTestStore(t)
|
||||||
|
sessionId := "session123"
|
||||||
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
|
|
||||||
|
err := store.WriteEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS, []byte(string("2")))
|
||||||
|
if err != nil {
|
||||||
|
t.Logf(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
h := &Handlers{
|
||||||
|
userdataStore: store,
|
||||||
|
}
|
||||||
|
h.resetIncorrectPINAttempts(ctx, sessionId)
|
||||||
|
incorrectAttempts, err := store.ReadEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Logf(err.Error())
|
||||||
|
}
|
||||||
|
assert.Equal(t, "0", string(incorrectAttempts))
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user