Adopt VCR cassette style for Grafana tests
Refactor the Grafana driver test to use the shared recorder and VCR helpers used by other access-review drivers. This aligns the test with the existing cassette workflow and adds a committed cassette fixture for deterministic replay. Signed-off-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Bryan FRIMIN <bryan@frimin.fr>
This commit is contained in:
committed by
Bryan Frimin
parent
f5a632ffac
commit
5ca1e369c0
@@ -16,68 +16,26 @@ package drivers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
)
|
||||
|
||||
func TestGrafanaDriverListAccounts(t *testing.T) {
|
||||
func TestGrafanaDriver(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Helper()
|
||||
require.Equal(t, "/api/org/users", r.URL.Path)
|
||||
require.Equal(t, "100", r.URL.Query().Get("perpage"))
|
||||
rec := newRecorder(t, "testdata/grafana", "GRAFANA_TOKEN")
|
||||
client := newVCRClient(rec, bearerAuth(os.Getenv("GRAFANA_TOKEN")))
|
||||
|
||||
page, err := strconv.Atoi(r.URL.Query().Get("page"))
|
||||
require.NoError(t, err)
|
||||
baseURL := os.Getenv("GRAFANA_BASE_URL")
|
||||
if baseURL == "" {
|
||||
baseURL = "https://grafana.example.com"
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
switch page {
|
||||
case 1:
|
||||
users := make([]map[string]any, 0, grafanaUsersPageSize)
|
||||
users = append(users, map[string]any{
|
||||
"userId": 1,
|
||||
"email": "admin@example.com",
|
||||
"name": "Admin User",
|
||||
"role": "Admin",
|
||||
"isDisabled": false,
|
||||
"lastSeenAt": "2026-05-20T10:00:00Z",
|
||||
})
|
||||
for i := 1; i < grafanaUsersPageSize; i++ {
|
||||
users = append(users, map[string]any{
|
||||
"userId": i + 100,
|
||||
"name": "Ignored User",
|
||||
"role": "Viewer",
|
||||
})
|
||||
}
|
||||
|
||||
_ = json.NewEncoder(w).Encode(users)
|
||||
case 2:
|
||||
_ = json.NewEncoder(w).Encode([]map[string]any{
|
||||
{
|
||||
"userId": 2,
|
||||
"login": "viewer@example.com",
|
||||
"name": "Viewer User",
|
||||
"role": "Viewer",
|
||||
"isDisabled": true,
|
||||
},
|
||||
})
|
||||
default:
|
||||
t.Fatalf("unexpected page %d", page)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(ts.Close)
|
||||
|
||||
driver := NewGrafanaDriver(ts.Client(), ts.URL)
|
||||
driver := NewGrafanaDriver(client, baseURL)
|
||||
records, err := driver.ListAccounts(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Len(t, records, 2)
|
||||
@@ -86,38 +44,20 @@ func TestGrafanaDriverListAccounts(t *testing.T) {
|
||||
assert.Equal(t, "Admin User", records[0].FullName)
|
||||
assert.Equal(t, "Admin", records[0].Role)
|
||||
assert.True(t, records[0].IsAdmin)
|
||||
assert.Equal(t, "1", records[0].ExternalID)
|
||||
assert.Equal(t, strconv.Itoa(1), records[0].ExternalID)
|
||||
require.NotNil(t, records[0].Active)
|
||||
assert.True(t, *records[0].Active)
|
||||
assert.Equal(t, coredata.AccessEntryAccountTypeUser, records[0].AccountType)
|
||||
assert.Equal(t, coredata.AccessEntryAuthMethodUnknown, records[0].AuthMethod)
|
||||
assert.Equal(t, coredata.MFAStatusUnknown, records[0].MFAStatus)
|
||||
require.NotNil(t, records[0].LastLogin)
|
||||
assert.Equal(t, time.Date(2026, 5, 20, 10, 0, 0, 0, time.UTC), *records[0].LastLogin)
|
||||
|
||||
assert.Equal(t, "viewer@example.com", records[1].Email)
|
||||
assert.Equal(t, "Viewer User", records[1].FullName)
|
||||
assert.Equal(t, "Viewer", records[1].Role)
|
||||
assert.False(t, records[1].IsAdmin)
|
||||
assert.Equal(t, "2", records[1].ExternalID)
|
||||
assert.Equal(t, strconv.Itoa(2), records[1].ExternalID)
|
||||
require.NotNil(t, records[1].Active)
|
||||
assert.False(t, *records[1].Active)
|
||||
}
|
||||
|
||||
func TestGrafanaNameResolver(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Helper()
|
||||
require.Equal(t, "/api/org", r.URL.Path)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"name": "Acme Grafana",
|
||||
})
|
||||
}))
|
||||
t.Cleanup(ts.Close)
|
||||
|
||||
resolver := NewGrafanaNameResolver(ts.Client(), ts.URL)
|
||||
resolver := NewGrafanaNameResolver(client, baseURL)
|
||||
name, err := resolver.ResolveInstanceName(context.Background())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "Acme Grafana", name)
|
||||
|
||||
58
pkg/accessreview/drivers/testdata/grafana.yaml
vendored
Normal file
58
pkg/accessreview/drivers/testdata/grafana.yaml
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
---
|
||||
version: 2
|
||||
interactions:
|
||||
- id: 0
|
||||
request:
|
||||
proto: HTTP/1.1
|
||||
proto_major: 1
|
||||
proto_minor: 1
|
||||
content_length: 0
|
||||
host: grafana.example.com
|
||||
form:
|
||||
page:
|
||||
- "1"
|
||||
perpage:
|
||||
- "100"
|
||||
headers:
|
||||
Accept:
|
||||
- application/json
|
||||
url: https://grafana.example.com/api/org/users?page=1&perpage=100
|
||||
method: GET
|
||||
response:
|
||||
proto: HTTP/2.0
|
||||
proto_major: 2
|
||||
proto_minor: 0
|
||||
content_length: -1
|
||||
uncompressed: true
|
||||
body: '[{"userId":1,"email":"admin@example.com","login":"admin@example.com","name":"Admin User","role":"Admin","lastSeenAt":"2026-05-20T10:00:00Z","isDisabled":false},{"userId":2,"email":"","login":"viewer@example.com","name":"Viewer User","role":"Viewer","lastSeenAt":"","isDisabled":true}]'
|
||||
headers:
|
||||
Content-Type:
|
||||
- application/json
|
||||
status: 200 OK
|
||||
code: 200
|
||||
duration: 100ms
|
||||
- id: 1
|
||||
request:
|
||||
proto: HTTP/1.1
|
||||
proto_major: 1
|
||||
proto_minor: 1
|
||||
content_length: 0
|
||||
host: grafana.example.com
|
||||
headers:
|
||||
Accept:
|
||||
- application/json
|
||||
url: https://grafana.example.com/api/org
|
||||
method: GET
|
||||
response:
|
||||
proto: HTTP/2.0
|
||||
proto_major: 2
|
||||
proto_minor: 0
|
||||
content_length: -1
|
||||
uncompressed: true
|
||||
body: '{"id":1,"name":"Acme Grafana"}'
|
||||
headers:
|
||||
Content-Type:
|
||||
- application/json
|
||||
status: 200 OK
|
||||
code: 200
|
||||
duration: 100ms
|
||||
Reference in New Issue
Block a user