| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 18:57:05 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							|  |  |  | 	. "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") | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-06-23 10:15:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Should be able to save a data source", func() { | 
					
						
							|  |  |  | 			loggedInUserScenario("When calling DELETE on non-existing", "/api/datasources/name/12345", func(sc *scenarioContext) { | 
					
						
							|  |  |  | 				sc.handlerFunc = DeleteDataSourceByName | 
					
						
							|  |  |  | 				sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec() | 
					
						
							|  |  |  | 				So(sc.resp.Code, ShouldEqual, 404) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2017-02-08 05:15:52 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } |