1
0

api_token_cli_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package main
  2. // GetApiToken rotates a credential rather than displaying one, so these pin
  3. // which token name it destroys — the whole point of the -tokenName flag.
  4. import (
  5. "flag"
  6. "testing"
  7. "github.com/mhsanaei/3x-ui/v3/internal/config"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database"
  9. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  10. "github.com/mhsanaei/3x-ui/v3/internal/web/service/panel"
  11. )
  12. func newTokenCLIEnv(t *testing.T) {
  13. t.Helper()
  14. t.Setenv("XUI_DB_FOLDER", t.TempDir())
  15. if err := database.InitDB(config.GetDBPath()); err != nil {
  16. t.Fatalf("init db: %v", err)
  17. }
  18. t.Cleanup(func() { _ = database.CloseDB() })
  19. }
  20. func tokenNames(t *testing.T) []string {
  21. t.Helper()
  22. tokens, err := (&panel.ApiTokenService{}).List()
  23. if err != nil {
  24. t.Fatalf("list tokens: %v", err)
  25. }
  26. names := make([]string, 0, len(tokens))
  27. for _, token := range tokens {
  28. names = append(names, token.Name)
  29. }
  30. return names
  31. }
  32. func tokenRow(t *testing.T, name string) model.ApiToken {
  33. t.Helper()
  34. var row model.ApiToken
  35. if err := database.GetDB().Where("name = ?", name).First(&row).Error; err != nil {
  36. t.Fatalf("load token %q: %v", name, err)
  37. }
  38. return row
  39. }
  40. func hasName(names []string, want string) bool {
  41. for _, name := range names {
  42. if name == want {
  43. return true
  44. }
  45. }
  46. return false
  47. }
  48. // The bug: two callers sharing one hardcoded slot silently revoke each other.
  49. // A named token must leave an differently-named one authenticating.
  50. func TestGetApiTokenRotatesOnlyTheNamedToken(t *testing.T) {
  51. newTokenCLIEnv(t)
  52. svc := panel.ApiTokenService{}
  53. weekly, err := svc.RecreateByName("weekly-report")
  54. if err != nil {
  55. t.Fatalf("seed weekly-report: %v", err)
  56. }
  57. GetApiToken(true, "ci-bot")
  58. names := tokenNames(t)
  59. if !hasName(names, "ci-bot") {
  60. t.Fatalf("token names = %v, want ci-bot among them", names)
  61. }
  62. if !svc.Match(weekly.Token) {
  63. t.Fatal("weekly-report was revoked by a call naming ci-bot")
  64. }
  65. }
  66. // An explicit name has to win on both branches, or the same command would
  67. // produce ci-bot on a populated panel and "install" on a fresh one.
  68. func TestGetApiTokenUsesGivenNameOnEmptyDatabase(t *testing.T) {
  69. newTokenCLIEnv(t)
  70. GetApiToken(true, "ci-bot")
  71. names := tokenNames(t)
  72. if !hasName(names, "ci-bot") {
  73. t.Fatalf("token names = %v, want ci-bot among them", names)
  74. }
  75. if hasName(names, installTokenName) {
  76. t.Fatalf("token names = %v, want no %s when a name was given", names, installTokenName)
  77. }
  78. }
  79. // install.sh records the token it gets on a fresh panel. A later bare
  80. // -getApiToken must rotate the fallback slot and leave that record valid.
  81. func TestGetApiTokenPreservesInstallTokenWhenRotating(t *testing.T) {
  82. newTokenCLIEnv(t)
  83. GetApiToken(true, "")
  84. installed := tokenRow(t, installTokenName)
  85. GetApiToken(true, "")
  86. names := tokenNames(t)
  87. if !hasName(names, cliFallbackTokenName) {
  88. t.Fatalf("token names = %v, want %s among them", names, cliFallbackTokenName)
  89. }
  90. if got := tokenRow(t, installTokenName); got.Id != installed.Id {
  91. t.Fatalf("%s row id = %d, want %d — the installer's token was replaced", installTokenName, got.Id, installed.Id)
  92. }
  93. if got := tokenRow(t, installTokenName); got.Token != installed.Token {
  94. t.Fatalf("the %s token hash changed, so the recorded credential stopped working", installTokenName)
  95. }
  96. }
  97. // `-getApiToken true -tokenName ci-bot` parses tokenName as "", because flag
  98. // stops at the positional. The command must not then rotate the shared slot.
  99. func TestGetApiTokenWarnsOnIgnoredPositionalArgs(t *testing.T) {
  100. set := flag.NewFlagSet("setting", flag.ContinueOnError)
  101. var getApiToken bool
  102. var tokenName string
  103. set.BoolVar(&getApiToken, "getApiToken", false, "")
  104. set.StringVar(&tokenName, "tokenName", "", "")
  105. if err := set.Parse([]string{"-getApiToken", "true", "-tokenName", "ci-bot"}); err != nil {
  106. t.Fatalf("parse: %v", err)
  107. }
  108. if tokenName != "" {
  109. t.Fatalf("tokenName = %q; this test guards the case where flag drops it", tokenName)
  110. }
  111. if got := set.Args(); len(got) == 0 {
  112. t.Fatal("leftover arguments must be visible so the CLI can warn instead of silently rotating cli-fallback")
  113. }
  114. }
  115. func TestGetApiTokenTrimsName(t *testing.T) {
  116. newTokenCLIEnv(t)
  117. if _, err := (&panel.ApiTokenService{}).RecreateByName("seed"); err != nil {
  118. t.Fatalf("seed: %v", err)
  119. }
  120. GetApiToken(true, " ")
  121. names := tokenNames(t)
  122. if !hasName(names, cliFallbackTokenName) {
  123. t.Fatalf("token names = %v, want a whitespace-only name to fall back to %s", names, cliFallbackTokenName)
  124. }
  125. }