| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | package sqlstore | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 22:56:14 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							|  |  |  | 	. "github.com/smartystreets/goconvey/convey" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestAlertNotificationSQLAccess(t *testing.T) { | 
					
						
							|  |  |  | 	Convey("Testing Alert notification sql access", t, func() { | 
					
						
							|  |  |  | 		InitTestDB(t) | 
					
						
							| 
									
										
										
										
											2016-06-14 14:33:50 +08:00
										 |  |  | 		var err error | 
					
						
							| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Alert notifications should be empty", func() { | 
					
						
							| 
									
										
										
										
											2016-08-01 20:34:58 +08:00
										 |  |  | 			cmd := &m.GetAlertNotificationsQuery{ | 
					
						
							|  |  |  | 				OrgId: 2, | 
					
						
							| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | 				Name:  "email", | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-01 20:34:58 +08:00
										 |  |  | 			err := GetAlertNotifications(cmd) | 
					
						
							| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | 			fmt.Printf("errror %v", err) | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							|  |  |  | 			So(len(cmd.Result), ShouldEqual, 0) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 14:33:50 +08:00
										 |  |  | 		Convey("Can save Alert Notification", func() { | 
					
						
							|  |  |  | 			cmd := &m.CreateAlertNotificationCommand{ | 
					
						
							| 
									
										
										
										
											2016-08-01 20:34:58 +08:00
										 |  |  | 				Name:     "ops", | 
					
						
							|  |  |  | 				Type:     "email", | 
					
						
							|  |  |  | 				OrgId:    1, | 
					
						
							|  |  |  | 				Settings: simplejson.New(), | 
					
						
							| 
									
										
										
										
											2016-06-14 14:33:50 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			err = CreateAlertNotificationCommand(cmd) | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							|  |  |  | 			So(cmd.Result.Id, ShouldNotEqual, 0) | 
					
						
							| 
									
										
										
										
											2016-06-14 22:56:14 +08:00
										 |  |  | 			So(cmd.Result.OrgId, ShouldNotEqual, 0) | 
					
						
							|  |  |  | 			So(cmd.Result.Type, ShouldEqual, "email") | 
					
						
							| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 14:33:50 +08:00
										 |  |  | 			Convey("Cannot save Alert Notification with the same name", func() { | 
					
						
							|  |  |  | 				err = CreateAlertNotificationCommand(cmd) | 
					
						
							|  |  |  | 				So(err, ShouldNotBeNil) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 22:56:14 +08:00
										 |  |  | 			Convey("Can update alert notification", func() { | 
					
						
							| 
									
										
										
										
											2016-06-14 14:33:50 +08:00
										 |  |  | 				newCmd := &m.UpdateAlertNotificationCommand{ | 
					
						
							| 
									
										
										
										
											2016-08-01 20:34:58 +08:00
										 |  |  | 					Name:     "NewName", | 
					
						
							|  |  |  | 					Type:     "webhook", | 
					
						
							|  |  |  | 					OrgId:    cmd.Result.OrgId, | 
					
						
							|  |  |  | 					Settings: simplejson.New(), | 
					
						
							|  |  |  | 					Id:       cmd.Result.Id, | 
					
						
							| 
									
										
										
										
											2016-06-14 14:33:50 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-06-14 22:56:14 +08:00
										 |  |  | 				err := UpdateAlertNotification(newCmd) | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(newCmd.Result.Name, ShouldEqual, "NewName") | 
					
						
							| 
									
										
										
										
											2016-06-14 14:33:50 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2016-06-14 22:56:14 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-06-14 14:33:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 22:56:14 +08:00
										 |  |  | 		Convey("Can search using an array of ids", func() { | 
					
						
							| 
									
										
										
										
											2016-08-01 20:34:58 +08:00
										 |  |  | 			cmd1 := m.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, Settings: simplejson.New()} | 
					
						
							|  |  |  | 			cmd2 := m.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, Settings: simplejson.New()} | 
					
						
							|  |  |  | 			cmd3 := m.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, Settings: simplejson.New()} | 
					
						
							| 
									
										
										
										
											2016-09-06 03:33:05 +08:00
										 |  |  | 			cmd4 := m.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, Settings: simplejson.New()} | 
					
						
							| 
									
										
										
										
											2016-06-20 21:24:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-01 20:34:58 +08:00
										 |  |  | 			So(CreateAlertNotificationCommand(&cmd1), ShouldBeNil) | 
					
						
							|  |  |  | 			So(CreateAlertNotificationCommand(&cmd2), ShouldBeNil) | 
					
						
							|  |  |  | 			So(CreateAlertNotificationCommand(&cmd3), ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-09-06 03:33:05 +08:00
										 |  |  | 			So(CreateAlertNotificationCommand(&cmd4), ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-06-14 22:56:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			Convey("search", func() { | 
					
						
							| 
									
										
										
										
											2016-08-01 20:34:58 +08:00
										 |  |  | 				query := &m.GetAlertNotificationsQuery{ | 
					
						
							|  |  |  | 					Ids:   []int64{cmd1.Result.Id, cmd2.Result.Id, 112341231}, | 
					
						
							|  |  |  | 					OrgId: 1, | 
					
						
							| 
									
										
										
										
											2016-06-14 14:33:50 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-06-14 22:56:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-01 20:34:58 +08:00
										 |  |  | 				err := GetAlertNotifications(query) | 
					
						
							| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-09-06 03:33:05 +08:00
										 |  |  | 				So(len(query.Result), ShouldEqual, 3) | 
					
						
							| 
									
										
										
										
											2016-06-14 14:33:50 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } |