| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-12-01 22:43:31 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/api/routing" | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/annotations" | 
					
						
							| 
									
										
										
										
											2022-02-03 16:20:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/sqlstore/mockstore" | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | func TestAnnotationsAPIEndpoint(t *testing.T) { | 
					
						
							|  |  |  | 	t.Run("Given an annotation without a dashboard ID", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 		cmd := dtos.PostAnnotationsCmd{ | 
					
						
							| 
									
										
										
										
											2019-08-16 16:49:30 +08:00
										 |  |  | 			Time: 1000, | 
					
						
							|  |  |  | 			Text: "annotation text", | 
					
						
							|  |  |  | 			Tags: []string{"tag1", "tag2"}, | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		updateCmd := dtos.UpdateAnnotationsCmd{ | 
					
						
							| 
									
										
										
										
											2019-08-16 16:49:30 +08:00
										 |  |  | 			Time: 1000, | 
					
						
							|  |  |  | 			Text: "annotation text", | 
					
						
							|  |  |  | 			Tags: []string{"tag1", "tag2"}, | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 		patchCmd := dtos.PatchAnnotationsCmd{ | 
					
						
							|  |  |  | 			Time: 1000, | 
					
						
							|  |  |  | 			Text: "annotation text", | 
					
						
							|  |  |  | 			Tags: []string{"tag1", "tag2"}, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		t.Run("When user is an Org Viewer", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 			role := models.ROLE_VIEWER | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 			t.Run("Should not be allowed to save an annotation", func(t *testing.T) { | 
					
						
							|  |  |  | 				postAnnotationScenario(t, "When calling POST on", "/api/annotations", "/api/annotations", role, | 
					
						
							|  |  |  | 					cmd, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 						sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec() | 
					
						
							|  |  |  | 						assert.Equal(t, 403, sc.resp.Code) | 
					
						
							|  |  |  | 					}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				putAnnotationScenario(t, "When calling PUT on", "/api/annotations/1", "/api/annotations/:annotationId", | 
					
						
							|  |  |  | 					role, updateCmd, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 						sc.fakeReqWithParams("PUT", sc.url, map[string]string{}).exec() | 
					
						
							|  |  |  | 						assert.Equal(t, 403, sc.resp.Code) | 
					
						
							|  |  |  | 					}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				patchAnnotationScenario(t, "When calling PATCH on", "/api/annotations/1", | 
					
						
							|  |  |  | 					"/api/annotations/:annotationId", role, patchCmd, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 						sc.fakeReqWithParams("PATCH", sc.url, map[string]string{}).exec() | 
					
						
							|  |  |  | 						assert.Equal(t, 403, sc.resp.Code) | 
					
						
							|  |  |  | 					}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-03 16:20:20 +08:00
										 |  |  | 				mock := mockstore.NewSQLStoreMock() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				loggedInUserScenarioWithRole(t, "When calling DELETE on", "DELETE", "/api/annotations/1", | 
					
						
							|  |  |  | 					"/api/annotations/:annotationId", role, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 						fakeAnnoRepo = &fakeAnnotationsRepo{} | 
					
						
							|  |  |  | 						annotations.SetRepository(fakeAnnoRepo) | 
					
						
							|  |  |  | 						sc.handlerFunc = DeleteAnnotationByID | 
					
						
							|  |  |  | 						sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec() | 
					
						
							|  |  |  | 						assert.Equal(t, 403, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2022-02-03 16:20:20 +08:00
										 |  |  | 					}, mock) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		t.Run("When user is an Org Editor", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 			role := models.ROLE_EDITOR | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 			t.Run("Should be able to save an annotation", func(t *testing.T) { | 
					
						
							|  |  |  | 				postAnnotationScenario(t, "When calling POST on", "/api/annotations", "/api/annotations", role, | 
					
						
							|  |  |  | 					cmd, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 						sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec() | 
					
						
							|  |  |  | 						assert.Equal(t, 200, sc.resp.Code) | 
					
						
							|  |  |  | 					}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				putAnnotationScenario(t, "When calling PUT on", "/api/annotations/1", "/api/annotations/:annotationId", role, updateCmd, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 					sc.fakeReqWithParams("PUT", sc.url, map[string]string{}).exec() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 					assert.Equal(t, 200, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 				}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				patchAnnotationScenario(t, "When calling PATCH on", "/api/annotations/1", "/api/annotations/:annotationId", role, patchCmd, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 					sc.fakeReqWithParams("PATCH", sc.url, map[string]string{}).exec() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 					assert.Equal(t, 200, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2022-02-03 16:20:20 +08:00
										 |  |  | 				mock := mockstore.NewSQLStoreMock() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				loggedInUserScenarioWithRole(t, "When calling DELETE on", "DELETE", "/api/annotations/1", | 
					
						
							|  |  |  | 					"/api/annotations/:annotationId", role, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 						fakeAnnoRepo = &fakeAnnotationsRepo{} | 
					
						
							|  |  |  | 						annotations.SetRepository(fakeAnnoRepo) | 
					
						
							|  |  |  | 						sc.handlerFunc = DeleteAnnotationByID | 
					
						
							|  |  |  | 						sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec() | 
					
						
							|  |  |  | 						assert.Equal(t, 200, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2022-02-03 16:20:20 +08:00
										 |  |  | 					}, mock) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	t.Run("Given an annotation with a dashboard ID and the dashboard does not have an ACL", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 		cmd := dtos.PostAnnotationsCmd{ | 
					
						
							|  |  |  | 			Time:        1000, | 
					
						
							|  |  |  | 			Text:        "annotation text", | 
					
						
							|  |  |  | 			Tags:        []string{"tag1", "tag2"}, | 
					
						
							|  |  |  | 			DashboardId: 1, | 
					
						
							|  |  |  | 			PanelId:     1, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		updateCmd := dtos.UpdateAnnotationsCmd{ | 
					
						
							| 
									
										
										
										
											2019-08-16 16:49:30 +08:00
										 |  |  | 			Time: 1000, | 
					
						
							|  |  |  | 			Text: "annotation text", | 
					
						
							|  |  |  | 			Tags: []string{"tag1", "tag2"}, | 
					
						
							|  |  |  | 			Id:   1, | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 		patchCmd := dtos.PatchAnnotationsCmd{ | 
					
						
							|  |  |  | 			Time: 8000, | 
					
						
							|  |  |  | 			Text: "annotation text 50", | 
					
						
							|  |  |  | 			Tags: []string{"foo", "bar"}, | 
					
						
							|  |  |  | 			Id:   1, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-25 19:58:49 +08:00
										 |  |  | 		deleteCmd := dtos.DeleteAnnotationsCmd{ | 
					
						
							|  |  |  | 			DashboardId: 1, | 
					
						
							|  |  |  | 			PanelId:     1, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		viewerRole := models.ROLE_VIEWER | 
					
						
							|  |  |  | 		editorRole := models.ROLE_EDITOR | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		aclMockResp := []*models.DashboardAclInfoDTO{ | 
					
						
							|  |  |  | 			{Role: &viewerRole, Permission: models.PERMISSION_VIEW}, | 
					
						
							|  |  |  | 			{Role: &editorRole, Permission: models.PERMISSION_EDIT}, | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		setUp := func() { | 
					
						
							| 
									
										
										
										
											2021-12-28 23:08:07 +08:00
										 |  |  | 			bus.AddHandler("test", func(ctx context.Context, query *models.GetDashboardAclInfoListQuery) error { | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				query.Result = aclMockResp | 
					
						
							|  |  |  | 				return nil | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-28 23:08:07 +08:00
										 |  |  | 			bus.AddHandler("test", func(ctx context.Context, query *models.GetTeamsByUserQuery) error { | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				query.Result = []*models.TeamDTO{} | 
					
						
							|  |  |  | 				return nil | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		t.Run("When user is an Org Viewer", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 			role := models.ROLE_VIEWER | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 			t.Run("Should not be allowed to save an annotation", func(t *testing.T) { | 
					
						
							|  |  |  | 				postAnnotationScenario(t, "When calling POST on", "/api/annotations", "/api/annotations", role, cmd, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 					setUp() | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 					sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 					assert.Equal(t, 403, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 				}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				putAnnotationScenario(t, "When calling PUT on", "/api/annotations/1", "/api/annotations/:annotationId", role, updateCmd, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 					setUp() | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 					sc.fakeReqWithParams("PUT", sc.url, map[string]string{}).exec() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 					assert.Equal(t, 403, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 				}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				patchAnnotationScenario(t, "When calling PATCH on", "/api/annotations/1", "/api/annotations/:annotationId", role, patchCmd, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 					setUp() | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 					sc.fakeReqWithParams("PATCH", sc.url, map[string]string{}).exec() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 					assert.Equal(t, 403, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2022-02-03 16:20:20 +08:00
										 |  |  | 				mock := mockstore.NewSQLStoreMock() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				loggedInUserScenarioWithRole(t, "When calling DELETE on", "DELETE", "/api/annotations/1", | 
					
						
							|  |  |  | 					"/api/annotations/:annotationId", role, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 						setUp() | 
					
						
							|  |  |  | 						fakeAnnoRepo = &fakeAnnotationsRepo{} | 
					
						
							|  |  |  | 						annotations.SetRepository(fakeAnnoRepo) | 
					
						
							|  |  |  | 						sc.handlerFunc = DeleteAnnotationByID | 
					
						
							|  |  |  | 						sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec() | 
					
						
							|  |  |  | 						assert.Equal(t, 403, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2022-02-03 16:20:20 +08:00
										 |  |  | 					}, mock) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		t.Run("When user is an Org Editor", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 			role := models.ROLE_EDITOR | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 			t.Run("Should be able to save an annotation", func(t *testing.T) { | 
					
						
							|  |  |  | 				postAnnotationScenario(t, "When calling POST on", "/api/annotations", "/api/annotations", role, cmd, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 					setUp() | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 					sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 					assert.Equal(t, 200, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 				}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				putAnnotationScenario(t, "When calling PUT on", "/api/annotations/1", "/api/annotations/:annotationId", role, updateCmd, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 					setUp() | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 					sc.fakeReqWithParams("PUT", sc.url, map[string]string{}).exec() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 					assert.Equal(t, 200, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 				}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				patchAnnotationScenario(t, "When calling PATCH on", "/api/annotations/1", "/api/annotations/:annotationId", role, patchCmd, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 					setUp() | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 					sc.fakeReqWithParams("PATCH", sc.url, map[string]string{}).exec() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 					assert.Equal(t, 200, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2022-02-03 16:20:20 +08:00
										 |  |  | 				mock := mockstore.NewSQLStoreMock() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				loggedInUserScenarioWithRole(t, "When calling DELETE on", "DELETE", "/api/annotations/1", | 
					
						
							|  |  |  | 					"/api/annotations/:annotationId", role, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 						setUp() | 
					
						
							|  |  |  | 						fakeAnnoRepo = &fakeAnnotationsRepo{} | 
					
						
							|  |  |  | 						annotations.SetRepository(fakeAnnoRepo) | 
					
						
							|  |  |  | 						sc.handlerFunc = DeleteAnnotationByID | 
					
						
							|  |  |  | 						sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec() | 
					
						
							|  |  |  | 						assert.Equal(t, 200, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2022-02-03 16:20:20 +08:00
										 |  |  | 					}, mock) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-06-25 19:58:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		t.Run("When user is an Admin", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 			role := models.ROLE_ADMIN | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 			t.Run("Should be able to do anything", func(t *testing.T) { | 
					
						
							|  |  |  | 				postAnnotationScenario(t, "When calling POST on", "/api/annotations", "/api/annotations", role, cmd, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 					setUp() | 
					
						
							| 
									
										
										
										
											2018-06-25 19:58:49 +08:00
										 |  |  | 					sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 					assert.Equal(t, 200, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2018-06-25 19:58:49 +08:00
										 |  |  | 				}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				putAnnotationScenario(t, "When calling PUT on", "/api/annotations/1", "/api/annotations/:annotationId", role, updateCmd, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 					setUp() | 
					
						
							| 
									
										
										
										
											2018-06-25 19:58:49 +08:00
										 |  |  | 					sc.fakeReqWithParams("PUT", sc.url, map[string]string{}).exec() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 					assert.Equal(t, 200, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2018-06-25 19:58:49 +08:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				patchAnnotationScenario(t, "When calling PATCH on", "/api/annotations/1", "/api/annotations/:annotationId", role, patchCmd, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 					setUp() | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 					sc.fakeReqWithParams("PATCH", sc.url, map[string]string{}).exec() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 					assert.Equal(t, 200, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 				}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				deleteAnnotationsScenario(t, "When calling POST on", "/api/annotations/mass-delete", | 
					
						
							|  |  |  | 					"/api/annotations/mass-delete", role, deleteCmd, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 						setUp() | 
					
						
							|  |  |  | 						sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec() | 
					
						
							|  |  |  | 						assert.Equal(t, 200, sc.resp.Code) | 
					
						
							|  |  |  | 					}) | 
					
						
							| 
									
										
										
										
											2018-06-25 19:58:49 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type fakeAnnotationsRepo struct { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (repo *fakeAnnotationsRepo) Delete(params *annotations.DeleteParams) error { | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | func (repo *fakeAnnotationsRepo) Save(item *annotations.Item) error { | 
					
						
							|  |  |  | 	item.Id = 1 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | func (repo *fakeAnnotationsRepo) Update(item *annotations.Item) error { | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | func (repo *fakeAnnotationsRepo) Find(query *annotations.ItemQuery) ([]*annotations.ItemDTO, error) { | 
					
						
							| 
									
										
										
										
											2017-12-21 15:34:57 +08:00
										 |  |  | 	annotations := []*annotations.ItemDTO{{Id: 1}} | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 	return annotations, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-06-30 19:42:54 +08:00
										 |  |  | func (repo *fakeAnnotationsRepo) FindTags(query *annotations.TagsQuery) (annotations.FindTagsResult, error) { | 
					
						
							|  |  |  | 	result := annotations.FindTagsResult{ | 
					
						
							|  |  |  | 		Tags: []*annotations.TagsDTO{}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return result, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | var fakeAnnoRepo *fakeAnnotationsRepo | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | func postAnnotationScenario(t *testing.T, desc string, url string, routePattern string, role models.RoleType, | 
					
						
							|  |  |  | 	cmd dtos.PostAnnotationsCmd, fn scenarioFunc) { | 
					
						
							|  |  |  | 	t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) { | 
					
						
							|  |  |  | 		t.Cleanup(bus.ClearBusHandlers) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		sc := setupScenarioContext(t, url) | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 			c.Req.Body = mockRequestBody(cmd) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 			sc.context = c | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 			sc.context.UserId = testUserID | 
					
						
							|  |  |  | 			sc.context.OrgId = testOrgID | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 			sc.context.OrgRole = role | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 			return PostAnnotation(c) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fakeAnnoRepo = &fakeAnnotationsRepo{} | 
					
						
							|  |  |  | 		annotations.SetRepository(fakeAnnoRepo) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		sc.m.Post(routePattern, sc.defaultHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fn(sc) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | func putAnnotationScenario(t *testing.T, desc string, url string, routePattern string, role models.RoleType, | 
					
						
							|  |  |  | 	cmd dtos.UpdateAnnotationsCmd, fn scenarioFunc) { | 
					
						
							|  |  |  | 	t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) { | 
					
						
							|  |  |  | 		t.Cleanup(bus.ClearBusHandlers) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		sc := setupScenarioContext(t, url) | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 			c.Req.Body = mockRequestBody(cmd) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 			sc.context = c | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 			sc.context.UserId = testUserID | 
					
						
							|  |  |  | 			sc.context.OrgId = testOrgID | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 			sc.context.OrgRole = role | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 			return UpdateAnnotation(c) | 
					
						
							| 
									
										
										
										
											2017-12-21 07:52:21 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fakeAnnoRepo = &fakeAnnotationsRepo{} | 
					
						
							|  |  |  | 		annotations.SetRepository(fakeAnnoRepo) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		sc.m.Put(routePattern, sc.defaultHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fn(sc) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-06-25 19:58:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | func patchAnnotationScenario(t *testing.T, desc string, url string, routePattern string, role models.RoleType, cmd dtos.PatchAnnotationsCmd, fn scenarioFunc) { | 
					
						
							|  |  |  | 	t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 		defer bus.ClearBusHandlers() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		sc := setupScenarioContext(t, url) | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 			c.Req.Body = mockRequestBody(cmd) | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 			sc.context = c | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 			sc.context.UserId = testUserID | 
					
						
							|  |  |  | 			sc.context.OrgId = testOrgID | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 			sc.context.OrgRole = role | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 			return PatchAnnotation(c) | 
					
						
							| 
									
										
										
										
											2019-01-27 19:49:22 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fakeAnnoRepo = &fakeAnnotationsRepo{} | 
					
						
							|  |  |  | 		annotations.SetRepository(fakeAnnoRepo) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		sc.m.Patch(routePattern, sc.defaultHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fn(sc) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | func deleteAnnotationsScenario(t *testing.T, desc string, url string, routePattern string, role models.RoleType, | 
					
						
							|  |  |  | 	cmd dtos.DeleteAnnotationsCmd, fn scenarioFunc) { | 
					
						
							|  |  |  | 	t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2018-06-25 19:58:49 +08:00
										 |  |  | 		defer bus.ClearBusHandlers() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		sc := setupScenarioContext(t, url) | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 			c.Req.Body = mockRequestBody(cmd) | 
					
						
							| 
									
										
										
										
											2018-06-25 19:58:49 +08:00
										 |  |  | 			sc.context = c | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 			sc.context.UserId = testUserID | 
					
						
							|  |  |  | 			sc.context.OrgId = testOrgID | 
					
						
							| 
									
										
										
										
											2018-06-25 19:58:49 +08:00
										 |  |  | 			sc.context.OrgRole = role | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 			return DeleteAnnotations(c) | 
					
						
							| 
									
										
										
										
											2018-06-25 19:58:49 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fakeAnnoRepo = &fakeAnnotationsRepo{} | 
					
						
							|  |  |  | 		annotations.SetRepository(fakeAnnoRepo) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		sc.m.Post(routePattern, sc.defaultHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fn(sc) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } |