Pārlūkot izejas kodu

sdk添加登录

qdy 3 nedēļas atpakaļ
revīzija
b488b4eeff
2 mainītis faili ar 101 papildinājumiem un 1 dzēšanām
  1. 1
    1
      internal/tables/dic_table_field.go
  2. 100
    0
      test/sdk_token_test.go

+ 1
- 1
internal/tables/dic_table_field.go Parādīt failu

@@ -9,7 +9,7 @@ import (
9 9
 func init() {
10 10
 	sqldef.AddRegistration(func(r *sqldef.Registry) {
11 11
 		tb := sqldef.NewTable("dic_table_field", "数据库表字段字典表 - 管理数据库表字段字典").
12
-			ID("id", 128).NotNull().Comment("主键").End().
12
+			ID("id", 128).NotNull().Comment("主键。table_id.field_id组成").End().
13 13
 			String("field_id", 128).NotNull().Comment("字段ID").End().
14 14
 			String("table_id", 64).NotNull().Comment("表ID").End().
15 15
 			String("filed_type", 20).NotNull().Comment("字段类型: 实际字段,计算字段").End().

+ 100
- 0
test/sdk_token_test.go Parādīt failu

@@ -460,3 +460,103 @@ func TestBatchSaveWithSDK(t *testing.T) {
460 460
 
461 461
 	t.Log("SDK批量保存测试全部完成")
462 462
 }
463
+
464
+// TestSDKUserLogin 测试SDK用户登录功能
465
+func TestSDKUserLogin(t *testing.T) {
466
+	// 创建客户端(登录端点无需认证,但需要BaseURL)
467
+	// 使用任意认证凭证,因为登录时skipAuth=true
468
+	client, err := configure.NewBasicAuthClient("http://localhost:8080", "test", "test")
469
+	if err != nil {
470
+		t.Fatalf("创建SDK客户端失败: %v", err)
471
+	}
472
+
473
+	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
474
+	defer cancel()
475
+
476
+	// 测试登录功能
477
+	t.Run("UserLoginWithSDK", func(t *testing.T) {
478
+		// 尝试使用测试用户登录
479
+		req := &configure.UserLoginRequest{
480
+			UserID:   "test-user-001",
481
+			Password: "password123",
482
+		}
483
+
484
+		startTime := time.Now()
485
+		token, err := client.LoginUser(ctx, req)
486
+		elapsed := time.Since(startTime)
487
+
488
+		if err != nil {
489
+			// 检查是否是连接错误(服务未启动)
490
+			if isConnectionError(err) {
491
+				t.Skipf("配置中心服务未启动,跳过测试: %v", err)
492
+			}
493
+			t.Fatalf("SDK用户登录失败: %v (耗时: %v)", err, elapsed)
494
+		}
495
+
496
+		if token == "" {
497
+			t.Fatal("SDK用户登录返回空token")
498
+		}
499
+
500
+		t.Logf("SDK用户登录成功!获取Token: %s... (耗时: %v)", token[:min(50, len(token))], elapsed)
501
+
502
+		// 验证token可用于创建新的认证客户端
503
+		t.Run("CreateTokenClient", func(t *testing.T) {
504
+			tokenClient, err := configure.NewTokenAuthClient("http://localhost:8080", token)
505
+			if err != nil {
506
+				t.Fatalf("使用获取的token创建客户端失败: %v", err)
507
+			}
508
+
509
+			t.Logf("Token客户端创建成功,BaseURL: %s", tokenClient.GetConfig().BaseURL)
510
+
511
+			// 测试token客户端是否可正常工作(查询表列表)
512
+			query := &configure.DicTableQueryRequest{
513
+				QueryRequest: queryreq.QueryRequest{
514
+					Page:     0,
515
+					PageSize: 5,
516
+				},
517
+			}
518
+
519
+			result, err := tokenClient.ListTables(ctx, query)
520
+			if err != nil {
521
+				// 如果token无效或权限不足,可能失败
522
+				t.Logf("使用token查询表列表失败(可能是token权限问题): %v", err)
523
+			} else {
524
+				t.Logf("使用token查询表列表成功,总记录数: %d", result.TotalCount)
525
+			}
526
+		})
527
+	})
528
+
529
+	// 测试无效凭证登录
530
+	t.Run("InvalidCredentialsLogin", func(t *testing.T) {
531
+		req := &configure.UserLoginRequest{
532
+			UserID:   "invalid-user",
533
+			Password: "wrong-password",
534
+		}
535
+
536
+		_, err := client.LoginUser(ctx, req)
537
+		if err == nil {
538
+			t.Fatal("使用无效凭证登录应该失败,但成功了")
539
+		}
540
+
541
+		// 检查错误类型
542
+		t.Logf("无效凭证登录失败(预期): %v", err)
543
+	})
544
+}
545
+
546
+// isConnectionError 检查错误是否是连接错误(服务未启动)
547
+func isConnectionError(err error) bool {
548
+	errStr := err.Error()
549
+	return contains(errStr, "connection refused") ||
550
+		contains(errStr, "connect: connection refused") ||
551
+		contains(errStr, "dial tcp") ||
552
+		contains(errStr, "EOF") ||
553
+		contains(errStr, "timeout")
554
+}
555
+
556
+// min 返回两个整数的最小值
557
+func min(a, b int) int {
558
+	if a < b {
559
+		return a
560
+	}
561
+	return b
562
+}

Notiek ielāde…
Atcelt
Saglabāt