From 57b0fcb55f393dc75d7b0378d4e42529a9485b38 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 3 Jul 2025 17:13:51 +0300 Subject: [PATCH] move authorization tests --- handlers/application/authorization_test.go | 181 +++++++++++++++++++++ handlers/application/menuhandler_test.go | 167 ------------------- 2 files changed, 181 insertions(+), 167 deletions(-) create mode 100644 handlers/application/authorization_test.go diff --git a/handlers/application/authorization_test.go b/handlers/application/authorization_test.go new file mode 100644 index 0000000..8fc0aa5 --- /dev/null +++ b/handlers/application/authorization_test.go @@ -0,0 +1,181 @@ +package application + +import ( + "context" + "log" + "testing" + + "git.defalsify.org/vise.git/resource" + "git.defalsify.org/vise.git/state" + "git.grassecon.net/grassrootseconomics/common/pin" + "git.grassecon.net/grassrootseconomics/sarafu-api/testutil/mocks" + storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db" + "github.com/alecthomas/assert/v2" +) + +func TestAuthorize(t *testing.T) { + sessionId := "session123" + ctx, store := InitializeTestStore(t) + ctx = context.WithValue(ctx, "SessionId", sessionId) + + fm, err := NewFlagManager(flagsPath) + if err != nil { + t.Logf(err.Error()) + } + + // Create required mocks + mockAccountService := new(mocks.MockAccountService) + mockState := state.NewState(16) + flag_incorrect_pin, _ := fm.GetFlag("flag_incorrect_pin") + flag_account_authorized, _ := fm.GetFlag("flag_account_authorized") + flag_allow_update, _ := fm.GetFlag("flag_allow_update") + + // Set 1234 is the correct account pin + accountPIN := "1234" + + h := &MenuHandlers{ + userdataStore: store, + accountService: mockAccountService, + flagManager: fm, + st: mockState, + } + + tests := []struct { + name string + input []byte + expectedResult resource.Result + }{ + { + name: "Test with correct PIN", + input: []byte("1234"), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_incorrect_pin}, + FlagSet: []uint32{flag_allow_update, flag_account_authorized}, + }, + }, + { + name: "Test with incorrect PIN", + input: []byte("1235"), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_account_authorized, flag_allow_update}, + FlagSet: []uint32{flag_incorrect_pin}, + }, + }, + { + name: "Test with PIN that is not a 4 digit", + input: []byte("1235aqds"), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_account_authorized, flag_allow_update}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Hash the PIN + hashedPIN, err := pin.HashPIN(accountPIN) + if err != nil { + logg.ErrorCtxf(ctx, "failed to hash temporaryPin", "error", err) + t.Fatal(err) + } + + err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACCOUNT_PIN, []byte(hashedPIN)) + if err != nil { + t.Fatal(err) + } + + // Call the method under test + res, err := h.Authorize(ctx, "authorize", []byte(tt.input)) + + // Assert that no errors occurred + assert.NoError(t, err) + + //Assert that the account created flag has been set to the result + assert.Equal(t, res, tt.expectedResult, "Expected result should be equal to the actual result") + }) + } +} + +func TestResetAllowUpdate(t *testing.T) { + fm, err := NewFlagManager(flagsPath) + if err != nil { + log.Fatal(err) + } + + flag_allow_update, _ := fm.GetFlag("flag_allow_update") + + // Define test cases + tests := []struct { + name string + input []byte + expectedResult resource.Result + }{ + { + name: "Resets allow update", + input: []byte(""), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_allow_update}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Create the MenuHandlers instance with the mock flag manager + h := &MenuHandlers{ + flagManager: fm, + } + + // Call the method + res, err := h.ResetAllowUpdate(context.Background(), "reset_allow update", tt.input) + if err != nil { + t.Error(err) + } + + // Assert that the Result FlagSet has the required flags after language switch + assert.Equal(t, res, tt.expectedResult, "Flags should be equal to account created") + }) + } +} + +func TestResetAccountAuthorized(t *testing.T) { + fm, err := NewFlagManager(flagsPath) + if err != nil { + log.Fatal(err) + } + + flag_account_authorized, _ := fm.GetFlag("flag_account_authorized") + + // Define test cases + tests := []struct { + name string + input []byte + expectedResult resource.Result + }{ + { + name: "Resets account authorized", + input: []byte(""), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_account_authorized}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Create the MenuHandlers instance with the mock flag manager + h := &MenuHandlers{ + flagManager: fm, + } + + // Call the method + res, err := h.ResetAccountAuthorized(context.Background(), "reset_account_authorized", tt.input) + if err != nil { + t.Error(err) + } + + // Assert that the Result FlagSet has the required flags after language switch + assert.Equal(t, res, tt.expectedResult, "Result should contain flag(s) that have been reset") + }) + } +} diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index c13ba9c..85ffe03 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -829,90 +829,6 @@ func TestSetLanguage(t *testing.T) { } } -func TestResetAllowUpdate(t *testing.T) { - fm, err := NewFlagManager(flagsPath) - if err != nil { - log.Fatal(err) - } - - flag_allow_update, _ := fm.GetFlag("flag_allow_update") - - // Define test cases - tests := []struct { - name string - input []byte - expectedResult resource.Result - }{ - { - name: "Resets allow update", - input: []byte(""), - expectedResult: resource.Result{ - FlagReset: []uint32{flag_allow_update}, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - // Create the MenuHandlers instance with the mock flag manager - h := &MenuHandlers{ - flagManager: fm, - } - - // Call the method - res, err := h.ResetAllowUpdate(context.Background(), "reset_allow update", tt.input) - if err != nil { - t.Error(err) - } - - // Assert that the Result FlagSet has the required flags after language switch - assert.Equal(t, res, tt.expectedResult, "Flags should be equal to account created") - }) - } -} - -func TestResetAccountAuthorized(t *testing.T) { - fm, err := NewFlagManager(flagsPath) - if err != nil { - log.Fatal(err) - } - - flag_account_authorized, _ := fm.GetFlag("flag_account_authorized") - - // Define test cases - tests := []struct { - name string - input []byte - expectedResult resource.Result - }{ - { - name: "Resets account authorized", - input: []byte(""), - expectedResult: resource.Result{ - FlagReset: []uint32{flag_account_authorized}, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - // Create the MenuHandlers instance with the mock flag manager - h := &MenuHandlers{ - flagManager: fm, - } - - // Call the method - res, err := h.ResetAccountAuthorized(context.Background(), "reset_account_authorized", tt.input) - if err != nil { - t.Error(err) - } - - // Assert that the Result FlagSet has the required flags after language switch - assert.Equal(t, res, tt.expectedResult, "Result should contain flag(s) that have been reset") - }) - } -} - func TestIncorrectPinReset(t *testing.T) { sessionId := "session123" ctx, store := InitializeTestStore(t) @@ -1039,89 +955,6 @@ func TestResetIncorrectYob(t *testing.T) { } } -func TestAuthorize(t *testing.T) { - sessionId := "session123" - ctx, store := InitializeTestStore(t) - ctx = context.WithValue(ctx, "SessionId", sessionId) - - fm, err := NewFlagManager(flagsPath) - if err != nil { - t.Logf(err.Error()) - } - - // Create required mocks - mockAccountService := new(mocks.MockAccountService) - mockState := state.NewState(16) - flag_incorrect_pin, _ := fm.GetFlag("flag_incorrect_pin") - flag_account_authorized, _ := fm.GetFlag("flag_account_authorized") - flag_allow_update, _ := fm.GetFlag("flag_allow_update") - - // Set 1234 is the correct account pin - accountPIN := "1234" - - h := &MenuHandlers{ - userdataStore: store, - accountService: mockAccountService, - flagManager: fm, - st: mockState, - } - - tests := []struct { - name string - input []byte - expectedResult resource.Result - }{ - { - name: "Test with correct PIN", - input: []byte("1234"), - expectedResult: resource.Result{ - FlagReset: []uint32{flag_incorrect_pin}, - FlagSet: []uint32{flag_allow_update, flag_account_authorized}, - }, - }, - { - name: "Test with incorrect PIN", - input: []byte("1235"), - expectedResult: resource.Result{ - FlagReset: []uint32{flag_account_authorized, flag_allow_update}, - FlagSet: []uint32{flag_incorrect_pin}, - }, - }, - { - name: "Test with PIN that is not a 4 digit", - input: []byte("1235aqds"), - expectedResult: resource.Result{ - FlagReset: []uint32{flag_account_authorized, flag_allow_update}, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - // Hash the PIN - hashedPIN, err := pin.HashPIN(accountPIN) - if err != nil { - logg.ErrorCtxf(ctx, "failed to hash temporaryPin", "error", err) - t.Fatal(err) - } - - err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACCOUNT_PIN, []byte(hashedPIN)) - if err != nil { - t.Fatal(err) - } - - // Call the method under test - res, err := h.Authorize(ctx, "authorize", []byte(tt.input)) - - // Assert that no errors occurred - assert.NoError(t, err) - - //Assert that the account created flag has been set to the result - assert.Equal(t, res, tt.expectedResult, "Expected result should be equal to the actual result") - }) - } -} - func TestVerifyYob(t *testing.T) { fm, err := NewFlagManager(flagsPath) if err != nil {