From 5e166fd680cf744dc08c4d7aa2eab8afba0a242f Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 3 Jul 2025 17:33:11 +0300 Subject: [PATCH] move registration tests --- handlers/application/menuhandler_test.go | 64 ------------------- handlers/application/registration_test.go | 76 +++++++++++++++++++++++ 2 files changed, 76 insertions(+), 64 deletions(-) create mode 100644 handlers/application/registration_test.go diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index a2fce4c..b87cfc5 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -208,70 +208,6 @@ func TestInit(t *testing.T) { } } -func TestCreateAccount(t *testing.T) { - sessionId := "session123" - ctx, userStore := InitializeTestStore(t) - ctx = context.WithValue(ctx, "SessionId", sessionId) - _, logdb := InitializeTestLogdbStore(t) - - logDb := store.LogDb{ - Db: logdb, - } - - fm, err := NewFlagManager(flagsPath) - if err != nil { - t.Logf(err.Error()) - } - - flag_account_created, err := fm.GetFlag("flag_account_created") - flag_account_creation_failed, _ := fm.GetFlag("flag_account_creation_failed") - - if err != nil { - t.Logf(err.Error()) - } - - tests := []struct { - name string - serverResponse *models.AccountResult - expectedResult resource.Result - }{ - { - name: "Test account creation success", - serverResponse: &models.AccountResult{ - TrackingId: "1234567890", - PublicKey: "0xD3adB33f", - }, - expectedResult: resource.Result{ - FlagSet: []uint32{flag_account_created}, - FlagReset: []uint32{flag_account_creation_failed}, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - mockAccountService := new(mocks.MockAccountService) - - h := &MenuHandlers{ - userdataStore: userStore, - accountService: mockAccountService, - logDb: logDb, - flagManager: fm, - } - - mockAccountService.On("CreateAccount").Return(tt.serverResponse, nil) - - // Call the method you want to test - res, err := h.CreateAccount(ctx, "create_account", []byte("")) - - // 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 TestWithPersister(t *testing.T) { // Test case: Setting a persister h := &MenuHandlers{} diff --git a/handlers/application/registration_test.go b/handlers/application/registration_test.go new file mode 100644 index 0000000..45e30f5 --- /dev/null +++ b/handlers/application/registration_test.go @@ -0,0 +1,76 @@ +package application + +import ( + "context" + "testing" + + "git.defalsify.org/vise.git/resource" + "git.grassecon.net/grassrootseconomics/sarafu-api/models" + "git.grassecon.net/grassrootseconomics/sarafu-api/testutil/mocks" + "git.grassecon.net/grassrootseconomics/sarafu-vise/store" + "github.com/alecthomas/assert/v2" +) + +func TestCreateAccount(t *testing.T) { + sessionId := "session123" + ctx, userStore := InitializeTestStore(t) + ctx = context.WithValue(ctx, "SessionId", sessionId) + _, logdb := InitializeTestLogdbStore(t) + + logDb := store.LogDb{ + Db: logdb, + } + + fm, err := NewFlagManager(flagsPath) + if err != nil { + t.Logf(err.Error()) + } + + flag_account_created, err := fm.GetFlag("flag_account_created") + flag_account_creation_failed, _ := fm.GetFlag("flag_account_creation_failed") + + if err != nil { + t.Logf(err.Error()) + } + + tests := []struct { + name string + serverResponse *models.AccountResult + expectedResult resource.Result + }{ + { + name: "Test account creation success", + serverResponse: &models.AccountResult{ + TrackingId: "1234567890", + PublicKey: "0xD3adB33f", + }, + expectedResult: resource.Result{ + FlagSet: []uint32{flag_account_created}, + FlagReset: []uint32{flag_account_creation_failed}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockAccountService := new(mocks.MockAccountService) + + h := &MenuHandlers{ + userdataStore: userStore, + accountService: mockAccountService, + logDb: logDb, + flagManager: fm, + } + + mockAccountService.On("CreateAccount").Return(tt.serverResponse, nil) + + // Call the method you want to test + res, err := h.CreateAccount(ctx, "create_account", []byte("")) + + // 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") + }) + } +}