You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.7 KiB
47 lines
1.7 KiB
|
2 months ago
|
package api
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"strings"
|
||
|
|
"tera-clone/services/tera-api/internal/adapters/mock"
|
||
|
|
"tera-clone/services/tera-api/internal/domain"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestClearAllLogsRequiresExactConfirmationAndAdmin(t *testing.T) {
|
||
|
|
s := mock.NewStore(fixedClock)
|
||
|
|
defer s.Close()
|
||
|
|
h := NewRouter(s)
|
||
|
|
for _, tc := range []struct {
|
||
|
|
path, body, actor string
|
||
|
|
status int
|
||
|
|
}{
|
||
|
|
{"/api/v1/logs/port/clear", `{"channel":"ALL","confirmation":"CH01"}`, "mock-admin", 400},
|
||
|
|
{"/api/v1/logs/port/clear", `{"channel":"ALL","confirmation":"ALL"}`, "mock-port-operator", 403},
|
||
|
|
{"/api/v1/logs/links/clear", `{"confirmation":"LINKS"}`, "mock-port-operator", 403},
|
||
|
|
{"/api/v1/logs/links/clear", `{"confirmation":"LINKS"}`, "", 401},
|
||
|
|
{"/api/v1/logs/links/clear", `{"confirmation":"SYSTEM"}`, "mock-admin", 400},
|
||
|
|
{"/api/v1/logs/links/clear", `{"confirmation":"LINKS","class":"audit"}`, "mock-admin", 400},
|
||
|
|
} {
|
||
|
|
r := httptest.NewRequest(http.MethodPost, tc.path, strings.NewReader(tc.body))
|
||
|
|
r.Header.Set("X-Mock-Actor", tc.actor)
|
||
|
|
w := httptest.NewRecorder()
|
||
|
|
h.ServeHTTP(w, r)
|
||
|
|
if w.Code != tc.status {
|
||
|
|
t.Fatalf("%s: %d %s", tc.path, w.Code, w.Body.String())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for _, ch := range []string{"CH01", "CH48"} {
|
||
|
|
if _, err := s.RecordPortLog(context.Background(), domain.PortLogEvent{Channel: ch, EventType: "command", Command: "test", ReturnValue: "ok", Message: "test", Actor: "test"}); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
result := getJSONAs(t, h, http.MethodPost, "/api/v1/logs/port/clear", `{"channel":"ALL","confirmation":"ALL"}`, "mock-admin")
|
||
|
|
if result["deleted"] != float64(2) {
|
||
|
|
t.Fatal(result)
|
||
|
|
}
|
||
|
|
getJSONAs(t, h, http.MethodPost, "/api/v1/logs/links/clear", `{"confirmation":"LINKS"}`, "mock-admin")
|
||
|
|
}
|