Partly resolves issue #86 Reviewed-on: #90 Co-authored-by: alfred-mk <alfredmwaik@gmail.com> Co-committed-by: alfred-mk <alfredmwaik@gmail.com>
		
			
				
	
	
		
			182 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			182 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| 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")
 | |
| 		})
 | |
| 	}
 | |
| }
 |