Commit 253e1a2c23
Verified · cmc ci/build: success ci/test: success
Layout: unified · split
internal/control/token.go +4 −9
| @@ -69,21 +69,16 @@ func runTokenCreate(c *Ctx, args []string) int { | ||
| 69 | 69 | if err != nil { |
| 70 | 70 | return c.fail(protocol.ExitUsage, "%v", err) |
| 71 | 71 | } |
| 72 | name, scope, ttl := f.Value("--name"), "read", f.Value("--ttl") | |
| 72 | name, scope := f.Value("--name"), "read" | |
| 73 | 73 | if f.Has("--scope") { |
| 74 | 74 | scope = f.Value("--scope") |
| 75 | 75 | } |
| 76 | 76 | if name == "" || (scope != "full" && scope != "read") { |
| 77 | 77 | return c.usage() |
| 78 | 78 | } |
| 79 | var expires *time.Time | |
| 80 | if ttl != "" { | |
| 81 | d, err := parseTTL(ttl) | |
| 82 | if err != nil { | |
| 83 | return c.failInput(err) | |
| 84 | } | |
| 85 | t := time.Now().Add(d) | |
| 86 | expires = &t | |
| 79 | expires, code := c.ttlFlag(f) | |
| 80 | if code >= 0 { | |
| 81 | return code | |
| 87 | 82 | } |
| 88 | 83 | raw, _, err := store.NewToken() |
| 89 | 84 | if err != nil { |
internal/control/token_test.go +14
| @@ -102,3 +102,17 @@ func TestTokenCreateDefaultsToReadAndRecordsCreator(t *testing.T) { | ||
| 102 | 102 | t.Fatal(`no token named "child"`) |
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | ||
| 106 | func TestTokenCreateRefusesNonPositiveTTL(t *testing.T) { | |
| 107 | st, _, uid := newQueueTestRepo(t) | |
| 108 | for _, ttl := range []string{"0s", "-1h"} { | |
| 109 | c, _ := pruneCtx(st, t.TempDir(), store.User{ID: uid, Username: "alice"}) | |
| 110 | c.Cfg.Limits.WriteRate = -1 | |
| 111 | if code := Dispatch(c, []string{"token", "create", "--name", "x", "--ttl", ttl}); code != protocol.ExitUsage { | |
| 112 | t.Errorf("--ttl %s: exit %d", ttl, code) | |
| 113 | } | |
| 114 | } | |
| 115 | if toks, err := st.ListAPITokens(uid); err != nil || len(toks) != 0 { | |
| 116 | t.Fatalf("tokens: %+v %v", toks, err) | |
| 117 | } | |
| 118 | } | |