tokens_api_test.go 3.6 KB

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