This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package applicationapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/itworx/pulse/internal/application"
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
)
|
||||
|
||||
type provider struct{ value application.Snapshot }
|
||||
|
||||
func (p provider) Snapshot(context.Context) (application.Snapshot, error) { return p.value, nil }
|
||||
func request(method, path string) *http.Request {
|
||||
r := httptest.NewRequest(method, path, nil)
|
||||
return r.WithContext(auth.WithPrincipal(r.Context(), auth.Principal{Subject: "viewer", Role: auth.RoleViewer}))
|
||||
}
|
||||
func TestHandlerListDetailAndAuth(t *testing.T) {
|
||||
snapshot := application.Snapshot{ContractVersion: application.ContractVersion, Applications: []application.Application{{ID: "app-1", Name: "Pulse", Status: application.StateHealthy}}}
|
||||
handler := Handler{Provider: provider{value: snapshot}}
|
||||
list := httptest.NewRecorder()
|
||||
handler.ServeHTTP(list, request(http.MethodGet, "/api/v1/applications"))
|
||||
if list.Code != http.StatusOK || !strings.Contains(list.Body.String(), "\"name\":\"Pulse\"") {
|
||||
t.Fatalf("list status=%d body=%s", list.Code, list.Body.String())
|
||||
}
|
||||
detail := httptest.NewRecorder()
|
||||
handler.ServeHTTP(detail, request(http.MethodGet, "/api/v1/applications/app-1"))
|
||||
if detail.Code != http.StatusOK || !strings.Contains(detail.Body.String(), "\"id\":\"app-1\"") {
|
||||
t.Fatalf("detail status=%d body=%s", detail.Code, detail.Body.String())
|
||||
}
|
||||
mutation := httptest.NewRecorder()
|
||||
handler.ServeHTTP(mutation, request(http.MethodPost, "/api/v1/applications/app-1"))
|
||||
if mutation.Code != http.StatusNotFound {
|
||||
t.Fatalf("mutation status=%d", mutation.Code)
|
||||
}
|
||||
}
|
||||
func TestHandlerRequiresAuthentication(t *testing.T) {
|
||||
response := httptest.NewRecorder()
|
||||
Handler{}.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/applications", nil))
|
||||
if response.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status=%d", response.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user