tokens_api_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package httpapi_test
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "testing"
  7. )
  8. func TestTokensCRUD(t *testing.T) {
  9. ts := makeTestServer(t)
  10. defer ts.Close()
  11. _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
  12. createBody, _ := json.Marshal(map[string]any{
  13. "name": "test-token", "scope": "read", "expiresInDays": 0,
  14. })
  15. var created struct {
  16. ID string `json:"id"`
  17. Token string `json:"token"`
  18. }
  19. {
  20. req, _ := http.NewRequest("POST", ts.URL+"/api/tokens", bytes.NewReader(createBody))
  21. req.Header.Set("Content-Type", "application/json")
  22. req.Header.Set("Cookie", "session="+cookie)
  23. resp, _ := http.DefaultClient.Do(req)
  24. defer resp.Body.Close()
  25. if resp.StatusCode != http.StatusCreated {
  26. t.Fatalf("create: expected 201, got %d", resp.StatusCode)
  27. }
  28. _ = json.NewDecoder(resp.Body).Decode(&created)
  29. if created.ID == "" || created.Token == "" {
  30. t.Fatal("create: empty id or token")
  31. }
  32. }
  33. {
  34. req, _ := http.NewRequest("GET", ts.URL+"/api/tokens", nil)
  35. req.Header.Set("Cookie", "session="+cookie)
  36. resp, _ := http.DefaultClient.Do(req)
  37. defer resp.Body.Close()
  38. if resp.StatusCode != http.StatusOK {
  39. t.Fatalf("list: expected 200, got %d", resp.StatusCode)
  40. }
  41. var body struct {
  42. Tokens []struct {
  43. ID string `json:"id"`
  44. Name string `json:"name"`
  45. } `json:"tokens"`
  46. }
  47. _ = json.NewDecoder(resp.Body).Decode(&body)
  48. if len(body.Tokens) != 1 || body.Tokens[0].Name != "test-token" {
  49. t.Fatalf("list: unexpected: %+v", body.Tokens)
  50. }
  51. }
  52. {
  53. req, _ := http.NewRequest("DELETE", ts.URL+"/api/tokens/"+created.ID, nil)
  54. req.Header.Set("Cookie", "session="+cookie)
  55. resp, _ := http.DefaultClient.Do(req)
  56. defer resp.Body.Close()
  57. if resp.StatusCode != http.StatusNoContent {
  58. t.Fatalf("delete: expected 204, got %d", resp.StatusCode)
  59. }
  60. }
  61. {
  62. req, _ := http.NewRequest("GET", ts.URL+"/api/tokens", nil)
  63. req.Header.Set("Cookie", "session="+cookie)
  64. resp, _ := http.DefaultClient.Do(req)
  65. defer resp.Body.Close()
  66. var body struct {
  67. Tokens []struct{ ID string } `json:"tokens"`
  68. }
  69. _ = json.NewDecoder(resp.Body).Decode(&body)
  70. if len(body.Tokens) != 0 {
  71. t.Fatalf("after delete: expected 0, got %d", len(body.Tokens))
  72. }
  73. }
  74. }
  75. func TestTokenBearerAuth(t *testing.T) {
  76. ts := makeTestServer(t)
  77. defer ts.Close()
  78. _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
  79. createBody, _ := json.Marshal(map[string]any{
  80. "name": "bearer-test", "scope": "read", "expiresInDays": 0,
  81. })
  82. req, _ := http.NewRequest("POST", ts.URL+"/api/tokens", bytes.NewReader(createBody))
  83. req.Header.Set("Content-Type", "application/json")
  84. req.Header.Set("Cookie", "session="+cookie)
  85. resp, _ := http.DefaultClient.Do(req)
  86. defer resp.Body.Close()
  87. var created struct {
  88. Token string `json:"token"`
  89. }
  90. _ = json.NewDecoder(resp.Body).Decode(&created)
  91. if created.Token == "" {
  92. t.Fatal("no token created")
  93. }
  94. req, _ = http.NewRequest("GET", ts.URL+"/api/overview", nil)
  95. req.Header.Set("Authorization", "Bearer "+created.Token)
  96. resp2, _ := http.DefaultClient.Do(req)
  97. defer resp2.Body.Close()
  98. if resp2.StatusCode == http.StatusUnauthorized {
  99. t.Fatal("bearer token should authenticate, got 401")
  100. }
  101. }
  102. func TestTokenBearerInvalid(t *testing.T) {
  103. ts := makeTestServer(t)
  104. defer ts.Close()
  105. req, _ := http.NewRequest("GET", ts.URL+"/api/overview", nil)
  106. req.Header.Set("Authorization", "Bearer np_invalidtoken")
  107. resp, _ := http.DefaultClient.Do(req)
  108. defer resp.Body.Close()
  109. if resp.StatusCode != http.StatusUnauthorized {
  110. t.Fatalf("invalid bearer should get 401, got %d", resp.StatusCode)
  111. }
  112. }