| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | package sqlstore | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-09-28 17:17:03 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2018-08-25 01:09:19 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 22:56:14 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | 	. "github.com/smartystreets/goconvey/convey" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestAlertNotificationSQLAccess(t *testing.T) { | 
					
						
							|  |  |  | 	Convey("Testing Alert notification sql access", t, func() { | 
					
						
							|  |  |  | 		InitTestDB(t) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 		Convey("Alert notification state", func() { | 
					
						
							|  |  |  | 			var alertID int64 = 7 | 
					
						
							|  |  |  | 			var orgID int64 = 5 | 
					
						
							|  |  |  | 			var notifierID int64 = 10 | 
					
						
							|  |  |  | 			oldTimeNow := timeNow | 
					
						
							|  |  |  | 			now := time.Date(2018, 9, 30, 0, 0, 0, 0, time.UTC) | 
					
						
							|  |  |  | 			timeNow = func() time.Time { return now } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("Get no existing state should create a new state", func() { | 
					
						
							|  |  |  | 				query := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} | 
					
						
							|  |  |  | 				err := GetOrCreateAlertNotificationState(context.Background(), query) | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(query.Result, ShouldNotBeNil) | 
					
						
							|  |  |  | 				So(query.Result.State, ShouldEqual, "unknown") | 
					
						
							|  |  |  | 				So(query.Result.Version, ShouldEqual, 0) | 
					
						
							|  |  |  | 				So(query.Result.UpdatedAt, ShouldEqual, now.Unix()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Convey("Get existing state should not create a new state", func() { | 
					
						
							|  |  |  | 					query2 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} | 
					
						
							|  |  |  | 					err := GetOrCreateAlertNotificationState(context.Background(), query2) | 
					
						
							|  |  |  | 					So(err, ShouldBeNil) | 
					
						
							|  |  |  | 					So(query2.Result, ShouldNotBeNil) | 
					
						
							|  |  |  | 					So(query2.Result.Id, ShouldEqual, query.Result.Id) | 
					
						
							|  |  |  | 					So(query2.Result.UpdatedAt, ShouldEqual, now.Unix()) | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2018-09-28 20:12:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 				Convey("Update existing state to pending with correct version should update database", func() { | 
					
						
							|  |  |  | 					s := *query.Result | 
					
						
							| 
									
										
										
										
											2018-10-02 19:57:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 					cmd := models.SetAlertNotificationStateToPendingCommand{ | 
					
						
							|  |  |  | 						Id:                           s.Id, | 
					
						
							|  |  |  | 						Version:                      s.Version, | 
					
						
							|  |  |  | 						AlertRuleStateUpdatedVersion: s.AlertRuleStateUpdatedVersion, | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-10-02 19:57:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 					err := SetAlertNotificationStateToPendingCommand(context.Background(), &cmd) | 
					
						
							| 
									
										
										
										
											2018-09-28 20:12:26 +08:00
										 |  |  | 					So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 					So(cmd.ResultVersion, ShouldEqual, 1) | 
					
						
							| 
									
										
										
										
											2018-09-28 20:12:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 					query2 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} | 
					
						
							|  |  |  | 					err = GetOrCreateAlertNotificationState(context.Background(), query2) | 
					
						
							| 
									
										
										
										
											2018-10-17 05:38:59 +08:00
										 |  |  | 					So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 					So(query2.Result.Version, ShouldEqual, 1) | 
					
						
							|  |  |  | 					So(query2.Result.State, ShouldEqual, models.AlertNotificationStatePending) | 
					
						
							|  |  |  | 					So(query2.Result.UpdatedAt, ShouldEqual, now.Unix()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					Convey("Update existing state to completed should update database", func() { | 
					
						
							|  |  |  | 						s := *query.Result | 
					
						
							|  |  |  | 						setStateCmd := models.SetAlertNotificationStateToCompleteCommand{ | 
					
						
							|  |  |  | 							Id:      s.Id, | 
					
						
							|  |  |  | 							Version: cmd.ResultVersion, | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						err := SetAlertNotificationStateToCompleteCommand(context.Background(), &setStateCmd) | 
					
						
							|  |  |  | 						So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2018-09-28 20:12:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 						query3 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} | 
					
						
							|  |  |  | 						err = GetOrCreateAlertNotificationState(context.Background(), query3) | 
					
						
							| 
									
										
										
										
											2018-09-28 20:12:26 +08:00
										 |  |  | 						So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 						So(query3.Result.Version, ShouldEqual, 2) | 
					
						
							|  |  |  | 						So(query3.Result.State, ShouldEqual, models.AlertNotificationStateCompleted) | 
					
						
							|  |  |  | 						So(query3.Result.UpdatedAt, ShouldEqual, now.Unix()) | 
					
						
							|  |  |  | 					}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					Convey("Update existing state to completed should update database. regardless of version", func() { | 
					
						
							|  |  |  | 						s := *query.Result | 
					
						
							|  |  |  | 						unknownVersion := int64(1000) | 
					
						
							|  |  |  | 						cmd := models.SetAlertNotificationStateToCompleteCommand{ | 
					
						
							|  |  |  | 							Id:      s.Id, | 
					
						
							|  |  |  | 							Version: unknownVersion, | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						err := SetAlertNotificationStateToCompleteCommand(context.Background(), &cmd) | 
					
						
							|  |  |  | 						So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						query3 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} | 
					
						
							|  |  |  | 						err = GetOrCreateAlertNotificationState(context.Background(), query3) | 
					
						
							|  |  |  | 						So(err, ShouldBeNil) | 
					
						
							|  |  |  | 						So(query3.Result.Version, ShouldEqual, unknownVersion+1) | 
					
						
							|  |  |  | 						So(query3.Result.State, ShouldEqual, models.AlertNotificationStateCompleted) | 
					
						
							|  |  |  | 						So(query3.Result.UpdatedAt, ShouldEqual, now.Unix()) | 
					
						
							| 
									
										
										
										
											2018-09-28 20:12:26 +08:00
										 |  |  | 					}) | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				Convey("Update existing state to pending with incorrect version should return version mismatch error", func() { | 
					
						
							|  |  |  | 					s := *query.Result | 
					
						
							|  |  |  | 					s.Version = 1000 | 
					
						
							|  |  |  | 					cmd := models.SetAlertNotificationStateToPendingCommand{ | 
					
						
							|  |  |  | 						Id:                           s.NotifierId, | 
					
						
							|  |  |  | 						Version:                      s.Version, | 
					
						
							|  |  |  | 						AlertRuleStateUpdatedVersion: s.AlertRuleStateUpdatedVersion, | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					err := SetAlertNotificationStateToPendingCommand(context.Background(), &cmd) | 
					
						
							|  |  |  | 					So(err, ShouldEqual, models.ErrAlertNotificationStateVersionConflict) | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Convey("Updating existing state to pending with incorrect version since alert rule state update version is higher", func() { | 
					
						
							|  |  |  | 					s := *query.Result | 
					
						
							|  |  |  | 					cmd := models.SetAlertNotificationStateToPendingCommand{ | 
					
						
							|  |  |  | 						Id:                           s.Id, | 
					
						
							|  |  |  | 						Version:                      s.Version, | 
					
						
							|  |  |  | 						AlertRuleStateUpdatedVersion: 1000, | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					err := SetAlertNotificationStateToPendingCommand(context.Background(), &cmd) | 
					
						
							|  |  |  | 					So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					So(cmd.ResultVersion, ShouldEqual, 1) | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Convey("different version and same alert state change version should return error", func() { | 
					
						
							|  |  |  | 					s := *query.Result | 
					
						
							|  |  |  | 					s.Version = 1000 | 
					
						
							|  |  |  | 					cmd := models.SetAlertNotificationStateToPendingCommand{ | 
					
						
							|  |  |  | 						Id:                           s.Id, | 
					
						
							|  |  |  | 						Version:                      s.Version, | 
					
						
							|  |  |  | 						AlertRuleStateUpdatedVersion: s.AlertRuleStateUpdatedVersion, | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					err := SetAlertNotificationStateToPendingCommand(context.Background(), &cmd) | 
					
						
							|  |  |  | 					So(err, ShouldNotBeNil) | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Reset(func() { | 
					
						
							|  |  |  | 				timeNow = oldTimeNow | 
					
						
							| 
									
										
										
										
											2018-10-01 03:52:50 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2018-09-28 17:17:03 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-09-28 16:48:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | 		Convey("Alert notifications should be empty", func() { | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 			cmd := &models.GetAlertNotificationsQuery{ | 
					
						
							| 
									
										
										
										
											2016-08-01 20:34:58 +08:00
										 |  |  | 				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
										 |  |  | 			So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-09-06 14:42:35 +08:00
										 |  |  | 			So(cmd.Result, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-05 16:27:29 +08:00
										 |  |  | 		Convey("Cannot save alert notifier with send reminder = true", func() { | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 			cmd := &models.CreateAlertNotificationCommand{ | 
					
						
							| 
									
										
										
										
											2018-06-05 16:27:29 +08:00
										 |  |  | 				Name:         "ops", | 
					
						
							|  |  |  | 				Type:         "email", | 
					
						
							|  |  |  | 				OrgId:        1, | 
					
						
							|  |  |  | 				SendReminder: true, | 
					
						
							|  |  |  | 				Settings:     simplejson.New(), | 
					
						
							| 
									
										
										
										
											2018-06-05 04:19:27 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("and missing frequency", func() { | 
					
						
							|  |  |  | 				err := CreateAlertNotificationCommand(cmd) | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 				So(err, ShouldEqual, models.ErrNotificationFrequencyNotFound) | 
					
						
							| 
									
										
										
										
											2018-06-05 04:19:27 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("invalid frequency", func() { | 
					
						
							|  |  |  | 				cmd.Frequency = "invalid duration" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				err := CreateAlertNotificationCommand(cmd) | 
					
						
							|  |  |  | 				So(err.Error(), ShouldEqual, "time: invalid duration invalid duration") | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-25 01:09:19 +08:00
										 |  |  | 		Convey("Cannot update alert notifier with send reminder = false", func() { | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 			cmd := &models.CreateAlertNotificationCommand{ | 
					
						
							| 
									
										
										
										
											2018-06-05 16:27:29 +08:00
										 |  |  | 				Name:         "ops update", | 
					
						
							|  |  |  | 				Type:         "email", | 
					
						
							|  |  |  | 				OrgId:        1, | 
					
						
							|  |  |  | 				SendReminder: false, | 
					
						
							|  |  |  | 				Settings:     simplejson.New(), | 
					
						
							| 
									
										
										
										
											2018-06-05 04:19:27 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			err := CreateAlertNotificationCommand(cmd) | 
					
						
							|  |  |  | 			So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 			updateCmd := &models.UpdateAlertNotificationCommand{ | 
					
						
							| 
									
										
										
										
											2018-06-05 16:27:29 +08:00
										 |  |  | 				Id:           cmd.Result.Id, | 
					
						
							|  |  |  | 				SendReminder: true, | 
					
						
							| 
									
										
										
										
											2018-06-05 04:19:27 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("and missing frequency", func() { | 
					
						
							|  |  |  | 				err := UpdateAlertNotification(updateCmd) | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 				So(err, ShouldEqual, models.ErrNotificationFrequencyNotFound) | 
					
						
							| 
									
										
										
										
											2018-06-05 04:19:27 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("invalid frequency", func() { | 
					
						
							|  |  |  | 				updateCmd.Frequency = "invalid duration" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				err := UpdateAlertNotification(updateCmd) | 
					
						
							| 
									
										
										
										
											2018-06-05 16:27:29 +08:00
										 |  |  | 				So(err, ShouldNotBeNil) | 
					
						
							| 
									
										
										
										
											2018-06-05 04:19:27 +08:00
										 |  |  | 				So(err.Error(), ShouldEqual, "time: invalid duration invalid duration") | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 14:33:50 +08:00
										 |  |  | 		Convey("Can save Alert Notification", func() { | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 			cmd := &models.CreateAlertNotificationCommand{ | 
					
						
							| 
									
										
										
										
											2018-06-05 16:27:29 +08:00
										 |  |  | 				Name:         "ops", | 
					
						
							|  |  |  | 				Type:         "email", | 
					
						
							|  |  |  | 				OrgId:        1, | 
					
						
							|  |  |  | 				SendReminder: true, | 
					
						
							|  |  |  | 				Frequency:    "10s", | 
					
						
							|  |  |  | 				Settings:     simplejson.New(), | 
					
						
							| 
									
										
										
										
											2016-06-14 14:33:50 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-05 04:19:27 +08:00
										 |  |  | 			err := CreateAlertNotificationCommand(cmd) | 
					
						
							| 
									
										
										
										
											2016-06-14 14:33:50 +08:00
										 |  |  | 			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") | 
					
						
							| 
									
										
										
										
											2018-08-25 01:09:19 +08:00
										 |  |  | 			So(cmd.Result.Frequency, ShouldEqual, 10*time.Second) | 
					
						
							| 
									
										
										
										
											2018-10-17 16:41:18 +08:00
										 |  |  | 			So(cmd.Result.DisableResolveMessage, ShouldBeFalse) | 
					
						
							| 
									
										
										
										
											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() { | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 				newCmd := &models.UpdateAlertNotificationCommand{ | 
					
						
							| 
									
										
										
										
											2018-10-17 16:41:18 +08:00
										 |  |  | 					Name:                  "NewName", | 
					
						
							|  |  |  | 					Type:                  "webhook", | 
					
						
							|  |  |  | 					OrgId:                 cmd.Result.OrgId, | 
					
						
							|  |  |  | 					SendReminder:          true, | 
					
						
							|  |  |  | 					DisableResolveMessage: true, | 
					
						
							|  |  |  | 					Frequency:             "60s", | 
					
						
							|  |  |  | 					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") | 
					
						
							| 
									
										
										
										
											2018-08-25 01:09:19 +08:00
										 |  |  | 				So(newCmd.Result.Frequency, ShouldEqual, 60*time.Second) | 
					
						
							| 
									
										
										
										
											2018-10-17 16:41:18 +08:00
										 |  |  | 				So(newCmd.Result.DisableResolveMessage, ShouldBeTrue) | 
					
						
							| 
									
										
										
										
											2018-08-25 01:09:19 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("Can update alert notification to disable sending of reminders", func() { | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 				newCmd := &models.UpdateAlertNotificationCommand{ | 
					
						
							| 
									
										
										
										
											2018-08-25 01:09:19 +08:00
										 |  |  | 					Name:         "NewName", | 
					
						
							|  |  |  | 					Type:         "webhook", | 
					
						
							|  |  |  | 					OrgId:        cmd.Result.OrgId, | 
					
						
							|  |  |  | 					SendReminder: false, | 
					
						
							|  |  |  | 					Settings:     simplejson.New(), | 
					
						
							|  |  |  | 					Id:           cmd.Result.Id, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				err := UpdateAlertNotification(newCmd) | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(newCmd.Result.SendReminder, ShouldBeFalse) | 
					
						
							| 
									
										
										
										
											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() { | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 			cmd1 := models.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()} | 
					
						
							|  |  |  | 			cmd2 := models.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()} | 
					
						
							|  |  |  | 			cmd3 := models.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()} | 
					
						
							|  |  |  | 			cmd4 := models.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()} | 
					
						
							| 
									
										
										
										
											2016-06-20 21:24:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 			otherOrg := models.CreateAlertNotificationCommand{Name: "default", Type: "email", OrgId: 2, SendReminder: true, Frequency: "10s", Settings: simplejson.New()} | 
					
						
							| 
									
										
										
										
											2016-09-06 14:42:35 +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-09-06 14:42:35 +08:00
										 |  |  | 			So(CreateAlertNotificationCommand(&otherOrg), ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-06-14 22:56:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			Convey("search", func() { | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 				query := &models.GetAlertNotificationsToSendQuery{ | 
					
						
							| 
									
										
										
										
											2016-08-01 20:34:58 +08:00
										 |  |  | 					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-09-06 14:42:35 +08:00
										 |  |  | 				err := GetAlertNotificationsToSend(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-09-06 14:42:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			Convey("all", func() { | 
					
						
							| 
									
										
										
										
											2018-10-17 08:39:07 +08:00
										 |  |  | 				query := &models.GetAllAlertNotificationsQuery{ | 
					
						
							| 
									
										
										
										
											2016-09-06 14:42:35 +08:00
										 |  |  | 					OrgId: 1, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				err := GetAllAlertNotifications(query) | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(len(query.Result), ShouldEqual, 4) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2016-06-14 14:33:50 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-06-13 22:39:00 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } |