| 
									
										
										
										
											2018-01-30 20:17:48 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"net/http/httptest" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/middleware" | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2019-03-08 22:15:38 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/auth" | 
					
						
							| 
									
										
										
										
											2018-01-30 20:17:48 +08:00
										 |  |  | 	. "github.com/smartystreets/goconvey/convey" | 
					
						
							| 
									
										
										
										
											2019-04-08 19:31:46 +08:00
										 |  |  | 	"gopkg.in/macaron.v1" | 
					
						
							| 
									
										
										
										
											2018-01-30 20:17:48 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func loggedInUserScenario(desc string, url string, fn scenarioFunc) { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	loggedInUserScenarioWithRole(desc, "GET", url, url, models.ROLE_EDITOR, fn) | 
					
						
							| 
									
										
										
										
											2018-01-30 20:17:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | func loggedInUserScenarioWithRole(desc string, method string, url string, routePattern string, role models.RoleType, fn scenarioFunc) { | 
					
						
							| 
									
										
										
										
											2018-01-30 20:17:48 +08:00
										 |  |  | 	Convey(desc+" "+url, func() { | 
					
						
							|  |  |  | 		defer bus.ClearBusHandlers() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		sc := setupScenarioContext(url) | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		sc.defaultHandler = Wrap(func(c *models.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2018-01-30 20:17:48 +08:00
										 |  |  | 			sc.context = c | 
					
						
							|  |  |  | 			sc.context.UserId = TestUserID | 
					
						
							|  |  |  | 			sc.context.OrgId = TestOrgID | 
					
						
							|  |  |  | 			sc.context.OrgRole = role | 
					
						
							|  |  |  | 			if sc.handlerFunc != nil { | 
					
						
							|  |  |  | 				return sc.handlerFunc(sc.context) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		switch method { | 
					
						
							|  |  |  | 		case "GET": | 
					
						
							|  |  |  | 			sc.m.Get(routePattern, sc.defaultHandler) | 
					
						
							|  |  |  | 		case "DELETE": | 
					
						
							|  |  |  | 			sc.m.Delete(routePattern, sc.defaultHandler) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fn(sc) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-24 14:55:16 +08:00
										 |  |  | func anonymousUserScenario(desc string, method string, url string, routePattern string, fn scenarioFunc) { | 
					
						
							|  |  |  | 	Convey(desc+" "+url, func() { | 
					
						
							|  |  |  | 		defer bus.ClearBusHandlers() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		sc := setupScenarioContext(url) | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		sc.defaultHandler = Wrap(func(c *models.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2018-05-24 14:55:16 +08:00
										 |  |  | 			sc.context = c | 
					
						
							|  |  |  | 			if sc.handlerFunc != nil { | 
					
						
							|  |  |  | 				return sc.handlerFunc(sc.context) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		switch method { | 
					
						
							|  |  |  | 		case "GET": | 
					
						
							|  |  |  | 			sc.m.Get(routePattern, sc.defaultHandler) | 
					
						
							|  |  |  | 		case "DELETE": | 
					
						
							|  |  |  | 			sc.m.Delete(routePattern, sc.defaultHandler) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fn(sc) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-30 20:17:48 +08:00
										 |  |  | func (sc *scenarioContext) fakeReq(method, url string) *scenarioContext { | 
					
						
							|  |  |  | 	sc.resp = httptest.NewRecorder() | 
					
						
							|  |  |  | 	req, err := http.NewRequest(method, url, nil) | 
					
						
							|  |  |  | 	So(err, ShouldBeNil) | 
					
						
							|  |  |  | 	sc.req = req | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return sc | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (sc *scenarioContext) fakeReqWithParams(method, url string, queryParams map[string]string) *scenarioContext { | 
					
						
							|  |  |  | 	sc.resp = httptest.NewRecorder() | 
					
						
							|  |  |  | 	req, err := http.NewRequest(method, url, nil) | 
					
						
							|  |  |  | 	q := req.URL.Query() | 
					
						
							|  |  |  | 	for k, v := range queryParams { | 
					
						
							|  |  |  | 		q.Add(k, v) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	req.URL.RawQuery = q.Encode() | 
					
						
							|  |  |  | 	So(err, ShouldBeNil) | 
					
						
							|  |  |  | 	sc.req = req | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return sc | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 14:37:24 +08:00
										 |  |  | func (sc *scenarioContext) fakeReqNoAssertions(method, url string) *scenarioContext { | 
					
						
							|  |  |  | 	sc.resp = httptest.NewRecorder() | 
					
						
							|  |  |  | 	req, _ := http.NewRequest(method, url, nil) | 
					
						
							|  |  |  | 	sc.req = req | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return sc | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (sc *scenarioContext) fakeReqNoAssertionsWithCookie(method, url string, cookie http.Cookie) *scenarioContext { | 
					
						
							|  |  |  | 	sc.resp = httptest.NewRecorder() | 
					
						
							|  |  |  | 	http.SetCookie(sc.resp, &cookie) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	req, _ := http.NewRequest(method, url, nil) | 
					
						
							|  |  |  | 	req.Header = http.Header{"Cookie": sc.resp.Header()["Set-Cookie"]} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sc.req = req | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return sc | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-30 20:17:48 +08:00
										 |  |  | type scenarioContext struct { | 
					
						
							| 
									
										
										
										
											2019-03-08 22:15:38 +08:00
										 |  |  | 	m                    *macaron.Macaron | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	context              *models.ReqContext | 
					
						
							| 
									
										
										
										
											2019-03-08 22:15:38 +08:00
										 |  |  | 	resp                 *httptest.ResponseRecorder | 
					
						
							|  |  |  | 	handlerFunc          handlerFunc | 
					
						
							|  |  |  | 	defaultHandler       macaron.Handler | 
					
						
							|  |  |  | 	req                  *http.Request | 
					
						
							|  |  |  | 	url                  string | 
					
						
							|  |  |  | 	userAuthTokenService *auth.FakeUserAuthTokenService | 
					
						
							| 
									
										
										
										
											2018-01-30 20:17:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (sc *scenarioContext) exec() { | 
					
						
							|  |  |  | 	sc.m.ServeHTTP(sc.resp, sc.req) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type scenarioFunc func(c *scenarioContext) | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | type handlerFunc func(c *models.ReqContext) Response | 
					
						
							| 
									
										
										
										
											2018-01-30 20:17:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func setupScenarioContext(url string) *scenarioContext { | 
					
						
							|  |  |  | 	sc := &scenarioContext{ | 
					
						
							|  |  |  | 		url: url, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	viewsPath, _ := filepath.Abs("../../public/views") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sc.m = macaron.New() | 
					
						
							|  |  |  | 	sc.m.Use(macaron.Renderer(macaron.RenderOptions{ | 
					
						
							|  |  |  | 		Directory: viewsPath, | 
					
						
							|  |  |  | 		Delims:    macaron.Delims{Left: "[[", Right: "]]"}, | 
					
						
							|  |  |  | 	})) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-20 02:47:39 +08:00
										 |  |  | 	sc.m.Use(middleware.GetContextHandler(nil, nil, nil)) | 
					
						
							| 
									
										
										
										
											2018-01-30 20:17:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return sc | 
					
						
							|  |  |  | } |