package service import ( "testing" "github.com/glebarez/sqlite" "gorm.io/gorm" "wails-app/internal/dao" "wails-app/internal/global" "wails-app/internal/modules/central-payment/model/request" utils "wails-app/internal/pkg" ) func TestDeviceLoginAndTokenAuthentication(t *testing.T) { db, err := gorm.Open(sqlite.Open("file:central_payment_login?mode=memory&cache=shared"), &gorm.Config{}) if err != nil { t.Fatal(err) } if err := db.AutoMigrate(&dao.CentralPaymentDevice{}, &dao.CentralPaymentToken{}, &dao.CentralPaymentRequestLog{}); err != nil { t.Fatal(err) } previousDB := global.GVA_DB global.GVA_DB = db t.Cleanup(func() { global.GVA_DB = previousDB }) device := dao.CentralPaymentDevice{ DeviceID: "POF-TEST-01", Username: "pof-test", PasswordHash: utils.BcryptHash("test-password"), Enabled: true, PaymentEntry: PaymentEntryCentral, } if err := db.Create(&device).Error; err != nil { t.Fatal(err) } svc := New() token, err := svc.Login(request.LoginRequest{UserName: "pof-test", Password: "test-password"}) if err != nil { t.Fatal(err) } if token == "" { t.Fatal("expected a token") } authenticated, err := svc.Authenticate(token) if err != nil { t.Fatal(err) } if authenticated.DeviceID != device.DeviceID { t.Fatalf("authenticated device = %q, want %q", authenticated.DeviceID, device.DeviceID) } if _, err := svc.Login(request.LoginRequest{UserName: "pof-test", Password: "wrong"}); err == nil { t.Fatal("expected invalid password to fail") } }