auth_test.go 822 B

1234567891011121314151617181920212223242526272829303132333435
  1. package middleware
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. )
  6. func TestMiddlewareAuth(t *testing.T) {
  7. Convey("Given the grafana middleware", t, func() {
  8. reqSignIn := Auth(&AuthOptions{ReqSignedIn: true})
  9. middlewareScenario("ReqSignIn true and unauthenticated request", func(sc *scenarioContext) {
  10. sc.m.Get("/secure", reqSignIn, sc.defaultHandler)
  11. sc.fakeReq("GET", "/secure").exec()
  12. Convey("Should redirect to login", func() {
  13. So(sc.resp.Code, ShouldEqual, 302)
  14. })
  15. })
  16. middlewareScenario("ReqSignIn true and unauthenticated API request", func(sc *scenarioContext) {
  17. sc.m.Get("/api/secure", reqSignIn, sc.defaultHandler)
  18. sc.fakeReq("GET", "/api/secure").exec()
  19. Convey("Should return 401", func() {
  20. So(sc.resp.Code, ShouldEqual, 401)
  21. })
  22. })
  23. })
  24. }