2019-02-19 23:56:41 +00:00
|
|
|
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Drone Non-Commercial License
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-02-28 07:07:13 +00:00
|
|
|
// +build !oss
|
|
|
|
|
2019-02-19 23:56:41 +00:00
|
|
|
package metric
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/drone/drone/core"
|
|
|
|
"github.com/drone/drone/mock"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandleMetrics(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
|
|
|
|
mockUser := &core.User{Admin: false, Machine: true}
|
|
|
|
session := mock.NewMockSession(controller)
|
|
|
|
session.EXPECT().Get(r).Return(mockUser, nil)
|
|
|
|
|
2019-04-24 22:22:55 +00:00
|
|
|
NewServer(session, false).ServeHTTP(w, r)
|
2019-02-19 23:56:41 +00:00
|
|
|
if got, want := w.Code, 200; got != want {
|
|
|
|
t.Errorf("Want status code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := w.HeaderMap.Get("Content-Type"), "text/plain; version=0.0.4; charset=utf-8"; got != want {
|
|
|
|
t.Errorf("Want prometheus header %q, got %q", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleMetrics_NoSession(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
|
|
|
|
session := mock.NewMockSession(controller)
|
|
|
|
session.EXPECT().Get(r).Return(nil, nil)
|
|
|
|
|
2019-04-24 22:22:55 +00:00
|
|
|
NewServer(session, false).ServeHTTP(w, r)
|
2019-02-19 23:56:41 +00:00
|
|
|
|
|
|
|
if got, want := w.Code, 401; got != want {
|
|
|
|
t.Errorf("Want status code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 22:22:55 +00:00
|
|
|
func TestHandleMetrics_NoSessionButAnonymousAccessEnabled(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
|
|
|
|
session := mock.NewMockSession(controller)
|
|
|
|
session.EXPECT().Get(r).Return(nil, nil)
|
|
|
|
|
|
|
|
NewServer(session, true).ServeHTTP(w, r)
|
|
|
|
|
|
|
|
if got, want := w.Code, 200; got != want {
|
|
|
|
t.Errorf("Want status code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 23:56:41 +00:00
|
|
|
func TestHandleMetrics_AccessDenied(t *testing.T) {
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
|
|
|
|
mockUser := &core.User{Admin: false, Machine: false}
|
|
|
|
session := mock.NewMockSession(controller)
|
|
|
|
session.EXPECT().Get(r).Return(mockUser, nil)
|
|
|
|
|
2019-04-24 22:22:55 +00:00
|
|
|
NewServer(session, false).ServeHTTP(w, r)
|
2019-02-19 23:56:41 +00:00
|
|
|
if got, want := w.Code, 403; got != want {
|
|
|
|
t.Errorf("Want status code %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
}
|