| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"net/http/httptest" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 18:57:05 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | 	macaron "gopkg.in/macaron.v1" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/go-macaron/session" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/middleware" | 
					
						
							|  |  |  | 	. "github.com/smartystreets/goconvey/convey" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	TestOrgID  = 1 | 
					
						
							|  |  |  | 	TestUserID = 1 | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestDataSourcesProxy(t *testing.T) { | 
					
						
							|  |  |  | 	Convey("Given a user is logged in", t, func() { | 
					
						
							|  |  |  | 		loggedInUserScenario("When calling GET on", "/api/datasources/", func(sc *scenarioContext) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Stubs the database query
 | 
					
						
							| 
									
										
										
										
											2017-02-08 18:57:05 +08:00
										 |  |  | 			bus.AddHandler("test", func(query *models.GetDataSourcesQuery) error { | 
					
						
							| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | 				So(query.OrgId, ShouldEqual, TestOrgID) | 
					
						
							| 
									
										
										
										
											2017-02-08 18:57:05 +08:00
										 |  |  | 				query.Result = []*models.DataSource{ | 
					
						
							| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | 					{Name: "mmm"}, | 
					
						
							|  |  |  | 					{Name: "ZZZ"}, | 
					
						
							|  |  |  | 					{Name: "BBB"}, | 
					
						
							|  |  |  | 					{Name: "aaa"}, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return nil | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// handler func being tested
 | 
					
						
							|  |  |  | 			sc.handlerFunc = GetDataSources | 
					
						
							|  |  |  | 			sc.fakeReq("GET", "/api/datasources").exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			respJSON := []map[string]interface{}{} | 
					
						
							|  |  |  | 			err := json.NewDecoder(sc.resp.Body).Decode(&respJSON) | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("should return list of datasources for org sorted alphabetically and case insensitively", func() { | 
					
						
							|  |  |  | 				So(respJSON[0]["name"], ShouldEqual, "aaa") | 
					
						
							|  |  |  | 				So(respJSON[1]["name"], ShouldEqual, "BBB") | 
					
						
							|  |  |  | 				So(respJSON[2]["name"], ShouldEqual, "mmm") | 
					
						
							|  |  |  | 				So(respJSON[3]["name"], ShouldEqual, "ZZZ") | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func loggedInUserScenario(desc string, url string, fn scenarioFunc) { | 
					
						
							| 
									
										
										
										
											2017-05-08 21:35:34 +08:00
										 |  |  | 	loggedInUserScenarioWithRole(desc, url, models.ROLE_EDITOR, fn) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func loggedInUserScenarioWithRole(desc string, url string, role models.RoleType, fn scenarioFunc) { | 
					
						
							| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | 	Convey(desc+" "+url, func() { | 
					
						
							|  |  |  | 		defer bus.ClearBusHandlers() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | 		sc := &scenarioContext{ | 
					
						
							|  |  |  | 			url: url, | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | 		viewsPath, _ := filepath.Abs("../../public/views") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		sc.m = macaron.New() | 
					
						
							|  |  |  | 		sc.m.Use(macaron.Renderer(macaron.RenderOptions{ | 
					
						
							|  |  |  | 			Directory: viewsPath, | 
					
						
							|  |  |  | 			Delims:    macaron.Delims{Left: "[[", Right: "]]"}, | 
					
						
							|  |  |  | 		})) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		sc.m.Use(middleware.GetContextHandler()) | 
					
						
							|  |  |  | 		sc.m.Use(middleware.Sessioner(&session.Options{})) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | 		sc.defaultHandler = wrap(func(c *middleware.Context) Response { | 
					
						
							| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | 			sc.context = c | 
					
						
							|  |  |  | 			sc.context.UserId = TestUserID | 
					
						
							|  |  |  | 			sc.context.OrgId = TestOrgID | 
					
						
							| 
									
										
										
										
											2017-05-08 21:35:34 +08:00
										 |  |  | 			sc.context.OrgRole = role | 
					
						
							| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | 			if sc.handlerFunc != nil { | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | 				return sc.handlerFunc(sc.context) | 
					
						
							| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | 		sc.m.Get(url, sc.defaultHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fn(sc) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | 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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | type scenarioContext struct { | 
					
						
							|  |  |  | 	m              *macaron.Macaron | 
					
						
							|  |  |  | 	context        *middleware.Context | 
					
						
							|  |  |  | 	resp           *httptest.ResponseRecorder | 
					
						
							|  |  |  | 	handlerFunc    handlerFunc | 
					
						
							|  |  |  | 	defaultHandler macaron.Handler | 
					
						
							| 
									
										
										
										
											2017-02-08 18:57:05 +08:00
										 |  |  | 	req            *http.Request | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | 	url            string | 
					
						
							| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (sc *scenarioContext) exec() { | 
					
						
							|  |  |  | 	sc.m.ServeHTTP(sc.resp, sc.req) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type scenarioFunc func(c *scenarioContext) | 
					
						
							| 
									
										
										
										
											2017-02-08 21:20:07 +08:00
										 |  |  | type handlerFunc func(c *middleware.Context) Response |