store_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package apitoken
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/gnacho/netpulse/server-go/internal/auth"
  6. "github.com/gnacho/netpulse/server-go/internal/config"
  7. "github.com/gnacho/netpulse/server-go/internal/db"
  8. )
  9. func setup(t *testing.T) (*Store, *db.DB) {
  10. t.Helper()
  11. dir := t.TempDir()
  12. d, err := db.Open(dir)
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. t.Cleanup(func() { _ = d.Close() })
  17. if err := EnsureSchema(d); err != nil {
  18. t.Fatal(err)
  19. }
  20. cfg := &config.Config{AuthUser: "admin", AuthPass: "test123456"}
  21. if err := auth.EnsureUsers(d, cfg); err != nil {
  22. t.Fatal(err)
  23. }
  24. return NewStore(d, "test-secret"), d
  25. }
  26. func TestCreateAndValidate(t *testing.T) {
  27. s, _ := setup(t)
  28. id, raw, err := s.Create("my-token", ScopeRead, 1, 0)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. if id == "" || raw == "" {
  33. t.Fatal("empty id or raw")
  34. }
  35. if raw[:3] != TokenPrefix {
  36. t.Fatalf("raw should start with %s, got %s", TokenPrefix, raw[:3])
  37. }
  38. tok := s.Validate(raw)
  39. if tok == nil {
  40. t.Fatal("validate returned nil")
  41. }
  42. if tok.Name != "my-token" || tok.Scope != ScopeRead {
  43. t.Fatalf("unexpected token: %+v", tok)
  44. }
  45. }
  46. func TestValidateExpired(t *testing.T) {
  47. s, d := setup(t)
  48. _, raw, err := s.Create("exp", ScopeRead, 1, 1)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. tok := s.Validate(raw)
  53. if tok == nil {
  54. t.Fatal("should be valid now")
  55. }
  56. _, _ = d.Exec("UPDATE api_tokens SET expires_at = 1 WHERE name = 'exp'")
  57. tok = s.Validate(raw)
  58. if tok != nil {
  59. t.Fatal("should be nil after expiry")
  60. }
  61. }
  62. func TestValidateBadToken(t *testing.T) {
  63. s, _ := setup(t)
  64. if tok := s.Validate("np_bogus"); tok != nil {
  65. t.Fatal("should be nil for bogus token")
  66. }
  67. if tok := s.Validate("not-a-token"); tok != nil {
  68. t.Fatal("should be nil for non-prefixed token")
  69. }
  70. }
  71. func TestListAndDelete(t *testing.T) {
  72. s, _ := setup(t)
  73. s.Create("t1", ScopeRead, 1, 0)
  74. s.Create("t2", ScopeWrite, 1, 0)
  75. tokens, err := s.List(1, false)
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. if len(tokens) != 2 {
  80. t.Fatalf("expected 2 tokens, got %d", len(tokens))
  81. }
  82. if err := s.Delete(tokens[0].ID, 1, false); err != nil {
  83. t.Fatal(err)
  84. }
  85. tokens, _ = s.List(1, false)
  86. if len(tokens) != 1 {
  87. t.Fatalf("expected 1 token after delete, got %d", len(tokens))
  88. }
  89. }
  90. func TestValidateBearerScope(t *testing.T) {
  91. s, _ := setup(t)
  92. _, raw, _ := s.Create("admin-tok", ScopeAdmin, 1, 0)
  93. user, scope := s.ValidateBearer(raw)
  94. if user == nil {
  95. t.Fatal("admin token should validate")
  96. }
  97. if scope != ScopeAdmin {
  98. t.Fatalf("expected admin scope, got %s", scope)
  99. }
  100. if user.Role != "admin" {
  101. t.Fatalf("expected admin user, got %s", user.Role)
  102. }
  103. }
  104. func TestValidateBearerRead(t *testing.T) {
  105. s, _ := setup(t)
  106. _, raw, _ := s.Create("read-tok", ScopeRead, 1, 0)
  107. user, scope := s.ValidateBearer(raw)
  108. if user == nil {
  109. t.Fatal("read token should validate")
  110. }
  111. if scope != ScopeRead {
  112. t.Fatalf("expected read scope, got %s", scope)
  113. }
  114. }
  115. func TestInvalidScope(t *testing.T) {
  116. s, _ := setup(t)
  117. _, _, err := s.Create("bad", "superadmin", 1, 0)
  118. if err == nil {
  119. t.Fatal("should reject invalid scope")
  120. }
  121. }
  122. func TestEnsureSchemaIdempotent(t *testing.T) {
  123. dir := t.TempDir()
  124. d, err := db.Open(dir)
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. defer d.Close()
  129. if err := EnsureSchema(d); err != nil {
  130. t.Fatal(err)
  131. }
  132. if err := EnsureSchema(d); err != nil {
  133. t.Fatal("second call should be idempotent")
  134. }
  135. }
  136. func TestMain(m *testing.M) {
  137. os.Exit(m.Run())
  138. }