mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-24 21:29:00 +08:00
fix: 延长宿主机安装 token 过期时间
This commit is contained in:
1 parent
3f2dc659e4
commit
da28c22bc5
4 files changed
+124
-2
No files matched your search
@@ -176,7 +176,7 @@ func (h *HostUsecase) GetInstallCommand(ctx context.Context, user *domain.User)
|
||||
return "", err
|
||||
}
|
||||
key := fmt.Sprintf("host:token:%s", token)
|
||||
if err := h.redis.Set(ctx, key, string(ub), 15*time.Minute).Err(); err != nil {
|
||||
if err := h.redis.Set(ctx, key, string(ub), 2*time.Hour).Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -21,6 +22,38 @@ import (
|
||||
"github.com/chaitin/MonkeyCode/backend/pkg/taskflow"
|
||||
)
|
||||
|
||||
func TestGetInstallCommandStoresTokenForTwoHours(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
mr := miniredis.RunT(t)
|
||||
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||
t.Cleanup(func() { _ = rdb.Close() })
|
||||
|
||||
u := &HostUsecase{
|
||||
cfg: &config.Config{
|
||||
Server: struct {
|
||||
Addr string `mapstructure:"addr"`
|
||||
BaseURL string `mapstructure:"base_url"`
|
||||
}{BaseURL: "http://monkeycode.local"},
|
||||
},
|
||||
redis: rdb,
|
||||
}
|
||||
|
||||
cmd, err := u.GetInstallCommand(ctx, &domain.User{ID: uuid.New(), Name: "tester"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token := installTokenFromCommand(t, cmd)
|
||||
ttl, err := rdb.TTL(ctx, "host:token:"+token).Result()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ttl != 2*time.Hour {
|
||||
t.Fatalf("host install token ttl = %s, want %s", ttl, 2*time.Hour)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallScriptDefaultsToOnlineInstaller(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -136,6 +169,25 @@ func assertInstallScriptChecksAVX(t *testing.T, script string) {
|
||||
}
|
||||
}
|
||||
|
||||
func installTokenFromCommand(t *testing.T, cmd string) string {
|
||||
t.Helper()
|
||||
|
||||
start := strings.Index(cmd, "'")
|
||||
end := strings.LastIndex(cmd, "'")
|
||||
if start == -1 || end <= start {
|
||||
t.Fatalf("install command missing quoted url: %s", cmd)
|
||||
}
|
||||
u, err := url.Parse(cmd[start+1 : end])
|
||||
if err != nil {
|
||||
t.Fatalf("parse install command url: %v", err)
|
||||
}
|
||||
token := u.Query().Get("token")
|
||||
if token == "" {
|
||||
t.Fatalf("install command missing token: %s", cmd)
|
||||
}
|
||||
return token
|
||||
}
|
||||
|
||||
func TestHostUsecase_markRecycledTasksFinished(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ func (u *TeamHostUsecase) GetInstallCommand(ctx context.Context, teamUser *domai
|
||||
return "", err
|
||||
}
|
||||
key := fmt.Sprintf("host:token:%s", token)
|
||||
if err := u.redis.Set(ctx, key, string(ub), 15*time.Minute).Err(); err != nil {
|
||||
if err := u.redis.Set(ctx, key, string(ub), 2*time.Hour).Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package usecase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/google/uuid"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"github.com/chaitin/MonkeyCode/backend/config"
|
||||
"github.com/chaitin/MonkeyCode/backend/domain"
|
||||
)
|
||||
|
||||
func TestGetInstallCommandStoresTokenForTwoHours(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
mr := miniredis.RunT(t)
|
||||
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||
t.Cleanup(func() { _ = rdb.Close() })
|
||||
|
||||
u := &TeamHostUsecase{
|
||||
cfg: &config.Config{
|
||||
Server: struct {
|
||||
Addr string `mapstructure:"addr"`
|
||||
BaseURL string `mapstructure:"base_url"`
|
||||
}{BaseURL: "http://monkeycode.local"},
|
||||
},
|
||||
redis: rdb,
|
||||
}
|
||||
|
||||
cmd, err := u.GetInstallCommand(ctx, &domain.TeamUser{
|
||||
User: &domain.User{ID: uuid.New(), Name: "tester"},
|
||||
Team: &domain.Team{ID: uuid.New(), Name: "team"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token := installTokenFromCommand(t, cmd)
|
||||
ttl, err := rdb.TTL(ctx, "host:token:"+token).Result()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ttl != 2*time.Hour {
|
||||
t.Fatalf("team host install token ttl = %s, want %s", ttl, 2*time.Hour)
|
||||
}
|
||||
}
|
||||
|
||||
func installTokenFromCommand(t *testing.T, cmd string) string {
|
||||
t.Helper()
|
||||
|
||||
start := strings.Index(cmd, "'")
|
||||
end := strings.LastIndex(cmd, "'")
|
||||
if start == -1 || end <= start {
|
||||
t.Fatalf("install command missing quoted url: %s", cmd)
|
||||
}
|
||||
u, err := url.Parse(cmd[start+1 : end])
|
||||
if err != nil {
|
||||
t.Fatalf("parse install command url: %v", err)
|
||||
}
|
||||
token := u.Query().Get("token")
|
||||
if token == "" {
|
||||
t.Fatalf("install command missing token: %s", cmd)
|
||||
}
|
||||
return token
|
||||
}
|
||||
Reference in new issue
Block a user